Skip to content

Commit 44dc738

Browse files
jonathantanmygitster
authored andcommitted
sequencer: add newline before adding footers
When encountering a commit message that does not end in a newline, sequencer does not complete the line before determining if a blank line should be added. This causes the "(cherry picked..." and sign-off lines to sometimes appear on the same line as the last line of the commit message. This behavior was introduced by commit 967dfd4 ("sequencer: use trailer's trailer layout", 2016-11-29). However, a revert of that commit would not resolve this issue completely: prior to that commit, a conforming footer was deemed to be non-conforming by has_conforming_footer() if there was no terminating newline, resulting in both conforming and non-conforming footers being treated the same when they should not be. Resolve this issue, both for conforming and non-conforming footers, and in both do_pick_commit() and append_signoff(), by always adding a newline to the commit message if it does not end in one before checking the footer for conformity. Reported-by: Brian Norris <[email protected]> Signed-off-by: Jonathan Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 967dfd4 commit 44dc738

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

sequencer.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
698698
strbuf_addstr(&msgbuf, skip_blank_lines(p + 2));
699699

700700
if (opts->record_origin) {
701+
strbuf_complete_line(&msgbuf);
701702
if (!has_conforming_footer(&msgbuf, NULL, 0))
702703
strbuf_addch(&msgbuf, '\n');
703704
strbuf_addstr(&msgbuf, cherry_picked_prefix);
@@ -1362,6 +1363,9 @@ void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag)
13621363
getenv("GIT_COMMITTER_EMAIL")));
13631364
strbuf_addch(&sob, '\n');
13641365

1366+
if (!ignore_footer)
1367+
strbuf_complete_line(msgbuf);
1368+
13651369
/*
13661370
* If the whole message buffer is equal to the sob, pretend that we
13671371
* found a conforming footer with a matching sob
@@ -1382,13 +1386,6 @@ void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag)
13821386
* the title and body to be filled in by the user.
13831387
*/
13841388
append_newlines = "\n\n";
1385-
} else if (msgbuf->buf[len - 1] != '\n') {
1386-
/*
1387-
* Incomplete line. Complete the line and add a
1388-
* blank one so that there is an empty line between
1389-
* the message body and the sob.
1390-
*/
1391-
append_newlines = "\n\n";
13921389
} else if (len == 1) {
13931390
/*
13941391
* Buffer contains a single newline. Add another

t/t3511-cherry-pick-x.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,50 @@ test_expect_success 'cherry-pick -x -s adds sob even when trailing sob exists fo
208208
test_cmp expect actual
209209
'
210210

211+
test_expect_success 'cherry-pick -x handles commits with no NL at end of message' '
212+
pristine_detach initial &&
213+
printf "title\n\nSigned-off-by: A <[email protected]>" >msg &&
214+
sha1=$(git commit-tree -p initial mesg-with-footer^{tree} <msg) &&
215+
git cherry-pick -x $sha1 &&
216+
git log -1 --pretty=format:%B >actual &&
217+
218+
printf "\n(cherry picked from commit %s)\n" $sha1 >>msg &&
219+
test_cmp msg actual
220+
'
221+
222+
test_expect_success 'cherry-pick -x handles commits with no footer and no NL at end of message' '
223+
pristine_detach initial &&
224+
printf "title\n\nnot a footer" >msg &&
225+
sha1=$(git commit-tree -p initial mesg-with-footer^{tree} <msg) &&
226+
git cherry-pick -x $sha1 &&
227+
git log -1 --pretty=format:%B >actual &&
228+
229+
printf "\n\n(cherry picked from commit %s)\n" $sha1 >>msg &&
230+
test_cmp msg actual
231+
'
232+
233+
test_expect_success 'cherry-pick -s handles commits with no NL at end of message' '
234+
pristine_detach initial &&
235+
printf "title\n\nSigned-off-by: A <[email protected]>" >msg &&
236+
sha1=$(git commit-tree -p initial mesg-with-footer^{tree} <msg) &&
237+
git cherry-pick -s $sha1 &&
238+
git log -1 --pretty=format:%B >actual &&
239+
240+
printf "\nSigned-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>\n" >>msg &&
241+
test_cmp msg actual
242+
'
243+
244+
test_expect_success 'cherry-pick -s handles commits with no footer and no NL at end of message' '
245+
pristine_detach initial &&
246+
printf "title\n\nnot a footer" >msg &&
247+
sha1=$(git commit-tree -p initial mesg-with-footer^{tree} <msg) &&
248+
git cherry-pick -s $sha1 &&
249+
git log -1 --pretty=format:%B >actual &&
250+
251+
printf "\n\nSigned-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>\n" >>msg &&
252+
test_cmp msg actual
253+
'
254+
211255
test_expect_success 'cherry-pick -x treats "(cherry picked from..." line as part of footer' '
212256
pristine_detach initial &&
213257
sha1=$(git rev-parse mesg-with-cherry-footer^0) &&

0 commit comments

Comments
 (0)