Skip to content

Commit 8250ba3

Browse files
lbonanomidscho
authored andcommitted
commit: accept "scissors" with CR/LF line endings
This change enhances `git commit --cleanup=scissors` by detecting scissors lines ending in either LF (UNIX-style) or CR/LF (DOS-style). Regression tests are included to specifically test for trailing comments after a CR/LF-terminated scissors line. Signed-off-by: Luke Bonanomi <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 5501b4a commit 8250ba3

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

t/t7502-commit-porcelain.sh

+42
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,48 @@ test_expect_success 'cleanup commit messages (scissors option,-F,-e, scissors on
586586
test_must_be_empty actual
587587
'
588588

589+
test_expect_success 'helper-editor' '
590+
591+
write_script lf-to-crlf.sh <<-\EOF
592+
sed "s/\$/Q/" <"$1" | tr Q "\\015" >"$1".new &&
593+
mv -f "$1".new "$1"
594+
EOF
595+
'
596+
597+
test_expect_success 'cleanup commit messages (scissors option,-F,-e, CR/LF line endings)' '
598+
599+
test_config core.editor "\"$PWD/lf-to-crlf.sh\"" &&
600+
scissors="# ------------------------ >8 ------------------------" &&
601+
602+
test_write_lines >text \
603+
"# Keep this comment" "" " $scissors" \
604+
"# Keep this comment, too" "$scissors" \
605+
"# Remove this comment" "$scissors" \
606+
"Remove this comment, too" &&
607+
608+
test_write_lines >expect \
609+
"# Keep this comment" "" " $scissors" \
610+
"# Keep this comment, too" &&
611+
612+
git commit --cleanup=scissors -e -F text --allow-empty &&
613+
git cat-file -p HEAD >raw &&
614+
sed -e "1,/^\$/d" raw >actual &&
615+
test_cmp expect actual
616+
'
617+
618+
test_expect_success 'cleanup commit messages (scissors option,-F,-e, scissors on first line, CR/LF line endings)' '
619+
620+
scissors="# ------------------------ >8 ------------------------" &&
621+
test_write_lines >text \
622+
"$scissors" \
623+
"# Remove this comment and any following lines" &&
624+
cp text /tmp/test2-text &&
625+
git commit --cleanup=scissors -e -F text --allow-empty --allow-empty-message &&
626+
git cat-file -p HEAD >raw &&
627+
sed -e "1,/^\$/d" raw >actual &&
628+
test_must_be_empty actual
629+
'
630+
589631
test_expect_success 'cleanup commit messages (strip option,-F)' '
590632
591633
echo >>negative &&

wt-status.c

+10-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#define AB_DELAY_WARNING_IN_MS (2 * 1000)
2323

2424
static const char cut_line[] =
25-
"------------------------ >8 ------------------------\n";
25+
"------------------------ >8 ------------------------";
2626

2727
static char default_wt_status_colors[][COLOR_MAXLEN] = {
2828
GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
@@ -1070,15 +1070,22 @@ static void wt_longstatus_print_other(struct wt_status *s,
10701070
status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
10711071
}
10721072

1073+
static inline int starts_with_newline(const char *p)
1074+
{
1075+
return *p == '\n' || (*p == '\r' && p[1] == '\n');
1076+
}
1077+
10731078
size_t wt_status_locate_end(const char *s, size_t len)
10741079
{
10751080
const char *p;
10761081
struct strbuf pattern = STRBUF_INIT;
10771082

10781083
strbuf_addf(&pattern, "\n%c %s", comment_line_char, cut_line);
1079-
if (starts_with(s, pattern.buf + 1))
1084+
if (starts_with(s, pattern.buf + 1) &&
1085+
starts_with_newline(s + pattern.len - 1))
10801086
len = 0;
1081-
else if ((p = strstr(s, pattern.buf)))
1087+
else if ((p = strstr(s, pattern.buf)) &&
1088+
starts_with_newline(p + pattern.len))
10821089
len = p - s + 1;
10831090
strbuf_release(&pattern);
10841091
return len;

0 commit comments

Comments
 (0)