Commit
354c736978bdc7a1d0bb0f19b81060182a374be6
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 branches, for example:
$ git replay --onto main topic1 topic2
Without `--linearize` this replays 'topic1' and 'topic2' onto 'main'
(keeping shared portions of history shared and divergent parts
divergent) and updates both refs.
Due to current implementation limitations, replaying multiple branches
with `--linearize` is disallowed to avoid concatenating unrelated
histories into a single 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 <toon@iotcl.com> · 2026-08-31 13:13 UTC · committed by Junio C Hamano
5 files changed,
+174
−23
modified Documentation/git-replay.adoc
| @@ -10,7 +10,7 @@ SYNOPSIS | |||
| 10 | 10 | -------- | |
| 11 | 11 | [verse] | |
| 12 | 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 | 14 | ||
| 15 | 15 | DESCRIPTION | |
| 16 | 16 | ----------- | |
| @@ -88,6 +88,21 @@ incompatible with `--contained` (which is a modifier for `--onto` only). | |||
| 88 | 88 | + | |
| 89 | 89 | The default mode can be configured via the `replay.refAction` configuration variable. | |
| 90 | 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, all commits in the range reachable | |
| 97 | + | from the merge commit are replayed into the linear history, and the | |
| 98 | + | merge commit itself is dropped. A ref that pointed to a merge commit is | |
| 99 | + | updated to the merge's last replayed ancestor. (This matches the | |
| 100 | + | behavior of git-rebase(1)'s `--no-rebase-merges` option.) | |
| 101 | + | + | |
| 102 | + | `--linearize` cannot be combined with multiple branches or with | |
| 103 | + | `--contained`. To linearize several branches, replay them in separate | |
| 104 | + | `git replay` invocations. | |
| 105 | + | ||
| 91 | 106 | <revision-range>:: | |
| 92 | 107 | Range of commits to replay; see "Specifying Ranges" in | |
| 93 | 108 | linkgit:git-rev-parse[1]. In `--advance=<branch>` or | |
modified builtin/replay.c
| @@ -85,7 +85,7 @@ int cmd_replay(int argc, | |||
| 85 | 85 | const char *const replay_usage[] = { | |
| 86 | 86 | N_("(EXPERIMENTAL!) git replay " | |
| 87 | 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 | 89 | NULL | |
| 90 | 90 | }; | |
| 91 | 91 | struct option replay_options[] = { | |
| @@ -111,6 +111,8 @@ int cmd_replay(int argc, | |||
| 111 | 111 | N_("mode"), | |
| 112 | 112 | N_("control ref update behavior (update|print)"), | |
| 113 | 113 | PARSE_OPT_NONEG), | |
| 114 | + | OPT_BOOL(0, "linearize", &opts.linearize, | |
| 115 | + | N_("drop merge commits, replaying only non-merge commits")), | |
| 114 | 116 | OPT_END() | |
| 115 | 117 | }; | |
| 116 | 118 | ||
| @@ -132,6 +134,8 @@ int cmd_replay(int argc, | |||
| 132 | 134 | opts.contained, "--contained"); | |
| 133 | 135 | die_for_incompatible_opt2(!!opts.ref, "--ref", | |
| 134 | 136 | !!opts.contained, "--contained"); | |
| 137 | + | die_for_incompatible_opt2(opts.linearize, "--linearize", | |
| 138 | + | !!opts.contained, "--contained"); | |
| 135 | 139 | ||
| 136 | 140 | /* Parse ref action mode from command line or config */ | |
| 137 | 141 | ref_mode = get_ref_action_mode(repo, ref_action); | |
modified replay.c
| @@ -404,6 +404,12 @@ int replay_revisions(struct rev_info *revs, | |||
| 404 | 404 | set_up_replay_mode(revs->repo, &revs->cmdline, opts->onto, | |
| 405 | 405 | &detached_head, &advance, &revert, &onto, &update_refs); | |
| 406 | 406 | ||
| 407 | + | if (opts->linearize && | |
| 408 | + | update_refs && strset_get_size(update_refs) > 1) { | |
| 409 | + | ret = error(_("'--linearize' cannot be used with multiple branches")); | |
| 410 | + | goto out; | |
| 411 | + | } | |
| 412 | + | ||
| 407 | 413 | if (opts->ref) { | |
| 408 | 414 | struct object_id oid; | |
| 409 | 415 | ||
| @@ -437,26 +443,40 @@ int replay_revisions(struct rev_info *revs, | |||
| 437 | 443 | while ((commit = get_revision(revs))) { | |
| 438 | 444 | const struct name_decoration *decoration; | |
| 439 | 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 | + | ||
| 460 | 480 | if (!last_commit) | |
| 461 | 481 | break; | |
| 462 | 482 | ||
modified replay.h
| @@ -62,6 +62,11 @@ struct replay_revisions_options { | |||
| 62 | 62 | * Defaults to REPLAY_EMPTY_COMMIT_DROP. | |
| 63 | 63 | */ | |
| 64 | 64 | enum replay_empty_commit_action empty; | |
| 65 | + | ||
| 66 | + | /* | |
| 67 | + | * Whether to linearize the commits (i.e. drop merge commits). | |
| 68 | + | */ | |
| 69 | + | int linearize; | |
| 65 | 70 | }; | |
| 66 | 71 | ||
| 67 | 72 | /* This struct is used as an out-parameter by `replay_revisions()`. */ | |
modified t/t3650-replay-basics.sh
| @@ -52,8 +52,19 @@ test_expect_success 'setup' ' | |||
| 52 | 52 | test_merge P O --no-ff && | |
| 53 | 53 | git switch main && | |
| 54 | 54 | ||
| 55 | + | git switch --orphan unrelated && | |
| 56 | + | test_commit unrelated-root && | |
| 57 | + | ||
| 55 | 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 | |
| 57 | 68 | ' | |
| 58 | 69 | ||
| 59 | 70 | test_expect_success 'setup bare' ' | |
| @@ -565,4 +576,100 @@ test_expect_success '--onto with --ref rejects multiple revision ranges' ' | |||
| 565 | 576 | test_grep "cannot be used with multiple revision ranges" err | |
| 566 | 577 | ' | |
| 567 | 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 branches' ' | |
| 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 branches" 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 | + | ||
| 568 | 675 | test_done | |