From: Patrick Steinhardt Date: Mon, 27 Apr 2026 05:53:52 +0000 (+0200) Subject: builtin/history: generalize function to commit trees X-Git-Tag: v2.55.0-rc0~80^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=88191ea02330627a85664909026fb6953e0d8af8;p=thirdparty%2Fgit.git 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 Signed-off-by: Junio C Hamano --- diff --git a/builtin/history.c b/builtin/history.c index 9526938085..549e352c74 100644 --- a/builtin/history.c +++ b/builtin/history.c @@ -91,13 +91,18 @@ static int fill_commit_message(struct repository *repo, return 0; } -static int commit_tree_with_edited_message_ext(struct repository *repo, - const char *action, - struct commit *commit_with_message, - const struct commit_list *parents, - const struct object_id *old_tree, - const struct object_id *new_tree, - struct commit **out) +enum commit_tree_flags { + COMMIT_TREE_EDIT_MESSAGE = (1 << 0), +}; + +static int commit_tree_ext(struct repository *repo, + const char *action, + struct commit *commit_with_message, + const struct commit_list *parents, + const struct object_id *old_tree, + const struct object_id *new_tree, + struct commit **out, + enum commit_tree_flags flags) { const char *exclude_gpgsig[] = { /* 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, original_author = xmemdupz(ptr, len); find_commit_subject(original_message, &original_body); - ret = fill_commit_message(repo, old_tree, new_tree, - original_body, action, &commit_message); - if (ret < 0) - goto out; + if (flags & COMMIT_TREE_EDIT_MESSAGE) { + ret = fill_commit_message(repo, old_tree, new_tree, + original_body, action, &commit_message); + if (ret < 0) + goto out; + } else { + strbuf_addstr(&commit_message, original_body); + } original_extra_headers = read_commit_extra_headers(commit_with_message, exclude_gpgsig); @@ -168,8 +177,8 @@ static int commit_tree_with_edited_message(struct repository *repo, oidcpy(&parent_tree_oid, repo->hash_algo->empty_tree); } - return commit_tree_with_edited_message_ext(repo, action, original, original->parents, - &parent_tree_oid, tree_oid, out); + return commit_tree_ext(repo, action, original, original->parents, + &parent_tree_oid, tree_oid, out, COMMIT_TREE_EDIT_MESSAGE); } enum ref_action { @@ -616,9 +625,8 @@ static int split_commit(struct repository *repo, * The first commit is constructed from the split-out tree. The base * that shall be diffed against is the parent of the original commit. */ - ret = commit_tree_with_edited_message_ext(repo, "split-out", original, - original->parents, &parent_tree_oid, - &split_tree->object.oid, &first_commit); + ret = commit_tree_ext(repo, "split-out", original, original->parents, &parent_tree_oid, + &split_tree->object.oid, &first_commit, COMMIT_TREE_EDIT_MESSAGE); if (ret < 0) { ret = error(_("failed writing first commit")); goto out; @@ -634,9 +642,8 @@ static int split_commit(struct repository *repo, old_tree_oid = &repo_get_commit_tree(repo, first_commit)->object.oid; new_tree_oid = &repo_get_commit_tree(repo, original)->object.oid; - ret = commit_tree_with_edited_message_ext(repo, "split-out", original, - parents, old_tree_oid, - new_tree_oid, &second_commit); + ret = commit_tree_ext(repo, "split-out", original, parents, old_tree_oid, + new_tree_oid, &second_commit, COMMIT_TREE_EDIT_MESSAGE); if (ret < 0) { ret = error(_("failed writing second commit")); goto out;