replay: add helper to put entry into replayed_commits

The function replay_revisions() in replay.c is rather lengthy. Extract
the logic to put a commit entry into a `struct mapped_commits` into a
helper function put_mapped_commit().

While at it, rename mapped_commit() to get_mapped_commit() to pair with
this new function.

Signed-off-by: Toon Claes <toon@iotcl.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
jch
Toon Claes 2026-07-07 21:07:25 +02:00 committed by Junio C Hamano
parent 1ff279f340
commit 4ae2ac8153
1 changed files with 20 additions and 11 deletions

View File

@ -250,9 +250,9 @@ static void set_up_replay_mode(struct repository *repo,
strset_clear(&rinfo.positive_refs);
}

static struct commit *mapped_commit(kh_oid_map_t *replayed_commits,
struct commit *commit,
struct commit *fallback)
static struct commit *get_mapped_commit(kh_oid_map_t *replayed_commits,
struct commit *commit,
struct commit *fallback)
{
khint_t pos;
if (!commit)
@ -263,6 +263,21 @@ static struct commit *mapped_commit(kh_oid_map_t *replayed_commits,
return kh_value(replayed_commits, pos);
}

static void put_mapped_commit(kh_oid_map_t *replayed_commits,
struct commit *commit,
struct commit *new_commit)
{
khint_t pos;
int ret;

pos = kh_put_oid_map(replayed_commits, commit->object.oid, &ret);
if (ret == 0)
BUG("Duplicate rewritten commit: %s",
oid_to_hex(&commit->object.oid));

kh_value(replayed_commits, pos) = new_commit;
}

static struct commit *pick_regular_commit(struct repository *repo,
struct commit *pickme,
kh_oid_map_t *replayed_commits,
@ -283,7 +298,7 @@ static struct commit *pick_regular_commit(struct repository *repo,
base_tree = lookup_tree(repo, repo->hash_algo->empty_tree);
}

replayed_base = mapped_commit(replayed_commits, base, onto);
replayed_base = get_mapped_commit(replayed_commits, base, onto);
replayed_base_tree = repo_get_commit_tree(repo, replayed_base);
pickme_tree = repo_get_commit_tree(repo, pickme);

@ -423,8 +438,6 @@ int replay_revisions(struct rev_info *revs,
replayed_commits = kh_init_oid_map();
while ((commit = get_revision(revs))) {
const struct name_decoration *decoration;
khint_t pos;
int hr;

if (commit->parents && commit->parents->next)
die(_("replaying merge commits is not supported yet!"));
@ -436,11 +449,7 @@ int replay_revisions(struct rev_info *revs,
break;

/* Record commit -> last_commit mapping */
pos = kh_put_oid_map(replayed_commits, commit->object.oid, &hr);
if (hr == 0)
BUG("Duplicate rewritten commit: %s\n",
oid_to_hex(&commit->object.oid));
kh_value(replayed_commits, pos) = last_commit;
put_mapped_commit(replayed_commits, commit, last_commit);

/* Update any necessary branches */
if (ref)