Subject: [PATCH 2/3] builtin/am.c: add a message-id commit header

From: James Bottomley <[email-masked]>
Date: Thu, 16 Oct 2025 14:57:57 -0400

Now that mailinfo is updated to collect the message_id all the time,
use this in do_commit to add a "message-id" extra header containing
the message_id if it exists.  This means that git am will always
record the message-id if it can be found in the commit.  It will still
add it to the trailer if -m is specified, keeping the behaviour
backwards compatible.

Signed-off-by: James Bottomley <[email-masked]>
---
 builtin/am.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/builtin/am.c b/builtin/am.c
index 277c2e7937..ab05701a8d 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -119,6 +119,7 @@ struct am_state {
 	char *author_name;
 	char *author_email;
 	char *author_date;
+	char *msg_id;
 	char *msg;
 	size_t msg_len;
 
@@ -187,6 +188,7 @@ static void am_state_release(struct am_state *state)
 	free(state->author_email);
 	free(state->author_date);
 	free(state->msg);
+	free(state->msg_id);
 	strvec_clear(&state->git_apply_opts);
 }
 
@@ -1313,6 +1315,9 @@ static int parse_mail(struct am_state *state, const char *mail)
 	assert(!state->msg);
 	state->msg = strbuf_detach(&msg, &state->msg_len);
 
+	assert(!state->msg_id);
+	state->msg_id = xstrdup_or_null(mi.message_id);
+
 finish:
 	strbuf_release(&msg);
 	strbuf_release(&author_date);
@@ -1668,6 +1673,7 @@ static void do_commit(const struct am_state *state)
 	struct commit_list *parents = NULL;
 	const char *reflog_msg, *author, *committer = NULL;
 	struct strbuf sb = STRBUF_INIT;
+	struct commit_extra_header *extra = NULL;
 
 	if (!state->no_verify && run_hooks(the_repository, "pre-applypatch"))
 		exit(1);
@@ -1699,9 +1705,16 @@ static void do_commit(const struct am_state *state)
 							 : state->author_date,
 				      IDENT_STRICT);
 
+	if (state->msg_id) {
+		CALLOC_ARRAY(extra, 1);
+		extra->key = xstrdup("message-id");
+		extra->value = xstrdup(state->msg_id);
+		extra->len = strlen(extra->value);
+	}
+
 	if (commit_tree_extended(state->msg, state->msg_len, &tree, parents,
 				 &commit, author, committer, state->sign_commit,
-				 NULL))
+				 extra))
 		die(_("failed to write commit object"));
 
 	reflog_msg = getenv("GIT_REFLOG_ACTION");
-- 
2.51.0