@matheusbsbs / matheus-bagliano / commits / f0a96e8d4c
Fork 0

submodule: fall back to remote's HEAD for missing remote.<name>.branch

When `remote.<name>.branch` is not configured, `git submodule update` currently falls back to using the branch name `master`. A much better idea, however, is to use the remote `HEAD`: on all Git servers running reasonably recent Git versions, the symref `HEAD` points to the main branch. Note: t7419 demonstrates that there _might_ be use cases out there that _expect_ `git submodule update --remote` to update submodules to the remote `master` branch even if the remote `HEAD` points to another branch. Arguably, this patch makes the behavior more intuitive, but there is a slight possibility that this might cause regressions in obscure setups. Even so, it should be okay to fix this behavior without anything like a longer transition period: - The `git submodule update --remote` command is not really common. - Current Git's behavior when running this command is outright confusing, unless the remote repository's current branch _is_ `master` (in which case the proposed behavior matches the old behavior). - If a user encounters a regression due to the changed behavior, the fix is actually trivial: setting `submodule.<name>.branch` to `master` will reinstate the old behavior. Helped-by: Philippe Blain <levraiphilippeblain@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Jun 24, 2020 at 14:46 UTC f0a96e8d4c98c2394dc726b57b914f95cbc7a0de
5 files changed +31 -12
Documentation/git-submodule.txt
+6 -6
@@ -183,7 +183,7 @@ set-branch (-d|--default) [--] <path>::
183 Sets the default remote tracking branch for the submodule. The
184 `--branch` option allows the remote branch to be specified. The
185 `--default` option removes the submodule.<name>.branch configuration
186 - key, which causes the tracking branch to default to 'master'.
186 + key, which causes the tracking branch to default to the remote 'HEAD'.
187
188 set-url [--] <path> <newurl>::
189 Sets the URL of the specified submodule to <newurl>. Then, it will
@@ -284,7 +284,7 @@ OPTIONS
284 `.gitmodules` for `update --remote`. A special value of `.` is used to
285 indicate that the name of the branch in the submodule should be the
286 same name as the current branch in the current repository. If the
287 - option is not specified, it defaults to 'master'.
287 + option is not specified, it defaults to the remote 'HEAD'.
288
289 -f::
290 --force::
@@ -322,10 +322,10 @@ OPTIONS
322 the superproject's recorded SHA-1 to update the submodule, use the
323 status of the submodule's remote-tracking branch. The remote used
324 is branch's remote (`branch.<name>.remote`), defaulting to `origin`.
325 - The remote branch used defaults to `master`, but the branch name may
326 - be overridden by setting the `submodule.<name>.branch` option in
327 - either `.gitmodules` or `.git/config` (with `.git/config` taking
328 - precedence).
325 + The remote branch used defaults to the remote `HEAD`, but the branch
326 + name may be overridden by setting the `submodule.<name>.branch`
327 + option in either `.gitmodules` or `.git/config` (with `.git/config`
328 + taking precedence).
329 +
330 This works for any of the supported update procedures (`--checkout`,
331 `--rebase`, etc.). The only change is the source of the target SHA-1.
Documentation/gitmodules.txt
+3 -3
@@ -49,9 +49,9 @@ submodule.<name>.update::
49
50 submodule.<name>.branch::
51 A remote branch name for tracking updates in the upstream submodule.
52 - If the option is not specified, it defaults to 'master'. A special
53 - value of `.` is used to indicate that the name of the branch in the
54 - submodule should be the same name as the current branch in the
52 + If the option is not specified, it defaults to the remote 'HEAD'.
53 + A special value of `.` is used to indicate that the name of the branch
54 + in the submodule should be the same name as the current branch in the
55 current repository. See the `--remote` documentation in
56 linkgit:git-submodule[1] for details.
57
builtin/submodule--helper.c
+1 -1
@@ -1981,7 +1981,7 @@ static const char *remote_submodule_branch(const char *path)
1981 free(key);
1982
1983 if (!branch)
1984 - return "master";
1984 + return "HEAD";
1985
1986 if (!strcmp(branch, ".")) {
1987 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
t/t7406-submodule-update.sh
+16
@@ -70,6 +70,22 @@ test_expect_success 'setup a submodule tree' '
70 )
71 '
72
73 +test_expect_success 'update --remote falls back to using HEAD' '
74 + test_create_repo main-branch-submodule &&
75 + test_commit -C main-branch-submodule initial &&
76 +
77 + test_create_repo main-branch &&
78 + git -C main-branch submodule add ../main-branch-submodule &&
79 + git -C main-branch commit -m add-submodule &&
80 +
81 + git -C main-branch-submodule switch -c hello &&
82 + test_commit -C main-branch-submodule world &&
83 +
84 + git clone --recursive main-branch main-branch-clone &&
85 + git -C main-branch-clone submodule update --remote main-branch-submodule &&
86 + test_path_exists main-branch-clone/main-branch-submodule/world.t
87 +'
88 +
89 test_expect_success 'submodule update detaching the HEAD ' '
90 (cd super/submodule &&
91 git reset --hard HEAD~1
t/t7419-submodule-set-branch.sh
+5 -2
@@ -52,12 +52,13 @@ test_expect_success 'test submodule set-branch --branch' '
52 '
53
54 test_expect_success 'test submodule set-branch --default' '
55 + test_commit -C submodule c &&
56 (cd super &&
57 git submodule set-branch --default submodule &&
58 ! grep branch .gitmodules &&
59 git submodule update --remote &&
60 cat <<-\EOF >expect &&
60 - a
61 + c
62 EOF
63 git -C submodule show -s --pretty=%s >actual &&
64 test_cmp expect actual
@@ -65,6 +66,7 @@ test_expect_success 'test submodule set-branch --default' '
66 '
67
68 test_expect_success 'test submodule set-branch -b' '
69 + test_commit -C submodule b &&
70 (cd super &&
71 git submodule set-branch -b topic submodule &&
72 grep "branch = topic" .gitmodules &&
@@ -78,12 +80,13 @@ test_expect_success 'test submodule set-branch -b' '
80 '
81
82 test_expect_success 'test submodule set-branch -d' '
83 + test_commit -C submodule d &&
84 (cd super &&
85 git submodule set-branch -d submodule &&
86 ! grep branch .gitmodules &&
87 git submodule update --remote &&
88 cat <<-\EOF >expect &&
86 - a
89 + d
90 EOF
91 git -C submodule show -s --pretty=%s >actual &&
92 test_cmp expect actual