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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



	git submodule add -b $branch $repository

fails when HEAD already points to $branch in $repository.

When the freshly cloned submodules HEAD is the same as the checked out
branch, it doesn't make sense to update it again as "git checkout -b"
would fail with »fatal: git checkout: branch $branch already exists«.

Reported-by: Klaus Ethgen <Klaus@xxxxxxxxx>
Thanks-to: Jonathan Nieder <jrnieder@xxxxxxxxx>
Signed-off-by: Jens Lehmann <Jens.Lehmann@xxxxxx>
---

Am 08.12.2010 22:35, schrieb Jens Lehmann:
> Am 07.12.2010 23:57, schrieb Junio C Hamano:
>> Jens Lehmann <Jens.Lehmann@xxxxxx> writes:
>>
>>> Nope, these lines date back to the time before I got involved in the
>>> submodule business ... Seems like this "git checkout" was added in
>>> March 2008 by Mark Levedahl (CCed), maybe he can shed some light on
>>> that.
>>>
>>> But to me your change looks good, so feel free to add:
>>> Acked-by: Jens Lehmann <Jens.Lehmann@xxxxxx>
>>
>> Does either of you want to add a test for this?
> 
> Will do.

And as it happens from time to time, while writing the test you find
out that the first attempt to fix the bug didn't work as expected ...

 git-submodule.sh           |    4 +++-
 t/t7400-submodule-basic.sh |    7 +++++++
 2 files changed, 10 insertions(+), 1 deletions(-)

diff --git a/git-submodule.sh b/git-submodule.sh
index 33bc41f..bf2803f 100755
--- 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
+				git checkout -f -q -b "$branch" "origin/$branch"
+			fi ;;
 			esac
 		) || die "Unable to checkout submodule '$path'"
 	fi
diff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh
index 782b0a3..e224da4 100755
--- a/t/t7400-submodule-basic.sh
+++ b/t/t7400-submodule-basic.sh
@@ -131,6 +131,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 &&
-- 
1.7.3.3.580.ged75d

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]