git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v4 3/9] submodule: fall back to remote's HEAD for missing remote.<name>.branch

From
Johannes Schindelin via GitGitGadget <gitgitgadget@gmail.com>
Date
Jun 24, 2020, 14:46 UTC
Message-ID
<54563642dbf96fcb36993c2c2d36ebefaf220cce.1593009996.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.656.v4.git.1593009996.gitgitgadget@gmail.com>
From: Johannes Schindelin <johannes.schindelin@gmx.de>

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>
---
 Documentation/git-submodule.txt | 12 ++++++------
 Documentation/gitmodules.txt    |  6 +++---
 builtin/submodule--helper.c     |  2 +-
 t/t7406-submodule-update.sh     | 16 ++++++++++++++++
 t/t7419-submodule-set-branch.sh |  7 +++++--
 5 files changed, 31 insertions(+), 12 deletions(-)
diff --git a/Documentation/git-submodule.txt b/Documentation/git-submodule.txt
index c9ed2bf3d5..7e5f995f77 100644
--- a/Documentation/git-submodule.txt
+++ b/Documentation/git-submodule.txt
@@ -183,7 +183,7 @@ set-branch (-d|--default) [--] <path>::
 	Sets the default remote tracking branch for the submodule. The
 	`--branch` option allows the remote branch to be specified. The
 	`--default` option removes the submodule.<name>.branch configuration
-	key, which causes the tracking branch to default to 'master'.
+	key, which causes the tracking branch to default to the remote 'HEAD'.
 
 set-url [--] <path> <newurl>::
 	Sets the URL of the specified submodule to <newurl>. Then, it will
@@ -284,7 +284,7 @@ OPTIONS
 	`.gitmodules` for `update --remote`.  A special value of `.` is used to
 	indicate that the name of the branch in the submodule should be the
 	same name as the current branch in the current repository.  If the
-	option is not specified, it defaults to 'master'.
+	option is not specified, it defaults to the remote 'HEAD'.
 
 -f::
 --force::
@@ -322,10 +322,10 @@ OPTIONS
 	the superproject's recorded SHA-1 to update the submodule, use the
 	status of the submodule's remote-tracking branch.  The remote used
 	is branch's remote (`branch.<name>.remote`), defaulting to `origin`.
-	The remote branch used defaults to `master`, but the branch name may
-	be overridden by setting the `submodule.<name>.branch` option in
-	either `.gitmodules` or `.git/config` (with `.git/config` taking
-	precedence).
+	The remote branch used defaults to the remote `HEAD`, but the branch
+	name may be overridden by setting the `submodule.<name>.branch`
+	option in either `.gitmodules` or `.git/config` (with `.git/config`
+	taking precedence).
 +
 This works for any of the supported update procedures (`--checkout`,
 `--rebase`, etc.).  The only change is the source of the target SHA-1.
diff --git a/Documentation/gitmodules.txt b/Documentation/gitmodules.txt
index 67275fd187..539b4e1997 100644
--- a/Documentation/gitmodules.txt
+++ b/Documentation/gitmodules.txt
@@ -49,9 +49,9 @@ submodule.<name>.update::
 
 submodule.<name>.branch::
 	A remote branch name for tracking updates in the upstream submodule.
-	If the option is not specified, it defaults to 'master'.  A special
-	value of `.` is used to indicate that the name of the branch in the
-	submodule should be the same name as the current branch in the
+	If the option is not specified, it defaults to the remote 'HEAD'.
+	A special value of `.` is used to indicate that the name of the branch
+	in the submodule should be the same name as the current branch in the
 	current repository.  See the `--remote` documentation in
 	linkgit:git-submodule[1] for details.
 
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 46c03d2a12..f55f7b7704 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -1981,7 +1981,7 @@ static const char *remote_submodule_branch(const char *path)
 	free(key);
 
 	if (!branch)
-		return "master";
+		return "HEAD";
 
 	if (!strcmp(branch, ".")) {
 		const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
diff --git a/t/t7406-submodule-update.sh b/t/t7406-submodule-update.sh
index 4fb447a143..aa19ff3a2e 100755
--- a/t/t7406-submodule-update.sh
+++ b/t/t7406-submodule-update.sh
@@ -70,6 +70,22 @@ test_expect_success 'setup a submodule tree' '
 	)
 '
 
+test_expect_success 'update --remote falls back to using HEAD' '
+	test_create_repo main-branch-submodule &&
+	test_commit -C main-branch-submodule initial &&
+
+	test_create_repo main-branch &&
+	git -C main-branch submodule add ../main-branch-submodule &&
+	git -C main-branch commit -m add-submodule &&
+
+	git -C main-branch-submodule switch -c hello &&
+	test_commit -C main-branch-submodule world &&
+
+	git clone --recursive main-branch main-branch-clone &&
+	git -C main-branch-clone submodule update --remote main-branch-submodule &&
+	test_path_exists main-branch-clone/main-branch-submodule/world.t
+'
+
 test_expect_success 'submodule update detaching the HEAD ' '
 	(cd super/submodule &&
 	 git reset --hard HEAD~1
diff --git a/t/t7419-submodule-set-branch.sh b/t/t7419-submodule-set-branch.sh
index fd25f786a3..3b925c302f 100755
--- a/t/t7419-submodule-set-branch.sh
+++ b/t/t7419-submodule-set-branch.sh
@@ -52,12 +52,13 @@ test_expect_success 'test submodule set-branch --branch' '
 '
 
 test_expect_success 'test submodule set-branch --default' '
+	test_commit -C submodule c &&
 	(cd super &&
 		git submodule set-branch --default submodule &&
 		! grep branch .gitmodules &&
 		git submodule update --remote &&
 		cat <<-\EOF >expect &&
-		a
+		c
 		EOF
 		git -C submodule show -s --pretty=%s >actual &&
 		test_cmp expect actual
@@ -65,6 +66,7 @@ test_expect_success 'test submodule set-branch --default' '
 '
 
 test_expect_success 'test submodule set-branch -b' '
+	test_commit -C submodule b &&
 	(cd super &&
 		git submodule set-branch -b topic submodule &&
 		grep "branch = topic" .gitmodules &&
@@ -78,12 +80,13 @@ test_expect_success 'test submodule set-branch -b' '
 '
 
 test_expect_success 'test submodule set-branch -d' '
+	test_commit -C submodule d &&
 	(cd super &&
 		git submodule set-branch -d submodule &&
 		! grep branch .gitmodules &&
 		git submodule update --remote &&
 		cat <<-\EOF >expect &&
-		a
+		d
 		EOF
 		git -C submodule show -s --pretty=%s >actual &&
 		test_cmp expect actual
-- 
gitgitgadget
Previous: Johannes Schindelin via GitGitGadgetNext: Junio C Hamano
Message 160 of 180 in “Allow overriding the default name of the default branch”
  1. 0/9 Allow overriding the default name of the default branchJohannes Schindelin via GitGitGadget, Jun 10, 2020
  2. 1/9 init: allow overriding the default branch name for new repositoriesDon Goodman-Wilson via GitGitGadget, Jun 10, 2020
  3. brian m. carlsonJun 10, 2020
  4. Eric SunshineJun 11, 2020
  5. Johannes SchindelinJun 11, 2020
  6. Junio C HamanoJun 11, 2020
  7. Jeff KingJun 16, 2020
  8. Jeff KingJun 16, 2020
  9. Johannes SchindelinJun 18, 2020
  10. Johannes SchindelinJun 23, 2020
  11. Phillip WoodJun 11, 2020
  12. Johannes SchindelinJun 12, 2020
  13. Junio C HamanoJun 12, 2020
  14. Phillip WoodJun 15, 2020
  15. Johannes SchindelinJun 14, 2020
  16. Alban GruinJun 11, 2020
  17. Junio C HamanoJun 11, 2020
  18. brian m. carlsonJun 11, 2020
  19. Johannes SchindelinJun 12, 2020
  20. Alban GruinJun 13, 2020
  21. Johannes SchindelinJun 14, 2020
  22. Jeff KingJun 16, 2020
  23. Johannes SchindelinJun 18, 2020
  24. 3/9 send-pack/transport-helper: respect `core.defaultBranchName`Johannes Schindelin via GitGitGadget, Jun 10, 2020
  25. 2/9 remote: respect `core.defaultBranchName`Johannes Schindelin via GitGitGadget, Jun 10, 2020
  26. Jeff KingJun 16, 2020
  27. Johannes SchindelinJun 18, 2020
  28. Jeff KingJun 18, 2020
  29. Johannes SchindelinJun 23, 2020
  30. 7/9 fmt-merge-msg: learn about the possibly-configured default branch nameJohannes Schindelin via GitGitGadget, Jun 10, 2020
  31. Junio C HamanoJun 10, 2020
  32. 6/9 clone: learn about the possibly-configured default branch nameJohannes Schindelin via GitGitGadget, Jun 10, 2020
  33. Junio C HamanoJun 10, 2020
  34. 8/9 fast-export: respect the possibly-overridden default branch nameJohannes Schindelin via GitGitGadget, Jun 10, 2020
  35. Matt RogersJun 10, 2020
  36. Junio C HamanoJun 10, 2020
  37. brian m. carlsonJun 10, 2020
  38. Matt RogersJun 11, 2020
  39. Junio C HamanoJun 11, 2020
  40. Johannes SchindelinJun 11, 2020
  41. Re* [PATCH 8/9] fast-export: respect the possibly-overridden default branch nameJunio C Hamano, Jun 11, 2020
  42. Junio C HamanoJun 11, 2020
  43. Junio C HamanoJun 11, 2020
  44. Johannes SchindelinJun 12, 2020
  45. Junio C HamanoJun 12, 2020
  46. Johannes SchindelinJun 12, 2020
  47. Junio C HamanoJun 12, 2020
  48. Johannes SchindelinJun 12, 2020
  49. Johannes SchindelinJun 12, 2020
  50. Junio C HamanoJun 12, 2020
  51. Junio C HamanoJun 12, 2020
  52. Johannes SchindelinJun 13, 2020
  53. Junio C HamanoJun 12, 2020
  54. Johannes SixtJun 13, 2020
  55. Junio C HamanoJun 13, 2020
  56. Johannes SchindelinJun 13, 2020
  57. Junio C HamanoJun 13, 2020
  58. Johannes SchindelinJun 14, 2020
  59. Junio C HamanoJun 17, 2020
  60. Johannes SchindelinJun 23, 2020
  61. Junio C HamanoJun 23, 2020
  62. Johannes SchindelinJun 13, 2020
  63. Johannes SchindelinJun 11, 2020
  64. Junio C HamanoJun 11, 2020
  65. Johannes SchindelinJun 12, 2020
  66. 9/9 Document how the default branch name can be overriddenJohannes Schindelin via GitGitGadget, Jun 10, 2020
  67. Junio C HamanoJun 11, 2020
  68. 5/9 submodule: use the (possibly overridden) default branch nameJohannes Schindelin via GitGitGadget, Jun 10, 2020
  69. Denton LiuJun 15, 2020
  70. 4/9 testsvn: respect `core.defaultBranchName`Johannes Schindelin via GitGitGadget, Jun 10, 2020
  71. Junio C HamanoJun 10, 2020
  72. Junio C HamanoJun 11, 2020
  73. Johannes SchindelinJun 11, 2020
  74. Junio C HamanoJun 11, 2020
  75. brian m. carlsonJun 10, 2020
  76. Taylor BlauJun 11, 2020
  77. Johannes SchindelinJun 11, 2020
  78. Pratyush YadavJun 15, 2020
  79. Johannes SchindelinJun 14, 2020
  80. Denton LiuJun 16, 2020
  81. Johannes SchindelinJun 23, 2020
  82. brian m. carlsonJun 15, 2020
  83. 00/12 Allow overriding the default name of the default branchJohannes Schindelin via GitGitGadget, Jun 15, 2020
  84. 01/12 fast-export: do anonymize the primary branch nameJunio C Hamano via GitGitGadget, Jun 15, 2020
  85. Jeff KingJun 16, 2020
  86. Junio C HamanoJun 17, 2020
  87. Jeff KingJun 17, 2020
  88. Elijah NewrenJun 18, 2020
  89. Junio C HamanoJun 18, 2020
  90. Elijah NewrenJun 18, 2020
  91. Jeff KingJun 18, 2020
  92. 02/12 fmt-merge-msg: introduce a way to override the main branch nameJohannes Schindelin via GitGitGadget, Jun 15, 2020
  93. Phillip WoodJun 15, 2020
  94. Johannes SchindelinJun 23, 2020
  95. Junio C HamanoJun 15, 2020
  96. Johannes SchindelinJun 23, 2020
  97. Ævar Arnfjörð BjarmasonJun 16, 2020
  98. Junio C HamanoJun 17, 2020
  99. Jeff KingJun 16, 2020
  100. Junio C HamanoJun 17, 2020
  101. Johannes SchindelinJun 18, 2020
  102. Johannes SixtJun 17, 2020
  103. Junio C HamanoJun 17, 2020
  104. Johannes SchindelinJun 23, 2020
  105. 05/12 fast-export: handle overridden main branch names correctlyJohannes Schindelin via GitGitGadget, Jun 15, 2020
  106. Phillip WoodJun 15, 2020
  107. Jeff KingJun 16, 2020
  108. Phillip WoodJun 16, 2020
  109. Johannes SchindelinJun 18, 2020
  110. Junio C HamanoJun 15, 2020
  111. Johannes SchindelinJun 23, 2020
  112. 04/12 git_main_branch_name(): optionally report the full ref nameJohannes Schindelin via GitGitGadget, Jun 15, 2020
  113. Phillip WoodJun 15, 2020
  114. Johannes SchindelinJun 23, 2020
  115. 03/12 send-pack/transport-helper: respect `core.mainBranch`Johannes Schindelin via GitGitGadget, Jun 15, 2020
  116. 06/12 branch -m: adjust `core.mainBranch` if necessaryJohannes Schindelin via GitGitGadget, Jun 15, 2020
  117. 12/12 testsvn: respect `init.defaultBranch`Johannes Schindelin via GitGitGadget, Jun 15, 2020
  118. Jeff KingJun 16, 2020
  119. Johannes SchindelinJun 23, 2020
  120. 10/12 remote: learn about the possibly-overridden default main branch nameJohannes Schindelin via GitGitGadget, Jun 15, 2020
  121. 11/12 submodule: use the correct default for the main branch nameJohannes Schindelin via GitGitGadget, Jun 15, 2020
  122. Jeff KingJun 16, 2020
  123. Johannes SchindelinJun 23, 2020
  124. Jeff KingJun 23, 2020
  125. 09/12 clone: handle overridden main branch namesJohannes Schindelin via GitGitGadget, Jun 15, 2020
  126. Jeff KingJun 16, 2020
  127. Johannes SchindelinJun 23, 2020
  128. 08/12 init: allow overriding the default main branch name via the configDon Goodman-Wilson via GitGitGadget, Jun 15, 2020
  129. 07/12 init: allow specifying the main branch name for the new repositoryJohannes Schindelin via GitGitGadget, Jun 15, 2020
  130. 0/8 Allow overriding the default name of the default branchJohannes Schindelin via GitGitGadget, Jun 23, 2020
  131. 2/8 send-pack/transport-helper: avoid mentioning a particular branchJohannes Schindelin via GitGitGadget, Jun 23, 2020
  132. Junio C HamanoJun 24, 2020
  133. Johannes SchindelinJun 24, 2020
  134. Junio C HamanoJun 24, 2020
  135. Johannes SchindelinJun 25, 2020
  136. 1/8 fmt-merge-msg: stop treating `master` speciallyJohannes Schindelin via GitGitGadget, Jun 23, 2020
  137. Junio C HamanoJun 24, 2020
  138. Johannes SchindelinJun 25, 2020
  139. 8/8 testsvn: respect `init.defaultBranch`Johannes Schindelin via GitGitGadget, Jun 23, 2020
  140. 4/8 init: allow specifying the initial branch name for the new repositoryJohannes Schindelin via GitGitGadget, Jun 23, 2020
  141. Junio C HamanoJun 24, 2020
  142. Johannes SchindelinJun 24, 2020
  143. 7/8 remote: use the configured default branch name when appropriateJohannes Schindelin via GitGitGadget, Jun 23, 2020
  144. Junio C HamanoJun 24, 2020
  145. Johannes SchindelinJun 24, 2020
  146. 3/8 submodule: use a better fall-back for missing remote.<name>.branchJohannes Schindelin via GitGitGadget, Jun 23, 2020
  147. Philippe BlainJun 24, 2020
  148. Johannes SchindelinJun 24, 2020
  149. 6/8 clone: use configured default branch name when appropriateJohannes Schindelin via GitGitGadget, Jun 23, 2020
  150. 5/8 init: allow setting the default for the initial branch name via the configDon Goodman-Wilson via GitGitGadget, Jun 23, 2020
  151. Junio C HamanoJun 24, 2020
  152. Johannes SchindelinJun 24, 2020
  153. Junio C HamanoJun 24, 2020
  154. 0/9 Allow overriding the default name of the default branchJohannes Schindelin via GitGitGadget, Jun 24, 2020
  155. 2/9 send-pack/transport-helper: avoid mentioning a particular branchJohannes Schindelin via GitGitGadget, Jun 24, 2020
  156. 7/9 clone: use configured default branch name when appropriateJohannes Schindelin via GitGitGadget, Jun 24, 2020
  157. 9/9 testsvn: respect `init.defaultBranch`Johannes Schindelin via GitGitGadget, Jun 24, 2020
  158. 8/9 remote: use the configured default branch name when appropriateJohannes Schindelin via GitGitGadget, Jun 24, 2020
  159. 4/9 docs: add missing diamond bracketsJohannes Schindelin via GitGitGadget, Jun 24, 2020
  160. 3/9 submodule: fall back to remote's HEAD for missing remote.<name>.branchJohannes Schindelin via GitGitGadget, Jun 24, 2020
  161. Junio C HamanoJun 24, 2020
  162. 1/9 fmt-merge-msg: stop treating `master` speciallyJohannes Schindelin via GitGitGadget, Jun 24, 2020
  163. Đoàn Trần Công DanhJun 29, 2020
  164. Johannes SchindelinJun 29, 2020
  165. Đoàn Trần Công DanhJun 30, 2020
  166. Johannes SchindelinJul 1, 2020
  167. Junio C HamanoJul 1, 2020
  168. 6/9 init: allow setting the default for the initial branch name via the configDon Goodman-Wilson via GitGitGadget, Jun 24, 2020
  169. 5/9 init: allow specifying the initial branch name for the new repositoryJohannes Schindelin via GitGitGadget, Jun 24, 2020
  170. Junio C HamanoJun 24, 2020
  171. Johannes SchindelinJun 25, 2020
  172. brian m. carlsonJun 29, 2020
  173. Edward ThomsonJul 12, 2020
  174. Johannes SchindelinJul 12, 2020
  175. Ævar Arnfjörð BjarmasonJun 16, 2020
  176. Jeff KingJun 16, 2020
  177. Jeff KingJun 16, 2020
  178. Johannes SchindelinJun 23, 2020
  179. Junio C HamanoJun 17, 2020
  180. Johannes SchindelinJul 14, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.