revert.c: defer writing CHERRY_PICK_HEAD till it is safe to do so

do_pick_commit() writes out CHERRY_PICK_HEAD before invoking merge (either via do_recursive_merge() or try_merge_command()) on the assumption that if the merge fails it is due to conflict. However, if the tree is dirty, the merge may not even start, aborting before do_pick_commit() can remove CHERRY_PICK_HEAD. Instead, defer writing CHERRY_PICK_HEAD till after merge has returned. At this point we know the merge has either succeeded or failed due to conflict. In either case, we want CHERRY_PICK_HEAD to be written so that it may be picked up by the subsequent invocation of commit. Note that do_recursive_merge() aborts if the merge cannot start, while try_merge_command() returns a non-zero value other than 1. Signed-off-by: Jay Soffian <jaysoffian@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jay Soffian committed Oct 6, 2011 at 13:48 UTC 9fa8aecdebf6339ad4f897ee45f36192fd75325e
2 files changed +24 -2
builtin/revert.c
+9 -2
@@ -481,8 +481,6 @@ static int do_pick_commit(void)
481 strbuf_addstr(&msgbuf, sha1_to_hex(commit->object.sha1));
482 strbuf_addstr(&msgbuf, ")\n");
483 }
484 - if (!no_commit)
485 - write_cherry_pick_head();
484 }
485
486 if (!strategy || !strcmp(strategy, "recursive") || action == REVERT) {
@@ -503,6 +501,15 @@ static int do_pick_commit(void)
501 free_commit_list(remotes);
502 }
503
504 + /*
505 + * If the merge was clean or if it failed due to conflict, we write
506 + * CHERRY_PICK_HEAD for the subsequent invocation of commit to use.
507 + * However, if the merge did not even start, then we don't want to
508 + * write it at all.
509 + */
510 + if (action == CHERRY_PICK && !no_commit && (res == 0 || res == 1))
511 + write_cherry_pick_head();
512 +
513 if (res) {
514 error(action == REVERT
515 ? _("could not revert %s... %s")
t/t3507-cherry-pick-conflict.sh
+15
@@ -77,6 +77,21 @@ test_expect_success 'cherry-pick --no-commit does not set CHERRY_PICK_HEAD' '
77 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
78 '
79
80 +test_expect_success 'cherry-pick w/dirty tree does not set CHERRY_PICK_HEAD' '
81 + pristine_detach initial &&
82 + echo foo > foo &&
83 + test_must_fail git cherry-pick base &&
84 + test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
85 +'
86 +
87 +test_expect_success \
88 + 'cherry-pick --strategy=resolve w/dirty tree does not set CHERRY_PICK_HEAD' '
89 + pristine_detach initial &&
90 + echo foo > foo &&
91 + test_must_fail git cherry-pick --strategy=resolve base &&
92 + test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
93 +'
94 +
95 test_expect_success 'GIT_CHERRY_PICK_HELP suppresses CHERRY_PICK_HEAD' '
96 pristine_detach initial &&
97 (