builtin/history: generalize function to commit trees

The function `commit_tree_with_edited_message_ext()` can be used to commit a tree with a specific list of parents with an edited commit message. This function is useful outside of editing the commit message though, as it also performs the plumbing to extract the original commit message and strip some headers from it. Refactor the function to receive a flags field that allows the caller to control whether or not the commit message should be edited, or whether it should be retained as-is. This will be used in a subsequent commit. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Apr 27, 2026 at 07:53 UTC 88191ea02330627a85664909026fb6953e0d8af8
1 file changed +26 -19
builtin/history.c
+26 -19
@@ -91,13 +91,18 @@ static int fill_commit_message(struct repository *repo,
91 return 0;
92 }
93
94 -static int commit_tree_with_edited_message_ext(struct repository *repo,
95 - const char *action,
96 - struct commit *commit_with_message,
97 - const struct commit_list *parents,
98 - const struct object_id *old_tree,
99 - const struct object_id *new_tree,
100 - struct commit **out)
94 +enum commit_tree_flags {
95 + COMMIT_TREE_EDIT_MESSAGE = (1 << 0),
96 +};
97 +
98 +static int commit_tree_ext(struct repository *repo,
99 + const char *action,
100 + struct commit *commit_with_message,
101 + const struct commit_list *parents,
102 + const struct object_id *old_tree,
103 + const struct object_id *new_tree,
104 + struct commit **out,
105 + enum commit_tree_flags flags)
106 {
107 const char *exclude_gpgsig[] = {
108 /* We reencode the message, so the encoding needs to be stripped. */
@@ -122,10 +127,14 @@ static int commit_tree_with_edited_message_ext(struct repository *repo,
127 original_author = xmemdupz(ptr, len);
128 find_commit_subject(original_message, &original_body);
129
125 - ret = fill_commit_message(repo, old_tree, new_tree,
126 - original_body, action, &commit_message);
127 - if (ret < 0)
128 - goto out;
130 + if (flags & COMMIT_TREE_EDIT_MESSAGE) {
131 + ret = fill_commit_message(repo, old_tree, new_tree,
132 + original_body, action, &commit_message);
133 + if (ret < 0)
134 + goto out;
135 + } else {
136 + strbuf_addstr(&commit_message, original_body);
137 + }
138
139 original_extra_headers = read_commit_extra_headers(commit_with_message,
140 exclude_gpgsig);
@@ -168,8 +177,8 @@ static int commit_tree_with_edited_message(struct repository *repo,
177 oidcpy(&parent_tree_oid, repo->hash_algo->empty_tree);
178 }
179
171 - return commit_tree_with_edited_message_ext(repo, action, original, original->parents,
172 - &parent_tree_oid, tree_oid, out);
180 + return commit_tree_ext(repo, action, original, original->parents,
181 + &parent_tree_oid, tree_oid, out, COMMIT_TREE_EDIT_MESSAGE);
182 }
183
184 enum ref_action {
@@ -616,9 +625,8 @@ static int split_commit(struct repository *repo,
625 * The first commit is constructed from the split-out tree. The base
626 * that shall be diffed against is the parent of the original commit.
627 */
619 - ret = commit_tree_with_edited_message_ext(repo, "split-out", original,
620 - original->parents, &parent_tree_oid,
621 - &split_tree->object.oid, &first_commit);
628 + ret = commit_tree_ext(repo, "split-out", original, original->parents, &parent_tree_oid,
629 + &split_tree->object.oid, &first_commit, COMMIT_TREE_EDIT_MESSAGE);
630 if (ret < 0) {
631 ret = error(_("failed writing first commit"));
632 goto out;
@@ -634,9 +642,8 @@ static int split_commit(struct repository *repo,
642 old_tree_oid = &repo_get_commit_tree(repo, first_commit)->object.oid;
643 new_tree_oid = &repo_get_commit_tree(repo, original)->object.oid;
644
637 - ret = commit_tree_with_edited_message_ext(repo, "split-out", original,
638 - parents, old_tree_oid,
639 - new_tree_oid, &second_commit);
645 + ret = commit_tree_ext(repo, "split-out", original, parents, old_tree_oid,
646 + new_tree_oid, &second_commit, COMMIT_TREE_EDIT_MESSAGE);
647 if (ret < 0) {
648 ret = error(_("failed writing second commit"));
649 goto out;