history: extract helper for a commit's parent tree
Three places resolve the tree of a commit's first parent, falling back to the empty tree for a root commit, each repeating the same parse and oidcpy dance. Extract a first_parent_tree_oid() helper and route the existing callers through it. No change in behavior. Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Harald Nordgren committed
Aug 1, 2026 at 06:53 UTC
e421233248c22ed38a9dad2ce6b54eee4e9b57a1
1 file changed
+26
-32
builtin/history.c
+26
-32
@@ -164,6 +164,25 @@ out:
164
return ret;
165
}
166
167
+static int first_parent_tree_oid(struct repository *repo,
168
+ struct commit *commit,
169
+ struct object_id *out)
170
+{
171
+ struct commit *parent = commit->parents ? commit->parents->item : NULL;
172
+
173
+ if (!parent) {
174
+ oidcpy(out, repo->hash_algo->empty_tree);
175
+ return 0;
176
+ }
177
+
178
+ if (repo_parse_commit(repo, parent))
179
+ return error(_("unable to parse parent commit %s"),
180
+ oid_to_hex(&parent->object.oid));
181
+
182
+ oidcpy(out, &repo_get_commit_tree(repo, parent)->object.oid);
183
+ return 0;
184
+}
185
+
186
static int commit_tree_with_edited_message(struct repository *repo,
187
const char *action,
188
struct commit *original,
@@ -171,21 +190,11 @@ static int commit_tree_with_edited_message(struct repository *repo,
190
{
191
struct object_id parent_tree_oid;
192
const struct object_id *tree_oid;
174
- struct commit *parent;
193
194
tree_oid = &repo_get_commit_tree(repo, original)->object.oid;
195
178
- parent = original->parents ? original->parents->item : NULL;
179
- if (parent) {
180
- if (repo_parse_commit(repo, parent)) {
181
- return error(_("unable to parse parent commit %s"),
182
- oid_to_hex(&parent->object.oid));
183
- }
184
-
185
- parent_tree_oid = repo_get_commit_tree(repo, parent)->object.oid;
186
- } else {
187
- oidcpy(&parent_tree_oid, repo->hash_algo->empty_tree);
188
- }
196
+ if (first_parent_tree_oid(repo, original, &parent_tree_oid) < 0)
197
+ return -1;
198
199
return commit_tree_ext(repo, action, original, original->parents,
200
&parent_tree_oid, tree_oid, out, COMMIT_TREE_EDIT_MESSAGE);
@@ -475,18 +484,10 @@ static int commit_became_empty(struct repository *repo,
484
struct commit *original,
485
struct tree *result)
486
{
478
- struct commit *parent = original->parents ? original->parents->item : NULL;
487
struct object_id parent_tree_oid;
488
481
- if (parent) {
482
- if (repo_parse_commit(repo, parent))
483
- return error(_("unable to parse parent of %s"),
484
- oid_to_hex(&original->object.oid));
485
-
486
- parent_tree_oid = repo_get_commit_tree(repo, parent)->object.oid;
487
- } else {
488
- oidcpy(&parent_tree_oid, repo->hash_algo->empty_tree);
489
- }
489
+ if (first_parent_tree_oid(repo, original, &parent_tree_oid) < 0)
490
+ return -1;
491
492
return oideq(&result->object.oid, &parent_tree_oid);
493
}
@@ -830,16 +831,9 @@ static int split_commit(struct repository *repo,
831
struct tree *split_tree;
832
int ret;
833
833
- if (original->parents) {
834
- if (repo_parse_commit(repo, original->parents->item)) {
835
- ret = error(_("unable to parse parent commit %s"),
836
- oid_to_hex(&original->parents->item->object.oid));
837
- goto out;
838
- }
839
-
840
- parent_tree_oid = *get_commit_tree_oid(original->parents->item);
841
- } else {
842
- oidcpy(&parent_tree_oid, repo->hash_algo->empty_tree);
834
+ if (first_parent_tree_oid(repo, original, &parent_tree_oid) < 0) {
835
+ ret = -1;
836
+ goto out;
837
}
838
original_commit_tree_oid = get_commit_tree_oid(original);
839