]> git.ipfire.org Git - thirdparty/git.git/commitdiff
interpret-trailers: handle message without trailing newline
authorBrian Lyles <brianmlyles@gmail.com>
Fri, 6 Sep 2024 14:50:08 +0000 (09:50 -0500)
committerJunio C Hamano <gitster@pobox.com>
Fri, 6 Sep 2024 16:21:44 +0000 (09:21 -0700)
When git-interpret-trailers is used to add a trailer to a message that
does not end in a trailing newline, the new trailer is added on the line
immediately following the message instead of as a trailer block
separated from the message by a blank line.

For example, if a message's text was exactly "The subject" with no
trailing newline present, `git interpret-trailers --trailer
my-trailer=true` will result in the following malformed commit message:

    The subject
    my-trailer: true

While it is generally expected that a commit message should end with a
newline character, git-interpret-trailers should not be returning an
invalid message in this case.

Use `strbuf_complete_line` to ensure that the message ends with a
newline character when reading the input.

Signed-off-by: Brian Lyles <brianmlyles@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/interpret-trailers.c
t/t7513-interpret-trailers.sh

index 1d969494cf54712d906947a9e6e591f2ecbfce7e..e6f22459f15334e38461ae652a2beddf54ebcecd 100644 (file)
@@ -132,6 +132,7 @@ static void read_input_file(struct strbuf *sb, const char *file)
                if (strbuf_read(sb, fileno(stdin), 0) < 0)
                        die_errno(_("could not read from stdin"));
        }
+       strbuf_complete_line(sb);
 }
 
 static void interpret_trailers(const struct process_trailer_options *opts,
index 3d3e13ccf87215adc1f8dcc8cc674e13d0f4bc91..0f7d8938d984d9a2c560b4b8d491c10124a9c301 100755 (executable)
@@ -175,6 +175,46 @@ test_expect_success 'with only a title in the message' '
        test_cmp expected actual
 '
 
+test_expect_success 'with a bodiless message that lacks a trailing newline after the subject' '
+       cat >expected <<-\EOF &&
+               area: change
+
+               Reviewed-by: Peff
+               Acked-by: Johan
+       EOF
+       printf "area: change" |
+       git interpret-trailers --trailer "Reviewed-by: Peff" \
+               --trailer "Acked-by: Johan" >actual &&
+       test_cmp expected actual
+'
+
+test_expect_success 'with a bodied message that lacks a trailing newline after the body' '
+       cat >expected <<-\EOF &&
+               area: change
+
+               details about the change.
+
+               Reviewed-by: Peff
+               Acked-by: Johan
+       EOF
+       printf "area: change\n\ndetails about the change." |
+       git interpret-trailers --trailer "Reviewed-by: Peff" \
+               --trailer "Acked-by: Johan" >actual &&
+       test_cmp expected actual
+'
+
+test_expect_success 'with a message that lacks a trailing newline after the trailers' '
+       cat >expected <<-\EOF &&
+               area: change
+
+               Reviewed-by: Peff
+               Acked-by: Johan
+       EOF
+       printf "area: change\n\nReviewed-by: Peff" |
+       git interpret-trailers --trailer "Acked-by: Johan" >actual &&
+       test_cmp expected actual
+'
+
 test_expect_success 'with multiline title in the message' '
        cat >expected <<-\EOF &&
                place of