commit -v: strip diffs and submodule shortlogs from the commit message

When using the '-v' option of "git commit" the diff added to the commit message temporarily for editing is stripped off after the user exited the editor by searching for "\ndiff --git " and truncating the commmit message there if it is found. But this approach has two problems: - when the commit message itself contains a line starting with "diff --git" it will be truncated there prematurely; and - when the "diff.submodule" setting is set to "log", the diff may start with "Submodule <hash1>..<hash2>", which will be left in the commit message while it shouldn't. Fix that by introducing a special scissor separator line starting with the comment character ('#' or the core.commentChar config if set) followed by two lines describing what it is for. The scissor line - which will not be translated - is used to reliably detect the start of the diff so it can be chopped off from the commit message, no matter what the user enters there. Turn a known test failure fixed by this change into a successful test; also add one for a diff starting with a submodule log and another one for proper handling of the comment char. Reported-by: Ari Pollak <ari@debian.org> Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jens Lehmann committed Dec 5, 2013 at 20:44 UTC 1a72cfd7fa88e5a6c2b94568ac2fe69dfbd87f87
4 files changed +58 -9
builtin/commit.c
+3 -6
@@ -1505,7 +1505,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1505 struct strbuf sb = STRBUF_INIT;
1506 struct strbuf author_ident = STRBUF_INIT;
1507 const char *index_file, *reflog_msg;
1508 - char *nl, *p;
1508 + char *nl;
1509 unsigned char sha1[20];
1510 struct ref_lock *ref_lock;
1511 struct commit_list *parents = NULL, **pptr = &parents;
@@ -1601,11 +1601,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1601 }
1602
1603 /* Truncate the message just before the diff, if any. */
1604 - if (verbose) {
1605 - p = strstr(sb.buf, "\ndiff --git ");
1606 - if (p != NULL)
1607 - strbuf_setlen(&sb, p - sb.buf + 1);
1608 - }
1604 + if (verbose)
1605 + wt_status_truncate_message_at_cut_line(&sb);
1606
1607 if (cleanup_mode != CLEANUP_NONE)
1608 stripspace(&sb, cleanup_mode == CLEANUP_ALL);
t/t7507-commit-verbose.sh
+27 -1
@@ -65,9 +65,35 @@ test_expect_success 'diff in message is retained without -v' '
65 check_message diff
66 '
67
68 -test_expect_failure 'diff in message is retained with -v' '
68 +test_expect_success 'diff in message is retained with -v' '
69 git commit --amend -F diff -v &&
70 check_message diff
71 '
72
73 +test_expect_success 'submodule log is stripped out too with -v' '
74 + git config diff.submodule log &&
75 + git submodule add ./. sub &&
76 + git commit -m "sub added" &&
77 + (
78 + cd sub &&
79 + echo "more" >>file &&
80 + git commit -a -m "submodule commit"
81 + ) &&
82 + (
83 + GIT_EDITOR=cat &&
84 + export GIT_EDITOR &&
85 + test_must_fail git commit -a -v 2>err
86 + ) &&
87 + test_i18ngrep "Aborting commit due to empty commit message." err
88 +'
89 +
90 +test_expect_success 'verbose diff is stripped out with set core.commentChar' '
91 + (
92 + GIT_EDITOR=cat &&
93 + export GIT_EDITOR &&
94 + test_must_fail git -c core.commentchar=";" commit -a -v 2>err
95 + ) &&
96 + test_i18ngrep "Aborting commit due to empty commit message." err
97 +'
98 +
99 test_done
wt-status.c
+27 -2
@@ -16,6 +16,9 @@
16 #include "column.h"
17 #include "strbuf.h"
18
19 +static char cut_line[] =
20 +"------------------------ >8 ------------------------\n";
21 +
22 static char default_wt_status_colors[][COLOR_MAXLEN] = {
23 GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
24 GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
@@ -767,6 +770,18 @@ conclude:
770 status_printf_ln(s, GIT_COLOR_NORMAL, "");
771 }
772
773 +void wt_status_truncate_message_at_cut_line(struct strbuf *buf)
774 +{
775 + const char *p;
776 + struct strbuf pattern = STRBUF_INIT;
777 +
778 + strbuf_addf(&pattern, "%c %s", comment_line_char, cut_line);
779 + p = strstr(buf->buf, pattern.buf);
780 + if (p && (p == buf->buf || p[-1] == '\n'))
781 + strbuf_setlen(buf, p - buf->buf);
782 + strbuf_release(&pattern);
783 +}
784 +
785 static void wt_status_print_verbose(struct wt_status *s)
786 {
787 struct rev_info rev;
@@ -787,10 +802,20 @@ static void wt_status_print_verbose(struct wt_status *s)
802 * If we're not going to stdout, then we definitely don't
803 * want color, since we are going to the commit message
804 * file (and even the "auto" setting won't work, since it
790 - * will have checked isatty on stdout).
805 + * will have checked isatty on stdout). But we then do want
806 + * to insert the scissor line here to reliably remove the
807 + * diff before committing.
808 */
792 - if (s->fp != stdout)
809 + if (s->fp != stdout) {
810 + const char *explanation = _("Do not touch the line above.\nEverything below will be removed.");
811 + struct strbuf buf = STRBUF_INIT;
812 +
813 rev.diffopt.use_color = 0;
814 + fprintf(s->fp, "%c %s", comment_line_char, cut_line);
815 + strbuf_add_commented_lines(&buf, explanation, strlen(explanation));
816 + fputs(buf.buf, s->fp);
817 + strbuf_release(&buf);
818 + }
819 run_diff_index(&rev, 1);
820 }
821
wt-status.h
+1
@@ -91,6 +91,7 @@ struct wt_status_state {
91 unsigned char cherry_pick_head_sha1[20];
92 };
93
94 +void wt_status_truncate_message_at_cut_line(struct strbuf *);
95 void wt_status_prepare(struct wt_status *s);
96 void wt_status_print(struct wt_status *s);
97 void wt_status_collect(struct wt_status *s);