@matheusbsbs / matheus-bagliano / commits / 49d3c4b481
Fork 0

get_superproject_working_tree(): return strbuf

Together with the previous commits, this commit fully fixes the problem of using shared buffer for `real_path()` in `get_superproject_working_tree()`. Signed-off-by: Alexandr Miloslavskiy <alexandr.miloslavskiy@syntevo.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Alexandr Miloslavskiy committed Mar 10, 2020 at 13:11 UTC 49d3c4b481f12c2ec655a71d5a5b9259a398d059
3 files changed +14 -14
builtin/rev-parse.c
+4 -3
@@ -808,9 +808,10 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
808 continue;
809 }
810 if (!strcmp(arg, "--show-superproject-working-tree")) {
811 - const char *superproject = get_superproject_working_tree();
812 - if (superproject)
813 - puts(superproject);
811 + struct strbuf superproject = STRBUF_INIT;
812 + if (get_superproject_working_tree(&superproject))
813 + puts(superproject.buf);
814 + strbuf_release(&superproject);
815 continue;
816 }
817 if (!strcmp(arg, "--show-prefix")) {
submodule.c
+8 -9
@@ -2168,14 +2168,13 @@ void absorb_git_dir_into_superproject(const char *path,
2168 }
2169 }
2170
2171 -const char *get_superproject_working_tree(void)
2171 +int get_superproject_working_tree(struct strbuf *buf)
2172 {
2173 - static struct strbuf realpath = STRBUF_INIT;
2173 struct child_process cp = CHILD_PROCESS_INIT;
2174 struct strbuf sb = STRBUF_INIT;
2175 struct strbuf one_up = STRBUF_INIT;
2176 const char *cwd = xgetcwd();
2178 - const char *ret = NULL;
2177 + int ret = 0;
2178 const char *subpath;
2179 int code;
2180 ssize_t len;
@@ -2186,10 +2185,10 @@ const char *get_superproject_working_tree(void)
2185 * We might have a superproject, but it is harder
2186 * to determine.
2187 */
2189 - return NULL;
2188 + return 0;
2189
2190 if (!strbuf_realpath(&one_up, "../", 0))
2192 - return NULL;
2191 + return 0;
2192
2193 subpath = relative_path(cwd, one_up.buf, &sb);
2194 strbuf_release(&one_up);
@@ -2233,8 +2232,8 @@ const char *get_superproject_working_tree(void)
2232 super_wt = xstrdup(cwd);
2233 super_wt[cwd_len - super_sub_len] = '\0';
2234
2236 - strbuf_realpath(&realpath, super_wt, 1);
2237 - ret = realpath.buf;
2235 + strbuf_realpath(buf, super_wt, 1);
2236 + ret = 1;
2237 free(super_wt);
2238 }
2239 strbuf_release(&sb);
@@ -2243,10 +2242,10 @@ const char *get_superproject_working_tree(void)
2242
2243 if (code == 128)
2244 /* '../' is not a git repository */
2246 - return NULL;
2245 + return 0;
2246 if (code == 0 && len == 0)
2247 /* There is an unrelated git repository at '../' */
2249 - return NULL;
2248 + return 0;
2249 if (code)
2250 die(_("ls-tree returned unexpected return code %d"), code);
2251
submodule.h
+2 -2
@@ -152,8 +152,8 @@ void absorb_git_dir_into_superproject(const char *path,
152 /*
153 * Return the absolute path of the working tree of the superproject, which this
154 * project is a submodule of. If this repository is not a submodule of
155 - * another repository, return NULL.
155 + * another repository, return 0.
156 */
157 -const char *get_superproject_working_tree(void);
157 +int get_superproject_working_tree(struct strbuf *buf);
158
159 #endif