git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 4/4] cherry-pick/revert: Use advise() for hints

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Aug 11, 2010, 08:37 UTC
Message-ID
<20100811083751.GE16495@burratino>
In-Reply-To
<20100811083100.GA16495@burratino>

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>
---
Thanks for reading.
 builtin/revert.c                |   36 ++++++++++++------------------------
 git-rebase--interactive.sh      |    6 +++---
 t/t3507-cherry-pick-conflict.sh |   20 ++++++++++++++++++++
 3 files changed, 35 insertions(+), 27 deletions(-)
diff --git a/builtin/revert.c b/builtin/revert.c
index 9a7483b..7f35cc6 100644
--- a/builtin/revert.c
+++ b/builtin/revert.c
@@ -250,27 +250,21 @@ static void advise(const char *advice, ...)
 	va_end(params);
 }
 
-static char *help_msg(void)
+static void print_advice(void)
 {
-	struct strbuf helpbuf = STRBUF_INIT;
 	char *msg = getenv("GIT_CHERRY_PICK_HELP");
 
-	if (msg)
-		return msg;
-
-	strbuf_addstr(&helpbuf, "  After resolving the conflicts,\n"
-		"mark the corrected paths with 'git add <paths>' or 'git rm <paths>'\n"
-		"and commit the result");
-
-	if (action == CHERRY_PICK) {
-		strbuf_addf(&helpbuf, " with: \n"
-			"\n"
-			"        git commit -c %s\n",
-			    sha1_to_hex(commit->object.sha1));
+	if (msg) {
+		fprintf(stderr, "%s\n", msg);
+		return;
 	}
-	else
-		strbuf_addch(&helpbuf, '.');
-	return strbuf_detach(&helpbuf, NULL);
+
+	advise("after resolving the conflicts, mark the corrected paths");
+	advise("with 'git add <paths>' or 'git rm <paths>'");
+
+	if (action == CHERRY_PICK)
+		advise("and commit the result with 'git commit -c %s'",
+		       find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
 }
 
 static void write_message(struct strbuf *msgbuf, const char *filename)
@@ -404,7 +398,6 @@ static int do_pick_commit(void)
 	struct commit_message msg = { NULL, NULL, NULL, NULL, NULL };
 	char *defmsg = NULL;
 	struct strbuf msgbuf = STRBUF_INIT;
-	struct strbuf mebuf = STRBUF_INIT;
 	int res;
 
 	if (no_commit) {
@@ -501,9 +494,6 @@ static int do_pick_commit(void)
 		}
 	}
 
-	strbuf_addf(&mebuf, "%s of commit %s", me,
-		    find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
-
 	if (!strategy || !strcmp(strategy, "recursive") || action == REVERT) {
 		res = do_recursive_merge(base, next, base_label, next_label,
 					 head, &msgbuf);
@@ -512,7 +502,6 @@ static int do_pick_commit(void)
 		struct commit_list *common = NULL;
 		struct commit_list *remotes = NULL;
 
-		strbuf_addf(&mebuf, " with strategy %s", strategy);
 		write_message(&msgbuf, defmsg);
 
 		commit_list_insert(base, &common);
@@ -528,14 +517,13 @@ static int do_pick_commit(void)
 		      action == REVERT ? "revert" : "apply",
 		      find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV),
 		      msg.subject);
-		fprintf(stderr, help_msg());
+		print_advice();
 		rerere(allow_rerere_auto);
 	} else {
 		if (!no_commit)
 			res = run_git_commit(defmsg);
 	}
 
-	strbuf_release(&mebuf);
 	free_message(&msg);
 	free(defmsg);
 
diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh
index 31e6860..8f6876d 100755
--- a/git-rebase--interactive.sh
+++ b/git-rebase--interactive.sh
@@ -113,9 +113,9 @@ REBASE_ROOT=
 AUTOSQUASH=
 NEVER_FF=
 
-GIT_CHERRY_PICK_HELP="  After resolving the conflicts,
-mark the corrected paths with 'git add <paths>', and
-run 'git rebase --continue'"
+GIT_CHERRY_PICK_HELP="\
+hint: after resolving the conflicts, mark the corrected paths
+hint: with 'git add <paths>' and run 'git rebase --continue'"
 export GIT_CHERRY_PICK_HELP
 
 warn () {
diff --git a/t/t3507-cherry-pick-conflict.sh b/t/t3507-cherry-pick-conflict.sh
index e25cf80..3f29594 100755
--- a/t/t3507-cherry-pick-conflict.sh
+++ b/t/t3507-cherry-pick-conflict.sh
@@ -38,6 +38,26 @@ test_expect_success 'failed cherry-pick does not advance HEAD' '
 	test "$head" = "$newhead"
 '
 
+test_expect_success 'advice from failed cherry-pick' '
+	git checkout -f initial^0 &&
+	git read-tree -u --reset HEAD &&
+	git clean -d -f -f -q -x &&
+
+	git update-index --refresh &&
+	git diff-index --exit-code HEAD &&
+
+	picked=$(git rev-parse --short picked) &&
+	cat <<-EOF >expected &&
+	error: could not apply $picked... picked
+	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 $picked'
+	EOF
+	test_must_fail git cherry-pick picked 2>actual &&
+
+	test_cmp expected actual
+'
+
 test_expect_success 'failed cherry-pick produces dirty index' '
 
 	git checkout -f initial^0 &&
-- 
1.7.2.1.544.ga752d.dirty
Previous: Jonathan NiederNext: Nguyen Thai Ngoc Duy
Message 28 of 38 in “commit: more focused advice in the no-changes-staged case”
  1. 0/9 commit: more focused advice in the no-changes-staged caseJonathan Nieder, Jul 25, 2010
  2. 1/9 wt-status: split wt_status_print into digestible piecesJonathan Nieder, Jul 25, 2010
  3. 2/9 wt-status: split off a function for printing submodule summaryJonathan Nieder, Jul 25, 2010
  4. 3/9 commit: split off a function to fetch the default log messageJonathan Nieder, Jul 25, 2010
  5. 4/9 commit: split commit -s handling into its own functionJonathan Nieder, Jul 25, 2010
  6. 5/9 commit: split off the piece that writes statusJonathan Nieder, Jul 25, 2010
  7. 6/9 t7508 (status): modernize styleJonathan Nieder, Jul 25, 2010
  8. Ævar Arnfjörð BjarmasonJul 25, 2010
  9. 7/9 commit: give empty-commit avoidance code its own functionJonathan Nieder, Jul 25, 2010
  10. 8/9 commit --dry-run: give advice on empty amendJonathan Nieder, Jul 25, 2010
  11. 9/9 commit: suppress status summary when no changes stagedJonathan Nieder, Jul 25, 2010
  12. Thomas RastAug 11, 2010
  13. Jonathan NiederAug 11, 2010
  14. t6040 (branch tracking): check “status” instead of “commit”Jonathan Nieder, Aug 11, 2010
  15. Ævar Arnfjörð BjarmasonAug 12, 2010
  16. Ævar Arnfjörð BjarmasonAug 11, 2010
  17. Jonathan NiederAug 11, 2010
  18. Ævar Arnfjörð BjarmasonAug 12, 2010
  19. Jonathan NiederAug 12, 2010
  20. Ævar Arnfjörð BjarmasonJul 25, 2010
  21. Thomas RastJul 25, 2010
  22. Making error messages stand out (Re: [RFC/PATCH 0/9] commit: more focused advice in the no-changes-staged case)Jonathan Nieder, Jul 29, 2010
  23. Sverre RabbelierJul 30, 2010
  24. 0/4 Re: Making error messages stand outJonathan Nieder, Aug 11, 2010
  25. 1/4 Eliminate “Finished cherry-pick/revert” messageJonathan Nieder, Aug 11, 2010
  26. 2/4 Introduce advise() to print hintsJonathan Nieder, Aug 11, 2010
  27. 3/4 cherry-pick/revert: Use error() for failure messageJonathan Nieder, Aug 11, 2010
  28. 4/4 cherry-pick/revert: Use advise() for hintsJonathan Nieder, Aug 11, 2010
  29. Nguyen Thai Ngoc DuyAug 11, 2010
  30. Matthieu MoyAug 11, 2010
  31. Nguyen Thai Ngoc DuyAug 11, 2010
  32. Sverre RabbelierAug 11, 2010
  33. tests: fix syntax error in "Use advise() for hints" testÆvar Arnfjörð Bjarmason, Aug 18, 2010
  34. Jonathan NiederAug 19, 2010
  35. Ævar Arnfjörð BjarmasonAug 19, 2010
  36. Raja R HarinathAug 20, 2010
  37. Ævar Arnfjörð BjarmasonAug 20, 2010
  38. Junio C HamanoAug 20, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.