]> git.ipfire.org Git - thirdparty/git.git/commitdiff
cat-file: fix mailmap application for different author and committer
authorSiddharth Asthana <siddharthasthana31@gmail.com>
Fri, 13 Jun 2025 11:57:17 +0000 (17:27 +0530)
committerJunio C Hamano <gitster@pobox.com>
Fri, 13 Jun 2025 15:54:51 +0000 (08:54 -0700)
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 <viakliushin@gitlab.com>
Reviewed-by: Christian Couder <christian.couder@gmail.com>
Signed-off-by: Siddharth Asthana <siddharthasthana31@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
ident.c
t/t4203-mailmap.sh

diff --git a/ident.c b/ident.c
index cc7afdbf8197e981e02b6257a4618197063db69f..5f14444f21c18a5580cff45956cdae24e533053a 100644 (file)
--- a/ident.c
+++ b/ident.c
@@ -412,6 +412,10 @@ void apply_mailmap_to_header(struct strbuf *buf, const char **header,
                                found_header = 1;
                                buf_offset += endp - line;
                                buf_offset += rewrite_ident_line(person, endp - person, buf, mailmap);
+                               /* Recompute endp after potential buffer reallocation */
+                               endp = buf->buf + buf_offset;
+                               if (*endp == '\n')
+                                       buf_offset++;
                                break;
                        }
 
index 8a88dd7900ca8a63327bf0853403a38a2c2bc5a9..66a6c1d09ddfa9a6c09055b713f69f498ccf4569 100755 (executable)
@@ -1087,4 +1087,37 @@ test_expect_success 'git cat-file --batch-command returns correct size with --us
        test_cmp expect actual
 '
 
+test_expect_success 'git cat-file --mailmap works with different author and committer' '
+       test_when_finished "rm .mailmap" &&
+       cat >.mailmap <<-\EOF &&
+       Mailmapped User <mailmapped-user@gitlab.com> C O Mitter <committer@example.com>
+       EOF
+       git commit --allow-empty -m "different author/committer" \
+               --author="Different Author <different@example.com>" &&
+       cat >expect <<-\EOF &&
+       author Different Author <different@example.com>
+       committer Mailmapped User <mailmapped-user@gitlab.com>
+       EOF
+       git cat-file --mailmap commit HEAD >log &&
+       sed -n -e "/^author /s/>.*/>/p" -e "/^committer /s/>.*/>/p" log >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success 'git cat-file --mailmap maps both author and committer when both need mapping' '
+       test_when_finished "rm .mailmap" &&
+       cat >.mailmap <<-\EOF &&
+       Mapped Author <mapped-author@example.com> <different@example.com>
+       Mapped Committer <mapped-committer@example.com> C O Mitter <committer@example.com>
+       EOF
+       git commit --allow-empty -m "both author and committer mapped" \
+               --author="Different Author <different@example.com>" &&
+       cat >expect <<-\EOF &&
+       author Mapped Author <mapped-author@example.com>
+       committer Mapped Committer <mapped-committer@example.com>
+       EOF
+       git cat-file --mailmap commit HEAD >log &&
+       sed -n -e "/^author /s/>.*/>/p" -e "/^committer /s/>.*/>/p" log >actual &&
+       test_cmp expect actual
+'
+
 test_done