Skip to content

Commit f0a96e8

Browse files
dschogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4d04658 commit f0a96e8

File tree

5 files changed

+31
-12
lines changed

5 files changed

+31
-12
lines changed

Documentation/git-submodule.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ set-branch (-d|--default) [--] <path>::
183183
Sets the default remote tracking branch for the submodule. The
184184
`--branch` option allows the remote branch to be specified. The
185185
`--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'.
187187

188188
set-url [--] <path> <newurl>::
189189
Sets the URL of the specified submodule to <newurl>. Then, it will
@@ -284,7 +284,7 @@ OPTIONS
284284
`.gitmodules` for `update --remote`. A special value of `.` is used to
285285
indicate that the name of the branch in the submodule should be the
286286
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'.
288288

289289
-f::
290290
--force::
@@ -322,10 +322,10 @@ OPTIONS
322322
the superproject's recorded SHA-1 to update the submodule, use the
323323
status of the submodule's remote-tracking branch. The remote used
324324
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).
329329
+
330330
This works for any of the supported update procedures (`--checkout`,
331331
`--rebase`, etc.). The only change is the source of the target SHA-1.

Documentation/gitmodules.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ submodule.<name>.update::
4949

5050
submodule.<name>.branch::
5151
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
5555
current repository. See the `--remote` documentation in
5656
linkgit:git-submodule[1] for details.
5757

builtin/submodule--helper.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1981,7 +1981,7 @@ static const char *remote_submodule_branch(const char *path)
19811981
free(key);
19821982

19831983
if (!branch)
1984-
return "master";
1984+
return "HEAD";
19851985

19861986
if (!strcmp(branch, ".")) {
19871987
const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);

t/t7406-submodule-update.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,22 @@ test_expect_success 'setup a submodule tree' '
7070
)
7171
'
7272

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+
7389
test_expect_success 'submodule update detaching the HEAD ' '
7490
(cd super/submodule &&
7591
git reset --hard HEAD~1

t/t7419-submodule-set-branch.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,21 @@ test_expect_success 'test submodule set-branch --branch' '
5252
'
5353

5454
test_expect_success 'test submodule set-branch --default' '
55+
test_commit -C submodule c &&
5556
(cd super &&
5657
git submodule set-branch --default submodule &&
5758
! grep branch .gitmodules &&
5859
git submodule update --remote &&
5960
cat <<-\EOF >expect &&
60-
a
61+
c
6162
EOF
6263
git -C submodule show -s --pretty=%s >actual &&
6364
test_cmp expect actual
6465
)
6566
'
6667

6768
test_expect_success 'test submodule set-branch -b' '
69+
test_commit -C submodule b &&
6870
(cd super &&
6971
git submodule set-branch -b topic submodule &&
7072
grep "branch = topic" .gitmodules &&
@@ -78,12 +80,13 @@ test_expect_success 'test submodule set-branch -b' '
7880
'
7981

8082
test_expect_success 'test submodule set-branch -d' '
83+
test_commit -C submodule d &&
8184
(cd super &&
8285
git submodule set-branch -d submodule &&
8386
! grep branch .gitmodules &&
8487
git submodule update --remote &&
8588
cat <<-\EOF >expect &&
86-
a
89+
d
8790
EOF
8891
git -C submodule show -s --pretty=%s >actual &&
8992
test_cmp expect actual

0 commit comments

Comments
 (0)