@matheusbsbs / matheus-bagliano / commits / 0b38227f28
Fork 0

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>

Jeff King committed Nov 12, 2008 at 03:25 UTC 0b38227f283e208866d3e2ac85bcd947a71f1b17
2 files changed +78 -3
builtin-commit.c
+5 -3
@@ -1004,9 +1004,11 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1004 }
1005
1006 /* Truncate the message just before the diff, if any. */
1007 - p = strstr(sb.buf, "\ndiff --git a/");
1008 - if (p != NULL)
1009 - strbuf_setlen(&sb, p - sb.buf + 1);
1007 + if (verbose) {
1008 + p = strstr(sb.buf, "\ndiff --git ");
1009 + if (p != NULL)
1010 + strbuf_setlen(&sb, p - sb.buf + 1);
1011 + }
1012
1013 if (cleanup_mode != CLEANUP_NONE)
1014 stripspace(&sb, cleanup_mode == CLEANUP_ALL);
t/t7507-commit-verbose.sh new
+73
@@ -0,0 +1,73 @@
1 +#!/bin/sh
2 +
3 +test_description='verbose commit template'
4 +. ./test-lib.sh
5 +
6 +cat >check-for-diff <<EOF
7 +#!$SHELL_PATH
8 +exec grep '^diff --git' "\$1"
9 +EOF
10 +chmod +x check-for-diff
11 +test_set_editor "$PWD/check-for-diff"
12 +
13 +cat >message <<'EOF'
14 +subject
15 +
16 +body
17 +EOF
18 +
19 +test_expect_success 'setup' '
20 + echo content >file &&
21 + git add file &&
22 + git commit -F message
23 +'
24 +
25 +test_expect_failure 'initial commit shows verbose diff' '
26 + git commit --amend -v
27 +'
28 +
29 +test_expect_success 'second commit' '
30 + echo content modified >file &&
31 + git add file &&
32 + git commit -F message
33 +'
34 +
35 +check_message() {
36 + git log -1 --pretty=format:%s%n%n%b >actual &&
37 + test_cmp "$1" actual
38 +}
39 +
40 +test_expect_success 'verbose diff is stripped out' '
41 + git commit --amend -v &&
42 + check_message message
43 +'
44 +
45 +test_expect_success 'verbose diff is stripped out (mnemonicprefix)' '
46 + git config diff.mnemonicprefix true &&
47 + git commit --amend -v &&
48 + check_message message
49 +'
50 +
51 +cat >diff <<'EOF'
52 +This is an example commit message that contains a diff.
53 +
54 +diff --git c/file i/file
55 +new file mode 100644
56 +index 0000000..f95c11d
57 +--- /dev/null
58 ++++ i/file
59 +@@ -0,0 +1 @@
60 ++this is some content
61 +EOF
62 +
63 +test_expect_success 'diff in message is retained without -v' '
64 + git commit --amend -F diff &&
65 + check_message diff
66 +'
67 +
68 +test_expect_failure 'diff in message is retained with -v' '
69 + git commit --amend -F diff -v &&
70 + check_message diff
71 +'
72 +
73 +test_done