Skip to content

Commit e06764c

Browse files
pyokagangitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 20c3fe7 commit e06764c

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

git-am.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,14 @@ then
514514
index_tree=$(git write-tree) &&
515515
orig_head=$(git rev-parse --verify -q ORIG_HEAD || echo $empty_tree) &&
516516
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
518525
fi
519526
rm -fr "$dotest"
520527
exit ;;

t/t4151-am-abort.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ test_expect_success setup '
1414
git add file-1 file-2 &&
1515
git commit -m initial &&
1616
git tag initial &&
17+
git format-patch --stdout --root initial >initial.patch &&
1718
for i in 2 3 4 5 6
1819
do
1920
echo $i >>file-1 &&
@@ -125,4 +126,20 @@ test_expect_success 'am -3 --abort removes otherfile-4 on unborn branch' '
125126
test_path_is_missing otherfile-4
126127
'
127128

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+
128145
test_done

0 commit comments

Comments
 (0)