interpret-trailers: handle message without trailing newline

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>

Brian Lyles committed Sep 6, 2024 at 09:50 UTC c02414a99796487a2124e4dbb17c3a88c8606404
2 files changed +41
builtin/interpret-trailers.c
+1
@@ -132,6 +132,7 @@ static void read_input_file(struct strbuf *sb, const char *file)
132 if (strbuf_read(sb, fileno(stdin), 0) < 0)
133 die_errno(_("could not read from stdin"));
134 }
135 + strbuf_complete_line(sb);
136 }
137
138 static void interpret_trailers(const struct process_trailer_options *opts,
t/t7513-interpret-trailers.sh
+40
@@ -175,6 +175,46 @@ test_expect_success 'with only a title in the message' '
175 test_cmp expected actual
176 '
177
178 +test_expect_success 'with a bodiless message that lacks a trailing newline after the subject' '
179 + cat >expected <<-\EOF &&
180 + area: change
181 +
182 + Reviewed-by: Peff
183 + Acked-by: Johan
184 + EOF
185 + printf "area: change" |
186 + git interpret-trailers --trailer "Reviewed-by: Peff" \
187 + --trailer "Acked-by: Johan" >actual &&
188 + test_cmp expected actual
189 +'
190 +
191 +test_expect_success 'with a bodied message that lacks a trailing newline after the body' '
192 + cat >expected <<-\EOF &&
193 + area: change
194 +
195 + details about the change.
196 +
197 + Reviewed-by: Peff
198 + Acked-by: Johan
199 + EOF
200 + printf "area: change\n\ndetails about the change." |
201 + git interpret-trailers --trailer "Reviewed-by: Peff" \
202 + --trailer "Acked-by: Johan" >actual &&
203 + test_cmp expected actual
204 +'
205 +
206 +test_expect_success 'with a message that lacks a trailing newline after the trailers' '
207 + cat >expected <<-\EOF &&
208 + area: change
209 +
210 + Reviewed-by: Peff
211 + Acked-by: Johan
212 + EOF
213 + printf "area: change\n\nReviewed-by: Peff" |
214 + git interpret-trailers --trailer "Acked-by: Johan" >actual &&
215 + test_cmp expected actual
216 +'
217 +
218 test_expect_success 'with multiline title in the message' '
219 cat >expected <<-\EOF &&
220 place of