interpret-trailers: tighten check for "---" patch boundary

The interpret-trailers command accepts not only raw commit messages, but it also can manipulate trailers in format-patch output. That means it must find the "---" boundary separating the commit message from the patch. However, it does so by looking for any line starting with "---", regardless of whether there is further content. This is overly lax compared to the parsing done in mailinfo.c's patchbreak(), and may cause false positives (e.g., t/perf output tables uses dashes; if you cut and paste them into your commit message, it fools the parser). We could try to reuse patchbreak() here, but it actually has several heuristics that are not of interest to us (e.g., matching "diff -" without a three-dash separator or even a CVS "Index:" line). We're not interested in taking in whatever random cruft people may send, but rather handling git-formatted patches. Note that the existing documentation was written in a loose way, so technically we are changing the behavior from what it said. But this should implement the original intent in a more accurate way. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Aug 22, 2018 at 20:48 UTC c188668e387509565237a865a1c63e890f6bbcd7
3 files changed +28 -3
Documentation/git-interpret-trailers.txt
+3 -2
@@ -56,8 +56,9 @@ least one Git-generated or user-configured trailer and consists of at
56 least 25% trailers.
57 The group must be preceded by one or more empty (or whitespace-only) lines.
58 The group must either be at the end of the message or be the last
59 -non-whitespace lines before a line that starts with '---'. Such three
60 -minus signs start the patch part of the message.
59 +non-whitespace lines before a line that starts with '---' (followed by a
60 +space or the end of the line). Such three minus signs start the patch
61 +part of the message.
62
63 When reading trailers, there can be whitespaces after the
64 token, the separator and the value. There can also be whitespaces
t/t7513-interpret-trailers.sh
+22
@@ -1417,4 +1417,26 @@ test_expect_success 'unfold' '
1417 test_cmp expected actual
1418 '
1419
1420 +test_expect_success 'handling of --- lines in input' '
1421 + echo "real-trailer: just right" >expected &&
1422 +
1423 + git interpret-trailers --parse >actual <<-\EOF &&
1424 + subject
1425 +
1426 + body
1427 +
1428 + not-a-trailer: too soon
1429 + ------ this is just a line in the commit message with a bunch of
1430 + ------ dashes; it does not have any syntactic meaning.
1431 +
1432 + real-trailer: just right
1433 + ---
1434 + below the dashed line may be a patch, etc.
1435 +
1436 + not-a-trailer: too late
1437 + EOF
1438 +
1439 + test_cmp expected actual
1440 +'
1441 +
1442 test_done
trailer.c
+3 -1
@@ -793,7 +793,9 @@ static size_t find_patch_start(const char *str)
793 const char *s;
794
795 for (s = str; *s; s = next_line(s)) {
796 - if (starts_with(s, "---"))
796 + const char *v;
797 +
798 + if (skip_prefix(s, "---", &v) && isspace(*v))
799 return s - str;
800 }
801