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>
seen
Harald Nordgren 2026-08-20 18:10:25 +00:00 committed by Junio C Hamano
parent 48bbf81c29
commit b2da63b5de
1 changed files with 26 additions and 32 deletions

View File

@ -164,6 +164,25 @@ out:
return ret;
}

static int first_parent_tree_oid(struct repository *repo,
struct commit *commit,
struct object_id *out)
{
struct commit *parent = commit->parents ? commit->parents->item : NULL;

if (!parent) {
oidcpy(out, repo->hash_algo->empty_tree);
return 0;
}

if (repo_parse_commit(repo, parent))
return error(_("unable to parse parent commit %s"),
oid_to_hex(&parent->object.oid));

oidcpy(out, &repo_get_commit_tree(repo, parent)->object.oid);
return 0;
}

static int commit_tree_with_edited_message(struct repository *repo,
const char *action,
struct commit *original,
@ -171,21 +190,11 @@ static int commit_tree_with_edited_message(struct repository *repo,
{
struct object_id parent_tree_oid;
const struct object_id *tree_oid;
struct commit *parent;

tree_oid = &repo_get_commit_tree(repo, original)->object.oid;

parent = original->parents ? original->parents->item : NULL;
if (parent) {
if (repo_parse_commit(repo, parent)) {
return error(_("unable to parse parent commit %s"),
oid_to_hex(&parent->object.oid));
}

parent_tree_oid = repo_get_commit_tree(repo, parent)->object.oid;
} else {
oidcpy(&parent_tree_oid, repo->hash_algo->empty_tree);
}
if (first_parent_tree_oid(repo, original, &parent_tree_oid) < 0)
return -1;

return commit_tree_ext(repo, action, original, original->parents,
&parent_tree_oid, tree_oid, out, COMMIT_TREE_EDIT_MESSAGE);
@ -475,18 +484,10 @@ static int commit_became_empty(struct repository *repo,
struct commit *original,
struct tree *result)
{
struct commit *parent = original->parents ? original->parents->item : NULL;
struct object_id parent_tree_oid;

if (parent) {
if (repo_parse_commit(repo, parent))
return error(_("unable to parse parent of %s"),
oid_to_hex(&original->object.oid));

parent_tree_oid = repo_get_commit_tree(repo, parent)->object.oid;
} else {
oidcpy(&parent_tree_oid, repo->hash_algo->empty_tree);
}
if (first_parent_tree_oid(repo, original, &parent_tree_oid) < 0)
return -1;

return oideq(&result->object.oid, &parent_tree_oid);
}
@ -830,16 +831,9 @@ static int split_commit(struct repository *repo,
struct tree *split_tree;
int ret;

if (original->parents) {
if (repo_parse_commit(repo, original->parents->item)) {
ret = error(_("unable to parse parent commit %s"),
oid_to_hex(&original->parents->item->object.oid));
goto out;
}

parent_tree_oid = *get_commit_tree_oid(original->parents->item);
} else {
oidcpy(&parent_tree_oid, repo->hash_algo->empty_tree);
if (first_parent_tree_oid(repo, original, &parent_tree_oid) < 0) {
ret = -1;
goto out;
}
original_commit_tree_oid = get_commit_tree_oid(original);