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

Re: [PATCH] git submodule -b ... of current HEAD fails

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Dec 8, 2010, 23:45 UTC
Message-ID
<20101208234508.GB8865@burratino>
In-Reply-To
<4D001292.3020503@web.de>
Jens Lehmann wrote:
Show 8 quoted lines
> --- a/git-submodule.sh
> +++ b/git-submodule.sh
> @@ -241,7 +241,9 @@ cmd_add()
>  			# ash fails to wordsplit ${branch:+-b "$branch"...}
>  			case "$branch" in
>  			'') git checkout -f -q ;;
> -			?*) git checkout -f -q -b "$branch" "origin/$branch" ;;
> +			?*) if [ "$(git branch)" != "* $branch"  ]; then
Agh.  The command to use is "git symbolic-ref -q HEAD", I suppose.
Maybe we can simplify by relying on "git clone".
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
 git-submodule.sh           |   16 ++++++----------
 t/t7400-submodule-basic.sh |    8 +++++++-
 2 files changed, 13 insertions(+), 11 deletions(-)
diff --git a/git-submodule.sh b/git-submodule.sh
index d937f0b..6fd09e7 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -219,17 +219,13 @@ cmd_add()
 		esac
 		git config submodule."$path".url "$url"
 	else
-
-		module_clone "$path" "$realrepo" "$reference" || exit
+		brancharg=${branch:+"-b $(git rev-parse --sq-quote "$branch")"}
+		refarg=${reference:+"$(git rev-parse --sq-quote "$reference")"}
 		(
-			clear_local_git_env
-			cd "$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 "Unable to checkout submodule '$path'"
+			export realrepo path &&
+			eval "git clone $brancharg $refarg" '"$realrepo" "$path"'
+		) ||
+		die "Clone of '$realrepo' into submodule path '$path' failed"
 	fi
 
 	git add $force "$path" ||
diff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh
index 2c49db9..77088cc 100755
--- a/t/t7400-submodule-basic.sh
+++ b/t/t7400-submodule-basic.sh
@@ -114,7 +114,6 @@ test_expect_success 'submodule add --branch' '
 	echo "refs/heads/initial" >expect-head &&
 	cat <<-\EOF >expect-heads &&
 	refs/heads/initial
-	refs/heads/master
 	EOF
 	>empty &&
 
@@ -131,6 +130,13 @@ test_expect_success 'submodule add --branch' '
 	test_cmp empty untracked
 '
 
+test_expect_success 'submodule add --branch succeeds even when branch is at HEAD' '
+	(
+		cd addtest &&
+		git submodule add -b master "$submodurl" submod-existing-branch
+	)
+'
+
 test_expect_success 'submodule add with ./ in path' '
 	echo "refs/heads/master" >expect &&
 	>empty &&
Previous: Jens LehmannNext: Junio C Hamano
Message 10 of 16 in “Re: git submodule -b ... of current HEAD fails”
  1. Re: git submodule -b ... of current HEAD failsJonathan Nieder, Dec 1, 2010
  2. Jens LehmannDec 2, 2010
  3. Mark LevedahlDec 3, 2010
  4. Ben JacksonDec 3, 2010
  5. Jonathan NiederDec 3, 2010
  6. git submodule: Remove now obsolete tests before cloning a repoJens Lehmann, Dec 4, 2010
  7. Junio C HamanoDec 7, 2010
  8. Jens LehmannDec 8, 2010
  9. git submodule -b ... of current HEAD failsJens Lehmann, Dec 8, 2010
  10. Jonathan NiederDec 8, 2010
  11. Junio C HamanoDec 28, 2010
  12. Jens LehmannDec 29, 2010
  13. Junio C HamanoDec 29, 2010
  14. Jens LehmannDec 29, 2010
  15. Re* [RFC/PATCH] Re: git submodule -b ... of current HEAD failsJunio C Hamano, Dec 29, 2010
  16. Jens LehmannDec 29, 2010

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.