Skip to content

Commit 6e7c14e

Browse files
stefanbellergitster
authored andcommitted
submodule update --init: display correct path from submodule
In the submodule helper we did not correctly handled the display path for initializing submodules when both the submodule is inside a subdirectory as well as the command being invoked from a subdirectory (as viewed from the superproject). This was broken in 3604242, which was written at a time where there was no super-prefix available, so we abused the --prefix option for the same purpose and could get only one case right (the call from within a subdirectory, not the submodule being in a subdirectory). Test-provided-by: David Turner <[email protected]> Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f6f8586 commit 6e7c14e

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

builtin/submodule--helper.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,12 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
317317
/* Only loads from .gitmodules, no overlay with .git/config */
318318
gitmodules_config();
319319

320-
if (prefix) {
321-
strbuf_addf(&sb, "%s%s", prefix, path);
320+
if (prefix && get_super_prefix())
321+
die("BUG: cannot have prefix and superprefix");
322+
else if (prefix)
323+
displaypath = xstrdup(relative_path(path, prefix, &sb));
324+
else if (get_super_prefix()) {
325+
strbuf_addf(&sb, "%s%s", get_super_prefix(), path);
322326
displaypath = strbuf_detach(&sb, NULL);
323327
} else
324328
displaypath = xstrdup(path);
@@ -403,9 +407,6 @@ static int module_init(int argc, const char **argv, const char *prefix)
403407
int i;
404408

405409
struct option module_init_options[] = {
406-
OPT_STRING(0, "prefix", &prefix,
407-
N_("path"),
408-
N_("alternative anchor for relative paths")),
409410
OPT__QUIET(&quiet, N_("Suppress output for initializing a submodule")),
410411
OPT_END()
411412
};
@@ -1129,7 +1130,7 @@ static struct cmd_struct commands[] = {
11291130
{"relative-path", resolve_relative_path, 0},
11301131
{"resolve-relative-url", resolve_relative_url, 0},
11311132
{"resolve-relative-url-test", resolve_relative_url_test, 0},
1132-
{"init", module_init, 0},
1133+
{"init", module_init, SUPPORT_SUPER_PREFIX},
11331134
{"remote-branch", resolve_remote_submodule_branch, 0},
11341135
{"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
11351136
};

git-submodule.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ cmd_init()
374374
shift
375375
done
376376

377-
git ${wt_prefix:+-C "$wt_prefix"} submodule--helper init ${GIT_QUIET:+--quiet} ${prefix:+--prefix "$prefix"} "$@"
377+
git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper init ${GIT_QUIET:+--quiet} "$@"
378378
}
379379

380380
#

t/t7406-submodule-update.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,23 @@ test_expect_success 'submodule update --init --recursive from subdirectory' '
140140
test_i18ncmp expect2 actual2
141141
'
142142

143+
cat <<EOF >expect2
144+
Submodule 'foo/sub' ($pwd/withsubs/../rebasing) registered for path 'sub'
145+
EOF
146+
147+
test_expect_success 'submodule update --init from and of subdirectory' '
148+
git init withsubs &&
149+
(cd withsubs &&
150+
mkdir foo &&
151+
git submodule add "$(pwd)/../rebasing" foo/sub &&
152+
(cd foo &&
153+
git submodule deinit -f sub &&
154+
git submodule update --init sub 2>../../actual2
155+
)
156+
) &&
157+
test_i18ncmp expect2 actual2
158+
'
159+
143160
apos="'";
144161
test_expect_success 'submodule update does not fetch already present commits' '
145162
(cd submodule &&

0 commit comments

Comments
 (0)