commit: cope with scissors lines in commit message

The diff and submodule shortlog appended to the commit message template by 'git commit --verbose' are not stripped when the commit message contains an indented scissors line. When cleaning up a commit message with 'git commit --verbose' or '--cleanup=scissors' the code is careful and triggers only on a pure scissors line, i.e. a line containing nothing but a comment character, a space, and the scissors cut. This is good, because people can embed scissors lines in the commit message while using 'git commit --verbose', and the text they write after their indented scissors line doesn't get deleted. While doing so, however, the cleanup function only looks at the first line matching the scissors pattern and if it doesn't start at the beginning of the line, then the function just returns without performing any cleanup. This is wrong, because a "real" scissors line added by 'git commit --verbose' might follow, and in that case the diff and submodule shortlog get included in the commit message. Fix this by changing the scissors pattern to match only at the beginning of the line, yet be careful to catch scissors on the first line as well. Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: SZEDER Gábor <szeder@ira.uka.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

SZEDER Gábor committed Jun 9, 2015 at 02:28 UTC fbfa0973faf47e475854a8919097d5db29acf536
2 files changed +28 -5
t/t7502-commit.sh
+23 -1
@@ -229,14 +229,36 @@ test_expect_success 'cleanup commit messages (scissors option,-F,-e)' '
229 cat >text <<EOF &&
230
231 # to be kept
232 +
233 + # ------------------------ >8 ------------------------
234 +# to be kept, too
235 # ------------------------ >8 ------------------------
236 to be removed
237 +# ------------------------ >8 ------------------------
238 +to be removed, too
239 +EOF
240 +
241 + cat >expect <<EOF &&
242 +# to be kept
243 +
244 + # ------------------------ >8 ------------------------
245 +# to be kept, too
246 EOF
235 - echo "# to be kept" >expect &&
247 git commit --cleanup=scissors -e -F text -a &&
248 git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
249 test_cmp expect actual
250 +'
251
252 +test_expect_success 'cleanup commit messages (scissors option,-F,-e, scissors on first line)' '
253 +
254 + echo >>negative &&
255 + cat >text <<EOF &&
256 +# ------------------------ >8 ------------------------
257 +to be removed
258 +EOF
259 + git commit --cleanup=scissors -e -F text -a --allow-empty-message &&
260 + git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
261 + test_must_be_empty actual
262 '
263
264 test_expect_success 'cleanup commit messages (strip option,-F)' '
wt-status.c
+5 -4
@@ -844,10 +844,11 @@ void wt_status_truncate_message_at_cut_line(struct strbuf *buf)
844 const char *p;
845 struct strbuf pattern = STRBUF_INIT;
846
847 - strbuf_addf(&pattern, "%c %s", comment_line_char, cut_line);
848 - p = strstr(buf->buf, pattern.buf);
849 - if (p && (p == buf->buf || p[-1] == '\n'))
850 - strbuf_setlen(buf, p - buf->buf);
847 + strbuf_addf(&pattern, "\n%c %s", comment_line_char, cut_line);
848 + if (starts_with(buf->buf, pattern.buf + 1))
849 + strbuf_setlen(buf, 0);
850 + else if ((p = strstr(buf->buf, pattern.buf)))
851 + strbuf_setlen(buf, p - buf->buf + 1);
852 strbuf_release(&pattern);
853 }
854