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

From: Robin Stocker <[email-masked]>
Date: Tue, 4 Sep 2012 19:16:46 +0200 (CEST)

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 <[email-masked]>
---
 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