]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/history: generalize function to commit trees
authorPatrick Steinhardt <ps@pks.im>
Mon, 27 Apr 2026 05:53:52 +0000 (07:53 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 27 Apr 2026 13:20:57 +0000 (22:20 +0900)
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>
builtin/history.c

index 952693808574b794363d61b6fd49465d1455cda5..549e352c746c871d7b0378cc15d8ffaacbc3ff4f 100644 (file)
@@ -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;