replay: resolve the replay base outside pick_regular_commit()

Depending on what gets passed into the function pick_regular_commit(), it decides the new base for the replayed commit. It first tries to find the replayed results of `pickme`'s parent in the `replayed_commits` map. If not found, it falls back to `onto`. When using git-replay(1) with --onto, the fallback is the revision passed in with this option, but when using --revert, the fallback is `last_commit`. It's rather confusing the base is decided partly inside pick_regular_commit() and partly by its caller. Move the base selection completely into the caller: replay_revisions(). This bundles all the logic of deciding on the base together. Also, this reduces the number of parameters of pick_regular_commit(), making its interface cleaner. This refactoring doesn't bring any behavior changes. Signed-off-by: Toon Claes <toon@iotcl.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Toon Claes committed Jul 7, 2026 at 21:07 UTC 8a7fba28ffae126149699129c73b92fe875ee7f9
1 file changed +21 -13
replay.c
+21 -13
@@ -280,25 +280,19 @@ static void put_mapped_commit(kh_oid_map_t *replayed_commits,
280
281 static struct commit *pick_regular_commit(struct repository *repo,
282 struct commit *pickme,
283 - kh_oid_map_t *replayed_commits,
284 - struct commit *onto,
283 + struct commit *replayed_base,
284 struct merge_options *merge_opt,
285 struct merge_result *result,
286 enum replay_mode mode,
287 enum replay_empty_commit_action empty)
288 {
290 - struct commit *base, *replayed_base;
289 struct tree *pickme_tree, *base_tree, *replayed_base_tree;
290
293 - if (pickme->parents) {
294 - base = pickme->parents->item;
295 - base_tree = repo_get_commit_tree(repo, base);
296 - } else {
297 - base = NULL;
291 + if (pickme->parents)
292 + base_tree = repo_get_commit_tree(repo, pickme->parents->item);
293 + else
294 base_tree = lookup_tree(repo, repo->hash_algo->empty_tree);
299 - }
295
301 - replayed_base = get_mapped_commit(replayed_commits, base, onto);
296 replayed_base_tree = repo_get_commit_tree(repo, replayed_base);
297 pickme_tree = repo_get_commit_tree(repo, pickme);
298
@@ -439,12 +433,26 @@ int replay_revisions(struct rev_info *revs,
433 while ((commit = get_revision(revs))) {
434 const struct name_decoration *decoration;
435
436 + /*
437 + * Decide where to replay this commit on.
438 + * If the parent commit was replayed already, the replayed result
439 + * can be found in `replayed_commits`. Otherwise fall back to `onto`.
440 + * When reverting, commits are replayed in reverse order and thus
441 + * its parent isn't replayed yet. Therefore revert commits are
442 + * always replayed onto `last_commit`.
443 + */
444 + struct commit *parent = commit->parents ? commit->parents->item : NULL;
445 + struct commit *base = get_mapped_commit(replayed_commits, parent, onto);
446 +
447 + if (mode == REPLAY_MODE_REVERT)
448 + base = last_commit;
449 +
450 if (commit->parents && commit->parents->next)
451 die(_("replaying merge commits is not supported yet!"));
452
445 - last_commit = pick_regular_commit(revs->repo, commit, replayed_commits,
446 - mode == REPLAY_MODE_REVERT ? last_commit : onto,
447 - &merge_opt, &result, mode, opts->empty);
453 + last_commit = pick_regular_commit(revs->repo, commit, base,
454 + &merge_opt, &result,
455 + mode, opts->empty);
456 if (!last_commit)
457 break;
458