Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
patch: prefer format-patch From line over email header for author
git format-patch embeds a 'From:' line as the first line of the patch
body, carrying the original commit author. Use this line - not the
email From header - as the authoritative author, because git
send-email may rewrite the header via .mailmap while the body line
preserves the original.

The parser reads the first line of the first text body part. If it
starts with 'From:', it extracts 'Name <email>'; when the name is
empty or 'unknown', only the email is kept. Body parts whose first
line is not a From line are ignored, leaving the existing header-
based author untouched.

Signed-off-by: Geliang Tang <geliang@kernel.org>
  • Loading branch information
geliangtang committed Nov 16, 2026
commit 939341719f6642eb95eb9a536785ef4978e8643b
40 changes: 40 additions & 0 deletions src/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,46 @@ pub fn parse_email(raw_email: &[u8]) -> Result<(PatchsetMetadata, Option<Patch>)
}
}

// Use the patch body's leading "From:" line (git format-patch format) as the
// authoritative author instead of the email From header: git send-email may
// apply .mailmap transformations to the email From header, causing it to
// differ from the original author recorded in the patch body.
// (Ported to current main: reads raw body parts because
// strip_format_patch_headers() removes the header lines from `body`.)
for i in 0..message.text_body_count() {
let Some(text) = message.body_text(i) else { continue };
let first_line = text
.replace("\r\n", "\n")
.lines()
.next()
.unwrap_or("")
.trim()
.to_string();
if first_line.starts_with("From:") {
let from_value = first_line.strip_prefix("From:").unwrap_or("").trim();
if !from_value.is_empty() {
if let Some(start_bracket) = from_value.rfind('<') {
if let Some(end_bracket) = from_value.rfind('>') {
if end_bracket > start_bracket {
let name = from_value[..start_bracket].trim();
let email = from_value[start_bracket + 1..end_bracket].trim();
if !email.is_empty() && email.contains('@') {
author = if name.is_empty() || name.to_lowercase() == "unknown" {
email.to_string()
} else {
format_mailbox(name, email)
};
}
}
}
} else if from_value.contains('@') {
author = from_value.to_string();
}
}
}
break;
}

let diff = if has_unquoted_diff(&body) {
body.clone()
} else {
Expand Down