[PATCH] git-submodule - Possibly inherit parent's default remote on init/clone

!MAILaRCHIVE_VOTE_RePLACE
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: <gitster@...>
Cc: <git@...>, Mark Levedahl <mlevedahl@...>
Date: Sunday, January 13, 2008 - 12:27 pm

For submodules defined relative to their parent, it is likely that the
parent's defined default remote is correct for the child as well. This
allows use of remote names other than "origin", important as managed
submodules are typically checked out on a detached head and therefore
submodule-update invokes git-fetch using the default remote. Without this
change, submodules effectively had to have a default remote of "origin."

Signed-off-by: Mark Levedahl <mlevedahl@gmail.com>
---
 Documentation/git-submodule.txt |    8 +++++---
 git-submodule.sh                |   19 +++++++++++++------
 2 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/Documentation/git-submodule.txt b/Documentation/git-submodule.txt
index cffc6d4..440e234 100644
--- a/Documentation/git-submodule.txt
+++ b/Documentation/git-submodule.txt
@@ -36,9 +36,11 @@ status::
 
 init::
 	Initialize the submodules, i.e. register in .git/config each submodule
-	name and url found in .gitmodules. The key used in .git/config is
-	`submodule.$name.url`. This command does not alter existing information
-	in .git/config.
+	name and url found in .gitmodules, along with the default remote origin.
+	For submodules using a relative url, the default remote is inherited
+	from the parent project, for absolute urls the default "origin" is used.
+	The key used in .git/config is submodule.$name.url`. This command does
+	not alter existing information in .git/config.
 
 update::
 	Update the registered submodules, i.e. clone missing submodules and
diff --git a/git-submodule.sh b/git-submodule.sh
index ad9fe62..42be4b9 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -7,6 +7,7 @@
 USAGE='[--quiet] [--cached] [add <repo> [-b branch]|status|init|update] [--] [<path>...]'
 OPTIONS_SPEC=
 . git-sh-setup
+. git-parse-remote
 require_work_tree
 
 add=
@@ -43,9 +44,7 @@ get_repo_base() {
 # Resolve relative url by appending to parent's url
 resolve_relative_url ()
 {
-	branch="$(git symbolic-ref HEAD 2>/dev/null)"
-	remote="$(git config branch.${branch#refs/heads/}.remote)"
-	remote="${remote:-origin}"
+	remote="$(get_default_remote)"
 	remoteurl="$(git config remote.$remote.url)" ||
 		die "remote ($remote) does not have a url in .git/config"
 	url="$1"
@@ -95,6 +94,7 @@ module_clone()
 {
 	path=$1
 	url=$2
+	origin=${3:-origin}
 
 	# If there already is a directory at the submodule path,
 	# expect it to be empty (since that is the default checkout
@@ -110,7 +110,7 @@ module_clone()
 	test -e "$path" &&
 	die "A file already exist at path '$path'"
 
-	git-clone -n "$url" "$path" ||
+	git-clone -n -o "$origin" "$url" "$path" ||
 	die "Clone of '$url' into submodule path '$path' failed"
 }
 
@@ -130,9 +130,11 @@ module_add()
 		usage
 	fi
 
+	origin=origin
 	case "$repo" in
 	./*|../*)
 		# dereference source url relative to parent's url
+		origin=$(get_default_remote)
 		realrepo="$(resolve_relative_url $repo)" ;;
 	*)
 		# Turn the source into an absolute path if
@@ -157,7 +159,7 @@ module_add()
 	git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
 	die "'$path' already exists in the index"
 
-	module_clone "$path" "$realrepo" || exit
+	module_clone "$path" "$realrepo" "$origin" || exit
 	(unset GIT_DIR; cd "$path" && git checkout -q ${branch:+-b "$branch" "origin/$branch"}) ||
 	die "Unable to checkout submodule '$path'"
 	git add "$path" ||
@@ -189,12 +191,15 @@ modules_init()
 		die "No url found for submodule path '$path' in .gitmodules"
 
 		# Possibly a url relative to parent
+		origin=origin
 		case "$url" in
 		./*|../*)
 			url="$(resolve_relative_url "$url")"
+			origin=$(get_default_remote)
 			;;
 		esac
 
+		git config submodule."$name".origin "$origin" &&
 		git config submodule."$name".url "$url" ||
 		die "Failed to register url for submodule path '$path'"
 
@@ -222,10 +227,12 @@ modules_update()
 			say "Submodule path '$path' not initialized"
 			continue
 		fi
+		origin=$(git config submodule."$name".origin)
+		origin=${origin:-origin}
 
 		if ! test -d "$path"/.git
 		then
-			module_clone "$path" "$url" || exit
+			module_clone "$path" "$url" "$origin" || exit
 			subsha1=
 		else
 			subsha1=$(unset GIT_DIR; cd "$path" &&
-- 
1.5.4.rc3.14.gc50f

-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
Re: [PATCH] Teach remote machinery about remotes.default con..., Johannes Schindelin, (Sun Jan 13, 5:27 pm)
What's not in 'master' but should be, Junio C Hamano, (Fri Jan 18, 5:41 am)
Re: What's not in 'master' but should be, Johannes Schindelin, (Fri Jan 18, 2:28 pm)
Re: [PATCH] http-push: fix webdav lock leak., Grégoire Barbier, (Sat Jan 19, 11:21 am)
Re: [PATCH] http-push: fix webdav lock leak., Johannes Schindelin, (Sat Jan 19, 7:38 pm)
Re: What's not in 'master' but should be, Mike Hommey, (Sat Jan 19, 2:14 am)
Re: What's not in 'master' but should be, Johannes Schindelin, (Fri Jan 18, 2:36 pm)
Re: What's not in 'master' but should be, Johannes Schindelin, (Mon Feb 18, 3:57 pm)
What's not in 'master', and likely not to be until 1.5.4, Junio C Hamano, (Fri Jan 18, 6:40 am)
Re: What's not in 'master', and likely not to be until 1.5.4, Steffen Prohaska, (Fri Jan 18, 8:53 am)
Re: What's not in 'master', and likely not to be until 1.5.4, Johannes Schindelin, (Fri Jan 18, 9:09 am)
Re: What's not in 'master', and likely not to be until 1.5.4, Steffen Prohaska, (Fri Jan 18, 9:23 am)
Re: What's not in 'master', and likely not to be until 1.5.4, Steffen Prohaska, (Mon Jan 21, 2:42 am)
Re: What's not in 'master', and likely not to be until 1.5.4, Johannes Schindelin, (Fri Jan 18, 4:36 pm)
Re: What's not in 'master', and likely not to be until 1.5.4, Johannes Schindelin, (Fri Jan 18, 4:58 pm)
Re: What's not in 'master', and likely not to be until 1.5.4, Shawn O. Pearce, (Mon Jan 21, 12:46 am)
Re: What's not in 'master', and likely not to be until 1.5.4, Johannes Schindelin, (Mon Jan 21, 6:37 am)
Re: What's not in 'master', and likely not to be until 1.5.4, Shawn O. Pearce, (Wed Jan 23, 12:44 am)
Re: What's not in 'master', and likely not to be until 1.5.4, Johannes Schindelin, (Wed Jan 23, 7:12 am)
Re: What's not in 'master', and likely not to be until 1.5.4, Steffen Prohaska, (Fri Jan 18, 9:04 am)
Re: What's not in 'master', and likely not to be until 1.5.4, Johannes Schindelin, (Fri Jan 18, 9:11 am)
Re: What's not in 'master' but should be, Lars Hjemli, (Fri Jan 18, 6:15 am)
Re: What's not in 'master' but should be, Junio C Hamano, (Fri Jan 18, 6:24 am)
Re: What's not in 'master' but should be, Lars Hjemli, (Fri Jan 18, 6:53 am)
Re: What's not in 'master' but should be, Junio C Hamano, (Fri Jan 18, 7:09 am)
Re: What's not in 'master' but should be, Lars Hjemli, (Fri Jan 18, 7:54 am)
Re: What's not in 'master' but should be, Johannes Schindelin, (Fri Jan 18, 8:34 am)
Re: What's not in 'master' but should be, Lars Hjemli, (Fri Jan 18, 10:19 am)
Re: [PATCH] Teach remote machinery about remotes.default con..., Johannes Schindelin, (Mon Jan 14, 7:18 am)
Re: safecrlf not in 1.5.4, Junio C Hamano, (Mon Jan 14, 3:30 am)
Re: safecrlf not in 1.5.4, Dmitry Potapov, (Mon Jan 14, 5:04 am)
Re: safecrlf not in 1.5.4, Pierre Habouzit, (Mon Jan 14, 1:35 pm)
Re: [msysGit] Re: safecrlf not in 1.5.4, Steffen Prohaska, (Mon Jan 14, 4:29 am)
Re: [msysGit] Re: safecrlf not in 1.5.4, Junio C Hamano, (Mon Jan 14, 3:41 pm)
[PATCH] git-clone - Set remotes.origin config variable, Mark Levedahl, (Sun Jan 13, 12:27 pm)
[PATCH] git-submodule - Possibly inherit parent's default re..., Mark Levedahl, (Sun Jan 13, 12:27 pm)