From 5f35afadb032e1d2585f6f0246bf05f2e4e49c27 Mon Sep 17 00:00:00 2001
From: Tay Ray Chuan <rctay89@gmail.com>
Date: Tue, 11 May 2010 01:17:45 +0800
Subject: t7604-merge-custom-message: shift expected output creation

Squash in a minor rename too.

Signed-off-by: Tay Ray Chuan <rctay89@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 t/t7604-merge-custom-message.sh | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/t/t7604-merge-custom-message.sh b/t/t7604-merge-custom-message.sh
index 269cfdf26..d79542d84 100755
--- a/t/t7604-merge-custom-message.sh
+++ b/t/t7604-merge-custom-message.sh
@@ -6,6 +6,10 @@ Testing merge when using a custom message for the merge commit.'
 
 . ./test-lib.sh
 
+create_merge_msgs() {
+	echo >exp.subject "custom message"
+}
+
 test_expect_success 'setup' '
 	echo c0 > c0.c &&
 	git add c0.c &&
@@ -19,16 +23,16 @@ test_expect_success 'setup' '
 	echo c2 > c2.c &&
 	git add c2.c &&
 	git commit -m c2 &&
-	git tag c2
+	git tag c2 &&
+	create_merge_msgs
 '
 
 
 test_expect_success 'merge c2 with a custom message' '
 	git reset --hard c1 &&
-	echo >expected "custom message" &&
-	git merge -m "custom message" c2 &&
+	git merge -m "$(cat exp.subject)" c2 &&
 	git cat-file commit HEAD | sed -e "1,/^$/d" >actual &&
-	test_cmp expected actual
+	test_cmp exp.subject actual
 '
 
 test_done
-- 
cgit v1.2.1


From d4e6c4bdc3e065f738b0c2fc47d9fe9c3778d746 Mon Sep 17 00:00:00 2001
From: Tay Ray Chuan <rctay89@gmail.com>
Date: Tue, 11 May 2010 01:17:46 +0800
Subject: t7604-merge-custom-message: show that --log doesn't append to -m

Signed-off-by: Tay Ray Chuan <rctay89@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 t/t7604-merge-custom-message.sh | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/t/t7604-merge-custom-message.sh b/t/t7604-merge-custom-message.sh
index d79542d84..af53df1c1 100755
--- a/t/t7604-merge-custom-message.sh
+++ b/t/t7604-merge-custom-message.sh
@@ -8,6 +8,11 @@ Testing merge when using a custom message for the merge commit.'
 
 create_merge_msgs() {
 	echo >exp.subject "custom message"
+
+	cp exp.subject exp.log &&
+	echo >>exp.log "" &&
+	echo >>exp.log "* commit 'c2':" &&
+	echo >>exp.log "  c2"
 }
 
 test_expect_success 'setup' '
@@ -35,4 +40,11 @@ test_expect_success 'merge c2 with a custom message' '
 	test_cmp exp.subject actual
 '
 
+test_expect_failure 'merge --log appends to custom message' '
+	git reset --hard c1 &&
+	git merge --log -m "$(cat exp.subject)" c2 &&
+	git cat-file commit HEAD | sed -e "1,/^$/d" >actual &&
+	test_cmp exp.log actual
+'
+
 test_done
-- 
cgit v1.2.1


From 75589220289ca8400388813af66c7219811e0334 Mon Sep 17 00:00:00 2001
From: Tay Ray Chuan <rctay89@gmail.com>
Date: Tue, 11 May 2010 01:17:47 +0800
Subject: merge: update comment

ce9d823 (merge: do not add standard message when message is given with
-m option) changed the behaviour of the code that the comment addressed,
but the comment was not similarly updated.

Fix this.

Signed-off-by: Tay Ray Chuan <rctay89@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/merge.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/builtin/merge.c b/builtin/merge.c
index c04306684..457bb5e48 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -981,7 +981,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 		/*
 		 * All the rest are the commits being merged;
 		 * prepare the standard merge summary message to
-		 * be appended to the given message.  If remote
+		 * used as the merge message.  If remote
 		 * is invalid we will die later in the common
 		 * codepath so we discard the error in this
 		 * loop.
-- 
cgit v1.2.1


From 97d45bcb2f6bdd9cb164d151a6e064ffdcd5e2bc Mon Sep 17 00:00:00 2001
From: Tay Ray Chuan <rctay89@gmail.com>
Date: Tue, 11 May 2010 01:17:48 +0800
Subject: merge: rename variable

It is more accurate to call it 'merge_names' instead of 'msg', as it
does not contain the final message.

Signed-off-by: Tay Ray Chuan <rctay89@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/merge.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/builtin/merge.c b/builtin/merge.c
index 457bb5e48..0ae480987 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -973,7 +973,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 		reset_hard(remote_head->sha1, 0);
 		return 0;
 	} else {
-		struct strbuf msg = STRBUF_INIT;
+		struct strbuf merge_names = STRBUF_INIT;
 
 		/* We are invoked directly as the first-class UI. */
 		head_arg = "HEAD";
@@ -988,8 +988,8 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 		 */
 		if (!have_message) {
 			for (i = 0; i < argc; i++)
-				merge_name(argv[i], &msg);
-			fmt_merge_msg(option_log, &msg, &merge_msg);
+				merge_name(argv[i], &merge_names);
+			fmt_merge_msg(option_log, &merge_names, &merge_msg);
 			if (merge_msg.len)
 				strbuf_setlen(&merge_msg, merge_msg.len-1);
 		}
-- 
cgit v1.2.1


From 2234ec5422201cd58f51b82fb4af146cf896767d Mon Sep 17 00:00:00 2001
From: Tay Ray Chuan <rctay89@gmail.com>
Date: Tue, 11 May 2010 01:17:49 +0800
Subject: fmt-merge-msg: minor refactor of fmt_merge_msg()

Shift implementation into a private function, do_fmt_merge_msg(). This
allows for further changes to the implementation, without affecting the
interface.

Signed-off-by: Tay Ray Chuan <rctay89@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/fmt-merge-msg.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/builtin/fmt-merge-msg.c b/builtin/fmt-merge-msg.c
index 379a03131..a2bccd6ea 100644
--- a/builtin/fmt-merge-msg.c
+++ b/builtin/fmt-merge-msg.c
@@ -202,7 +202,8 @@ static void shortlog(const char *name, unsigned char *sha1,
 	string_list_clear(&subjects, 0);
 }
 
-int fmt_merge_msg(int merge_summary, struct strbuf *in, struct strbuf *out) {
+static int do_fmt_merge_msg(int merge_summary, struct strbuf *in,
+	struct strbuf *out) {
 	int limit = 20, i = 0, pos = 0;
 	char *sep = "";
 	unsigned char head_sha1[20];
@@ -296,6 +297,10 @@ int fmt_merge_msg(int merge_summary, struct strbuf *in, struct strbuf *out) {
 	return 0;
 }
 
+int fmt_merge_msg(int merge_summary, struct strbuf *in, struct strbuf *out) {
+	return do_fmt_merge_msg(merge_summary, in, out);
+}
+
 int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
 {
 	const char *inpath = NULL;
-- 
cgit v1.2.1


From 403994e83dd3ad2424f0b3084c8dc4fd1ca6374a Mon Sep 17 00:00:00 2001
From: Tay Ray Chuan <rctay89@gmail.com>
Date: Tue, 11 May 2010 01:17:50 +0800
Subject: fmt-merge-msg: refactor merge title formatting

Signed-off-by: Tay Ray Chuan <rctay89@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/fmt-merge-msg.c | 65 +++++++++++++++++++++++++++----------------------
 1 file changed, 36 insertions(+), 29 deletions(-)

diff --git a/builtin/fmt-merge-msg.c b/builtin/fmt-merge-msg.c
index a2bccd6ea..d0160cb82 100644
--- a/builtin/fmt-merge-msg.c
+++ b/builtin/fmt-merge-msg.c
@@ -202,36 +202,10 @@ static void shortlog(const char *name, unsigned char *sha1,
 	string_list_clear(&subjects, 0);
 }
 
-static int do_fmt_merge_msg(int merge_summary, struct strbuf *in,
-	struct strbuf *out) {
-	int limit = 20, i = 0, pos = 0;
+static void do_fmt_merge_msg_title(struct strbuf *out,
+	const char *current_branch) {
+	int i = 0;
 	char *sep = "";
-	unsigned char head_sha1[20];
-	const char *current_branch;
-
-	/* get current branch */
-	current_branch = resolve_ref("HEAD", head_sha1, 1, NULL);
-	if (!current_branch)
-		die("No current branch");
-	if (!prefixcmp(current_branch, "refs/heads/"))
-		current_branch += 11;
-
-	/* get a line */
-	while (pos < in->len) {
-		int len;
-		char *newline, *p = in->buf + pos;
-
-		newline = strchr(p, '\n');
-		len = newline ? newline - p : strlen(p);
-		pos += len + !!newline;
-		i++;
-		p[len] = 0;
-		if (handle_line(p))
-			die ("Error in line %d: %.*s", i, len, p);
-	}
-
-	if (!srcs.nr)
-		return 0;
 
 	strbuf_addstr(out, "Merge ");
 	for (i = 0; i < srcs.nr; i++) {
@@ -279,6 +253,39 @@ static int do_fmt_merge_msg(int merge_summary, struct strbuf *in,
 		strbuf_addch(out, '\n');
 	else
 		strbuf_addf(out, " into %s\n", current_branch);
+}
+
+static int do_fmt_merge_msg(int merge_summary, struct strbuf *in,
+	struct strbuf *out) {
+	int limit = 20, i = 0, pos = 0;
+	unsigned char head_sha1[20];
+	const char *current_branch;
+
+	/* get current branch */
+	current_branch = resolve_ref("HEAD", head_sha1, 1, NULL);
+	if (!current_branch)
+		die("No current branch");
+	if (!prefixcmp(current_branch, "refs/heads/"))
+		current_branch += 11;
+
+	/* get a line */
+	while (pos < in->len) {
+		int len;
+		char *newline, *p = in->buf + pos;
+
+		newline = strchr(p, '\n');
+		len = newline ? newline - p : strlen(p);
+		pos += len + !!newline;
+		i++;
+		p[len] = 0;
+		if (handle_line(p))
+			die ("Error in line %d: %.*s", i, len, p);
+	}
+
+	if (!srcs.nr)
+		return 0;
+
+	do_fmt_merge_msg_title(out, current_branch);
 
 	if (merge_summary) {
 		struct commit *head;
-- 
cgit v1.2.1


From 8c6bdfdf8ba9a3d7a03d64b7a98305a460921130 Mon Sep 17 00:00:00 2001
From: Tay Ray Chuan <rctay89@gmail.com>
Date: Tue, 11 May 2010 01:17:51 +0800
Subject: fmt-merge-msg: add function to append shortlog only

Signed-off-by: Tay Ray Chuan <rctay89@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin.h               |  1 +
 builtin/fmt-merge-msg.c | 13 +++++++++----
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/builtin.h b/builtin.h
index 464588b29..322901f33 100644
--- a/builtin.h
+++ b/builtin.h
@@ -16,6 +16,7 @@ extern const char *help_unknown_cmd(const char *cmd);
 extern void prune_packed_objects(int);
 extern int fmt_merge_msg(int merge_summary, struct strbuf *in,
 	struct strbuf *out);
+extern int fmt_merge_msg_shortlog(struct strbuf *in, struct strbuf *out);
 extern int commit_tree(const char *msg, unsigned char *tree,
 		struct commit_list *parents, unsigned char *ret,
 		const char *author);
diff --git a/builtin/fmt-merge-msg.c b/builtin/fmt-merge-msg.c
index d0160cb82..48548cf11 100644
--- a/builtin/fmt-merge-msg.c
+++ b/builtin/fmt-merge-msg.c
@@ -255,8 +255,8 @@ static void do_fmt_merge_msg_title(struct strbuf *out,
 		strbuf_addf(out, " into %s\n", current_branch);
 }
 
-static int do_fmt_merge_msg(int merge_summary, struct strbuf *in,
-	struct strbuf *out) {
+static int do_fmt_merge_msg(int merge_title, int merge_summary,
+	struct strbuf *in, struct strbuf *out) {
 	int limit = 20, i = 0, pos = 0;
 	unsigned char head_sha1[20];
 	const char *current_branch;
@@ -285,7 +285,8 @@ static int do_fmt_merge_msg(int merge_summary, struct strbuf *in,
 	if (!srcs.nr)
 		return 0;
 
-	do_fmt_merge_msg_title(out, current_branch);
+	if (merge_title)
+		do_fmt_merge_msg_title(out, current_branch);
 
 	if (merge_summary) {
 		struct commit *head;
@@ -305,7 +306,11 @@ static int do_fmt_merge_msg(int merge_summary, struct strbuf *in,
 }
 
 int fmt_merge_msg(int merge_summary, struct strbuf *in, struct strbuf *out) {
-	return do_fmt_merge_msg(merge_summary, in, out);
+	return do_fmt_merge_msg(1, merge_summary, in, out);
+}
+
+int fmt_merge_msg_shortlog(struct strbuf *in, struct strbuf *out) {
+	return do_fmt_merge_msg(0, 1, in, out);
 }
 
 int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
-- 
cgit v1.2.1


From f0ecac2b70bf6b44b89dfa493abd2f544010b1eb Mon Sep 17 00:00:00 2001
From: Tay Ray Chuan <rctay89@gmail.com>
Date: Tue, 11 May 2010 01:17:52 +0800
Subject: merge: --log appends shortlog to message if specified

When the user specifies a message, use fmt_merge_msg_shortlog() to
append the shortlog.

Previously, when a message was specified, we ignored the merge title
("Merge <foo> into <bar>") and shortlog from fmt_merge_msg().

Update the documentation for -m to reflect this too.

Signed-off-by: Tay Ray Chuan <rctay89@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 Documentation/git-merge.txt     |  7 ++++++-
 builtin/fmt-merge-msg.c         |  3 +++
 builtin/merge.c                 | 18 +++++++++++-------
 t/t7604-merge-custom-message.sh |  2 +-
 4 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/Documentation/git-merge.txt b/Documentation/git-merge.txt
index c2325ef90..84043cc5b 100644
--- a/Documentation/git-merge.txt
+++ b/Documentation/git-merge.txt
@@ -58,7 +58,12 @@ include::merge-options.txt[]
 
 -m <msg>::
 	Set the commit message to be used for the merge commit (in
-	case one is created). The 'git fmt-merge-msg' command can be
+	case one is created).
+
+	If `--log` is specified, a shortlog of the commits being merged
+	will be appended to the specified message.
+
+	The 'git fmt-merge-msg' command can be
 	used to give a good default for automated 'git merge'
 	invocations.
 
diff --git a/builtin/fmt-merge-msg.c b/builtin/fmt-merge-msg.c
index 48548cf11..44204257c 100644
--- a/builtin/fmt-merge-msg.c
+++ b/builtin/fmt-merge-msg.c
@@ -298,6 +298,9 @@ static int do_fmt_merge_msg(int merge_title, int merge_summary,
 		rev.ignore_merges = 1;
 		rev.limited = 1;
 
+		if (suffixcmp(out->buf, "\n"))
+			strbuf_addch(out, '\n');
+
 		for (i = 0; i < origins.nr; i++)
 			shortlog(origins.items[i].string, origins.items[i].util,
 					head, &rev, limit, out);
diff --git a/builtin/merge.c b/builtin/merge.c
index 0ae480987..cae1cbee2 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -981,18 +981,22 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 		/*
 		 * All the rest are the commits being merged;
 		 * prepare the standard merge summary message to
-		 * used as the merge message.  If remote
+		 * be appended to the given message.  If remote
 		 * is invalid we will die later in the common
 		 * codepath so we discard the error in this
 		 * loop.
 		 */
-		if (!have_message) {
-			for (i = 0; i < argc; i++)
-				merge_name(argv[i], &merge_names);
+		for (i = 0; i < argc; i++)
+			merge_name(argv[i], &merge_names);
+
+		if (have_message && option_log)
+			fmt_merge_msg_shortlog(&merge_names, &merge_msg);
+		else if (!have_message)
 			fmt_merge_msg(option_log, &merge_names, &merge_msg);
-			if (merge_msg.len)
-				strbuf_setlen(&merge_msg, merge_msg.len-1);
-		}
+
+
+		if (!(have_message && !option_log) && merge_msg.len)
+			strbuf_setlen(&merge_msg, merge_msg.len-1);
 	}
 
 	if (head_invalid || !argc)
diff --git a/t/t7604-merge-custom-message.sh b/t/t7604-merge-custom-message.sh
index af53df1c1..9114785ef 100755
--- a/t/t7604-merge-custom-message.sh
+++ b/t/t7604-merge-custom-message.sh
@@ -40,7 +40,7 @@ test_expect_success 'merge c2 with a custom message' '
 	test_cmp exp.subject actual
 '
 
-test_expect_failure 'merge --log appends to custom message' '
+test_expect_success 'merge --log appends to custom message' '
 	git reset --hard c1 &&
 	git merge --log -m "$(cat exp.subject)" c2 &&
 	git cat-file commit HEAD | sed -e "1,/^$/d" >actual &&
-- 
cgit v1.2.1