[PATCH 5/5] commit: only strip diff from message in verbose mode

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 guess at the location of the diff by searching for the
"diff --git" header. This means that 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.

Signed-off-by: Jeff King <peff@xxxxxxxx>
---
Obviously this doesn't solve the problem entirely, but it at least makes
a reasonable rule: if we accidentally eat your sample diff output with
"-v", then try without it. :)

But more importantly, I suspect "-v" is used infrequently, so in
practice this should fix most cases.

 builtin-commit.c          |    8 +++++---
 t/t7507-commit-verbose.sh |   21 +++++++++++++++++++++
 2 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/builtin-commit.c b/builtin-commit.c
index a721990..591d16b 100644
--- a/builtin-commit.c
+++ b/builtin-commit.c
@@ -1015,9 +1015,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 ");
-	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
index be70166..2a3ea7a 100755
--- a/t/t7507-commit-verbose.sh
+++ b/t/t7507-commit-verbose.sh
@@ -42,4 +42,25 @@ test_expect_success 'verbose diff is stripped out (mnemonicprefix)' '
 	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
-- 
1.6.0.4.883.g4593ee.dirty
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux