Skip to content

Commit 3868254

Browse files
pks-tgitster
authored andcommitted
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>
1 parent 41fab83 commit 3868254

1 file changed

Lines changed: 26 additions & 19 deletions

File tree

builtin/history.c

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,18 @@ static int fill_commit_message(struct repository *repo,
9191
return 0;
9292
}
9393

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)
101106
{
102107
const char *exclude_gpgsig[] = {
103108
/* 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,
122127
original_author = xmemdupz(ptr, len);
123128
find_commit_subject(original_message, &original_body);
124129

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+
}
129138

130139
original_extra_headers = read_commit_extra_headers(commit_with_message,
131140
exclude_gpgsig);
@@ -168,8 +177,8 @@ static int commit_tree_with_edited_message(struct repository *repo,
168177
oidcpy(&parent_tree_oid, repo->hash_algo->empty_tree);
169178
}
170179

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);
173182
}
174183

175184
enum ref_action {
@@ -616,9 +625,8 @@ static int split_commit(struct repository *repo,
616625
* The first commit is constructed from the split-out tree. The base
617626
* that shall be diffed against is the parent of the original commit.
618627
*/
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);
622630
if (ret < 0) {
623631
ret = error(_("failed writing first commit"));
624632
goto out;
@@ -634,9 +642,8 @@ static int split_commit(struct repository *repo,
634642
old_tree_oid = &repo_get_commit_tree(repo, first_commit)->object.oid;
635643
new_tree_oid = &repo_get_commit_tree(repo, original)->object.oid;
636644

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);
640647
if (ret < 0) {
641648
ret = error(_("failed writing second commit"));
642649
goto out;

0 commit comments

Comments
 (0)