Skip to content

Commit 541b9cf

Browse files
committed
Merge branch 'js/no-cherry-pick-head-after-punted'
* js/no-cherry-pick-head-after-punted: cherry-pick: do not give irrelevant advice when cherry-pick punted revert.c: defer writing CHERRY_PICK_HEAD till it is safe to do so
2 parents 2201cc8 + 278f7e6 commit 541b9cf

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

builtin/revert.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ static void write_cherry_pick_head(struct commit *commit)
302302
strbuf_release(&buf);
303303
}
304304

305-
static void print_advice(void)
305+
static void print_advice(int show_hint)
306306
{
307307
char *msg = getenv("GIT_CHERRY_PICK_HELP");
308308

@@ -317,9 +317,11 @@ static void print_advice(void)
317317
return;
318318
}
319319

320-
advise("after resolving the conflicts, mark the corrected paths");
321-
advise("with 'git add <paths>' or 'git rm <paths>'");
322-
advise("and commit the result with 'git commit'");
320+
if (show_hint) {
321+
advise("after resolving the conflicts, mark the corrected paths");
322+
advise("with 'git add <paths>' or 'git rm <paths>'");
323+
advise("and commit the result with 'git commit'");
324+
}
323325
}
324326

325327
static void write_message(struct strbuf *msgbuf, const char *filename)
@@ -564,8 +566,6 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
564566
strbuf_addstr(&msgbuf, sha1_to_hex(commit->object.sha1));
565567
strbuf_addstr(&msgbuf, ")\n");
566568
}
567-
if (!opts->no_commit)
568-
write_cherry_pick_head(commit);
569569
}
570570

571571
if (!opts->strategy || !strcmp(opts->strategy, "recursive") || opts->action == REVERT) {
@@ -586,13 +586,22 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
586586
free_commit_list(remotes);
587587
}
588588

589+
/*
590+
* If the merge was clean or if it failed due to conflict, we write
591+
* CHERRY_PICK_HEAD for the subsequent invocation of commit to use.
592+
* However, if the merge did not even start, then we don't want to
593+
* write it at all.
594+
*/
595+
if (opts->action == CHERRY_PICK && !opts->no_commit && (res == 0 || res == 1))
596+
write_cherry_pick_head(commit);
597+
589598
if (res) {
590599
error(opts->action == REVERT
591600
? _("could not revert %s... %s")
592601
: _("could not apply %s... %s"),
593602
find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV),
594603
msg.subject);
595-
print_advice();
604+
print_advice(res == 1);
596605
rerere(opts->allow_rerere_auto);
597606
} else {
598607
if (!opts->no_commit)

t/t3507-cherry-pick-conflict.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,21 @@ test_expect_success 'cherry-pick --no-commit does not set CHERRY_PICK_HEAD' '
7777
test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
7878
'
7979

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+
8095
test_expect_success 'GIT_CHERRY_PICK_HELP suppresses CHERRY_PICK_HEAD' '
8196
pristine_detach initial &&
8297
(

0 commit comments

Comments
 (0)