Skip to content

Commit 314eeb6

Browse files
jrngitster
authored andcommitted
cherry-pick/revert: Use advise() for hints
When cherry-pick fails after picking a large series of commits, it can be hard to pick out the error message and advice. Prefix the advice with “hint: ” to help. Before: error: could not apply 7ab78c9... foo After resolving the conflicts, mark the corrected paths with 'git add <paths>' or 'git rm <paths>' and commit the result with: git commit -c 7ab78c9a7898b87127365478431289cb98f8d98f After: error: could not apply 7ab78c9... foo hint: after resolving the conflicts, mark the corrected paths hint: with 'git add <paths>' or 'git rm <paths>' hint: and commit the result with 'git commit -c 7ab78c9' Noticed-by: Thomas Rast <trast@student.ethz.ch> Encouraged-by: Sverre Rabbelier <srabbelier@gmail.com> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 981ff5c commit 314eeb6

3 files changed

Lines changed: 34 additions & 26 deletions

File tree

builtin/revert.c

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -250,27 +250,21 @@ static void advise(const char *advice, ...)
250250
va_end(params);
251251
}
252252

253-
static char *help_msg(void)
253+
static void print_advice(void)
254254
{
255-
struct strbuf helpbuf = STRBUF_INIT;
256255
char *msg = getenv("GIT_CHERRY_PICK_HELP");
257256

258-
if (msg)
259-
return msg;
257+
if (msg) {
258+
fprintf(stderr, "%s\n", msg);
259+
return;
260+
}
260261

261-
strbuf_addstr(&helpbuf, " After resolving the conflicts,\n"
262-
"mark the corrected paths with 'git add <paths>' or 'git rm <paths>'\n"
263-
"and commit the result");
262+
advise("after resolving the conflicts, mark the corrected paths");
263+
advise("with 'git add <paths>' or 'git rm <paths>'");
264264

265-
if (action == CHERRY_PICK) {
266-
strbuf_addf(&helpbuf, " with: \n"
267-
"\n"
268-
" git commit -c %s\n",
269-
sha1_to_hex(commit->object.sha1));
270-
}
271-
else
272-
strbuf_addch(&helpbuf, '.');
273-
return strbuf_detach(&helpbuf, NULL);
265+
if (action == CHERRY_PICK)
266+
advise("and commit the result with 'git commit -c %s'",
267+
find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
274268
}
275269

276270
static void write_message(struct strbuf *msgbuf, const char *filename)
@@ -404,7 +398,6 @@ static int do_pick_commit(void)
404398
struct commit_message msg = { NULL, NULL, NULL, NULL, NULL };
405399
char *defmsg = NULL;
406400
struct strbuf msgbuf = STRBUF_INIT;
407-
struct strbuf mebuf = STRBUF_INIT;
408401
int res;
409402

410403
if (no_commit) {
@@ -501,9 +494,6 @@ static int do_pick_commit(void)
501494
}
502495
}
503496

504-
strbuf_addf(&mebuf, "%s of commit %s", me,
505-
find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
506-
507497
if (!strategy || !strcmp(strategy, "recursive") || action == REVERT) {
508498
res = do_recursive_merge(base, next, base_label, next_label,
509499
head, &msgbuf);
@@ -512,7 +502,6 @@ static int do_pick_commit(void)
512502
struct commit_list *common = NULL;
513503
struct commit_list *remotes = NULL;
514504

515-
strbuf_addf(&mebuf, " with strategy %s", strategy);
516505
write_message(&msgbuf, defmsg);
517506

518507
commit_list_insert(base, &common);
@@ -528,14 +517,13 @@ static int do_pick_commit(void)
528517
action == REVERT ? "revert" : "apply",
529518
find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV),
530519
msg.subject);
531-
fprintf(stderr, help_msg());
520+
print_advice();
532521
rerere(allow_rerere_auto);
533522
} else {
534523
if (!no_commit)
535524
res = run_git_commit(defmsg);
536525
}
537526

538-
strbuf_release(&mebuf);
539527
free_message(&msg);
540528
free(defmsg);
541529

git-rebase--interactive.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ REBASE_ROOT=
113113
AUTOSQUASH=
114114
NEVER_FF=
115115

116-
GIT_CHERRY_PICK_HELP=" After resolving the conflicts,
117-
mark the corrected paths with 'git add <paths>', and
118-
run 'git rebase --continue'"
116+
GIT_CHERRY_PICK_HELP="\
117+
hint: after resolving the conflicts, mark the corrected paths
118+
hint: with 'git add <paths>' and run 'git rebase --continue'"
119119
export GIT_CHERRY_PICK_HELP
120120

121121
warn () {

t/t3507-cherry-pick-conflict.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,26 @@ test_expect_success 'failed cherry-pick does not advance HEAD' '
3838
test "$head" = "$newhead"
3939
'
4040

41+
test_expect_success 'advice from failed cherry-pick' '
42+
git checkout -f initial^0 &&
43+
git read-tree -u --reset HEAD &&
44+
git clean -d -f -f -q -x &&
45+
46+
git update-index --refresh &&
47+
git diff-index --exit-code HEAD &&
48+
49+
picked=$(git rev-parse --short picked) &&
50+
cat <<-EOF >expected &&
51+
error: could not apply $picked... picked
52+
hint: after resolving the conflicts, mark the corrected paths
53+
hint: with 'git add <paths>' or 'git rm <paths>'
54+
hint: and commit the result with 'git commit -c $picked'
55+
EOF
56+
test_must_fail git cherry-pick picked 2>actual &&
57+
58+
test_cmp expected actual
59+
'
60+
4161
test_expect_success 'failed cherry-pick produces dirty index' '
4262
4363
git checkout -f initial^0 &&

0 commit comments

Comments
 (0)