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 3604242f080, 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 <novalis@novalis.org> Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stefan Beller committed Jan 6, 2017 at 16:19 UTC 6e7c14e65c863b615a6a3abb2a3668b261008809
3 files changed +25 -7
builtin/submodule--helper.c
+7 -6
@@ -317,8 +317,12 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
317 /* Only loads from .gitmodules, no overlay with .git/config */
318 gitmodules_config();
319
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);
326 displaypath = strbuf_detach(&sb, NULL);
327 } else
328 displaypath = xstrdup(path);
@@ -403,9 +407,6 @@ static int module_init(int argc, const char **argv, const char *prefix)
407 int i;
408
409 struct option module_init_options[] = {
406 - OPT_STRING(0, "prefix", &prefix,
407 - N_("path"),
408 - N_("alternative anchor for relative paths")),
410 OPT__QUIET(&quiet, N_("Suppress output for initializing a submodule")),
411 OPT_END()
412 };
@@ -1129,7 +1130,7 @@ static struct cmd_struct commands[] = {
1130 {"relative-path", resolve_relative_path, 0},
1131 {"resolve-relative-url", resolve_relative_url, 0},
1132 {"resolve-relative-url-test", resolve_relative_url_test, 0},
1132 - {"init", module_init, 0},
1133 + {"init", module_init, SUPPORT_SUPER_PREFIX},
1134 {"remote-branch", resolve_remote_submodule_branch, 0},
1135 {"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
1136 };
git-submodule.sh
+1 -1
@@ -374,7 +374,7 @@ cmd_init()
374 shift
375 done
376
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} "$@"
378 }
379
380 #
t/t7406-submodule-update.sh
+17
@@ -140,6 +140,23 @@ test_expect_success 'submodule update --init --recursive from subdirectory' '
140 test_i18ncmp expect2 actual2
141 '
142
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 +
160 apos="'";
161 test_expect_success 'submodule update does not fetch already present commits' '
162 (cd submodule &&