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

[RFC v3 3/4] submodule: Teach 'add' about a configurable local-branch

From
W. Trevor King <wking@tremily.us>
Date
Jan 9, 2014, 06:17 UTC
Message-ID
<75e8c98df73273c2c8174e726e3fc961fbebd6a7.1389247320.git.wking@tremily.us>
In-Reply-To
<cover.1389247320.git.wking@tremily.us>
From: "W. Trevor King" <wking@tremily.us>

This patch teaches 'git submodule add' to look for a preferred local-branch, and to checkout that branch after the initial clone. The local branch will always point at the commit checked out by the internal 'git clone' operation. For example:

  $ git submodule add git://example.com/subproject.git submod

will checkout the branch pointed to by the cloned repository's HEAD, and call the local branch 'master'.

  $ git submodule add -b my-feature git://example.com/subproject.git submod

will checkout the branch pointed to by the cloned repository's my-feature, and *still* call the local branch 'master'.

'git submodule add' does not always make an initial clone (e.g. if a git repository already exists at the target path). In cases where 'git submodule add' does not clone a repository, we just leave the local branch alone.

This commit also shifts the post-clone branch checkout logic from
cmd_add to module_clone, so it can be shared with cmd_update.  The
previous code only checked out the requested branch in cmd_add but not
in cmd_update; this left the user on a detached HEAD after an update
initially cloned, and subsequent updates kept the HEAD detached,
unless the user moved to the desired branch himself.  Now, unless the
user explicitly asks to work on a detached HEAD, subsequent updates
all happen on the specified branch, which matches the end-user
expectation much better.
---
 git-submodule.sh | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)
diff --git a/git-submodule.sh b/git-submodule.sh
index c5ea7bd..7cee0bf 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -339,7 +339,19 @@ module_clone()
 	echo "gitdir: $rel/$a" >"$sm_path/.git"
 
 	rel=$(echo $a | sed -e 's|[^/][^/]*|..|g')
-	(clear_local_git_env; cd "$sm_path" && GIT_WORK_TREE=. git config core.worktree "$rel/$b")
+	superproject_branch=$(get_current_branch)
+	default_local_branch=$(get_submodule_config "$sm_name" local-branch)
+	(
+		clear_local_git_env
+		cd "$sm_path" &&
+		GIT_WORK_TREE=. git config core.worktree "$rel/$b" &&
+		local_branch=$(get_local_branch "${superproject_branch}" "${default_local_branch}") &&
+		# ash fails to wordsplit ${branch:+-b "$branch"...}
+		case "$branch" in
+		'') git checkout -f -q -B "$local_branch" ;;
+		?*) git checkout -f -q -B "$local_branch" "origin/$branch" ;;
+		esac
+	) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
 }
 
 isnumber()
@@ -503,15 +515,6 @@ Use -f if you really want to add it." >&2
 			fi
 		fi
 		module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" "$depth" || exit
-		(
-			clear_local_git_env
-			cd "$sm_path" &&
-			# ash fails to wordsplit ${branch:+-b "$branch"...}
-			case "$branch" in
-			'') git checkout -f -q ;;
-			?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
-			esac
-		) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
 	fi
 	git config submodule."$sm_name".url "$realrepo"
 
-- 
1.8.5.2.237.g01c62c6
Previous: W. Trevor KingNext: Francesco Pretto
Message 18 of 40 in “git-submodule.sh: Support 'checkout' as a valid update command”
  1. 1/2 git-submodule.sh: Support 'checkout' as a valid update commandFrancesco Pretto, Jan 5, 2014
  2. 2/2 Introduce git submodule attached updateFrancesco Pretto, Jan 5, 2014
  3. Francesco PrettoJan 5, 2014
  4. Heiko VoigtJan 5, 2014
  5. Francesco PrettoJan 5, 2014
  6. Heiko VoigtJan 6, 2014
  7. Francesco PrettoJan 6, 2014
  8. David EngsterJan 6, 2014
  9. W. Trevor KingJan 7, 2014
  10. W. Trevor KingJan 7, 2014
  11. Preferred local submodule branches (was: Introduce git submodule attached update)W. Trevor King, Jan 7, 2014
  12. W. Trevor KingJan 7, 2014
  13. W. Trevor KingJan 8, 2014
  14. W. Trevor KingJan 8, 2014
  15. 0/4 Preferred local submodule branchesW. Trevor King, Jan 9, 2014
  16. 1/4 submodule: Add helpers for configurable local branchesW. Trevor King, Jan 9, 2014
  17. 2/4 submodule: Teach 'update' to preserve local branchesW. Trevor King, Jan 9, 2014
  18. 3/4 submodule: Teach 'add' about a configurable local-branchW. Trevor King, Jan 9, 2014
  19. Francesco PrettoJan 15, 2014
  20. W. Trevor KingJan 15, 2014
  21. 4/4 submodule: Add a new 'checkout' commandW. Trevor King, Jan 9, 2014
  22. Tight submodule bindings (was: Preferred local submodule branches)W. Trevor King, Jan 12, 2014
  23. Jens LehmannJan 13, 2014
  24. W. Trevor KingJan 13, 2014
  25. Junio C HamanoJan 13, 2014
  26. W. Trevor KingJan 14, 2014
  27. Heiko VoigtJan 7, 2014
  28. W. Trevor KingJan 7, 2014
  29. Junio C HamanoJan 7, 2014
  30. Francesco PrettoJan 7, 2014
  31. Junio C HamanoJan 7, 2014
  32. Francesco PrettoJan 7, 2014
  33. Francesco PrettoJan 5, 2014
  34. Heiko VoigtJan 6, 2014
  35. W. Trevor KingJan 6, 2014
  36. Heiko VoigtJan 5, 2014
  37. W. Trevor KingJan 5, 2014
  38. Junio C HamanoJan 6, 2014
  39. Junio C HamanoJan 6, 2014
  40. Francesco PrettoJan 6, 2014

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.