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

Re: [PATCH v5 3/3] replay: offer an option to linearize the commit topology

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 26, 2026, 17:10 UTC
Message-ID
<xmqq5x358byf.fsf@gitster.g>
In-Reply-To
<20260626-toon-git-replay-drop-merges-v5-3-5e120738b9d0@iotcl.com>
Toon Claes <toon@iotcl.com> writes:
Show 6 quoted lines
>  Documentation/git-replay.adoc |  8 ++++-
>  builtin/replay.c              |  6 +++-
>  replay.c                      | 50 ++++++++++++++++----------
>  replay.h                      |  5 +++
>  t/t3650-replay-basics.sh      | 84 ++++++++++++++++++++++++++++++++++++++++++-
>  5 files changed, 132 insertions(+), 21 deletions(-)

"replay --linearize" behaves differently from the flattening rebase in a case where X and Y that forked from A are merged at Z, and we ask to flatten the history leading to Z, doesn't it?

     A----X
      \    \
       Y----Z (tip)

A typical flattening rebase would rewrite X to X', Y to Y', while dropping Z, and would leave us a flattened history, like

     A---X'---Y' (updated tip, the order of X' and Y' may be swapped)

I may be misreading the logic, but doesn't "replay --linearize" instead produce

     A----X' (dangling)
      \ 
       Y' (tip -- Z is dropped and gets mapped)

and leave X' dangling (or Y'; the point is that only one of them will survive), never incorporating it in the resulting history?

Show 34 quoted lines
> +		if (commit->parents && commit->parents->next) {
> +			if (!opts->linearize)
> +				die(_("replaying merge commits is not supported yet!"));
> +			/*
> +			 * Drop the merge commit: do not pick it, leave
> +			 * `last_commit` unchanged, and fall through to the
> +			 * rest of the loop. As a result:
> +			 * - the merge commit is mapped to `last_commit` in
> +			 *   `replayed_commits`, this will become the parent for
> +			 *   the child commits.
> +			 * - refs previously pointing to the merge commit are
> +			 *   rewritten to point to the previous non-merge commit.
> +			 */
> +		} else {
> +			/*
> +			 * pick_regular_commit() looks up the parent of `commit` in
> +			 * `replayed_commits` to determine the ancestor to replay onto.
> +			 * The `default_base` parameter is used when no ancestor is found,
> +			 * which happens for the first commit in the revision range.
> +			 * When reverting, commits are replayed in reverse order, so the
> +			 * lookup never succeeds, and we need to pass `last_commit`.
> +			 */
> +			struct commit *base = onto;
> +			if (mode == REPLAY_MODE_REVERT)
> +				base = last_commit;
> +
> +			last_commit = pick_regular_commit(revs->repo, commit, base,
> +							  replayed_commits,
> +							  &merge_opt, &result,
> +							  mode, opts->empty);
> +		}
> +
>  		if (!last_commit)
>  			break;
Immediately after this hunk beyond the post-context are these lines.
		/* Record commit -> last_commit mapping */
		put_mapped_commit(replayed_commits, commit, last_commit);

Let's imagine X gets processed first. X (and other commits on its branch) gets replayed, last_commit is set to X' (which is the rewritten X). replayed_commits mapping holds X->X' mapping.

Then let's imagine the history leading to Y is replayed next. last_commit becomes Y', and Y->Y' mapping is stored in replayed_commits.

Finally, we see Z. We are going to _drop_ it. last_commit is left unchanged, pointing at Y'. Then last_commit (i.e., Y') is used as the merge commit Z maps to (i.e., correctly dropping Z).

Any descendants of Z, if any, will be grafted as descendants of Y'. If X did not have any descendants other than Z in the rewritten part of the history, then X' (and commits leading to it) would be lost, no?

This "loss of the other branch" may be an inherent characteristic of this feature (i.e., I do not think it is necessarily a bug, and it may even be that the "bug" is in the way I am reading the patch), but then I wonder if the user may want to have control over which side branch should survive, perhaps? It would probably need to be documented, and a test or two to cast this behaviour in stone.

Show 5 quoted lines
> diff --git a/t/t3650-replay-basics.sh b/t/t3650-replay-basics.sh
> index 3353bc4a4d..34c038eab9 100755
> --- a/t/t3650-replay-basics.sh
> +++ b/t/t3650-replay-basics.sh
> @@ -52,8 +52,12 @@ test_expect_success 'setup' '
The pre-context here has
	git switch --detach topic4 &&
	test_commit N &&
	test_commit O &&
	git switch -c topic-with-merge topic4 &&
>  	test_merge P O --no-ff &&
>  	git switch main &&
The above does prepare topic-with-merge branch, but ...
> +test_expect_success 'replay to rebase merge commit with --linearize' '
> +	git replay --ref-action=print --linearize \
> +		--onto main I..topic-with-merge >result &&

... this does not really exersize linearizing replay in a typical mergy history. P merges O with --no-ff because otherwise there won't be a merge, since O is a descendant of the commit "test_merge P O" runs on (i.e., topic4 == topic-with-merge).

    topic4 --- N --- O
          \           \
           .-----------P

So, as long as O is replayed later than the parent of N (which is true), O' will be the surviving tip (corresponds to Y' that the dropped Z was mapped to in the earlier example), and nothing gets orphaned, I think.

Perhaps a test to try a real merge may look something like this.
diff --git c/t/t3650-replay-basics.sh w/t/t3650-replay-basics.sh
index 34c038eab9..bb737f729a 100755
--- c/t/t3650-replay-basics.sh
+++ w/t/t3650-replay-basics.sh
@@ -647,4 +647,37 @@ test_expect_success 'replay with --linearize to rebase multiple divergent branch
 	test_cmp expect actual
 '
 
+test_expect_success 'replay with --linearize of a divergent merge drops one branch' '
+	git switch -c topic-divergent-base main &&
+	test_commit base &&
+	# Fork 1: base -> X
+	git switch -c topic-divergent-x &&
+	test_commit X &&
+	# Fork 2: base -> Y
+	git switch topic-divergent-base &&
+	git switch -c topic-divergent-y &&
+	test_commit Y &&
+	# Merge them at Z
+	git switch topic-divergent-x &&
+	test_merge Z topic-divergent-y --no-ff &&
+
+	# History is now:
+	#
+	#       X - Z (topic-divergent-x)
+	#      /   /
+	#  base - Y
+	#
+
+	git replay --ref-action=print --linearize \
+		--onto main topic-divergent-base..topic-divergent-x >result &&
+	test_line_count = 1 result &&
+	tip=$(cut -f 3 -d " " result) &&
+	# Get the commits replayed onto main
+	git log --format=%s main..$tip >actual &&
+	# We expect exactly one commit to be replayed (either X or Y)
+	# because the other one is left dangling due to the merge being dropped.
+	test_line_count = 1 actual &&
+	test_grep "^[XY]$" actual
+'
+
 test_done
Previous: Junio C HamanoNext: Phillip Wood
Message 36 of 75 in “Teach git-replay(1) to linearize merge commits”
  1. 0/3 Teach git-replay(1) to linearize merge commitsToon Claes, Jun 8, 2026
  2. 1/3 replay: refactor enum replay_mode into a boolToon Claes, Jun 8, 2026
  3. 2/3 replay: add helper to put entry into mapped_commitsToon Claes, Jun 8, 2026
  4. 3/3 replay: offer an option to linearize the commit topologyToon Claes, Jun 8, 2026
  5. Junio C HamanoJun 8, 2026
  6. Toon ClaesJun 10, 2026
  7. 0/3 Teach git-replay(1) to linearize merge commitsToon Claes, Jun 10, 2026
  8. 1/3 replay: refactor enum replay_mode into a boolToon Claes, Jun 10, 2026
  9. 2/3 replay: add helper to put entry into mapped_commitsToon Claes, Jun 10, 2026
  10. 3/3 replay: offer an option to linearize the commit topologyToon Claes, Jun 10, 2026
  11. Junio C HamanoJun 10, 2026
  12. Justin ToblerJun 11, 2026
  13. Toon ClaesJun 12, 2026
  14. Elijah NewrenJun 14, 2026
  15. Toon ClaesJun 16, 2026
  16. Toon ClaesJun 16, 2026
  17. 0/3 Teach git-replay(1) to linearize merge commitsToon Claes, Jun 16, 2026
  18. 1/3 replay: refactor enum replay_mode into a boolToon Claes, Jun 16, 2026
  19. 2/3 replay: add helper to put entry into mapped_commitsToon Claes, Jun 16, 2026
  20. 3/3 replay: offer an option to linearize the commit topologyToon Claes, Jun 16, 2026
  21. 0/3 Teach git-replay(1) to linearize merge commitsToon Claes, Jun 22, 2026
  22. 1/3 replay: refactor enum replay_mode into a boolToon Claes, Jun 22, 2026
  23. 2/3 replay: add helper to put entry into mapped_commitsToon Claes, Jun 22, 2026
  24. 3/3 replay: offer an option to linearize the commit topologyToon Claes, Jun 22, 2026
  25. Patrick SteinhardtJun 22, 2026
  26. Patrick SteinhardtJun 22, 2026
  27. Patrick SteinhardtJun 22, 2026
  28. Junio C HamanoJun 22, 2026
  29. Toon ClaesJun 24, 2026
  30. Toon ClaesJun 26, 2026
  31. 0/3 Teach git-replay(1) to linearize merge commitsToon Claes, Jun 26, 2026
  32. 1/3 replay: add helper to put entry into mapped_commitsToon Claes, Jun 26, 2026
  33. 2/3 replay: better explain how pick_regular_commit() picks a baseToon Claes, Jun 26, 2026
  34. 3/3 replay: offer an option to linearize the commit topologyToon Claes, Jun 26, 2026
  35. Junio C HamanoJun 26, 2026
  36. Junio C HamanoJun 26, 2026
  37. Phillip WoodJun 27, 2026
  38. Johannes SchindelinJun 28, 2026
  39. Patrick SteinhardtJun 29, 2026
  40. Johannes SchindelinJun 30, 2026
  41. Patrick SteinhardtJun 30, 2026
  42. Toon ClaesJun 30, 2026
  43. Toon ClaesJul 1, 2026
  44. 0/3 Teach git-replay(1) to linearize merge commitsToon Claes, Jul 2, 2026
  45. 1/3 replay: add helper to put entry into replayed_commitsToon Claes, Jul 2, 2026
  46. 2/3 replay: resolve the replay base outside pick_regular_commit()Toon Claes, Jul 2, 2026
  47. 3/3 replay: offer an option to linearize the commit topologyToon Claes, Jul 2, 2026
  48. Junio C HamanoJul 3, 2026
  49. Toon ClaesJul 7, 2026
  50. 0/3 Teach git-replay(1) to linearize merge commitsToon Claes, Jul 7, 2026
  51. 1/3 replay: add helper to put entry into replayed_commitsToon Claes, Jul 7, 2026
  52. 2/3 replay: resolve the replay base outside pick_regular_commit()Toon Claes, Jul 7, 2026
  53. 3/3 replay: offer an option to linearize the commit topologyToon Claes, Jul 7, 2026
  54. Junio C HamanoJul 7, 2026
  55. Junio C HamanoJul 8, 2026
  56. Elijah NewrenJul 10, 2026
  57. Junio C HamanoJul 13, 2026
  58. Elijah NewrenJul 15, 2026
  59. Junio C HamanoJul 15, 2026
  60. Elijah NewrenJul 16, 2026
  61. Junio C HamanoJul 17, 2026
  62. Toon ClaesJul 27, 2026
  63. 0/3 Teach git-replay(1) to linearize merge commitsToon Claes, Jul 28, 2026
  64. 1/3 replay: add helper to put entry into replayed_commitsToon Claes, Jul 28, 2026
  65. 2/3 replay: resolve the replay base outside pick_regular_commit()Toon Claes, Jul 28, 2026
  66. 3/3 replay: offer an option to linearize the commit topologyToon Claes, Jul 28, 2026
  67. Junio C HamanoJul 28, 2026
  68. Justin ToblerAug 7, 2026
  69. Elijah NewrenAug 8, 2026
  70. Toon ClaesAug 31, 2026
  71. 0/3 Teach git-replay(1) to linearize merge commitsToon Claes, Aug 31, 2026
  72. 1/3 replay: add helper to put entry into replayed_commitsToon Claes, Aug 31, 2026
  73. 2/3 replay: resolve the replay base outside pick_regular_commit()Toon Claes, Aug 31, 2026
  74. 3/3 replay: offer an option to linearize the commit topologyToon Claes, Aug 31, 2026
  75. Elijah NewrenSep 1, 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.