Skip to content

Commit e2ae5ec

Browse files
committed
Merge branch 'jt/use-trailer-api-in-commands' into maint
"git cherry-pick" and other uses of the sequencer machinery mishandled a trailer block whose last line is an incomplete line. This has been fixed so that an additional sign-off etc. are added after completing the existing incomplete line. * jt/use-trailer-api-in-commands: sequencer: add newline before adding footers
2 parents 058d655 + 44dc738 commit e2ae5ec

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
@@ -1045,6 +1045,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
10451045
strbuf_addstr(&msgbuf, p);
10461046

10471047
if (opts->record_origin) {
1048+
strbuf_complete_line(&msgbuf);
10481049
if (!has_conforming_footer(&msgbuf, NULL, 0))
10491050
strbuf_addch(&msgbuf, '\n');
10501051
strbuf_addstr(&msgbuf, cherry_picked_prefix);
@@ -2360,6 +2361,9 @@ void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag)
23602361
getenv("GIT_COMMITTER_EMAIL")));
23612362
strbuf_addch(&sob, '\n');
23622363

2364+
if (!ignore_footer)
2365+
strbuf_complete_line(msgbuf);
2366+
23632367
/*
23642368
* If the whole message buffer is equal to the sob, pretend that we
23652369
* found a conforming footer with a matching sob
@@ -2380,13 +2384,6 @@ void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag)
23802384
* the title and body to be filled in by the user.
23812385
*/
23822386
append_newlines = "\n\n";
2383-
} else if (msgbuf->buf[len - 1] != '\n') {
2384-
/*
2385-
* Incomplete line. Complete the line and add a
2386-
* blank one so that there is an empty line between
2387-
* the message body and the sob.
2388-
*/
2389-
append_newlines = "\n\n";
23902387
} else if (len == 1) {
23912388
/*
23922389
* 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)