am --abort: support aborting to unborn branch

When git-am is first run on an unborn branch, no ORIG_HEAD is created. As such, any applied commits will remain even after a git am --abort. To be consistent with the behavior of git am --abort when it is not run from an unborn branch, we empty the index, and then destroy the branch pointed to by HEAD if there is no ORIG_HEAD. Signed-off-by: Paul Tan <pyokagan@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Paul Tan committed Jun 6, 2015 at 19:46 UTC e06764c8ebf87a80737dc0a6198a013799f18e32
2 files changed +25 -1
git-am.sh
+8 -1
@@ -514,7 +514,14 @@ then
514 index_tree=$(git write-tree) &&
515 orig_head=$(git rev-parse --verify -q ORIG_HEAD || echo $empty_tree) &&
516 git read-tree -m -u $index_tree $orig_head
517 - git reset ORIG_HEAD
517 + if git rev-parse --verify -q ORIG_HEAD >/dev/null 2>&1
518 + then
519 + git reset ORIG_HEAD
520 + else
521 + git read-tree $empty_tree
522 + curr_branch=$(git symbolic-ref HEAD 2>/dev/null) &&
523 + git update-ref -d $curr_branch
524 + fi
525 fi
526 rm -fr "$dotest"
527 exit ;;
t/t4151-am-abort.sh
+17
@@ -14,6 +14,7 @@ test_expect_success setup '
14 git add file-1 file-2 &&
15 git commit -m initial &&
16 git tag initial &&
17 + git format-patch --stdout --root initial >initial.patch &&
18 for i in 2 3 4 5 6
19 do
20 echo $i >>file-1 &&
@@ -125,4 +126,20 @@ test_expect_success 'am -3 --abort removes otherfile-4 on unborn branch' '
126 test_path_is_missing otherfile-4
127 '
128
129 +test_expect_success 'am -3 --abort on unborn branch removes applied commits' '
130 + git checkout -f --orphan orphan &&
131 + git reset &&
132 + rm -f otherfile-4 otherfile-2 file-1 file-2 &&
133 + test_must_fail git am -3 initial.patch 0003-*.patch &&
134 + test 3 -eq $(git ls-files -u | wc -l) &&
135 + test 4 = "$(cat otherfile-4)" &&
136 + git am --abort &&
137 + test -z "$(git ls-files -u)" &&
138 + test_path_is_missing otherfile-4 &&
139 + test_path_is_missing file-1 &&
140 + test_path_is_missing file-2 &&
141 + test 0 -eq $(git log --oneline 2>/dev/null | wc -l) &&
142 + test refs/heads/orphan = "$(git symbolic-ref HEAD)"
143 +'
144 +
145 test_done