replay: offer an option to linearize the commit topology

One of the stated goals of git-replay(1) is to allow implementing the git-rebase(1) functionality on the server side. The default mode of git-rebase(1) is to act as if `--no-rebase-merges` was given. This mode drops merge commits instead of replaying them, and linearizes the history into a sequence of regular (single-parent) commits. Add option `--linearize` to git-replay(1) to do the same. Each replayed commit is stacked on top of the previously replayed one. When a merge is encountered, the commits reachable from all of its sides are replayed into the single line and the merge itself is dropped. If a ref was pointing to a merge commit, that ref is updated to the merge's last replayed ancestor. git-replay(1) accepts multiple revision ranges, for example: $ git replay --onto main topic1 topic2 Without `--linearize` this replays 'topic1' and 'topic2' onto 'main' independently and updates both refs. For now this is disallowed with option `--linearize`. Linearizing more than one branch at once would concatenate unrelated histories into a single line, and update each branch to some point in that line. That won't be the result most users want, especially because the order depends on the order of the revision walk, not the order of the branch names on the command line. For the same reason disallow the use of `--contained` with `--linearize`. Users who want to linearize multiple branches are advised to do this in separate git-replay(1) invocations. Linearizing multiple branches at once might be added later. Note that `--linearize` is not modeled after git-rebase(1)'s `--rebase-merges[=<mode>]` interface. Recreating merges, by preserving their topology, is a distinct operation that would be a separate mode. `--linearize` only drops merges and replays commits linearly. So git-replay(1) uses its own option rather than reusing that interface. Based-on-patches-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Toon Claes <toon@iotcl.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Toon Claes committed Jul 28, 2026 at 17:45 UTC 6af34ada9641387947a0aa025029d6461283fe3c
5 files changed +176 -23
Documentation/git-replay.adoc
+18 -1
@@ -10,7 +10,7 @@ SYNOPSIS
10 --------
11 [verse]
12 (EXPERIMENTAL!) 'git replay' ([--contained] --onto=<newbase> | --advance=<branch> | --revert=<branch>)
13 - [--ref=<ref>] [--ref-action=<mode>] <revision-range>
13 + [--ref=<ref>] [--ref-action=<mode>] [--linearize] <revision-range>
14
15 DESCRIPTION
16 -----------
@@ -88,6 +88,23 @@ incompatible with `--contained` (which is a modifier for `--onto` only).
88 +
89 The default mode can be configured via the `replay.refAction` configuration variable.
90
91 +--linearize::
92 + In this mode, each replayed commit is stacked on top of the
93 + previously replayed one, so all replayed commits are flattened into
94 + a single linear history.
95 ++
96 +When a merge commit is encountered, the behavior of git-rebase(1)'s
97 +option `--no-rebase-merges` is imitated. All commits in the range
98 +reachable from the merge commit are replayed into a linear history, and
99 +the merge commit itself is dropped. A ref that pointed to a merge commit
100 +is updated to the merge's last replayed ancestor.
101 ++
102 +Only a single branch can be linearized at a time: `--linearize` cannot
103 +be combined with multiple positive revisions or with `--contained`,
104 +because that would concatenate otherwise unrelated histories into one
105 +line. To linearize several branches, replay them in separate `git
106 +replay` invocations.
107 +
108 <revision-range>::
109 Range of commits to replay; see "Specifying Ranges" in
110 linkgit:git-rev-parse[1]. In `--advance=<branch>` or
builtin/replay.c
+5 -1
@@ -85,7 +85,7 @@ int cmd_replay(int argc,
85 const char *const replay_usage[] = {
86 N_("(EXPERIMENTAL!) git replay "
87 "([--contained] --onto=<newbase> | --advance=<branch> | --revert=<branch>)\n"
88 - "[--ref=<ref>] [--ref-action=<mode>] <revision-range>"),
88 + "[--ref=<ref>] [--ref-action=<mode>] [--linearize] <revision-range>"),
89 NULL
90 };
91 struct option replay_options[] = {
@@ -111,6 +111,8 @@ int cmd_replay(int argc,
111 N_("mode"),
112 N_("control ref update behavior (update|print)"),
113 PARSE_OPT_NONEG),
114 + OPT_BOOL(0, "linearize", &opts.linearize,
115 + N_("drop merge commits, replaying only non-merge commits")),
116 OPT_END()
117 };
118
@@ -132,6 +134,8 @@ int cmd_replay(int argc,
134 opts.contained, "--contained");
135 die_for_incompatible_opt2(!!opts.ref, "--ref",
136 !!opts.contained, "--contained");
137 + die_for_incompatible_opt2(opts.linearize, "--linearize",
138 + !!opts.contained, "--contained");
139
140 /* Parse ref action mode from command line or config */
141 ref_mode = get_ref_action_mode(repo, ref_action);
replay.c
+40 -20
@@ -404,6 +404,12 @@ int replay_revisions(struct rev_info *revs,
404 set_up_replay_mode(revs->repo, &revs->cmdline, opts->onto,
405 &detached_head, &advance, &revert, &onto, &update_refs);
406
407 + if (opts->linearize &&
408 + update_refs && strset_get_size(update_refs) > 1) {
409 + ret = error(_("'--linearize' cannot be used with multiple revision ranges"));
410 + goto out;
411 + }
412 +
413 if (opts->ref) {
414 struct object_id oid;
415
@@ -437,26 +443,40 @@ int replay_revisions(struct rev_info *revs,
443 while ((commit = get_revision(revs))) {
444 const struct name_decoration *decoration;
445
440 - /*
441 - * Decide where to replay this commit on.
442 - * If the parent commit was replayed already, the replayed result
443 - * can be found in `replayed_commits`. Otherwise fall back to `onto`.
444 - * When reverting, commits are replayed in reverse order and thus
445 - * its parent isn't replayed yet. Therefore revert commits are
446 - * always replayed onto `last_commit`.
447 - */
448 - struct commit *parent = commit->parents ? commit->parents->item : NULL;
449 - struct commit *base = get_mapped_commit(replayed_commits, parent, onto);
450 -
451 - if (mode == REPLAY_MODE_REVERT)
452 - base = last_commit;
453 -
454 - if (commit->parents && commit->parents->next)
455 - die(_("replaying merge commits is not supported yet!"));
456 -
457 - last_commit = pick_regular_commit(revs->repo, commit, base,
458 - &merge_opt, &result,
459 - mode, opts->empty);
446 + if (commit->parents && commit->parents->next) {
447 + if (!opts->linearize)
448 + die(_("replaying merge commits is not supported yet!"));
449 + /*
450 + * Drop the merge commit: do not pick it, leave
451 + * `last_commit` unchanged, and fall through to the
452 + * rest of the loop. As a result:
453 + * - refs pointing to the merge commit will be updated
454 + * to `last_commit`.
455 + * - the next replayed commit uses `last_commit` as its
456 + * `base`.
457 + */
458 + } else {
459 + /*
460 + * Decide where to replay this commit onto.
461 + * If the parent commit was replayed already, the replayed result
462 + * can be found in `replayed_commits`. Otherwise fall back to `onto`.
463 + * When reverting, commits are replayed in reverse order and thus
464 + * its parent isn't replayed yet. Therefore revert commits are
465 + * always replayed onto `last_commit`.
466 + * Also when opts->linearize is true, set the base to
467 + * `last_commit` to create a single linear history.
468 + */
469 + struct commit *parent = commit->parents ? commit->parents->item : NULL;
470 + struct commit *base = get_mapped_commit(replayed_commits, parent, onto);
471 +
472 + if (opts->linearize || mode == REPLAY_MODE_REVERT)
473 + base = last_commit;
474 +
475 + last_commit = pick_regular_commit(revs->repo, commit, base,
476 + &merge_opt, &result,
477 + mode, opts->empty);
478 + }
479 +
480 if (!last_commit)
481 break;
482
replay.h
+5
@@ -62,6 +62,11 @@ struct replay_revisions_options {
62 * Defaults to REPLAY_EMPTY_COMMIT_DROP.
63 */
64 enum replay_empty_commit_action empty;
65 +
66 + /*
67 + * Whether to linearize the commits (i.e. drop merge commits).
68 + */
69 + int linearize;
70 };
71
72 /* This struct is used as an out-parameter by `replay_revisions()`. */
t/t3650-replay-basics.sh
+108 -1
@@ -52,8 +52,19 @@ test_expect_success 'setup' '
52 test_merge P O --no-ff &&
53 git switch main &&
54
55 + git switch --orphan unrelated &&
56 + test_commit unrelated-root &&
57 +
58 git switch -c conflict B &&
56 - test_commit C.conflict C.t conflict
59 + test_commit C.conflict C.t conflict &&
60 + git branch -D unrelated &&
61 +
62 + git switch -c divergent-x main &&
63 + test_commit X &&
64 + git switch -c divergent-y main &&
65 + test_commit Y &&
66 + git switch divergent-x &&
67 + test_merge Z divergent-y --no-ff
68 '
69
70 test_expect_success 'setup bare' '
@@ -565,4 +576,100 @@ test_expect_success '--onto with --ref rejects multiple revision ranges' '
576 test_grep "cannot be used with multiple revision ranges" err
577 '
578
579 +test_expect_success 'replay to rebase merge commit with --linearize' '
580 + git replay --ref-action=print --linearize \
581 + --onto main I..topic-with-merge >result &&
582 +
583 + test_line_count = 1 result &&
584 +
585 + git log --format=%s $(cut -f 3 -d " " result) >actual &&
586 + test_write_lines O N J M L B A >expect &&
587 + test_cmp expect actual
588 +'
589 +
590 +test_expect_success 'replay to rebase merge commit with --linearize down to the root commit' '
591 + git replay --ref-action=print --linearize \
592 + --onto unrelated-root topic-with-merge >result &&
593 +
594 + test_line_count = 1 result &&
595 +
596 + git log --format=%s $(cut -f 3 -d " " result) >actual &&
597 + test_write_lines O N J I B A unrelated-root >expect &&
598 + test_cmp expect actual
599 +'
600 +
601 +test_expect_success 'replay to cherry-pick merge commit with --linearize' '
602 + git replay --ref-action=print --linearize \
603 + --advance main I..topic-with-merge >result &&
604 +
605 + test_line_count = 1 result &&
606 +
607 + git log --format=%s $(cut -f 3 -d " " result) >actual &&
608 + test_write_lines O N J M L B A >expect &&
609 + test_cmp expect actual &&
610 +
611 + printf "update refs/heads/main " >expect &&
612 + printf "%s " $(cut -f 3 -d " " result) >>expect &&
613 + git rev-parse main >>expect &&
614 + test_cmp expect result
615 +'
616 +
617 +test_expect_success 'replay --linearize produces the same patches' '
618 + git replay --ref-action=print --linearize \
619 + --onto main I..topic-with-merge >result &&
620 +
621 + test_line_count = 1 result &&
622 + tip=$(cut -f 3 -d " " result) &&
623 +
624 + # range-diff does not care about the dropped merge,
625 + # so the original commits (I..topic-with-merge)
626 + # and the replayed chain (main..tip) must produce identical patches.
627 + git range-diff I..topic-with-merge main..$tip >out &&
628 + test_file_not_empty out &&
629 + test_grep ! -v "=" out &&
630 +
631 + git log --oneline main..$tip >out &&
632 + test_line_count = 3 out
633 +'
634 +
635 +test_expect_success '--linearize rejects multiple revision ranges' '
636 + test_must_fail git replay --ref-action=print --linearize \
637 + --onto main ^B topic2 topic3 topic4 2>err &&
638 + test_grep "cannot be used with multiple revision ranges" err
639 +'
640 +
641 +test_expect_success 'replay with --linearize of a divergent merge keeps both sides' '
642 + git replay --ref-action=print --linearize \
643 + --onto main main..divergent-x >result &&
644 + test_line_count = 1 result &&
645 + tip=$(cut -f 3 -d " " result) &&
646 +
647 + # The merge Z is dropped, but both X and Y are linearized onto main;
648 + # neither side is lost.
649 + git log --format=%s main..$tip >actual &&
650 + test_write_lines Y X >expect &&
651 + test_cmp expect actual
652 +'
653 +
654 +test_expect_success '--linearize and --contained cannot be used together' '
655 + test_must_fail git replay --ref-action=print --linearize --contained \
656 + --onto main ^B topic-with-merge 2>err &&
657 + test_grep "cannot be used together" err
658 +'
659 +
660 +test_expect_success 'replay --revert with --linearize reverts a range containing a merge' '
661 + git replay --ref-action=print --revert=divergent-x --linearize \
662 + main..divergent-x >result &&
663 + test_line_count = 1 result &&
664 + tip=$(cut -f 3 -d " " result) &&
665 +
666 + git log --format=%s $tip >actual &&
667 + test_write_lines \
668 + "Revert \"X\"" "Revert \"Y\"" Z Y X M L B A >expect &&
669 + test_cmp expect actual &&
670 +
671 + test_must_fail git cat-file -e $tip:X.t &&
672 + test_must_fail git cat-file -e $tip:Y.t
673 +'
674 +
675 test_done