Skip to content

Commit 278f7e6

Browse files
committed
Merge branch 'js/maint-no-cherry-pick-head-after-punted' into js/no-cherry-pick-head-after-punted
* js/maint-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 Conflicts: builtin/revert.c
2 parents 7f41b6b + 82352cb commit 278f7e6

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
@@ -304,7 +304,7 @@ static void write_cherry_pick_head(struct commit *commit)
304304
strbuf_release(&buf);
305305
}
306306

307-
static void print_advice(void)
307+
static void print_advice(int show_hint)
308308
{
309309
char *msg = getenv("GIT_CHERRY_PICK_HELP");
310310

@@ -319,9 +319,11 @@ static void print_advice(void)
319319
return;
320320
}
321321

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

327329
static void write_message(struct strbuf *msgbuf, const char *filename)
@@ -566,8 +568,6 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
566568
strbuf_addstr(&msgbuf, sha1_to_hex(commit->object.sha1));
567569
strbuf_addstr(&msgbuf, ")\n");
568570
}
569-
if (!opts->no_commit)
570-
write_cherry_pick_head(commit);
571571
}
572572

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

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