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

Re: Re: [PATCH v4 0/4] git-submodule add: Add --local-branch option

From
Heiko Voigt <hvoigt@hvoigt.net>
Date
Nov 27, 2012, 23:28 UTC
Message-ID
<20121127232858.GA4742@book.hvoigt.net>
In-Reply-To
<20121127190105.GQ10656@odin.tremily.us>
Hi,
On Tue, Nov 27, 2012 at 02:01:05PM -0500, W. Trevor King wrote:
Show 7 quoted lines
> On Tue, Nov 27, 2012 at 07:31:25PM +0100, Heiko Voigt wrote:
> The v4 series leaves the remote branch amigious, but it helps you
> point the local branch at the right hash so that future calls to
> 
>   $ git submodule foreach 'git pull'
> 
> can use the branch's .git/modules/<name>/config settings.

But IMO thats the functionality which should be implemented in submodule update and not left to the user.

Show 17 quoted lines
> > I would think more of some convention like:
> > 
> > 	$ git checkout -t origin/$branch
> > 
> > when first initialising the submodule with e.g.
> > 
> > 	$ git submodule update --init --branch
> > 
> > Then later calls of
> > 
> > 	$ git submodule update --branch
> > 
> > would have a branch configured to pull from. I imagine that results in
> > a similar behavior gerrit is doing on the server side?
> 
> That sounds like it's doing pretty much the same thing.  Can you think
> of a test that would distinguish it from my current v4 implementation?

Well the main difference is that gerrit is automatically updating the superproject AFAIK. I would like it if we could implement the same workflow support in the submodule script. It seems to me that this is already proven to be useful workflow.

I do not have a test but a small draft diff (completely untested, quick and dirty) to illustrate the approach I am talking about.

You can find the whole change at
https://github.com/hvoigt/git/commits/hv/floating_submodules_draft
and the interesting patch for easy commenting below[1].
Show 8 quoted lines
> > How about reusing the -b|--branch option for add? Since we only change
> > the behavior when submodule.$name.update is set to branch it seems
> > reasonable to me. Opinions?
> 
> That was the approach I used in v1, but people were concerned that we
> would be stomping on previously unclaimed config space.  Since noone
> has pointed out other uses besides Gerrit's very similar case, I'm not
> sure if that is still an issue.
Could you point me to that mail? I cannot seem to find it in my archive.
Show 23 quoted lines
> > > Because you need to recurse through submodules for `update --branch`
> > > even if "$subsha1" == "$sha1", I had to amend the conditional
> > > controlling that block.  This broke one of the existing tests, which I
> > > "fixed" in patch 4.  I think a proper fix would involve rewriting
> > > 
> > >   (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'")"
> > > 
> > > but I'm not familiar enough with rev-list to want to dig into that
> > > yet.  If feedback for the earlier three patches is positive, I'll work
> > > up a clean fix and resubmit.
> > 
> > You probably need to separate your handling here. The comparison of the
> > currently checked out sha1 and the recorded sha1 is an optimization
> > which skips unnecessary fetching in case the submodules commits are
> > already correct. This code snippet checks whether the to be checked out
> > sha1 is already local and also skips the fetch if it is. We should not
> > break that.
> 
> Agreed.  However, determining if the target $sha1 is local should have
> nothing to do with the current checked out $subsha1.
See my draft or the diff below for an illustration of the splitup.
Cheers Heiko
[1]
diff --git a/git-submodule.sh b/git-submodule.sh
index 9ad4370..3fa1465 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -183,6 +183,7 @@ module_clone()
 	sm_path=$1
 	url=$2
 	reference="$3"
+	branch="$4"
 	quiet=
 	if test -n "$GIT_QUIET"
 	then
@@ -209,6 +210,8 @@ module_clone()
 			clear_local_git_env
 			git clone $quiet -n ${reference:+"$reference"} \
 				--separate-git-dir "$gitdir" "$url" "$sm_path"
+			test -n "$branch" && (cd $sm_path &&
+				git checkout -t origin/$branch)
 		) ||
 		die "$(eval_gettext "Clone of '\$url' into submodule path '\$sm_path' failed")"
 	fi
@@ -361,7 +364,7 @@ Use -f if you really want to add it." >&2
 
 	else
 
-		module_clone "$sm_path" "$realrepo" "$reference" || exit
+		module_clone "$sm_path" "$realrepo" "$reference" "$local_branch" || exit
 		(
 			clear_local_git_env
 			cd "$sm_path" &&
@@ -577,6 +580,12 @@ handle_on_demand_update () {
 	fi
 }
 
+handle_tracking_branch_update () {
+	(clear_local_git_env; cd "$sm_path" &&
+		git-checkout $branch && git-pull --ff-only) ||
+	die "$(eval_gettext "Unable to pull branch '\$branch' in submodule path '\$sm_path'")"
+}
+
 #
 # Update each submodule path to correct revision, using clone and checkout as needed
 #
@@ -648,6 +657,7 @@ cmd_update()
 	cloned_modules=
 	module_list "$@" | {
 	err=
+	floating_submodules=
 	while read mode sha1 stage sm_path
 	do
 		die_if_unmatched "$mode"
@@ -684,7 +694,7 @@ Maybe you want to use 'update --init'?")"
 
 		if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
 		then
-			module_clone "$sm_path" "$url" "$reference"|| exit
+			module_clone "$sm_path" "$url" "$reference" "$branch" || exit
 			cloned_modules="$cloned_modules;$name"
 			subsha1=
 		else
@@ -693,7 +703,13 @@ Maybe you want to use 'update --init'?")"
 			die "$(eval_gettext "Unable to find current revision in submodule path '\$sm_path'")"
 		fi
 
-		handle_on_demand_update
+		if test "$update_module" = "branch"
+		then
+			handle_tracking_branch_update
+			floating_submodules="$floating_submodules $sm_path"
+		else
+			handle_on_demand_update
+		fi
 
 		if test -n "$recursive"
 		then
@@ -727,6 +743,11 @@ Maybe you want to use 'update --init'?")"
 		IFS=$OIFS
 		exit 1
 	fi
+	if test -n "$floating_submodules"
+	then
+		git add $floating_submodules &&
+		git commit -m "Updated submodules"
+	fi
 	}
 }
Previous: W. Trevor KingNext: W. Trevor King
Message 40 of 49 in “git-submodule add: Add -r/--record option”
  1. 0/3 git-submodule add: Add -r/--record optionW. Trevor King, Nov 9, 2012
  2. 1/3 git-submodule add: Add -r/--record optionW. Trevor King, Nov 9, 2012
  3. Junio C HamanoNov 9, 2012
  4. Heiko VoigtNov 9, 2012
  5. W. Trevor KingNov 10, 2012
  6. W. Trevor KingNov 10, 2012
  7. Heiko VoigtNov 17, 2012
  8. Junio C HamanoNov 11, 2012
  9. W. Trevor KingNov 11, 2012
  10. Heiko VoigtNov 17, 2012
  11. W. Trevor KingNov 17, 2012
  12. Heiko VoigtNov 17, 2012
  13. W. Trevor KingNov 17, 2012
  14. Junio C HamanoNov 20, 2012
  15. W. Trevor KingNov 20, 2012
  16. Junio C HamanoNov 20, 2012
  17. W. Trevor KingNov 20, 2012
  18. Junio C HamanoNov 20, 2012
  19. Heiko VoigtNov 23, 2012
  20. Sascha CunzNov 23, 2012
  21. Heiko VoigtNov 23, 2012
  22. W. Trevor KingNov 23, 2012
  23. W. Trevor KingNov 23, 2012
  24. W. Trevor KingNov 23, 2012
  25. 0/4 git-submodule add: Add --local-branch optionW. Trevor King, Nov 26, 2012
  26. 1/4 git-submodule add: Add --local-branch optionW. Trevor King, Nov 26, 2012
  27. 2/4 git-submodule init: Record submodule.<name>.branch in repository config.W. Trevor King, Nov 26, 2012
  28. Jens LehmannNov 27, 2012
  29. W. Trevor KingNov 28, 2012
  30. 3/4 git-submodule update: Add --branch optionW. Trevor King, Nov 26, 2012
  31. Heiko VoigtNov 27, 2012
  32. W. Trevor KingNov 27, 2012
  33. [RFC] git-submodule update: Add --commit optionW. Trevor King, Nov 29, 2012
  34. W. Trevor KingNov 29, 2012
  35. W. Trevor KingNov 29, 2012
  36. 4/4 Hack fix for 'submodule update does not fetch already present commits'W. Trevor King, Nov 26, 2012
  37. W. Trevor KingNov 27, 2012
  38. Heiko VoigtNov 27, 2012
  39. W. Trevor KingNov 27, 2012
  40. Heiko VoigtNov 27, 2012
  41. W. Trevor KingNov 28, 2012
  42. Phil HordNov 29, 2012
  43. W. Trevor KingNov 27, 2012
  44. Heiko VoigtNov 27, 2012
  45. 2/3 git-submodule foreach: export .gitmodules settings as variablesW. Trevor King, Nov 9, 2012
  46. Heiko VoigtNov 9, 2012
  47. W. Trevor KingNov 10, 2012
  48. 3/3 git-submodule: Motivate --record with an example use caseW. Trevor King, Nov 9, 2012
  49. W. Trevor KingNov 10, 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.