Introduce new `git replay` command
Subsystems (2)
PATCH v8 12/14 replay: add --advance or 'cherry-pick' mode
In reply to Christian CouderFrom: Elijah Newren <[email protected]>
There is already a 'rebase' mode with --onto. Let's add an 'advance' or 'cherry-pick' mode with --advance. This new mode will make the target branch advance as we replay commits onto it.
The replayed commits should have a single tip, so that it's clear where the target branch should be advanced. If they have more than one tip, this new mode will error out.
--- Documentation/git-replay.txt | 41 ++++++-- builtin/replay.c | 185 +++++++++++++++++++++++++++++++++-- t/t3650-replay-basics.sh | 34 +++++++ 3 files changed, 243 insertions(+), 17 deletions(-)
Documentation/git-replay.txt +33 -8
@@ -9,7 +9,7 @@ git-replay - EXPERIMENTAL: Replay commits on a new base, works with bare repos t SYNOPSIS -------- [verse] -(EXPERIMENTAL!) 'git replay' --onto <newbase> <revision-range>... +(EXPERIMENTAL!) 'git replay' (--onto <newbase> | --advance <branch>) <revision-range>... DESCRIPTION -----------
@@ -29,14 +29,25 @@ OPTIONS Starting point at which to create the new commits. May be any valid commit, and not just an existing branch name. + -The update-ref command(s) in the output will update the branch(es) in -the revision range to point at the new commits, similar to the way how -`git rebase --update-refs` updates multiple branches in the affected -range. +When `--onto` is specified, the update-ref command(s) in the output will +update the branch(es) in the revision range to point at the new +commits, similar to the way how `git rebase --update-refs` updates +multiple branches in the affected range. + +--advance <branch>:: + Starting point at which to create the new commits; must be a + branch name. ++ +When `--advance` is specified, the update-ref command(s) in the output +will update the branch passed as an argument to `--advance` to point at +the new commits (in other words, this mimics a cherry-pick operation). <revision-range>:: - Range of commits to replay; see "Specifying Ranges" in - linkgit:git-rev-parse and the "Commit Limiting" options below. + Range of commits to replay. More than one <revision-range> can + be passed, but in `--advance <branch>` mode, they should have + a single tip, so that it's clear where <branch> should point + to. See "Specifying Ranges" in linkgit:git-rev-parse and the + "Commit Limiting" options below. include::rev-list-options.txt[]
@@ -51,7 +62,9 @@ input to `git update-ref --stdin`. It is of the form: update refs/heads/branch3 ${NEW_branch3_HASH} ${OLD_branch3_HASH} where the number of refs updated depends on the arguments passed and -the shape of the history being replayed. +the shape of the history being replayed. When using `--advance`, the +number of refs updated is always one, but for `--onto`, it can be one +or more (rebasing multiple branches simultaneously is supported). EXIT STATUS -----------
@@ -71,6 +84,18 @@ $ git replay --onto target origin/main..mybranch update refs/heads/mybranch ${NEW_mybranch_HASH} ${OLD_mybranch_HASH} ------------ +To cherry-pick the commits from mybranch onto target: + +------------ +$ git replay --advance target origin/main..mybranch +update refs/heads/target ${NEW_target_HASH} ${OLD_target_HASH} +------------ + +Note that the first two examples replay the exact same commits and on +top of the exact same new base, they only differ in that the first +provides instructions to make mybranch point at the new commits and +the second provides instructions to make target point at them. + When calling `git replay`, one does not need to specify a range of commits to replay using the syntax `A..B`; any range expression will do:
builtin/replay.c +176 -9
255 lines not on this page · raw message
t/t3650-replay-basics.sh +34 -0
@@ -80,4 +80,38 @@ test_expect_success 'using replay on bare repo to rebase with a conflict' ' test_expect_code 1 git -C bare replay --onto topic1 B..conflict ' +test_expect_success 'using replay to perform basic cherry-pick' ' + # The differences between this test and previous ones are: + # --advance vs --onto + # 2nd field of result is refs/heads/main vs. refs/heads/topic2 + # 4th field of result is hash for main instead of hash for topic2 + + git replay --advance main topic1..topic2 >result && + + test_line_count = 1 result && + + git log --format=%s $(cut -f 3 -d " " result) >actual && + test_write_lines E D M L B A >expect && + test_cmp expect actual && + + printf "update refs/heads/main " >expect && + printf "%s " $(cut -f 3 -d " " result) >>expect && + git rev-parse main >>expect && + + test_cmp expect result +' + +test_expect_success 'using replay on bare repo to perform basic cherry-pick' ' + git -C bare replay --advance main topic1..topic2 >result-bare && + test_cmp expect result-bare +' + +test_expect_success 'replay on bare repo fails with both --advance and --onto' ' + test_must_fail git -C bare replay --advance main --onto main topic1..topic2 >result-bare +' + +test_expect_success 'replay fails when both --advance and --onto are omitted' ' + test_must_fail git replay topic1..topic2 >result +' + test_done
Signature · 1 line
2.43.0.14.g93e034faee
PATCH v8 13/14 replay: add --contained to rebase contained branches
In reply to Christian CouderFrom: Elijah Newren <[email protected]>
Let's add a --contained option that can be used along with --onto to rebase all the branches contained in the <revision-range> argument.
--- Documentation/git-replay.txt | 12 +++++++++++- builtin/replay.c | 14 ++++++++++++-- t/t3650-replay-basics.sh | 29 +++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 3 deletions(-)
Documentation/git-replay.txt +11 -1
@@ -9,7 +9,7 @@ git-replay - EXPERIMENTAL: Replay commits on a new base, works with bare repos t SYNOPSIS -------- [verse] -(EXPERIMENTAL!) 'git replay' (--onto <newbase> | --advance <branch>) <revision-range>... +(EXPERIMENTAL!) 'git replay' ([--contained] --onto <newbase> | --advance <branch>) <revision-range>... DESCRIPTION -----------
@@ -96,6 +96,16 @@ top of the exact same new base, they only differ in that the first provides instructions to make mybranch point at the new commits and the second provides instructions to make target point at them. +What if you have a stack of branches, one depending upon another, and +you'd really like to rebase the whole set? + +------------ +$ git replay --contained --onto origin/main origin/main..tipbranch +update refs/heads/branch1 ${NEW_branch1_HASH} ${OLD_branch1_HASH} +update refs/heads/branch2 ${NEW_branch2_HASH} ${OLD_branch2_HASH} +update refs/heads/tipbranch ${NEW_tipbranch_HASH} ${OLD_tipbranch_HASH} +------------ + When calling `git replay`, one does not need to specify a range of commits to replay using the syntax `A..B`; any range expression will do:
builtin/replay.c +12 -2
@@ -258,6 +258,7 @@ int cmd_replay(int argc, const char **argv, const char *prefix)const char *advance_name =NULL ;struct commit *onto =NULL ;const char *onto_name =NULL ; +int contained =0 ;struct rev_info revs;struct commit *last_commit =NULL ;
@@ -268,7 +269,9 @@ int cmd_replay(int argc, const char **argv, const char *prefix)int ret =0 ;const char *const replay_usage[] = { -N_ ("(EXPERIMENTAL!) git replay (--onto <newbase> | --advance <branch>) <revision-range>..." ), +N_ ("(EXPERIMENTAL!) git replay " +"([--contained] --onto <newbase> | --advance <branch>) " +"<revision-range>..." ),NULL };struct option replay_options[] = {
@@ -278,6 +281,8 @@ int cmd_replay(int argc, const char **argv, const char *prefix)OPT_STRING (0 ,"onto" , &onto_name,N_ ("revision" ),N_ ("replay onto given commit" )), +OPT_BOOL (0 ,"contained" , &contained, +N_ ("advance all branches contained in revision-range" )), OPT_END() };
@@ -289,6 +294,10 @@ int cmd_replay(int argc, const char **argv, const char *prefix)usage_with_options (replay_usage, replay_options); } +if (advance_name && contained) +die (_ ("options '%s' and '%s' cannot be used together" ), +"--advance" ,"--contained" ); +repo_init_revisions (the_repository, &revs, prefix);/*
@@ -377,7 +386,8 @@ int cmd_replay(int argc, const char **argv, const char *prefix)continue ;while (decoration) {if (decoration->type ==DECORATION_REF_LOCAL && -strset_contains (update_refs, decoration->name)) { + (contained ||strset_contains (update_refs, + decoration->name))) { printf("update %s %s %s\n", decoration->name, oid_to_hex(&last_commit->object.oid),
t/t3650-replay-basics.sh +29 -0
@@ -114,4 +114,33 @@ test_expect_success 'replay fails when both --advance and --onto are omitted' ' test_must_fail git replay topic1..topic2 >result ' +test_expect_success 'using replay to also rebase a contained branch' ' + git replay --contained --onto main main..topic3 >result && + + test_line_count = 2 result && + cut -f 3 -d " " result >new-branch-tips && + + git log --format=%s $(head -n 1 new-branch-tips) >actual && + test_write_lines F C M L B A >expect && + test_cmp expect actual && + + git log --format=%s $(tail -n 1 new-branch-tips) >actual && + test_write_lines H G F C M L B A >expect && + test_cmp expect actual && + + printf "update refs/heads/topic1 " >expect && + printf "%s " $(head -n 1 new-branch-tips) >>expect && + git rev-parse topic1 >>expect && + printf "update refs/heads/topic3 " >>expect && + printf "%s " $(tail -n 1 new-branch-tips) >>expect && + git rev-parse topic3 >>expect && + + test_cmp expect result +' + +test_expect_success 'using replay on bare repo to also rebase a contained branch' ' + git -C bare replay --contained --onto main main..topic3 >result-bare && + test_cmp expect result-bare +' + test_done
Signature · 1 line
2.43.0.14.g93e034faee
PATCH v8 14/14 replay: stop assuming replayed branches do not diverge
In reply to Christian CouderFrom: Elijah Newren <[email protected]>
The replay command is able to replay multiple branches but when some of them are based on other replayed branches, their commit should be replayed onto already replayed commits.
For this purpose, let's store the replayed commit and its original commit in a key value store, so that we can easily find and reuse a replayed commit instead of the original one.
--- builtin/replay.c | 44 ++++++++++++++++++++++++++-------- t/t3650-replay-basics.sh | 52 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 10 deletions(-)
builtin/replay.c +34 -10
@@ -223,20 +223,33 @@ static void determine_replay_mode(struct rev_cmdline_info *cmd_info,strset_clear (&rinfo.positive_refs); } +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); +if (pos ==kh_end (replayed_commits)) +return fallback; +return kh_value (replayed_commits, pos); +} +static struct commit *pick_regular_commit (struct commit *pickme, -struct commit *last_commit, +kh_oid_map_t *replayed_commits, +struct commit *onto,struct merge_options *merge_opt,struct merge_result *result) { -struct commit *base; +struct commit *base, *replayed_base;struct tree *pickme_tree, *base_tree; base = pickme->parents->item; + replayed_base =mapped_commit (replayed_commits, base, onto); + result->tree =repo_get_commit_tree (the_repository, replayed_base); pickme_tree =repo_get_commit_tree (the_repository, pickme); base_tree =repo_get_commit_tree (the_repository, base); - merge_opt->branch1 =short_commit_name (last_commit); + merge_opt->branch1 =short_commit_name (replayed_base); merge_opt->branch2 =short_commit_name (pickme); merge_opt->ancestor =xstrfmt ("parent of %s" , merge_opt->branch2);
@@ -250,7 +263,7 @@ static struct commit *pick_regular_commit(struct commit *pickme, merge_opt->ancestor =NULL ;if (!result->clean)return NULL ; -return create_commit (result->tree, pickme, last_commit); +return create_commit (result->tree, pickme, replayed_base); }int cmd_replay (int argc,const char **argv,const char *prefix)
@@ -266,6 +279,7 @@ int cmd_replay(int argc, const char **argv, const char *prefix)struct merge_options merge_opt;struct merge_result result;struct strset *update_refs =NULL ; +kh_oid_map_t *replayed_commits;int ret =0 ;const char *const replay_usage[] = {
@@ -363,21 +377,30 @@ int cmd_replay(int argc, const char **argv, const char *prefix)init_merge_options (&merge_opt, the_repository);memset (&result,0 ,sizeof (result)); merge_opt.show_rename_progress =0 ; - - result.tree =repo_get_commit_tree (the_repository, onto); last_commit = onto; + replayed_commits =kh_init_oid_map ();while ((commit =get_revision (&revs))) {const struct name_decoration *decoration; +khint_t pos; +int hr;if (!commit->parents)die (_ ("replaying down to root commit is not supported yet!" ));if (commit->parents->next)die (_ ("replaying merge commits is not supported yet!" )); - last_commit =pick_regular_commit (commit, last_commit, &merge_opt, &result); + last_commit =pick_regular_commit (commit, replayed_commits, onto, + &merge_opt, &result);if (!last_commit)break ; +/* Record commit -> last_commit mapping */ + pos =kh_put_oid_map (replayed_commits, commit->object.oid, &hr); +if (hr ==0 ) +BUG ("Duplicate rewritten commit: %s\n" , +oid_to_hex (&commit->object.oid)); +kh_value (replayed_commits, pos) = last_commit; +/* Update any necessary branches */ if (advance_name)continue ;
@@ -406,13 +429,14 @@ int cmd_replay(int argc, const char **argv, const char *prefix) }merge_finalize (&merge_opt, &result); - ret = result.clean; - -cleanup: +kh_destroy_oid_map (replayed_commits);if (update_refs) {strset_clear (update_refs);free (update_refs); } + ret = result.clean; + +cleanup:release_revisions (&revs);/* Return */
t/t3650-replay-basics.sh +52 -0
56 lines not on this page · raw message
Signature · 1 line
2.43.0.14.g93e034faee
Hi Christian,
Quoted 6 lines · Christian Couder
# Changes between v7 and v8
Thanks to Dscho, Linus Arver, Dragan Simic, Elijah, Junio, Derrick Stolee, Phillip Wood, Calvin Wan and Toon Claes for their suggestions on the previous versions! The only few changes compared to v7 are:
Quoted 8 lines
* The patch series was rebased onto master at
564d0252ca(Git 2.43, 2023-11-20). This is to make it stand on a stable base.* In patch 2/14 (replay: introduce new builtin), there is a synopsys change in the doc, and the corresponding usage message change as suggested by Dscho. This is just about replacing " # EXPERIMENTAL" at the end of both the synopsys and usage message with "(EXPERIMENTAL!) " at the beginning of them.
Thank you so much for going all the way to where we are now.
Quoted 6 lines
CI tests seem to pass according to:
https://github.com/chriscool/git/actions/runs/6979770154
(Sorry I am not waiting more than 20 minutes for the 3 last ones to finish.)
Yes, our test suite takes too long, and I fear that there are quite a few developers ignoring it as a consequence.
Quoted 5 lines
# Range-diff between v7 and v8
(A single change was made in patch 2/14, but unfortunately as the lines changed in that patch are also changed by other patches later, it looks like there are more changes in subsequent patches.)
Right, the lines added in 2/14 are changed multiple times over the course of the patch series.
Quoted 113 lines
1: cddcd967b2 = 1: 18fd9b0d5d t6429: remove switching aspects of fast-rebase 2: c8476fb093 ! 2: fc6bdf4de4 replay: introduce new builtin @@ Documentation/git-replay.txt (new) +SYNOPSIS +-------- +[verse] -+'git replay' --onto <newbase> <oldbase> <branch> # EXPERIMENTAL ++(EXPERIMENTAL!) 'git replay' --onto <newbase> <oldbase> <branch> + +DESCRIPTION +----------- @@ builtin/replay.c: int cmd__fast_rebase(int argc, const char **argv) - if (argc == 2 && !strcmp(argv[1], "-h")) { - printf("Sorry, I am not a psychiatrist; I can not give you the help you need. Oh, you meant usage...\n"); -+ printf("git replay --onto <newbase> <oldbase> <branch> # EXPERIMENTAL\n"); ++ printf("usage: (EXPERIMENTAL!) git replay --onto <newbase> <oldbase> <branch>\n"); exit(129); }3:
43322abd1e! 3:e96a66c352replay: start usingparse_optionsAPI
@@builtin/replay.c: int cmd_replay(int argc, const char **argv, const char *prefix
int ret = 0;- if (argc == 2 && !strcmp(argv[1], "-h")) {
--printf("git replay --onto <newbase> <oldbase> <branch> # EXPERIMENTAL\n");
+-printf("usage: (EXPERIMENTAL!) git replay --onto <newbase> <oldbase> <branch>\n");
- exit(129);
+ const char * const replay_usage[] = {
-+N_("git replay --onto <newbase> <oldbase> <branch> # EXPERIMENTAL"),
++N_("(EXPERIMENTAL!) git replay --onto <newbase> <oldbase> <branch>"),
+ NULL
+ };
+ struct option replay_options[] = {4: 6524c7f045 = 4: f819d283d9 replay: die() instead of failing assert() 5: 05d0efa3cb = 5: 68bbcf9492 replay: introduce pick_regular_commit() 6: c7a5aad3d6 = 6: 72221c647e replay: change rev walking options 7: 01f35f924b = 7: f54d8fce22 replay: add an important FIXME comment about gpg signing 8: 1498b24bad = 8: e50cc22522 replay: remove progress and info output 9: 6786fc147b = 9: 0c5ea3d18e replay: remove HEAD related sanity check 10: 9a24dbb530 = 10: 9fc636fc3d replay: make it a minimal server side command 11: ad6ca2fbef ! 11: 2096bcad79 replay: use standard revision ranges @@ Documentation/git-replay.txt: git-replay - EXPERIMENTAL: Replay commits on a new SYNOPSIS -------- [verse] --'git replay' --onto <newbase> <oldbase> <branch> # EXPERIMENTAL -+'git replay' --onto <newbase> <revision-range>... # EXPERIMENTAL +-(EXPERIMENTAL!) 'git replay' --onto <newbase> <oldbase> <branch> ++(EXPERIMENTAL!) 'git replay' --onto <newbase> <revision-range>... DESCRIPTION ----------- @@ builtin/replay.c: int cmd_replay(int argc, const char **argv, const char *prefix int ret = 0; const char * const replay_usage[] = { -- N_("git replay --onto <newbase> <oldbase> <branch> # EXPERIMENTAL"), -+ N_("git replay --onto <newbase> <revision-range>... # EXPERIMENTAL"), +- N_("(EXPERIMENTAL!) git replay --onto <newbase> <oldbase> <branch>"), ++ N_("(EXPERIMENTAL!) git replay --onto <newbase> <revision-range>..."), NULL }; struct option replay_options[] = { 12: 081864ed5f ! 12: d5414806ef replay: add --advance or 'cherry-pick' mode @@ Documentation/git-replay.txt: git-replay - EXPERIMENTAL: Replay commits on a new SYNOPSIS -------- [verse] --'git replay' --onto <newbase> <revision-range>... # EXPERIMENTAL -+'git replay' (--onto <newbase> | --advance <branch>) <revision-range>... # EXPERIMENTAL +-(EXPERIMENTAL!) 'git replay' --onto <newbase> <revision-range>... ++(EXPERIMENTAL!) 'git replay' (--onto <newbase> | --advance <branch>) <revision-range>... DESCRIPTION ----------- @@ builtin/replay.c: static struct commit *pick_regular_commit(struct commit *pickm int ret = 0; const char * const replay_usage[] = { -- N_("git replay --onto <newbase> <revision-range>... # EXPERIMENTAL"), -+ N_("git replay (--onto <newbase> | --advance <branch>) <revision-range>... # EXPERIMENTAL"), +- N_("(EXPERIMENTAL!) git replay --onto <newbase> <revision-range>..."), ++ N_("(EXPERIMENTAL!) git replay (--onto <newbase> | --advance <branch>) <revision-range>..."), NULL }; struct option replay_options[] = { 13: 19c4016c7c ! 13: 2a3e521c13 replay: add --contained to rebase contained branches @@ Documentation/git-replay.txt: git-replay - EXPERIMENTAL: Replay commits on a new SYNOPSIS -------- [verse] --'git replay' (--onto <newbase> | --advance <branch>) <revision-range>... # EXPERIMENTAL -+'git replay' ([--contained] --onto <newbase> | --advance <branch>) <revision-range>... # EXPERIMENTAL +-(EXPERIMENTAL!) 'git replay' (--onto <newbase> | --advance <branch>) <revision-range>... ++(EXPERIMENTAL!) 'git replay' ([--contained] --onto <newbase> | --advance <branch>) <revision-range>... DESCRIPTION ----------- @@ builtin/replay.c: int cmd_replay(int argc, const char **argv, const char *prefix int ret = 0; const char * const replay_usage[] = { -- N_("git replay (--onto <newbase> | --advance <branch>) <revision-range>... # EXPERIMENTAL"), -+ N_("git replay ([--contained] --onto <newbase> | --advance <branch>) " -+ "<revision-range>... # EXPERIMENTAL"), +- N_("(EXPERIMENTAL!) git replay (--onto <newbase> | --advance <branch>) <revision-range>..."), ++ N_("(EXPERIMENTAL!) git replay " ++ "([--contained] --onto <newbase> | --advance <branch>) " ++ "<revision-range>..."), NULL }; struct option replay_options[] = {
Quoted 1 line
14:
29556bcc86= 14:93e034faeereplay: stop assuming replayed branches do not diverge
The range-diff looks excellent!
Thank you for addressing all of my concerns, I am very much in favor of getting this version into git/git's main branch.
Thank you,
Johannes