Skip to content

Commit abf94a2

Browse files
edith007gitster
authored andcommitted
cat-file: fix mailmap application for different author and committer
The git cat-file command with --mailmap option fails to apply mailmap transformations to the committer field when the author and committer identities are different. This occurs due to a missing newline handling in apply_mailmap_to_header() after processing each identity line. When rewrite_ident_line() processes an identity, it stops at the end of the identity data (e.g., "Author Name <email> timestamp"), but doesn't account for the trailing newline. The current code adds the identity length to buf_offset but fails to advance past the newline character. This causes the next iteration to start parsing from the newline instead of the beginning of the next header line, making it impossible to match subsequent headers like "committer". Additionally, rewrite_ident_line() may reallocate the buffer during its operation. Any code using pointers into the old buffer would be using invalid memory after such a reallocation. This bug was introduced in e9c1b0e3 (revision: improve commit_rewrite_person(), 2022-07-19) when the much simpler version of commit_rewrite_person() that worked on one "person header" at a time was rewritten to use the current apply_mailmap_to_header() function. The original implementation processed author and committer separately, but the rewrite introduced this loop-based approach that failed to properly handle the transition between identity lines. Let's fix this by addressing both issues: 1. After processing an identity line, we now check if we're at a newline and advance past it, ensuring the next header line is parsed correctly. 2. We recompute the buffer position after rewrite_ident_line() to handle potential buffer reallocation. This ensures that all identity headers in commit and tag objects are consistently processed regardless of whether the author and committer are the same person. Reported-by: Vasilii Iakliushin <[email protected]> Reviewed-by: Christian Couder <[email protected]> Signed-off-by: Siddharth Asthana <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2f323bb commit abf94a2

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

ident.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,10 @@ void apply_mailmap_to_header(struct strbuf *buf, const char **header,
412412
found_header = 1;
413413
buf_offset += endp - line;
414414
buf_offset += rewrite_ident_line(person, endp - person, buf, mailmap);
415+
/* Recompute endp after potential buffer reallocation */
416+
endp = buf->buf + buf_offset;
417+
if (*endp == '\n')
418+
buf_offset++;
415419
break;
416420
}
417421

t/t4203-mailmap.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,4 +1087,37 @@ test_expect_success 'git cat-file --batch-command returns correct size with --us
10871087
test_cmp expect actual
10881088
'
10891089

1090+
test_expect_success 'git cat-file --mailmap works with different author and committer' '
1091+
test_when_finished "rm .mailmap" &&
1092+
cat >.mailmap <<-\EOF &&
1093+
Mailmapped User <[email protected]> C O Mitter <[email protected]>
1094+
EOF
1095+
git commit --allow-empty -m "different author/committer" \
1096+
--author="Different Author <[email protected]>" &&
1097+
cat >expect <<-\EOF &&
1098+
author Different Author <[email protected]>
1099+
committer Mailmapped User <[email protected]>
1100+
EOF
1101+
git cat-file --mailmap commit HEAD >log &&
1102+
sed -n -e "/^author /s/>.*/>/p" -e "/^committer /s/>.*/>/p" log >actual &&
1103+
test_cmp expect actual
1104+
'
1105+
1106+
test_expect_success 'git cat-file --mailmap maps both author and committer when both need mapping' '
1107+
test_when_finished "rm .mailmap" &&
1108+
cat >.mailmap <<-\EOF &&
1109+
1110+
Mapped Committer <[email protected]> C O Mitter <[email protected]>
1111+
EOF
1112+
git commit --allow-empty -m "both author and committer mapped" \
1113+
--author="Different Author <[email protected]>" &&
1114+
cat >expect <<-\EOF &&
1115+
author Mapped Author <[email protected]>
1116+
committer Mapped Committer <[email protected]>
1117+
EOF
1118+
git cat-file --mailmap commit HEAD >log &&
1119+
sed -n -e "/^author /s/>.*/>/p" -e "/^committer /s/>.*/>/p" log >actual &&
1120+
test_cmp expect actual
1121+
'
1122+
10901123
test_done

0 commit comments

Comments
 (0)