Skip to content

Commit bc47139

Browse files
peffgitster
authored andcommitted
trailer: fix comment/cut-line regression with opts->no_divider
Commit 97e9d0b (trailer: find the end of the log message, 2023-10-20) combined two code paths for finding the end of the log message. For the "no_divider" case, we used to use find_trailer_end(), and that has now been rolled into find_end_of_log_message(). But there's a regression; that function returns early when no_divider is set, returning the whole string. That's not how find_trailer_end() behaved. Although it did skip the "---" processing (which is what "no_divider" is meant to do), we should still respect ignored_log_message_bytes(), which covers things like comments, "commit -v" cut lines, and so on. The bug is actually in the interpret-trailers command, but the obvious way to experience it is by running "commit -v" with a "--trailer" option. The new trailer will be added at the end of the verbose diff, rather than before it (and consequently will be ignored entirely, since everything after the diff's intro scissors line is thrown away). I've added two tests here: one for interpret-trailers directly, which shows the bug via the parsing routines, and one for "commit -v". The fix itself is pretty simple: instead of returning early, no_divider just skips the "---" handling but still calls ignored_log_message_bytes(). Reported-by: Philippe Blain <levraiphilippeblain@gmail.com> Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent de7c27a commit bc47139

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

t/t7502-commit-porcelain.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,24 @@ test_expect_success 'commit --trailer not confused by --- separator' '
485485
test_cmp expected actual
486486
'
487487

488+
test_expect_success 'commit --trailer with --verbose' '
489+
cat >msg <<-\EOF &&
490+
subject
491+
492+
body
493+
EOF
494+
GIT_EDITOR=: git commit --edit -F msg --allow-empty \
495+
--trailer="my-trailer: value" --verbose &&
496+
{
497+
cat msg &&
498+
echo &&
499+
echo "my-trailer: value"
500+
} >expected &&
501+
git cat-file commit HEAD >commit.msg &&
502+
sed -e "1,/^\$/d" commit.msg >actual &&
503+
test_cmp expected actual
504+
'
505+
488506
test_expect_success 'multiple -m' '
489507
490508
>negative &&

t/t7513-interpret-trailers.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,4 +1560,23 @@ test_expect_success 'suppress --- handling' '
15601560
test_cmp expected actual
15611561
'
15621562

1563+
test_expect_success 'suppressing --- does not disable cut-line handling' '
1564+
echo "real-trailer: before the cut" >expected &&
1565+
1566+
git interpret-trailers --parse --no-divider >actual <<-\EOF &&
1567+
subject
1568+
1569+
This input has a cut-line in it; we should stop parsing when we see it
1570+
and consider only trailers before that line.
1571+
1572+
real-trailer: before the cut
1573+
1574+
# ------------------------ >8 ------------------------
1575+
# Nothing below this line counts as part of the commit message.
1576+
not-a-trailer: too late
1577+
EOF
1578+
1579+
test_cmp expected actual
1580+
'
1581+
15631582
test_done

trailer.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -837,16 +837,15 @@ static size_t find_end_of_log_message(const char *input, int no_divider)
837837
/* Assume the naive end of the input is already what we want. */
838838
end = strlen(input);
839839

840-
if (no_divider)
841-
return end;
842-
843840
/* Optionally skip over any patch part ("---" line and below). */
844-
for (s = input; *s; s = next_line(s)) {
845-
const char *v;
841+
if (!no_divider) {
842+
for (s = input; *s; s = next_line(s)) {
843+
const char *v;
846844

847-
if (skip_prefix(s, "---", &v) && isspace(*v)) {
848-
end = s - input;
849-
break;
845+
if (skip_prefix(s, "---", &v) && isspace(*v)) {
846+
end = s - input;
847+
break;
848+
}
850849
}
851850
}
852851

0 commit comments

Comments
 (0)