git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2] replay: support replaying down from root commit

From
Toon Claes <toon@iotcl.com>
Date
Mar 24, 2026, 19:35 UTC
Message-ID
<20260324-toon-replay-down-to-root-v2-1-34e723489f6e@iotcl.com>
In-Reply-To
<20260317-toon-replay-down-to-root-v1-1-cb5c249e15fd@iotcl.com>

git-replay(1) doesn't allow replaying commits all the way down to the root commit. Fix that.

Signed-off-by: Toon Claes <toon@iotcl.com>
---
These changes might conflict Siddharth's series[1] to add '--revert' to
git-replay(1), although resolving that should be trivial.
[1]: https://lore.kernel.org/git/20260313054035.26605-1-siddharthasthana31@gmail.com/
---
Changes in v2:
- Add NULL pointer check for `commit` in mapped_commit().
- Change ancestor message to "empty tree" when replaying root commit.
- Link to v1: https://patch.msgid.link/20260317-toon-replay-down-to-root-v1-1-cb5c249e15fd@iotcl.com
---
 replay.c                 | 27 +++++++++++++++++----------
 t/t3650-replay-basics.sh | 10 +++++++---
 2 files changed, 24 insertions(+), 13 deletions(-)
diff --git a/replay.c b/replay.c
index a63f6714c4..92f2279156 100644
--- a/replay.c
+++ b/replay.c
@@ -209,7 +209,10 @@ static struct commit *mapped_commit(kh_oid_map_t *replayed_commits,
 				    struct commit *commit,
 				    struct commit *fallback)
 {
-	khint_t pos = kh_get_oid_map(replayed_commits, commit->object.oid);
+	khint_t pos;
+	if (!commit)
+		return fallback;
+	pos = kh_get_oid_map(replayed_commits, commit->object.oid);
 	if (pos == kh_end(replayed_commits))
 		return fallback;
 	return kh_value(replayed_commits, pos);
@@ -225,16 +228,24 @@ static struct commit *pick_regular_commit(struct repository *repo,
 	struct commit *base, *replayed_base;
 	struct tree *pickme_tree, *base_tree, *replayed_base_tree;
 
-	base = pickme->parents->item;
-	replayed_base = mapped_commit(replayed_commits, base, onto);
+	if (pickme->parents) {
+		base = pickme->parents->item;
+		base_tree = repo_get_commit_tree(repo, base);
+	} else {
+		base = NULL;
+		base_tree = lookup_tree(repo, repo->hash_algo->empty_tree);
+	}
 
+	replayed_base = mapped_commit(replayed_commits, base, onto);
 	replayed_base_tree = repo_get_commit_tree(repo, replayed_base);
 	pickme_tree = repo_get_commit_tree(repo, pickme);
-	base_tree = repo_get_commit_tree(repo, base);
 
 	merge_opt->branch1 = short_commit_name(repo, replayed_base);
 	merge_opt->branch2 = short_commit_name(repo, pickme);
-	merge_opt->ancestor = xstrfmt("parent of %s", merge_opt->branch2);
+	if (pickme->parents)
+		merge_opt->ancestor = xstrfmt("parent of %s", merge_opt->branch2);
+	else
+		merge_opt->ancestor = xstrdup("empty tree");
 
 	merge_incore_nonrecursive(merge_opt,
 				  base_tree,
@@ -293,8 +304,6 @@ int replay_revisions(struct rev_info *revs,
 	set_up_replay_mode(revs->repo, &revs->cmdline, opts->onto,
 			   &detached_head, &advance, &onto, &update_refs);
 
-	/* FIXME: Should allow replaying commits with the first as a root commit */
-
 	if (prepare_revision_walk(revs) < 0) {
 		ret = error(_("error preparing revisions"));
 		goto out;
@@ -309,9 +318,7 @@ int replay_revisions(struct rev_info *revs,
 		khint_t pos;
 		int hr;
 
-		if (!commit->parents)
-			die(_("replaying down from root commit is not supported yet!"));
-		if (commit->parents->next)
+		if (commit->parents && commit->parents->next)
 			die(_("replaying merge commits is not supported yet!"));
 
 		last_commit = pick_regular_commit(revs->repo, commit, replayed_commits,
diff --git a/t/t3650-replay-basics.sh b/t/t3650-replay-basics.sh
index a03f8f9293..9c55b62757 100755
--- a/t/t3650-replay-basics.sh
+++ b/t/t3650-replay-basics.sh
@@ -81,9 +81,13 @@ test_expect_success 'option --onto or --advance is mandatory' '
 	test_cmp expect actual
 '
 
-test_expect_success 'no base or negative ref gives no-replaying down to root error' '
-	echo "fatal: replaying down from root commit is not supported yet!" >expect &&
-	test_must_fail git replay --onto=topic1 topic2 2>actual &&
+test_expect_success 'replay down to root onto another branch' '
+	git replay --ref-action=print --onto main topic2 >result &&
+
+	test_line_count = 1 result &&
+
+	git log --format=%s $(cut -f 3 -d " " result) >actual &&
+	test_write_lines E D C M L B A >expect &&
 	test_cmp expect actual
 '
 

---
base-commit: ca1db8a0f7dc0dbea892e99f5b37c5fe5861be71
change-id: 20260317-toon-replay-down-to-root-d412048f1741
Previous: Toon ClaesNext: Junio C Hamano
Message 4 of 17 in “replay: support replaying down from root commit”
  1. replay: support replaying down from root commitToon Claes, Mar 17, 2026
  2. Junio C HamanoMar 17, 2026
  3. Toon ClaesMar 24, 2026
  4. replay: support replaying down from root commitToon Claes, Mar 24, 2026
  5. Junio C HamanoMar 24, 2026
  6. Christian CouderMar 25, 2026
  7. Ben KnobleMar 25, 2026
  8. Toon ClaesMar 25, 2026
  9. Toon ClaesMar 25, 2026
  10. Toon ClaesMar 25, 2026
  11. Junio C HamanoMar 27, 2026
  12. Christian CouderMar 31, 2026
  13. Junio C HamanoMar 31, 2026
  14. Toon ClaesApr 2, 2026
  15. Junio C HamanoApr 2, 2026
  16. Christian CouderApr 3, 2026
  17. Junio C HamanoMar 25, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.