From 0b38227f283e208866d3e2ac85bcd947a71f1b17 Mon Sep 17 00:00:00 2001
From: Jeff King <peff@peff.net>
Date: Wed, 12 Nov 2008 03:25:52 -0500
Subject: [PATCH] commit: Fix stripping of patch in verbose mode.

When the "-v" option is given, we put diff of what is to be committed into
the commit template, and then strip it back out again after the user has
edited it.

We used to look for the diff by searching for the "diff --git a/"
header. With diff.mnemonicprefix set in the configuration, however, this
pattern does not match.  The pattern is loosened to cover this case.

Also, if the user puts their own diff in the message (e.g., as a sample
output), then we will accidentally trigger the pattern, removing part of
their output.

We can avoid doing this stripping altogether if the user didn't use "-v"
in the first place, so we know that any match we find will be a false
positive.

[jc: this fix was split out of a series originally meant for master.]

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin-commit.c          |  8 +++--
 t/t7507-commit-verbose.sh | 73 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+), 3 deletions(-)
 create mode 100755 t/t7507-commit-verbose.sh

diff --git a/builtin-commit.c b/builtin-commit.c
index b563a0d67ce..1e08399919f 100644
--- a/builtin-commit.c
+++ b/builtin-commit.c
@@ -1004,9 +1004,11 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 	}
 
 	/* Truncate the message just before the diff, if any. */
-	p = strstr(sb.buf, "\ndiff --git a/");
-	if (p != NULL)
-		strbuf_setlen(&sb, p - sb.buf + 1);
+	if (verbose) {
+		p = strstr(sb.buf, "\ndiff --git ");
+		if (p != NULL)
+			strbuf_setlen(&sb, p - sb.buf + 1);
+	}
 
 	if (cleanup_mode != CLEANUP_NONE)
 		stripspace(&sb, cleanup_mode == CLEANUP_ALL);
diff --git a/t/t7507-commit-verbose.sh b/t/t7507-commit-verbose.sh
new file mode 100755
index 00000000000..519adba80b4
--- /dev/null
+++ b/t/t7507-commit-verbose.sh
@@ -0,0 +1,73 @@
+#!/bin/sh
+
+test_description='verbose commit template'
+. ./test-lib.sh
+
+cat >check-for-diff <<EOF
+#!$SHELL_PATH
+exec grep '^diff --git' "\$1"
+EOF
+chmod +x check-for-diff
+test_set_editor "$PWD/check-for-diff"
+
+cat >message <<'EOF'
+subject
+
+body
+EOF
+
+test_expect_success 'setup' '
+	echo content >file &&
+	git add file &&
+	git commit -F message
+'
+
+test_expect_failure 'initial commit shows verbose diff' '
+	git commit --amend -v
+'
+
+test_expect_success 'second commit' '
+	echo content modified >file &&
+	git add file &&
+	git commit -F message
+'
+
+check_message() {
+	git log -1 --pretty=format:%s%n%n%b >actual &&
+	test_cmp "$1" actual
+}
+
+test_expect_success 'verbose diff is stripped out' '
+	git commit --amend -v &&
+	check_message message
+'
+
+test_expect_success 'verbose diff is stripped out (mnemonicprefix)' '
+	git config diff.mnemonicprefix true &&
+	git commit --amend -v &&
+	check_message message
+'
+
+cat >diff <<'EOF'
+This is an example commit message that contains a diff.
+
+diff --git c/file i/file
+new file mode 100644
+index 0000000..f95c11d
+--- /dev/null
++++ i/file
+@@ -0,0 +1 @@
++this is some content
+EOF
+
+test_expect_success 'diff in message is retained without -v' '
+	git commit --amend -F diff &&
+	check_message diff
+'
+
+test_expect_failure 'diff in message is retained with -v' '
+	git commit --amend -F diff -v &&
+	check_message diff
+'
+
+test_done