Re: Dangerous "git am --abort" behavior
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Dec 21, 2010, 18:47 UTC
- Message-ID
- <7voc8f7ykg.fsf@alter.siamese.dyndns.org>
- In-Reply-To
- <7vsjxr7zdn.fsf@alter.siamese.dyndns.org>
Junio C Hamano <gitster@pobox.com> writes:
Show 5 quoted lines
> So here is the first step in that direction. I suspect that stop_here > should also record what the current branch is, and safe_to_abort should > check it (the potentially risky sequence is "after a failed am, check out > a different branch and then realize you need to 'am --abort'"), but that > is left to interested others ;-) or a later round.
And here is that later round...
-- >8 -- Subject: [PATCH] am --abort: also check the current branch
If the user checks out another branch after an "am" failure, am --abort would have rewound the tip of that branch back to where the last failed "am" started from, which would not be fun.
Signed-off-by: Junio C Hamano <gitster@pobox.com> --- git-am.sh | 10 +++++++--- t/t4151-am-abort.sh | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-)
diff --git a/git-am.sh b/git-am.sh index e5671f6..ca3f910 100755 --- a/git-am.sh +++ b/git-am.sh @@ -68,7 +68,9 @@ sq () { stop_here () { echo "$1" >"$dotest/next" - git rev-parse --verify -q HEAD >"$dotest/abort-safety" + head=$(git rev-parse --verify -q HEAD) + branch=$(git symbolic-ref -q HEAD) + echo "$head,$branch" >"$dotest/abort-safety" exit 1 } @@ -84,11 +86,13 @@ safe_to_abort () { fi abort_safety=$(cat "$dotest/abort-safety") - if test "z$(git rev-parse --verify -q HEAD)" = "z$abort_safety" + head=$(git rev-parse --verify -q HEAD) + branch=$(git symbolic-ref -q HEAD) + if test "z$head,$branch" = "z$abort_safety" then return 0 fi - echo >&2 "You seem to have moved HEAD since the last 'am' failure." + echo >&2 "You seem to have done some other things since the last 'am' failure." echo >&2 "Not rewinding to ORIG_HEAD" return 1 } diff --git a/t/t4151-am-abort.sh b/t/t4151-am-abort.sh index 001b1e3..23a9fb0 100755 --- a/t/t4151-am-abort.sh +++ b/t/t4151-am-abort.sh @@ -71,4 +71,21 @@ test_expect_success 'am --abort will keep the local commits' ' test_cmp expect actual ' +test_expect_success 'am --abort will keep unrelated branch' ' + git reset --hard && + test_commit foo && + test_must_fail git am 0004-*.patch && + git checkout -b unrelated HEAD^ && + ( + git rev-parse HEAD + git symbolic-ref HEAD + ) >expect && + git am --abort && + ( + git rev-parse HEAD + git symbolic-ref HEAD + ) >actual && + test_cmp expect actual +' + test_done
-- 1.7.3.4.768.g2fa91