Skip to content

Commit d6a2b05

Browse files
committed
Merge branch 'jc/builtin-am-signoff-regression-fix'
Recent "git am" had regression when adding a Signed-off-by line with its "-s" option by an unintended tightening of how an existing trailer block is detected. * jc/builtin-am-signoff-regression-fix: am: match --signoff to the original scripted version
2 parents ec371ff + aab8454 commit d6a2b05

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

builtin/am.c

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,33 @@ static void NORETURN die_user_resolve(const struct am_state *state)
12071207
exit(128);
12081208
}
12091209

1210+
static void am_signoff(struct strbuf *sb)
1211+
{
1212+
char *cp;
1213+
struct strbuf mine = STRBUF_INIT;
1214+
1215+
/* Does it end with our own sign-off? */
1216+
strbuf_addf(&mine, "\n%s%s\n",
1217+
sign_off_header,
1218+
fmt_name(getenv("GIT_COMMITTER_NAME"),
1219+
getenv("GIT_COMMITTER_EMAIL")));
1220+
if (mine.len < sb->len &&
1221+
!strcmp(mine.buf, sb->buf + sb->len - mine.len))
1222+
goto exit; /* no need to duplicate */
1223+
1224+
/* Does it have any Signed-off-by: in the text */
1225+
for (cp = sb->buf;
1226+
cp && *cp && (cp = strstr(cp, sign_off_header)) != NULL;
1227+
cp = strchr(cp, '\n')) {
1228+
if (sb->buf == cp || cp[-1] == '\n')
1229+
break;
1230+
}
1231+
1232+
strbuf_addstr(sb, mine.buf + !!cp);
1233+
exit:
1234+
strbuf_release(&mine);
1235+
}
1236+
12101237
/**
12111238
* Appends signoff to the "msg" field of the am_state.
12121239
*/
@@ -1215,7 +1242,7 @@ static void am_append_signoff(struct am_state *state)
12151242
struct strbuf sb = STRBUF_INIT;
12161243

12171244
strbuf_attach(&sb, state->msg, state->msg_len, state->msg_len);
1218-
append_signoff(&sb, 0, 0);
1245+
am_signoff(&sb);
12191246
state->msg = strbuf_detach(&sb, &state->msg_len);
12201247
}
12211248

@@ -1319,7 +1346,7 @@ static int parse_mail(struct am_state *state, const char *mail)
13191346
stripspace(&msg, 0);
13201347

13211348
if (state->signoff)
1322-
append_signoff(&msg, 0, 0);
1349+
am_signoff(&msg);
13231350

13241351
assert(!state->author_name);
13251352
state->author_name = strbuf_detach(&author_name, NULL);

t/t4150-am.sh

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,4 +909,52 @@ test_expect_success 'am -3 works with rerere' '
909909
test_cmp expect file
910910
'
911911

912+
test_expect_success 'am -s unexpected trailer block' '
913+
rm -fr .git/rebase-apply &&
914+
git reset --hard &&
915+
echo signed >file &&
916+
git add file &&
917+
cat >msg <<-EOF &&
918+
subject here
919+
920+
Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
921+
[jc: tweaked log message]
922+
Signed-off-by: J C H <[email protected]>
923+
EOF
924+
git commit -F msg &&
925+
git cat-file commit HEAD | sed -e '1,/^$/d' >original &&
926+
git format-patch --stdout -1 >patch &&
927+
928+
git reset --hard HEAD^ &&
929+
git am -s patch &&
930+
(
931+
cat original &&
932+
echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
933+
) >expect &&
934+
git cat-file commit HEAD | sed -e '1,/^$/d' >actual &&
935+
test_cmp expect actual &&
936+
937+
cat >msg <<-\EOF &&
938+
subject here
939+
940+
We make sure that there is a blank line between the log
941+
message proper and Signed-off-by: line added.
942+
EOF
943+
git reset HEAD^ &&
944+
git commit -F msg file &&
945+
git cat-file commit HEAD | sed -e '1,/^$/d' >original &&
946+
git format-patch --stdout -1 >patch &&
947+
948+
git reset --hard HEAD^ &&
949+
git am -s patch &&
950+
951+
(
952+
cat original &&
953+
echo &&
954+
echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
955+
) >expect &&
956+
git cat-file commit HEAD | sed -e '1,/^$/d' >actual &&
957+
test_cmp expect actual
958+
'
959+
912960
test_done

0 commit comments

Comments
 (0)