@matheusbsbs / matheus-bagliano / commits / 314eeb6e48
Fork 0

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>

Jonathan Nieder committed Aug 11, 2010 at 03:37 UTC 314eeb6e483350cc7ef0bee0498ff24a12346495
3 files changed +34 -26
builtin/revert.c
+11 -23
@@ -250,27 +250,21 @@ static void advise(const char *advice, ...)
250 va_end(params);
251 }
252
253 -static char *help_msg(void)
253 +static void print_advice(void)
254 {
255 - struct strbuf helpbuf = STRBUF_INIT;
255 char *msg = getenv("GIT_CHERRY_PICK_HELP");
256
258 - if (msg)
259 - return msg;
257 + if (msg) {
258 + fprintf(stderr, "%s\n", msg);
259 + return;
260 + }
261
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>'");
264
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));
268 }
269
270 static void write_message(struct strbuf *msgbuf, const char *filename)
@@ -404,7 +398,6 @@ static int do_pick_commit(void)
398 struct commit_message msg = { NULL, NULL, NULL, NULL, NULL };
399 char *defmsg = NULL;
400 struct strbuf msgbuf = STRBUF_INIT;
407 - struct strbuf mebuf = STRBUF_INIT;
401 int res;
402
403 if (no_commit) {
@@ -501,9 +494,6 @@ static int do_pick_commit(void)
494 }
495 }
496
504 - strbuf_addf(&mebuf, "%s of commit %s", me,
505 - find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
506 -
497 if (!strategy || !strcmp(strategy, "recursive") || action == REVERT) {
498 res = do_recursive_merge(base, next, base_label, next_label,
499 head, &msgbuf);
@@ -512,7 +502,6 @@ static int do_pick_commit(void)
502 struct commit_list *common = NULL;
503 struct commit_list *remotes = NULL;
504
515 - strbuf_addf(&mebuf, " with strategy %s", strategy);
505 write_message(&msgbuf, defmsg);
506
507 commit_list_insert(base, &common);
@@ -528,14 +517,13 @@ static int do_pick_commit(void)
517 action == REVERT ? "revert" : "apply",
518 find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV),
519 msg.subject);
531 - fprintf(stderr, help_msg());
520 + print_advice();
521 rerere(allow_rerere_auto);
522 } else {
523 if (!no_commit)
524 res = run_git_commit(defmsg);
525 }
526
538 - strbuf_release(&mebuf);
527 free_message(&msg);
528 free(defmsg);
529
git-rebase--interactive.sh
+3 -3
@@ -113,9 +113,9 @@ REBASE_ROOT=
113 AUTOSQUASH=
114 NEVER_FF=
115
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'"
119 export GIT_CHERRY_PICK_HELP
120
121 warn () {
t/t3507-cherry-pick-conflict.sh
+20
@@ -38,6 +38,26 @@ test_expect_success 'failed cherry-pick does not advance HEAD' '
38 test "$head" = "$newhead"
39 '
40
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 +
61 test_expect_success 'failed cherry-pick produces dirty index' '
62
63 git checkout -f initial^0 &&