Skip to contentSkip to conversation

git-am: teach git-am to apply a patch to an unborn branch

PATCH In review Nanako Shiraishi 3 participants 3 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 git-am: teach git-am to apply a patch to an unborn branch

---
 git-am.sh     |   33 ++++++++++++++++++++++++++++-----
 t/t4150-am.sh |   15 +++++++++++++++
 2 files changed, 43 insertions(+), 5 deletions(-)
git-am.sh +28 -5
index d339075..c21642b 100755
@@ -36,6 +36,13 @@ cd_to_toplevel
 git var GIT_COMMITTER_IDENT >/dev/null ||
 	die "You need to set your committer info first"
 
+if git rev-parse --verify -q HEAD >/dev/null
+then
+	HAS_HEAD=yes
+else
+	HAS_HEAD=
+fi
+
 sq () {
 	for sqarg
 	do
@@ -290,16 +297,26 @@ else
 		: >"$dotest/rebasing"
 	else
 		: >"$dotest/applying"
-		git update-ref ORIG_HEAD HEAD
+		if test -n "$HAS_HEAD"
+		then
+			git update-ref ORIG_HEAD HEAD
+		else
+			git update-ref -d ORIG_HEAD >/dev/null 2>&1
+		fi
 	fi
 fi
 
 case "$resolved" in
 '')
-	files=$(git diff-index --cached --name-only HEAD --) || exit
+	if test -n "$HAS_HEAD"
+	then
+		files=$(git diff-index --cached --name-only HEAD --)
+	else
+		files=$(git ls-files)
+	fi || exit
 	if test "$files"
 	then
-		: >"$dotest/dirtyindex"
+		test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
 		die "Dirty index: cannot apply patches (dirty: $files)"
 	fi
 esac
@@ -541,7 +558,13 @@ do
 	fi
 
 	tree=$(git write-tree) &&
-	parent=$(git rev-parse --verify HEAD) &&
+	if parent=$(git rev-parse --verify -q HEAD)
+	then
+		pparent="-p $parent"
+	else
+		echo >&2 "applying to an empty history"
+		parent= pparent=
+	fi &&
 	commit=$(
 		if test -n "$ignore_date"
 		then
@@ -552,7 +575,7 @@ do
 			GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
 			export GIT_COMMITTER_DATE
 		fi &&
-		git commit-tree $tree -p $parent <"$dotest/final-commit"
+		git commit-tree $tree $pparent <"$dotest/final-commit"
 	) &&
 	git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
 	stop_here $this
t/t4150-am.sh +15 -0
index 5e65afa..b97d102 100755
@@ -290,4 +290,19 @@ test_expect_success 'am --ignore-date' '
 	echo "$at" | grep "+0000"
 '
 
+test_expect_success 'am in an unborn branch' '
+	rm -fr subdir &&
+	mkdir -p subdir &&
+	git format-patch --numbered-files -o subdir -1 first &&
+	(
+		cd subdir &&
+		git init &&
+		git am 1
+	) &&
+	result=$(
+		cd subdir && git rev-parse HEAD^{tree}
+	) &&
+	test "z$result" = "z$(git rev-parse first^{tree})"
+'
+
 test_done
Signature · 5 lines
1.6.2.1

-- 
Nanako Shiraishi
http://ivory.ap.teacup.com/nanako3/
#1 lore raw
Quoted 1 line · Nanako Shiraishi

Thanks for picking up this topic. It looks sane overall.

git-am.sh do · line 567

Quoted 8 lines
git-am.sh +7 -1
-	parent=$(git rev-parse --verify HEAD) &&
+	if parent=$(git rev-parse --verify -q HEAD)
+	then
+		pparent="-p $parent"
+	else
+		echo >&2 "applying to an empty history"
+		parent= pparent=
+	fi &&

Note that the '&&' here is now pointless, since one side of the conditional will always be true. We are losing the ability to detect errors besides "HEAD does not point to a valid object", but I don't think there is a way to tell the difference from the exit code of rev-parse.

-Peff

#2 lore raw
In reply to Nanako Shiraishi
Quoted 11 lines · Nanako Shiraishi
---
 git-am.sh     |   33 ++++++++++++++++++++++++++++-----
 t/t4150-am.sh |   15 +++++++++++++++
 2 files changed, 43 insertions(+), 5 deletions(-)
git-am.sh +0 -0
index d339075..c21642b 100755

git-am.sh cd_to_toplevel · line 45

Quoted 10 lines
git-am.sh +7 -0
@@ -36,6 +36,13 @@ cd_to_toplevel
 git var GIT_COMMITTER_IDENT >/dev/null ||
 	die "You need to set your committer info first"
 
+if git rev-parse --verify -q HEAD >/dev/null
+then
+	HAS_HEAD=yes
+else
+	HAS_HEAD=
+fi
+

Probably nicer this way than Peff's as I suspect we would need to special case the unborn-branch case a lot more. Have you tried --skip and --abort codepaths with your patch?

Quoted 4 lines
git-am.sh +0 -0
 sq () {
 	for sqarg
 	do

git-am.sh else · line 305

Quoted 10 lines
git-am.sh +6 -1
@@ -290,16 +297,26 @@ else
 		: >"$dotest/rebasing"
 	else
 		: >"$dotest/applying"
-		git update-ref ORIG_HEAD HEAD
+		if test -n "$HAS_HEAD"
+		then
+			git update-ref ORIG_HEAD HEAD
+		else
+			git update-ref -d ORIG_HEAD >/dev/null 2>&1
+		fi

So is this part.

Quoted 9 lines
git-am.sh +3 -1
 	fi
 fi
 
 case "$resolved" in
 '')
-	files=$(git diff-index --cached --name-only HEAD --) || exit
+	if test -n "$HAS_HEAD"
+	then
+		files=$(git diff-index --cached --name-only HEAD --)

git-am.sh else · line 322

Quoted 10 lines
git-am.sh +4 -1
+	else
+		files=$(git ls-files)
+	fi || exit
 	if test "$files"
 	then
-		: >"$dotest/dirtyindex"
+		test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
 		die "Dirty index: cannot apply patches (dirty: $files)"
 	fi
 esac

And here...

Quoted 16 lines
git-am.sh +7 -1
@@ -541,7 +558,13 @@ do
 	fi
 
 	tree=$(git write-tree) &&
-	parent=$(git rev-parse --verify HEAD) &&
+	if parent=$(git rev-parse --verify -q HEAD)
+	then
+		pparent="-p $parent"
+	else
+		echo >&2 "applying to an empty history"
+		parent= pparent=
+	fi &&
 	commit=$(
 		if test -n "$ignore_date"
 		then

git-am.sh do · line 578

Quoted 5 lines
git-am.sh +1 -1
@@ -552,7 +575,7 @@ do
 			GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
 			export GIT_COMMITTER_DATE
 		fi &&
-		git commit-tree $tree -p $parent <"$dotest/final-commit"
+		git commit-tree $tree $pparent <"$dotest/final-commit"

Peff's ${a+something $a} trick looks more "expert" here ;-).

Quoted 13 lines
t/t4150-am.sh +5 -0
index 5e65afa..b97d102 100755
@@ -290,4 +290,19 @@ test_expect_success 'am --ignore-date' '
 	echo "$at" | grep "+0000"
 '
 
+test_expect_success 'am in an unborn branch' '
+	rm -fr subdir &&
+	mkdir -p subdir &&
+	git format-patch --numbered-files -o subdir -1 first &&
+	(

t/t4150-am.sh test_expect_success 'am --ignore-date' ' · line 307

Quoted 10 lines
t/t4150-am.sh +10 -0
+		cd subdir &&
+		git init &&
+		git am 1
+	) &&
+	result=$(
+		cd subdir && git rev-parse HEAD^{tree}
+	) &&
+	test "z$result" = "z$(git rev-parse first^{tree})"
+'
+

Looks good.