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

[PATCH v6 4/4] submodule update: add submodule.<name>.remote config option

From
W. Trevor King <wking@tremily.us>
Date
Dec 2, 2012, 03:17 UTC
Message-ID
<b9635b844051e681af1c80447fedac5ee77280f7.1354417619.git.wking@tremily.us>
In-Reply-To
<cover.1354417618.git.wking@tremily.us>
From: "W. Trevor King" <wking@tremily.us>

Don't force the user to clone from the tracked repository (branch.<name>.remote) or `origin`. By setting submodule.<name>.remote in .gitmodules or the usual git config files, you can easily point a submodule at a different remote when using `submodule update --remote`.

The configured remote name is also used in `submodule sync` to determine which remote.<name>.url is updated with the submodule's origin URL.

Signed-off-by: W. Trevor King <wking@tremily.us>
---
 Documentation/config.txt        |  7 ++++---
 Documentation/git-submodule.txt | 10 +++++-----
 git-submodule.sh                | 27 +++++++++++++++++++++------
 t/t7406-submodule-update.sh     | 18 ++++++++++++++++++
 4 files changed, 48 insertions(+), 14 deletions(-)
diff --git a/Documentation/config.txt b/Documentation/config.txt
index 6f4663c..c54b9b4 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -1999,10 +1999,11 @@ submodule.<name>.update::
 	by 'git submodule init'; edit them to override the
 	URL and other values found in the `.gitmodules` file.  See
 
+submodule.<name>.remote::
 submodule.<name>.branch::
-	The remote branch name for a submodule, used by `git submodule
-	update --remote`.  Set this option to override the value found in
-	the `.gitmodules` file.  See linkgit:git-submodule[1] and
+	The remote repository and branch names for a submodule, used by `git
+	submodule update --remote`.  Set these options to override the value
+	found in the `.gitmodules` file.  See linkgit:git-submodule[1] and
 	linkgit:gitmodules[5] for details.
 
 submodule.<name>.fetchRecurseSubmodules::
diff --git a/Documentation/git-submodule.txt b/Documentation/git-submodule.txt
index 988bba9..1d8d5f1 100644
--- a/Documentation/git-submodule.txt
+++ b/Documentation/git-submodule.txt
@@ -242,11 +242,11 @@ OPTIONS
 	This option is only valid for the update command.  Instead of using
 	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).
+	is branch's remote (`branch.<name>.remote`, defaulting to `origin`),
+	and the remote branch used defaults to `master`, but either may be
+	overridden by setting the `submodule.<name>.remote` or
+	`submodule.<name>.branch` option in `.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/git-submodule.sh b/git-submodule.sh
index 27b02fe..3e39e29 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -179,6 +179,21 @@ get_submodule_config()
 	printf '%s' "${value:-$default}"
 }
 
+#
+# Print the name of a submodule's configured remote
+#
+# $1 = submodule name
+#
+get_submodule_remote()
+{
+	name="$1"
+	remote=$(get_submodule_config "$name" remote)
+	if test -z "$remote"
+	then
+		remote=$(get_default_remote)
+	fi
+	printf '%s' "${remote}"
+}
 
 #
 # Map submodule path to submodule name
@@ -605,6 +620,7 @@ cmd_update()
 		fi
 		name=$(module_name "$sm_path") || exit
 		url=$(git config submodule."$name".url)
+		remote_name=$(get_submodule_remote "$name")
 		branch=$(get_submodule_config "$name" branch master)
 		if ! test -z "$update"
 		then
@@ -645,10 +661,9 @@ Maybe you want to use 'update --init'?")"
 			if test -z "$nofetch"
 			then
 				# Fetch remote before determining tracking $sha1
-				(clear_local_git_env; cd "$sm_path" && git-fetch) ||
-				die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
+				(clear_local_git_env; cd "$sm_path" && git-fetch "$remote_name") ||
+				die "$(eval_gettext "Unable to fetch '\$remote_name' in submodule path '\$sm_path'")"
 			fi
-			remote_name=$(get_default_remote)
 			sha1=$(clear_local_git_env; cd "$sm_path" &&
 				git rev-parse --verify "${remote_name}/${branch}") ||
 			die "$(eval_gettext "Unable to find current ${remote_name}/${branch} revision in submodule path '\$sm_path'")"
@@ -669,8 +684,8 @@ Maybe you want to use 'update --init'?")"
 				# is not reachable from a ref.
 				(clear_local_git_env; cd "$sm_path" &&
 					( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
-					 test -z "$rev") || git-fetch)) ||
-				die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
+					 test -z "$rev") || git-fetch "$remote_name")) ||
+				die "$(eval_gettext "Unable to fetch '\$remote_name' in submodule path '\$sm_path'")"
 			fi
 
 			# Is this something we just cloned?
@@ -1110,7 +1125,7 @@ cmd_sync()
 			(
 				clear_local_git_env
 				cd "$sm_path"
-				remote=$(get_default_remote)
+				remote=$(get_submodule_remote "$name")
 				git config remote."$remote".url "$sub_origin_url"
 			)
 			fi
diff --git a/t/t7406-submodule-update.sh b/t/t7406-submodule-update.sh
index a567834..86c85f8 100755
--- a/t/t7406-submodule-update.sh
+++ b/t/t7406-submodule-update.sh
@@ -149,6 +149,24 @@ test_expect_success 'submodule update --remote should fetch upstream changes' '
 	)
 '
 
+test_expect_success 'local config should override .gitmodules remote' '
+	(cd submodule &&
+	 echo line5-master >> file &&
+	 git add file &&
+	 test_tick &&
+	 git commit -m "upstream line5-master"
+	) &&
+	(cd super/submodule &&
+	 git remote rename origin test-remote
+	) &&
+	(cd super &&
+	 git config submodule.submodule.remote test-remote &&
+	 git submodule update --remote --force submodule &&
+	 cd submodule &&
+	 test "$(git log -1 --oneline)" = "$(GIT_DIR=../../submodule/.git git log -1 --oneline)"
+	)
+'
+
 test_expect_success 'local config should override .gitmodules branch' '
 	(cd submodule &&
 	 git checkout -b test-branch &&
-- 
1.8.0.4.gf74b0fc.dirty
Previous: W. Trevor KingNext: Jens Lehmann
Message 66 of 67 in “Re: [PATCH v5 0/2] submodule update: add --remote for submodule's upstream changes”
  1. W. Trevor KingNov 29, 2012
  2. Phil HordNov 30, 2012
  3. W. Trevor KingNov 30, 2012
  4. [RFC] remove/deprecate 'submodule init' and 'sync'W. Trevor King, Nov 30, 2012
  5. W. Trevor KingNov 30, 2012
  6. Phil HordNov 30, 2012
  7. W. Trevor KingDec 1, 2012
  8. Jens LehmannDec 1, 2012
  9. W. Trevor KingDec 1, 2012
  10. Jens LehmannDec 1, 2012
  11. W. Trevor KingDec 1, 2012
  12. Jens LehmannDec 1, 2012
  13. W. Trevor KingDec 1, 2012
  14. Jens LehmannDec 1, 2012
  15. W. Trevor KingDec 1, 2012
  16. submodule: add 'deinit' commandJens Lehmann, Dec 1, 2012
  17. Junio C HamanoDec 2, 2012
  18. W. Trevor KingDec 2, 2012
  19. Jens LehmannDec 2, 2012
  20. W. Trevor KingDec 2, 2012
  21. W. Trevor KingDec 3, 2012
  22. Jens LehmannDec 2, 2012
  23. Junio C HamanoDec 3, 2012
  24. submodule: add 'deinit' commandJens Lehmann, Dec 4, 2012
  25. Junio C HamanoDec 4, 2012
  26. Michael J GruberDec 12, 2012
  27. Jens LehmannDec 12, 2012
  28. Junio C HamanoDec 12, 2012
  29. Jens LehmannDec 12, 2012
  30. Junio C HamanoDec 12, 2012
  31. W. Trevor KingDec 12, 2012
  32. Junio C HamanoDec 12, 2012
  33. W. Trevor KingDec 13, 2012
  34. Marc BranchaudDec 13, 2012
  35. Jens LehmannDec 1, 2012
  36. W. Trevor KingDec 1, 2012
  37. 0/4 submodule update: add --remote for submodule's upstream changesW. Trevor King, Dec 2, 2012
  38. 1/4 submodule: add get_submodule_config helper funtionW. Trevor King, Dec 2, 2012
  39. Junio C HamanoDec 3, 2012
  40. 2/4 submodule update: add --remote for submodule's upstream changesW. Trevor King, Dec 2, 2012
  41. Junio C HamanoDec 3, 2012
  42. W. Trevor KingDec 3, 2012
  43. W. Trevor KingDec 3, 2012
  44. Junio C HamanoDec 3, 2012
  45. W. Trevor KingDec 4, 2012
  46. 0/3 submodule update: add --remote for submodule's upstream changesW. Trevor King, Dec 11, 2012
  47. 1/3 submodule: add get_submodule_config helper funtionW. Trevor King, Dec 11, 2012
  48. 2/3 submodule update: add --remote for submodule's upstream changesW. Trevor King, Dec 11, 2012
  49. Phil HordDec 12, 2012
  50. Junio C HamanoDec 12, 2012
  51. W. Trevor KingDec 12, 2012
  52. 0/3 submodule update: add --remote for submodule's upstream changeswking@tremily.us, Dec 19, 2012
  53. 1/3 submodule: add get_submodule_config helper funtionwking@tremily.us, Dec 19, 2012
  54. Heiko VoigtDec 21, 2012
  55. W. Trevor KingDec 21, 2012
  56. 2/3 submodule update: add --remote for submodule's upstream changeswking@tremily.us, Dec 19, 2012
  57. 3/3 submodule add: If --branch is given, record it in .gitmoduleswking@tremily.us, Dec 19, 2012
  58. Junio C HamanoDec 19, 2012
  59. Heiko VoigtDec 21, 2012
  60. 3/3 submodule add: If --branch is given, record it in .gitmodulesW. Trevor King, Dec 11, 2012
  61. Junio C HamanoDec 12, 2012
  62. W. Trevor KingDec 12, 2012
  63. Junio C HamanoDec 12, 2012
  64. W. Trevor KingDec 12, 2012
  65. 3/4 submodule add: If --branch is given, record it in .gitmodulesW. Trevor King, Dec 2, 2012
  66. 4/4 submodule update: add submodule.<name>.remote config optionW. Trevor King, Dec 2, 2012
  67. Jens LehmannDec 2, 2012

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.