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

[PATCH] cherry-pick: Append -x line on separate paragraph

From
Robin Stocker <robin@nibor.org>
Date
Sep 4, 2012, 17:16 UTC
Message-ID
<1244407605.1231.1346779006295.JavaMail.root@bazinga.schuettel.ch>
In-Reply-To
<739367481.1229.1346778500787.JavaMail.root@bazinga.schuettel.ch>
Before this, git cherry-pick -x resulted in messages like this:
    Message of cherry-picked commit
    (cherry picked from commit 871e293c9acbeaacce59dcd98fab6028f552f5be)

Which is not the recommended way to write commit messages. When the commit message ends with a Signed-off-by, it's less bad. But having it on a line apart from the others also doesn't hurt there.

Now, care is taken that the line is appended as a separate paragraph:
    Message of cherry-picked commit
    (cherry picked from commit 871e293c9acbeaacce59dcd98fab6028f552f5be)

Because some Git implementations (JGit) don't always terminate the commit message with a newline, this change also inserts a second newline if necessary.

Signed-off-by: Robin Stocker <robin@nibor.org>
---
 sequencer.c                    |    4 ++++
 t/t3511-cherry-pick-message.sh |   26 ++++++++++++++++++++++++++
 2 files changed, 30 insertions(+), 0 deletions(-)
 create mode 100755 t/t3511-cherry-pick-message.sh
diff --git a/sequencer.c b/sequencer.c
index bf078f2..41852e6 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -484,6 +484,10 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
 		}
 
 		if (opts->record_origin) {
+			/* Some implementations don't terminate message with final \n, so add it */
+			if (msg.message[strlen(msg.message)-1] != '\n')
+				strbuf_addch(&msgbuf, '\n');
+			strbuf_addch(&msgbuf, '\n');
 			strbuf_addstr(&msgbuf, "(cherry picked from commit ");
 			strbuf_addstr(&msgbuf, sha1_to_hex(commit->object.sha1));
 			strbuf_addstr(&msgbuf, ")\n");
diff --git a/t/t3511-cherry-pick-message.sh b/t/t3511-cherry-pick-message.sh
new file mode 100755
index 0000000..6aba8b2
--- /dev/null
+++ b/t/t3511-cherry-pick-message.sh
@@ -0,0 +1,26 @@
+#!/bin/sh
+
+test_description='Test cherry-pick commit message with -x'
+
+. ./test-lib.sh
+
+test_expect_success setup '
+	git config advice.detachedhead false &&
+	test_commit initial foo a &&
+	test_commit base foo b &&
+	git checkout initial
+'
+
+test_expect_success 'cherry-pick -x appends message on separate line' '
+	git cherry-pick -x base &&
+	cat >expect <<-\EOF &&
+	base
+
+	(cherry picked from commit 462a97d89aa55720c97984a3ce09665989193981)
+
+	EOF
+	git log --format=%B -n 1 >actual &&
+	test_cmp expect actual
+'
+
+test_done
-- 
1.7.7.6
Next: Junio C Hamano
Message 1 of 6 in “cherry-pick: Append -x line on separate paragraph”
  1. cherry-pick: Append -x line on separate paragraphRobin Stocker, Sep 4, 2012
  2. Junio C HamanoSep 4, 2012
  3. Robin StockerSep 5, 2012
  4. Junio C HamanoSep 6, 2012
  5. Robin StockerSep 8, 2012
  6. Jeff KingSep 10, 2012

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.