Skip to content

Commit f8eaa0b

Browse files
stefanbellergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 47d5d64 commit f8eaa0b

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

builtin/submodule--helper.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,12 @@ static int clone_submodule(const char *path, const char *gitdir, const char *url
153153

154154
static int module_clone(int argc, const char **argv, const char *prefix)
155155
{
156-
const char *path = NULL, *name = NULL, *url = NULL;
156+
const char *name = NULL, *url = NULL;
157157
const char *reference = NULL, *depth = NULL;
158158
int quiet = 0;
159159
FILE *submodule_dot_git;
160-
char *sm_gitdir, *cwd, *p;
160+
char *sm_gitdir_rel, *p, *path = NULL;
161+
const char *sm_gitdir;
161162
struct strbuf rel_path = STRBUF_INIT;
162163
struct strbuf sb = STRBUF_INIT;
163164

@@ -198,7 +199,14 @@ static int module_clone(int argc, const char **argv, const char *prefix)
198199
die(_("submodule--helper: unspecified or empty --path"));
199200

200201
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);
202210

203211
if (!file_exists(sm_gitdir)) {
204212
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)
229237
strbuf_reset(&sb);
230238
strbuf_reset(&rel_path);
231239

232-
cwd = xgetcwd();
233240
/* 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);
241241
p = git_pathdup_submodule(path, "config");
242242
if (!p)
243243
die(_("could not get submodule directory for '%s'"), path);
244244
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));
246246
strbuf_release(&sb);
247247
strbuf_release(&rel_path);
248-
free(sm_gitdir);
249-
free(cwd);
248+
free(sm_gitdir_rel);
249+
free(path);
250250
free(p);
251251
return 0;
252252
}

t/t7400-submodule-basic.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ test_expect_success 'submodule add --name allows to replace a submodule with ano
818818
)
819819
'
820820

821-
test_expect_failure 'recursive relative submodules stay relative' '
821+
test_expect_success 'recursive relative submodules stay relative' '
822822
test_when_finished "rm -rf super clone2 subsub sub3" &&
823823
mkdir subsub &&
824824
(

0 commit comments

Comments
 (0)