submodule--helper, module_clone: always operate on absolute paths

When giving relative paths to `relative_path` to compute a relative path from one directory to another, this may fail in `relative_path`. Make sure both arguments to `relative_path` are always absolute. Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stefan Beller committed Mar 31, 2016 at 17:17 UTC f8eaa0ba98b3bd9cb9035eba184a2d9806d30b27
2 files changed +15 -15
builtin/submodule--helper.c
+14 -14
@@ -153,11 +153,12 @@ static int clone_submodule(const char *path, const char *gitdir, const char *url
153
154 static int module_clone(int argc, const char **argv, const char *prefix)
155 {
156 - const char *path = NULL, *name = NULL, *url = NULL;
156 + const char *name = NULL, *url = NULL;
157 const char *reference = NULL, *depth = NULL;
158 int quiet = 0;
159 FILE *submodule_dot_git;
160 - char *sm_gitdir, *cwd, *p;
160 + char *sm_gitdir_rel, *p, *path = NULL;
161 + const char *sm_gitdir;
162 struct strbuf rel_path = STRBUF_INIT;
163 struct strbuf sb = STRBUF_INIT;
164
@@ -198,7 +199,14 @@ static int module_clone(int argc, const char **argv, const char *prefix)
199 die(_("submodule--helper: unspecified or empty --path"));
200
201 strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
201 - sm_gitdir = strbuf_detach(&sb, NULL);
202 + sm_gitdir_rel = strbuf_detach(&sb, NULL);
203 + sm_gitdir = absolute_path(sm_gitdir_rel);
204 +
205 + if (!is_absolute_path(path)) {
206 + strbuf_addf(&sb, "%s/%s", get_git_work_tree(), path);
207 + path = strbuf_detach(&sb, NULL);
208 + } else
209 + path = xstrdup(path);
210
211 if (!file_exists(sm_gitdir)) {
212 if (safe_create_leading_directories_const(sm_gitdir) < 0)
@@ -229,24 +237,16 @@ static int module_clone(int argc, const char **argv, const char *prefix)
237 strbuf_reset(&sb);
238 strbuf_reset(&rel_path);
239
232 - cwd = xgetcwd();
240 /* Redirect the worktree of the submodule in the superproject's config */
234 - if (!is_absolute_path(sm_gitdir)) {
235 - strbuf_addf(&sb, "%s/%s", cwd, sm_gitdir);
236 - free(sm_gitdir);
237 - sm_gitdir = strbuf_detach(&sb, NULL);
238 - }
239 -
240 - strbuf_addf(&sb, "%s/%s", cwd, path);
241 p = git_pathdup_submodule(path, "config");
242 if (!p)
243 die(_("could not get submodule directory for '%s'"), path);
244 git_config_set_in_file(p, "core.worktree",
245 - relative_path(sb.buf, sm_gitdir, &rel_path));
245 + relative_path(path, sm_gitdir, &rel_path));
246 strbuf_release(&sb);
247 strbuf_release(&rel_path);
248 - free(sm_gitdir);
249 - free(cwd);
248 + free(sm_gitdir_rel);
249 + free(path);
250 free(p);
251 return 0;
252 }
t/t7400-submodule-basic.sh
+1 -1
@@ -818,7 +818,7 @@ test_expect_success 'submodule add --name allows to replace a submodule with ano
818 )
819 '
820
821 -test_expect_failure 'recursive relative submodules stay relative' '
821 +test_expect_success 'recursive relative submodules stay relative' '
822 test_when_finished "rm -rf super clone2 subsub sub3" &&
823 mkdir subsub &&
824 (