Skip to content

Commit 7b3b7e3

Browse files
committed
am --abort: keep unrelated commits since the last failure and warn
After making commits (either by pulling or doing their own work) after a failed "am", the user will be reminded by next "am" invocation that there was a failed "am" that the user needs to decide to resolve or to get rid of the old "am" attempt. The "am --abort" option was meant to help the latter. However, it rewinded the HEAD back to the beginning of the failed "am" attempt, discarding commits made (perhaps by mistake) since. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 05bb5a2 commit 7b3b7e3

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

git-am.sh

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,31 @@ sq () {
6868

6969
stop_here () {
7070
echo "$1" >"$dotest/next"
71+
git rev-parse --verify -q HEAD >"$dotest/abort-safety"
7172
exit 1
7273
}
7374

75+
safe_to_abort () {
76+
if test -f "$dotest/dirtyindex"
77+
then
78+
return 1
79+
fi
80+
81+
if ! test -s "$dotest/abort-safety"
82+
then
83+
return 0
84+
fi
85+
86+
abort_safety=$(cat "$dotest/abort-safety")
87+
if test "z$(git rev-parse --verify -q HEAD)" = "z$abort_safety"
88+
then
89+
return 0
90+
fi
91+
echo >&2 "You seem to have moved HEAD since the last 'am' failure."
92+
echo >&2 "Not rewinding to ORIG_HEAD"
93+
return 1
94+
}
95+
7496
stop_here_user_resolve () {
7597
if [ -n "$resolvemsg" ]; then
7698
printf '%s\n' "$resolvemsg"
@@ -419,10 +441,11 @@ then
419441
exec git rebase --abort
420442
fi
421443
git rerere clear
422-
test -f "$dotest/dirtyindex" || {
444+
if safe_to_abort
445+
then
423446
git read-tree --reset -u HEAD ORIG_HEAD
424447
git reset ORIG_HEAD
425-
}
448+
fi
426449
rm -fr "$dotest"
427450
exit ;;
428451
esac

t/t4151-am-abort.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,13 @@ do
6262

6363
done
6464

65+
test_expect_success 'am --abort will keep the local commits intact' '
66+
test_must_fail git am 0004-*.patch &&
67+
test_commit unrelated &&
68+
git rev-parse HEAD >expect &&
69+
git am --abort &&
70+
git rev-parse HEAD >actual &&
71+
test_cmp expect actual
72+
'
73+
6574
test_done

0 commit comments

Comments
 (0)