{"thread":{"id":"64783","subject":"[PATCH v10 1/8] builtin/replay: extract core logic to replay revisions","startedAt":"2026-01-12T14:17:06Z","lastAt":"2026-01-13T07:31:37Z","messageCount":19,"participants":["Patrick Steinhardt","Junio C Hamano","Elijah Newren"],"isPatch":true,"patchVersion":10,"patchTotal":8},"messages":[{"id":"533623","messageId":"20260112-b4-pks-history-builtin-v10-0-e3c6aa5b4cec@pks.im","threadId":"64783","inReplyTo":"20250819-b4-pks-history-builtin-v1-0-9b77c32688fe@pks.im","subject":"[PATCH v10 0/8] Introduce git-history(1) command for easy history editing","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-01-12T14:15:12Z","receivedAt":"2026-01-12T14:17:06Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"Hi,\n\nover recent months I've been playing around with Jujutsu quite\nfrequently. While I still prefer using Git, there's been a couple\nfeatures in it that I really like and that I'd like to have in Git, as\nwell.\n\nA copule of these features relate to history editing. Most importantly,\nI really dig the following commands:\n\n  - jj-abandon(1) to drop a specific commit from your history.\n\n  - jj-absorb(1) to take some changes and automatically apply them to\n    commits in your history that last modified the respective hunks.\n\n  - jj-split(1) to split a commit into two.\n\n  - jj-new(1) to insert a new commit after or before a specific other\n    commit.\n\nNot all of these commands can be ported directly into Git. jj-new(1) for\nexample doesn't really make a ton of sense for us, I'd claim. But some\nof these commands _do_ make sense.\n\nThis patch series is a starting point for such a command. For now, it\nonly implements rewording of commits. I'll continue introducing new\ncommands once this patch series has been merged though.\n\nChanges in v10:\n  - Split out changes that make `replay_revisions()` handle empty\n    revision ranges properly.\n  - Drop redundant `struct repository` parameter in\n    `replay_revisions()`.\n  - Move around `die_for_incompatible_opt2()`.\n  - Fix up some of the headers in \"replay.c\".\n  - Split out the changes that make `replay_revisions()` handle empty\n    revision ranges properly.\n  - Create a separate commit that fixes updating detached HEAD.\n  - Strip \"encoding\" header as we're reencoding commit messages.\n  - Merge the logic to update refs in in `REF_ACTION_HEAD` and\n    `REF_ACTION_BRANCHES` modes.\n  - Link to v9: https://lore.kernel.org/r/20260109-b4-pks-history-builtin-v9-0-8766101814c6@pks.im\n\nChanges in v9:\n  - Rename `struct replay_ref_updates` to `struct replay_result` to make\n    its semantics less focussed on ref updates, only.\n  - Clarify and fix return codes of git-replay(1) so that we return 1 on\n    conflict, 128 on an error and 0 on success.\n  - The usual small improvements to commit messages.\n  - Link to v8: https://lore.kernel.org/r/20260107-b4-pks-history-builtin-v8-0-18e9779e3a26@pks.im\n\nChanges in v8:\n  - The series is now built on top of e0bfec3dfc (The 15th batch,\n    2026-01-06) with kh/replay-invalid-onto-advance at 56b77a687e\n    (t3650: add more regression tests for failure conditions,\n    2026-01-05) merged into it. The latter is expected to hit 'next'\n    soonish.\n  - Another biggish revamp to build almost completely on top of the\n    git-replay(1) logic.\n  - We now advance all branches descending from the rewritten commit by\n    default.\n  - I've dropped the patches introducing `git history split` and will\n    send this as a follow-up patch series once this once has been\n    merged. This was done to focus attention on the underlying mechanics\n    as much as possible (and to keep my own sanity with the frequent\n    revamps).\n  - Link to v7: https://lore.kernel.org/r/20251203-b4-pks-history-builtin-v7-0-9e9f849bfd0e@pks.im\n\nChanges in v7:\n  - I've rebased the patch series on top of f0ef5b6d9b (The fifth batch,\n    2025-11-30) with pw/replay-exclude-gpgsig-fix at 9f3a115087 (replay:\n    do not copy \"gpgsign-sha256\" header, 2025-11-26) merged into it to\n    fix some conflicts.\n  - I refactored a bunch of code to be shared between split and reword.\n  - I am now using \"--ancestry-path\" and \"--reverse\" to enumerate\n    commits.\n  - `git history split` now asks for commit messages for both commits.\n  - `git history split` no longer allows the user to edit hunks so that\n    conflicts become impossible. This may be reintroduced at a later\n    point in time.\n  - A bunch of test improvements.\n  - Several commit message and documentation improvements.\n  - Link to v6: https://lore.kernel.org/r/20251027-b4-pks-history-builtin-v6-0-407dd3f57ad3@pks.im\n\nChanges in v6:\n  - I've rebased the patch series again to pull in the latest updates\n    from sa/replay-atomic-ref-updates and fix conflicts. It is now based\n    on 4e98b730f1 (The twenty-fourth batch, 2025-10-24) with ab661bb1bb\n    (replay: add replay.refAction config option, 2025-10-23) merged into\n    it.\n  - I've dropped the \"-m\" options for now, so commit messages are always\n    asked for via the editor. These can be introduced in a subsequent\n    patch series once discussion around them has settled.\n  - We don't use the merge machinery anymore to pick the commits.\n  - Drop the commit to parse commits in the replay machinery. It didn't\n    seem to be necessary in v5 anymore, and now that we don't use the\n    merge machinery at all we don't ever take that code path in the\n    first place.\n  - Link to v5:\n    https://lore.kernel.org/r/20251021-b4-pks-history-builtin-v5-0-78d23f578fe6@pks.im\n\nChanges in v5:\n  - I've changed the patch series to be based on top of 133d151831 (The\n    twenty-first batch, 2025-10-20) with sa/replay-atomic-ref-updates at\n    a1c22e627e (SQAUASH??? t0450 band-aid, 2025-10-14) merged into it.\n    This is one the one hand to fix a conflict, but also to get some of\n    the CI updates to make GitLab CI work again.\n  - Some slight commit message improvements.\n  - Deduplicate subcommand usage strings by using defines.\n  - Fix the desendancy checks to properly verify that HEAD is a\n    descendant of the commit to be rewritten. Also add some tests for\n    this.\n  - Fix the hint that mentions that lines starting with the comment\n    character will be tripped after having written the commit message.\n  - Move an include to the correct commit.\n  - Link to v4: https://lore.kernel.org/r/20251001-b4-pks-history-builtin-v4-0-8e61ddb86317@pks.im\n\nChanges in v4:\n  - I've rebuilt the patch series. It is now based on 821f583da6 (The\n    thirteenth batcn, 2025-09-29) with sa/replay-atomic-ref-updates\n    at 665c66a743 (replay: make atomic ref updates the default behavior,\n    2025-09-27) merged into it. This should fix all conflicts with seen.\n  - I've reworked this patch series to use the same infra as\n    git-replay(1), as discussed during the Contributor's Summit.\n  - I've slimmed down the patch series to only tackle those commands\n    that cannot result in a conflict to keep it simple. I also learned\n    that Elijah has been working on a \"git replay edit\" command, so I\n    dropped that command so that we can instead use his version.\n  - During the Contributor's Summit we have agreed that for now, we\n    won't care about hook execution just yet. This may be backfilled at\n    a later point in time.\n  - I dropped \"commit.verbose\" handling for now, as my understanding of\n    it was wrong at first. This is something we should backfill.\n  - Link to v3: https://lore.kernel.org/r/20250904-b4-pks-history-builtin-v3-0-509053514755@pks.im\n\nChanges in v3:\n  - Add logic to drive the \"post-rewrite\" hook and add tests to verify\n    that all hooks are executed as expected.\n  - Deduplicate logic to turn a replay action into a todo command.\n  - Move the addition of tests for the top-level git-history(1) command\n    to the correct commit.\n  - Some smaller commit message fixes.\n  - Honor \"commit.verbose\".\n  - Fix copy-paste error with an error message.\n  - Link to v2: https://lore.kernel.org/r/20250824-b4-pks-history-builtin-v2-0-964ac12f65bd@pks.im\n\nChanges in v2:\n  - Add a new \"reword\" subcommand.\n  - List git-history(1) in \"command-list.txt\".\n  - Add some missing error handling.\n  - Simplify calling convention of `apply_commits()` to handle root\n    commits internally instead of requiring every caller to do so.\n  - Add tests to verify that git-history(1) refuses to work with changes\n    in the worktree or index.\n  - Mark git-history(1) as experimental.\n  - Introduce commands to manage interrupted history edits.\n  - A bunch of improvements to the manpage.\n  - Link to v1: https://lore.kernel.org/r/20250819-b4-pks-history-builtin-v1-0-9b77c32688fe@pks.im\n\nThanks!\n\nPatrick\n\n---\nPatrick Steinhardt (8):\n      builtin/replay: extract core logic to replay revisions\n      builtin/replay: move core logic into \"libgit.a\"\n      replay: small set of cleanups\n      replay: support empty commit ranges\n      replay: support updating detached HEAD\n      wt-status: provide function to expose status for trees\n      builtin: add new \"history\" command\n      builtin/history: implement \"reword\" subcommand\n\n .gitignore                     |   1 +\n Documentation/git-history.adoc |  73 +++++++\n Documentation/meson.build      |   1 +\n Makefile                       |   2 +\n builtin.h                      |   1 +\n builtin/history.c              | 427 +++++++++++++++++++++++++++++++++++++++++\n builtin/replay.c               | 382 ++++--------------------------------\n command-list.txt               |   1 +\n git.c                          |   1 +\n meson.build                    |   2 +\n replay.c                       | 372 +++++++++++++++++++++++++++++++++++\n replay.h                       |  64 ++++++\n t/meson.build                  |   2 +\n t/t3450-history.sh             |  17 ++\n t/t3451-history-reword.sh      | 391 +++++++++++++++++++++++++++++++++++++\n t/t3650-replay-basics.sh       |   9 +\n wt-status.c                    |  24 +++\n wt-status.h                    |   9 +\n 18 files changed, 1434 insertions(+), 345 deletions(-)\n\nRange-diff versus v9:\n\n1:  0cae2364ee ! 1:  99b47e1af2 builtin/replay: extract core logic to replay revisions\n    @@ Commit message\n         Signed-off-by: Patrick Steinhardt <ps@pks.im>\n     \n      ## builtin/replay.c ##\n    -@@ builtin/replay.c: static enum ref_action_mode get_ref_action_mode(struct repository *repo, const c\n    - \treturn REF_ACTION_UPDATE;\n    +@@ builtin/replay.c: static void set_up_replay_mode(struct repository *repo,\n    + \tif (!rinfo.positive_refexprs)\n    + \t\tdie(_(\"need some commits to replay\"));\n    + \n    +-\tdie_for_incompatible_opt2(!!onto_name, \"--onto\",\n    +-\t\t\t\t  !!*advance_name, \"--advance\");\n    ++\tif (!(!!onto_name ^ !!*advance_name))\n    ++\t\tBUG(\"expected either onto_name or *advance_name in this function\");\n    ++\n    + \tif (onto_name) {\n    + \t\t*onto = peel_committish(repo, onto_name, \"--onto\");\n    + \t\tif (rinfo.positive_refexprs <\n    +@@ builtin/replay.c: static struct commit *pick_regular_commit(struct repository *repo,\n    + \treturn create_commit(repo, result->tree, pickme, replayed_base);\n      }\n      \n     +struct replay_revisions_options {\n    @@ builtin/replay.c: static enum ref_action_mode get_ref_action_mode(struct reposit\n     +\tresult->updates_nr++;\n     +}\n     +\n    -+static int replay_revisions(struct repository *repo, struct rev_info *revs,\n    ++static int replay_revisions(struct rev_info *revs,\n     +\t\t\t    struct replay_revisions_options *opts,\n     +\t\t\t    struct replay_result *out)\n     +{\n    @@ builtin/replay.c: static enum ref_action_mode get_ref_action_mode(struct reposit\n     +\tint ret;\n     +\n     +\tadvance = xstrdup_or_null(opts->advance);\n    -+\tset_up_replay_mode(repo, &revs->cmdline, opts->onto, &advance,\n    ++\tset_up_replay_mode(revs->repo, &revs->cmdline, opts->onto, &advance,\n     +\t\t\t   &onto, &update_refs);\n     +\n     +\t/* FIXME: Should allow replaying commits with the first as a root commit */\n    @@ builtin/replay.c: static enum ref_action_mode get_ref_action_mode(struct reposit\n     +\t\tgoto out;\n     +\t}\n     +\n    -+\tinit_basic_merge_options(&merge_opt, repo);\n    ++\tinit_basic_merge_options(&merge_opt, revs->repo);\n     +\tmemset(&result, 0, sizeof(result));\n     +\tmerge_opt.show_rename_progress = 0;\n     +\tlast_commit = onto;\n    @@ builtin/replay.c: static enum ref_action_mode get_ref_action_mode(struct reposit\n     +\t\tif (commit->parents->next)\n     +\t\t\tdie(_(\"replaying merge commits is not supported yet!\"));\n     +\n    -+\t\tlast_commit = pick_regular_commit(repo, commit, replayed_commits,\n    ++\t\tlast_commit = pick_regular_commit(revs->repo, commit, replayed_commits,\n     +\t\t\t\t\t\t  onto, &merge_opt, &result);\n     +\t\tif (!last_commit)\n     +\t\t\tbreak;\n    @@ builtin/replay.c: static enum ref_action_mode get_ref_action_mode(struct reposit\n     +\treturn ret;\n     +}\n     +\n    - static int handle_ref_update(enum ref_action_mode mode,\n    - \t\t\t     struct ref_transaction *transaction,\n    - \t\t\t     const char *refname,\n    + static enum ref_action_mode parse_ref_action_mode(const char *ref_action, const char *source)\n    + {\n    + \tif (!ref_action || !strcmp(ref_action, \"update\"))\n     @@ builtin/replay.c: int cmd_replay(int argc,\n      \t       const char *prefix,\n      \t       struct repository *repo)\n    @@ builtin/replay.c: int cmd_replay(int argc,\n     -\t\t\t\t  contained, \"--contained\");\n     +\tdie_for_incompatible_opt2(!!opts.advance, \"--advance\",\n     +\t\t\t\t  opts.contained, \"--contained\");\n    ++\tdie_for_incompatible_opt2(!!opts.advance, \"--advance\",\n    ++\t\t\t\t  !!opts.onto, \"--onto\");\n      \n      \t/* Parse ref action mode from command line or config */\n      \tref_mode = get_ref_action_mode(repo, ref_action);\n    @@ builtin/replay.c: int cmd_replay(int argc,\n     -\t\t\t   &onto, &update_refs);\n     -\n     -\t/* FIXME: Should allow replaying commits with the first as a root commit */\n    -+\tret = replay_revisions(repo, &revs, &opts, &result);\n    ++\tret = replay_revisions(&revs, &opts, &result);\n     +\tif (ret)\n     +\t\tgoto cleanup;\n      \n2:  c5fca546e5 ! 2:  0945b53ecb builtin/replay: move core logic into \"libgit.a\"\n    @@ builtin/replay.c\n     -\tif (!rinfo.positive_refexprs)\n     -\t\tdie(_(\"need some commits to replay\"));\n     -\n    --\tdie_for_incompatible_opt2(!!onto_name, \"--onto\",\n    --\t\t\t\t  !!*advance_name, \"--advance\");\n    +-\tif (!(!!onto_name ^ !!*advance_name))\n    +-\t\tBUG(\"expected either onto_name or *advance_name in this function\");\n    +-\n     -\tif (onto_name) {\n     -\t\t*onto = peel_committish(repo, onto_name, \"--onto\");\n     -\t\tif (rinfo.positive_refexprs <\n    @@ builtin/replay.c\n     -\treturn create_commit(repo, result->tree, pickme, replayed_base);\n     -}\n     -\n    - static enum ref_action_mode parse_ref_action_mode(const char *ref_action, const char *source)\n    - {\n    - \tif (!ref_action || !strcmp(ref_action, \"update\"))\n    -@@ builtin/replay.c: static enum ref_action_mode get_ref_action_mode(struct repository *repo, const c\n    - \treturn REF_ACTION_UPDATE;\n    - }\n    - \n     -struct replay_revisions_options {\n     -\tconst char *advance;\n     -\tconst char *onto;\n    @@ builtin/replay.c: static enum ref_action_mode get_ref_action_mode(struct reposit\n     -\tresult->updates_nr++;\n     -}\n     -\n    --static int replay_revisions(struct repository *repo, struct rev_info *revs,\n    +-static int replay_revisions(struct rev_info *revs,\n     -\t\t\t    struct replay_revisions_options *opts,\n     -\t\t\t    struct replay_result *out)\n     -{\n    @@ builtin/replay.c: static enum ref_action_mode get_ref_action_mode(struct reposit\n     -\tint ret;\n     -\n     -\tadvance = xstrdup_or_null(opts->advance);\n    --\tset_up_replay_mode(repo, &revs->cmdline, opts->onto, &advance,\n    +-\tset_up_replay_mode(revs->repo, &revs->cmdline, opts->onto, &advance,\n     -\t\t\t   &onto, &update_refs);\n     -\n     -\t/* FIXME: Should allow replaying commits with the first as a root commit */\n    @@ builtin/replay.c: static enum ref_action_mode get_ref_action_mode(struct reposit\n     -\t\tgoto out;\n     -\t}\n     -\n    --\tinit_basic_merge_options(&merge_opt, repo);\n    +-\tinit_basic_merge_options(&merge_opt, revs->repo);\n     -\tmemset(&result, 0, sizeof(result));\n     -\tmerge_opt.show_rename_progress = 0;\n     -\tlast_commit = onto;\n    @@ builtin/replay.c: static enum ref_action_mode get_ref_action_mode(struct reposit\n     -\t\tif (commit->parents->next)\n     -\t\t\tdie(_(\"replaying merge commits is not supported yet!\"));\n     -\n    --\t\tlast_commit = pick_regular_commit(repo, commit, replayed_commits,\n    +-\t\tlast_commit = pick_regular_commit(revs->repo, commit, replayed_commits,\n     -\t\t\t\t\t\t  onto, &merge_opt, &result);\n     -\t\tif (!last_commit)\n     -\t\t\tbreak;\n    @@ builtin/replay.c: static enum ref_action_mode get_ref_action_mode(struct reposit\n     -\treturn ret;\n     -}\n     -\n    - static int handle_ref_update(enum ref_action_mode mode,\n    - \t\t\t     struct ref_transaction *transaction,\n    - \t\t\t     const char *refname,\n    + static enum ref_action_mode parse_ref_action_mode(const char *ref_action, const char *source)\n    + {\n    + \tif (!ref_action || !strcmp(ref_action, \"update\"))\n     \n      ## meson.build ##\n     @@ meson.build: libgit_sources = [\n    @@ replay.c (new)\n     +#include \"hex.h\"\n     +#include \"merge-ort.h\"\n     +#include \"object-name.h\"\n    -+#include \"oidset.h\"\n    -+#include \"parse-options.h\"\n     +#include \"refs.h\"\n     +#include \"replay.h\"\n     +#include \"revision.h\"\n    ++#include \"strmap.h\"\n     +#include \"tree.h\"\n     +\n     +static const char *short_commit_name(struct repository *repo,\n    @@ replay.c (new)\n     +\tif (!rinfo.positive_refexprs)\n     +\t\tdie(_(\"need some commits to replay\"));\n     +\n    -+\tdie_for_incompatible_opt2(!!onto_name, \"--onto\",\n    -+\t\t\t\t  !!*advance_name, \"--advance\");\n    ++\tif (!(!!onto_name ^ !!*advance_name))\n    ++\t\tBUG(\"expected either onto_name or *advance_name in this function\");\n    ++\n     +\tif (onto_name) {\n     +\t\t*onto = peel_committish(repo, onto_name, \"--onto\");\n     +\t\tif (rinfo.positive_refexprs <\n    @@ replay.c (new)\n     +\tresult->updates_nr++;\n     +}\n     +\n    -+int replay_revisions(struct repository *repo, struct rev_info *revs,\n    ++int replay_revisions(struct rev_info *revs,\n     +\t\t     struct replay_revisions_options *opts,\n     +\t\t     struct replay_result *out)\n     +{\n    @@ replay.c (new)\n     +\tint ret;\n     +\n     +\tadvance = xstrdup_or_null(opts->advance);\n    -+\tset_up_replay_mode(repo, &revs->cmdline, opts->onto, &advance,\n    ++\tset_up_replay_mode(revs->repo, &revs->cmdline, opts->onto, &advance,\n     +\t\t\t   &onto, &update_refs);\n     +\n     +\t/* FIXME: Should allow replaying commits with the first as a root commit */\n    @@ replay.c (new)\n     +\t\tgoto out;\n     +\t}\n     +\n    -+\tinit_basic_merge_options(&merge_opt, repo);\n    ++\tinit_basic_merge_options(&merge_opt, revs->repo);\n     +\tmemset(&result, 0, sizeof(result));\n     +\tmerge_opt.show_rename_progress = 0;\n     +\tlast_commit = onto;\n    @@ replay.c (new)\n     +\t\tif (commit->parents->next)\n     +\t\t\tdie(_(\"replaying merge commits is not supported yet!\"));\n     +\n    -+\t\tlast_commit = pick_regular_commit(repo, commit, replayed_commits,\n    ++\t\tlast_commit = pick_regular_commit(revs->repo, commit, replayed_commits,\n     +\t\t\t\t\t\t  onto, &merge_opt, &result);\n     +\t\tif (!last_commit)\n     +\t\t\tbreak;\n    @@ replay.h (new)\n     + *\n     + * Returns 0 on success, a negative error code otherwise.\n     + */\n    -+int replay_revisions(struct repository *repo, struct rev_info *revs,\n    ++int replay_revisions(struct rev_info *revs,\n     +\t\t     struct replay_revisions_options *opts,\n     +\t\t     struct replay_result *out);\n     +\n3:  1d55aab054 = 3:  85ebe5853f replay: small set of cleanups\n4:  451717f951 < -:  ---------- replay: yield the object ID of the final rewritten commit\n-:  ---------- > 4:  a24641dbfb replay: support empty commit ranges\n-:  ---------- > 5:  e0ab803a46 replay: support updating detached HEAD\n5:  7cde288c8d = 6:  abc33b25ca wt-status: provide function to expose status for trees\n6:  b315c04169 ! 7:  387d1ded10 builtin: add new \"history\" command\n    @@ Documentation/git-history.adoc (new)\n     +your commit history that is simpler to use compared to linkgit:git-rebase[1] in\n     +general.\n     +\n    -+If you want to reapply a range of commits onto a different base, or interactive\n    -+rebases if you want to edit a range of commits.\n    ++Use linkgit:git-rebase[1] if you want to reapply a range of commits onto a\n    ++different base, or interactive rebases if you want to edit a range of commits\n    ++at once.\n     +\n     +LIMITATIONS\n     +-----------\n    @@ Documentation/git-history.adoc (new)\n     +COMMANDS\n     +--------\n     +\n    -+Several commands are available to rewrite history in different ways:\n    ++No commands are supported yet.\n     +\n     +GIT\n     +---\n7:  f71911586d ! 8:  aeb895475b builtin/history: implement \"reword\" subcommand\n    @@ Commit message\n             rewritten. This especially helps with workflows that use stacked\n             branches.\n     \n    +    Helped-by: Elijah Newren <newren@gmail.com>\n         Signed-off-by: Patrick Steinhardt <ps@pks.im>\n     \n      ## Documentation/git-history.adoc ##\n    @@ Documentation/git-history.adoc: git-history - EXPERIMENTAL: Rewrite history\n      \n      DESCRIPTION\n      -----------\n    -@@ Documentation/git-history.adoc: Overall, linkgit:git-history[1] aims to provide a more opinionated way to modify\n    - your commit history that is simpler to use compared to linkgit:git-rebase[1] in\n    - general.\n    - \n    --If you want to reapply a range of commits onto a different base, or interactive\n    --rebases if you want to edit a range of commits.\n    -+Use linkgit:git-rebase[1] if you want to reapply a range of commits onto a\n    -+different base, or interactive rebases if you want to edit a range of commits\n    -+at once.\n    - \n    - LIMITATIONS\n    - -----------\n    -@@ Documentation/git-history.adoc: COMMANDS\n    - \n    - Several commands are available to rewrite history in different ways:\n    +@@ Documentation/git-history.adoc: first-class conflicts.\n    + COMMANDS\n    + --------\n      \n    +-No commands are supported yet.\n    ++The following commands are available to rewrite history in different ways:\n    ++\n     +`reword <commit>`::\n     +\tRewrite the commit message of the specified commit. All the other\n     +\tdetails of this commit remain unchanged. This command will spawn an\n    @@ Documentation/git-history.adoc: COMMANDS\n     +`--ref-action=(branches|head|print)`::\n     +\tControl which references will be updated by the command, if any. With\n     +\t`branches`, all local branches that point to commits which are\n    -+\tdecendants of the original commit will be rewritten. With `head`, only\n    ++\tdescendants of the original commit will be rewritten. With `head`, only\n     +\tthe current `HEAD` reference will be rewritten. With `print`, all\n     +\tupdates as they would be performed with `branches` are printed in a\n     +\tformat that can be consumed by linkgit:git-update-ref[1].\n    -+\n    + \n      GIT\n      ---\n    - Part of the linkgit:git[1] suite\n     \n      ## builtin/history.c ##\n     @@\n    @@ builtin/history.c\n     +\t\t\t\t\t   struct commit *original,\n     +\t\t\t\t\t   struct commit **out)\n     +{\n    -+\tconst char *exclude_gpgsig[] = { \"gpgsig\", \"gpgsig-sha256\", NULL };\n    ++\tconst char *exclude_gpgsig[] = {\n    ++\t\t/* We reencode the message, so the encoding needs to be stripped. */\n    ++\t\t\"encoding\",\n    ++\t\t/* We need to strip signatures as those will become invalid. */\n    ++\t\t\"gpgsig\",\n    ++\t\t\"gpgsig-sha256\",\n    ++\t\tNULL,\n    ++\t};\n     +\tconst char *original_message, *original_body, *ptr;\n     +\tstruct commit_extra_header *original_extra_headers = NULL;\n     +\tstruct strbuf commit_message = STRBUF_INIT;\n    @@ builtin/history.c\n     +{\n     +\tconst struct name_decoration *decoration;\n     +\tstruct replay_revisions_options opts = { 0 };\n    -+\tstruct replay_result result = {\n    -+\t\t.final_oid = rewritten->object.oid,\n    -+\t};\n    ++\tstruct replay_result result = { 0 };\n     +\tstruct ref_transaction *transaction = NULL;\n     +\tstruct strvec args = STRVEC_INIT;\n     +\tstruct strbuf err = STRBUF_INIT;\n     +\tstruct commit *head = NULL;\n     +\tstruct rev_info revs;\n     +\tchar hex[GIT_MAX_HEXSZ + 1];\n    ++\tbool detached_head;\n    ++\tint head_flags = 0;\n     +\tint ret;\n     +\n    ++\trefs_read_ref_full(get_main_ref_store(repo), \"HEAD\",\n    ++\t\t\t   RESOLVE_REF_NO_RECURSE, NULL, &head_flags);\n    ++\tdetached_head = !(head_flags & REF_ISSYMREF);\n    ++\n     +\trepo_init_revisions(repo, &revs, NULL);\n     +\tstrvec_push(&args, \"ignored\");\n     +\tstrvec_push(&args, \"--reverse\");\n    @@ builtin/history.c\n     +\t\t\tgoto out;\n     +\t\t}\n     +\n    -+\t\tstrvec_push(&args, oid_to_hex(&head->object.oid));\n    ++\t\tstrvec_push(&args, \"HEAD\");\n     +\t} else {\n     +\t\tstrvec_push(&args, \"--branches\");\n    ++\t\tstrvec_push(&args, \"HEAD\");\n     +\t}\n     +\n     +\tsetup_revisions_from_strvec(&args, &revs, NULL);\n    @@ builtin/history.c\n     +\n     +\topts.onto = oid_to_hex_r(hex, &rewritten->object.oid);\n     +\n    -+\tret = replay_revisions(repo, &revs, &opts, &result);\n    ++\tret = replay_revisions(&revs, &opts, &result);\n     +\tif (ret)\n     +\t\tgoto out;\n     +\n     +\tswitch (action) {\n    -+\tcase REF_ACTION_DEFAULT:\n     +\tcase REF_ACTION_BRANCHES:\n    ++\tcase REF_ACTION_HEAD:\n     +\t\ttransaction = ref_store_transaction_begin(get_main_ref_store(repo), 0, &err);\n     +\t\tif (!transaction) {\n     +\t\t\tret = error(_(\"failed to begin ref transaction: %s\"), err.buf);\n    @@ builtin/history.c\n     +\t\t     decoration;\n     +\t\t     decoration = decoration->next)\n     +\t\t{\n    -+\t\t\tif (decoration->type != DECORATION_REF_LOCAL)\n    ++\t\t\tif (decoration->type != DECORATION_REF_LOCAL &&\n    ++\t\t\t    decoration->type != DECORATION_REF_HEAD)\n    ++\t\t\t\tcontinue;\n    ++\n    ++\t\t\tif (action == REF_ACTION_HEAD &&\n    ++\t\t\t    decoration->type != DECORATION_REF_HEAD)\n    ++\t\t\t\tcontinue;\n    ++\n    ++\t\t\t/*\n    ++\t\t\t * We only need to update HEAD separately in case it's\n    ++\t\t\t * detached. If it's not we'd already update the branch\n    ++\t\t\t * it is pointing to.\n    ++\t\t\t */\n    ++\t\t\tif (action == REF_ACTION_BRANCHES &&\n    ++\t\t\t    decoration->type == DECORATION_REF_HEAD &&\n    ++\t\t\t    !detached_head)\n     +\t\t\t\tcontinue;\n     +\n     +\t\t\tret = ref_transaction_update(transaction,\n    @@ builtin/history.c\n     +\t\t}\n     +\n     +\t\tbreak;\n    -+\tcase REF_ACTION_HEAD:\n    -+\t\tret = refs_update_ref(get_main_ref_store(repo), reflog_msg, \"HEAD\",\n    -+\t\t\t\t      &result.final_oid, &head->object.oid, 0,\n    -+\t\t\t\t      UPDATE_REFS_MSG_ON_ERR);\n    -+\t\tif (ret)\n    -+\t\t\tgoto out;\n    -+\t\tbreak;\n     +\tcase REF_ACTION_PRINT:\n     +\t\tfor (size_t i = 0; i < result.updates_nr; i++)\n     +\t\t\tprintf(\"update %s %s %s\\n\",\n    @@ builtin/history.c\n     +\t}\n     +\trepo_config(repo, git_default_config, NULL);\n     +\n    ++\tif (action == REF_ACTION_DEFAULT)\n    ++\t\taction = REF_ACTION_BRANCHES;\n    ++\n     +\toriginal = lookup_commit_reference_by_name(argv[0]);\n     +\tif (!original) {\n     +\t\tret = error(_(\"commit cannot be found: %s\"), argv[0]);\n    @@ builtin/history.c\n     +\treturn fn(argc, argv, prefix, repo);\n      }\n     \n    - ## replay.c ##\n    -@@ replay.c: int replay_revisions(struct repository *repo, struct rev_info *revs,\n    - \tstruct commit *commit;\n    - \tstruct commit *onto = NULL;\n    - \tstruct merge_options merge_opt;\n    --\tstruct merge_result result;\n    -+\tstruct merge_result result = {\n    -+\t\t.clean = 1,\n    -+\t};\n    - \tchar *advance;\n    - \tint ret;\n    - \n    -@@ replay.c: int replay_revisions(struct repository *repo, struct rev_info *revs,\n    - \t}\n    - \n    - \tinit_basic_merge_options(&merge_opt, repo);\n    --\tmemset(&result, 0, sizeof(result));\n    - \tmerge_opt.show_rename_progress = 0;\n    - \tlast_commit = onto;\n    - \treplayed_commits = kh_init_oid_map();\n    -\n      ## t/meson.build ##\n     @@ t/meson.build: integration_tests = [\n        't3437-rebase-fixup-options.sh',\n    @@ t/t3451-history-reword.sh (new)\n     +\t)\n     +'\n     +\n    ++test_expect_success 'can reword commit in the middle even on detached head' '\n    ++\ttest_when_finished \"rm -rf repo\" &&\n    ++\tgit init repo &&\n    ++\t(\n    ++\t\tcd repo &&\n    ++\t\ttest_commit first &&\n    ++\t\ttest_commit second &&\n    ++\t\ttest_commit third_on_main &&\n    ++\t\tgit checkout --detach HEAD^ &&\n    ++\t\ttest_commit third_on_head &&\n    ++\n    ++\t\treword_with_message HEAD~ <<-EOF &&\n    ++\t\tsecond reworded\n    ++\t\tEOF\n    ++\n    ++\t\texpect_graph HEAD --branches <<-\\EOF\n    ++\t\t* third_on_head\n    ++\t\t| * third_on_main\n    ++\t\t|/\n    ++\t\t* second reworded\n    ++\t\t* first\n    ++\t\tEOF\n    ++       )\n    ++'\n    ++\n    ++test_expect_success 'can reword the detached head' '\n    ++\ttest_when_finished \"rm -rf repo\" &&\n    ++\tgit init repo &&\n    ++\t(\n    ++\t\tcd repo &&\n    ++\t\ttest_commit first &&\n    ++\t\ttest_commit second &&\n    ++\t\tgit checkout --detach HEAD &&\n    ++\t\ttest_commit third &&\n    ++\n    ++\t\treword_with_message HEAD <<-EOF &&\n    ++\t\tthird reworded\n    ++\t\tEOF\n    ++\n    ++\t\texpect_log <<-\\EOF\n    ++\t\tthird reworded\n    ++\t\tsecond\n    ++\t\tfirst\n    ++\t\tEOF\n    ++\t)\n    ++'\n    ++\n     +test_expect_success 'can reword root commit' '\n     +\ttest_when_finished \"rm -rf repo\" &&\n     +\tgit init repo &&\n\n---\nbase-commit: 02bdc12118c43e0a65258eb742492965859bffb6\nchange-id: 20250819-b4-pks-history-builtin-83398f9a05f0\n\n"},{"id":"533622","messageId":"20260112-b4-pks-history-builtin-v10-1-e3c6aa5b4cec@pks.im","threadId":"64783","inReplyTo":"20260112-b4-pks-history-builtin-v10-0-e3c6aa5b4cec@pks.im","subject":"[PATCH v10 1/8] builtin/replay: extract core logic to replay revisions","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-01-12T14:15:13Z","receivedAt":"2026-01-12T14:17:08Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"We're about to move the core logic used to replay revisions onto a new\nbase into the \"libgit.a\" library. Prepare for this by pulling out the\nlogic into a new function `replay_revisions()` that:\n\n  1. Takes a set of revisions to replay and some options that tell it how\n     it ought to replay the revisions.\n\n  2. Replays the commits.\n\n  3. Records any reference updates that would be caused by replaying the\n     commits in a structure that is owned by the caller.\n\nThe logic itself will be moved into a separate file in the next commit.\nThis change is not expected to cause user-visible change in behaviour.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n builtin/replay.c | 281 +++++++++++++++++++++++++++++++++----------------------\n 1 file changed, 170 insertions(+), 111 deletions(-)\n\ndiff --git a/builtin/replay.c b/builtin/replay.c\nindex 1960bbbee8..df0dc04d02 100644\n--- a/builtin/replay.c\n+++ b/builtin/replay.c\n@@ -177,8 +177,9 @@ static void set_up_replay_mode(struct repository *repo,\n \tif (!rinfo.positive_refexprs)\n \t\tdie(_(\"need some commits to replay\"));\n \n-\tdie_for_incompatible_opt2(!!onto_name, \"--onto\",\n-\t\t\t\t  !!*advance_name, \"--advance\");\n+\tif (!(!!onto_name ^ !!*advance_name))\n+\t\tBUG(\"expected either onto_name or *advance_name in this function\");\n+\n \tif (onto_name) {\n \t\t*onto = peel_committish(repo, onto_name, \"--onto\");\n \t\tif (rinfo.positive_refexprs <\n@@ -253,6 +254,137 @@ static struct commit *pick_regular_commit(struct repository *repo,\n \treturn create_commit(repo, result->tree, pickme, replayed_base);\n }\n \n+struct replay_revisions_options {\n+\tconst char *advance;\n+\tconst char *onto;\n+\tint contained;\n+};\n+\n+struct replay_result {\n+\tstruct replay_ref_update {\n+\t\tchar *refname;\n+\t\tstruct object_id old_oid;\n+\t\tstruct object_id new_oid;\n+\t} *updates;\n+\tsize_t updates_nr, updates_alloc;\n+\n+\tbool merge_conflict;\n+};\n+\n+static void replay_result_release(struct replay_result *result)\n+{\n+\tfor (size_t i = 0; i < result->updates_nr; i++)\n+\t\tfree(result->updates[i].refname);\n+\tfree(result->updates);\n+}\n+\n+static void replay_result_queue_update(struct replay_result *result,\n+\t\t\t\t       const char *refname,\n+\t\t\t\t       const struct object_id *old_oid,\n+\t\t\t\t       const struct object_id *new_oid)\n+{\n+\tALLOC_GROW(result->updates, result->updates_nr + 1, result->updates_alloc);\n+\tresult->updates[result->updates_nr].refname = xstrdup(refname);\n+\tresult->updates[result->updates_nr].old_oid = *old_oid;\n+\tresult->updates[result->updates_nr].new_oid = *new_oid;\n+\tresult->updates_nr++;\n+}\n+\n+static int replay_revisions(struct rev_info *revs,\n+\t\t\t    struct replay_revisions_options *opts,\n+\t\t\t    struct replay_result *out)\n+{\n+\tkh_oid_map_t *replayed_commits = NULL;\n+\tstruct strset *update_refs = NULL;\n+\tstruct commit *last_commit = NULL;\n+\tstruct commit *commit;\n+\tstruct commit *onto = NULL;\n+\tstruct merge_options merge_opt;\n+\tstruct merge_result result;\n+\tchar *advance;\n+\tint ret;\n+\n+\tadvance = xstrdup_or_null(opts->advance);\n+\tset_up_replay_mode(revs->repo, &revs->cmdline, opts->onto, &advance,\n+\t\t\t   &onto, &update_refs);\n+\n+\t/* FIXME: Should allow replaying commits with the first as a root commit */\n+\n+\tif (prepare_revision_walk(revs) < 0) {\n+\t\tret = error(_(\"error preparing revisions\"));\n+\t\tgoto out;\n+\t}\n+\n+\tinit_basic_merge_options(&merge_opt, revs->repo);\n+\tmemset(&result, 0, sizeof(result));\n+\tmerge_opt.show_rename_progress = 0;\n+\tlast_commit = onto;\n+\treplayed_commits = kh_init_oid_map();\n+\twhile ((commit = get_revision(revs))) {\n+\t\tconst struct name_decoration *decoration;\n+\t\tkhint_t pos;\n+\t\tint hr;\n+\n+\t\tif (!commit->parents)\n+\t\t\tdie(_(\"replaying down from root commit is not supported yet!\"));\n+\t\tif (commit->parents->next)\n+\t\t\tdie(_(\"replaying merge commits is not supported yet!\"));\n+\n+\t\tlast_commit = pick_regular_commit(revs->repo, commit, replayed_commits,\n+\t\t\t\t\t\t  onto, &merge_opt, &result);\n+\t\tif (!last_commit)\n+\t\t\tbreak;\n+\n+\t\t/* Record commit -> last_commit mapping */\n+\t\tpos = kh_put_oid_map(replayed_commits, commit->object.oid, &hr);\n+\t\tif (hr == 0)\n+\t\t\tBUG(\"Duplicate rewritten commit: %s\\n\",\n+\t\t\t    oid_to_hex(&commit->object.oid));\n+\t\tkh_value(replayed_commits, pos) = last_commit;\n+\n+\t\t/* Update any necessary branches */\n+\t\tif (advance)\n+\t\t\tcontinue;\n+\t\tdecoration = get_name_decoration(&commit->object);\n+\t\tif (!decoration)\n+\t\t\tcontinue;\n+\t\twhile (decoration) {\n+\t\t\tif (decoration->type == DECORATION_REF_LOCAL &&\n+\t\t\t    (opts->contained || strset_contains(update_refs,\n+\t\t\t\t\t\t\t\tdecoration->name))) {\n+\t\t\t\treplay_result_queue_update(out, decoration->name,\n+\t\t\t\t\t\t\t   &commit->object.oid,\n+\t\t\t\t\t\t\t   &last_commit->object.oid);\n+\t\t\t}\n+\t\t\tdecoration = decoration->next;\n+\t\t}\n+\t}\n+\n+\tif (!result.clean) {\n+\t\tout->merge_conflict = true;\n+\t\tret = -1;\n+\t\tgoto out;\n+\t}\n+\n+\t/* In --advance mode, advance the target ref */\n+\tif (advance)\n+\t\treplay_result_queue_update(out, advance,\n+\t\t\t\t\t   &onto->object.oid,\n+\t\t\t\t\t   &last_commit->object.oid);\n+\n+\tret = 0;\n+\n+out:\n+\tif (update_refs) {\n+\t\tstrset_clear(update_refs);\n+\t\tfree(update_refs);\n+\t}\n+\tkh_destroy_oid_map(replayed_commits);\n+\tmerge_finalize(&merge_opt, &result);\n+\tfree(advance);\n+\treturn ret;\n+}\n+\n static enum ref_action_mode parse_ref_action_mode(const char *ref_action, const char *source)\n {\n \tif (!ref_action || !strcmp(ref_action, \"update\"))\n@@ -306,21 +438,11 @@ int cmd_replay(int argc,\n \t       const char *prefix,\n \t       struct repository *repo)\n {\n-\tconst char *advance_name_opt = NULL;\n-\tchar *advance_name = NULL;\n-\tstruct commit *onto = NULL;\n-\tconst char *onto_name = NULL;\n-\tint contained = 0;\n+\tstruct replay_revisions_options opts = { 0 };\n+\tstruct replay_result result = { 0 };\n \tconst char *ref_action = NULL;\n \tenum ref_action_mode ref_mode;\n-\n \tstruct rev_info revs;\n-\tstruct commit *last_commit = NULL;\n-\tstruct commit *commit;\n-\tstruct merge_options merge_opt;\n-\tstruct merge_result result;\n-\tstruct strset *update_refs = NULL;\n-\tkh_oid_map_t *replayed_commits;\n \tstruct ref_transaction *transaction = NULL;\n \tstruct strbuf transaction_err = STRBUF_INIT;\n \tstruct strbuf reflog_msg = STRBUF_INIT;\n@@ -333,13 +455,13 @@ int cmd_replay(int argc,\n \t\tNULL\n \t};\n \tstruct option replay_options[] = {\n-\t\tOPT_STRING(0, \"advance\", &advance_name_opt,\n+\t\tOPT_STRING(0, \"advance\", &opts.advance,\n \t\t\t   N_(\"branch\"),\n \t\t\t   N_(\"make replay advance given branch\")),\n-\t\tOPT_STRING(0, \"onto\", &onto_name,\n+\t\tOPT_STRING(0, \"onto\", &opts.onto,\n \t\t\t   N_(\"revision\"),\n \t\t\t   N_(\"replay onto given commit\")),\n-\t\tOPT_BOOL(0, \"contained\", &contained,\n+\t\tOPT_BOOL(0, \"contained\", &opts.contained,\n \t\t\t N_(\"update all branches that point at commits in <revision-range>\")),\n \t\tOPT_STRING(0, \"ref-action\", &ref_action,\n \t\t\t   N_(\"mode\"),\n@@ -350,19 +472,19 @@ int cmd_replay(int argc,\n \targc = parse_options(argc, argv, prefix, replay_options, replay_usage,\n \t\t\t     PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN_OPT);\n \n-\tif (!onto_name && !advance_name_opt) {\n+\tif (!opts.onto && !opts.advance) {\n \t\terror(_(\"option --onto or --advance is mandatory\"));\n \t\tusage_with_options(replay_usage, replay_options);\n \t}\n \n-\tdie_for_incompatible_opt2(!!advance_name_opt, \"--advance\",\n-\t\t\t\t  contained, \"--contained\");\n+\tdie_for_incompatible_opt2(!!opts.advance, \"--advance\",\n+\t\t\t\t  opts.contained, \"--contained\");\n+\tdie_for_incompatible_opt2(!!opts.advance, \"--advance\",\n+\t\t\t\t  !!opts.onto, \"--onto\");\n \n \t/* Parse ref action mode from command line or config */\n \tref_mode = get_ref_action_mode(repo, ref_action);\n \n-\tadvance_name = xstrdup_or_null(advance_name_opt);\n-\n \trepo_init_revisions(repo, &revs, prefix);\n \n \t/*\n@@ -414,18 +536,19 @@ int cmd_replay(int argc,\n \t\trevs.simplify_history = 0;\n \t}\n \n-\tset_up_replay_mode(repo, &revs.cmdline,\n-\t\t\t   onto_name, &advance_name,\n-\t\t\t   &onto, &update_refs);\n-\n-\t/* FIXME: Should allow replaying commits with the first as a root commit */\n+\tret = replay_revisions(&revs, &opts, &result);\n+\tif (ret)\n+\t\tgoto cleanup;\n \n \t/* Build reflog message */\n-\tif (advance_name_opt)\n-\t\tstrbuf_addf(&reflog_msg, \"replay --advance %s\", advance_name_opt);\n-\telse\n-\t\tstrbuf_addf(&reflog_msg, \"replay --onto %s\",\n-\t\t\t    oid_to_hex(&onto->object.oid));\n+\tif (opts.advance) {\n+\t\tstrbuf_addf(&reflog_msg, \"replay --advance %s\", opts.advance);\n+\t} else {\n+\t\tstruct object_id oid;\n+\t\tif (repo_get_oid_committish(repo, opts.onto, &oid))\n+\t\t\tBUG(\"--onto commit should have been resolved beforehand already\");\n+\t\tstrbuf_addf(&reflog_msg, \"replay --onto %s\", oid_to_hex(&oid));\n+\t}\n \n \t/* Initialize ref transaction if using update mode */\n \tif (ref_mode == REF_ACTION_UPDATE) {\n@@ -438,78 +561,19 @@ int cmd_replay(int argc,\n \t\t}\n \t}\n \n-\tif (prepare_revision_walk(&revs) < 0) {\n-\t\tret = error(_(\"error preparing revisions\"));\n-\t\tgoto cleanup;\n-\t}\n-\n-\tinit_basic_merge_options(&merge_opt, repo);\n-\tmemset(&result, 0, sizeof(result));\n-\tmerge_opt.show_rename_progress = 0;\n-\tlast_commit = onto;\n-\treplayed_commits = kh_init_oid_map();\n-\twhile ((commit = get_revision(&revs))) {\n-\t\tconst struct name_decoration *decoration;\n-\t\tkhint_t pos;\n-\t\tint hr;\n-\n-\t\tif (!commit->parents)\n-\t\t\tdie(_(\"replaying down from root commit is not supported yet!\"));\n-\t\tif (commit->parents->next)\n-\t\t\tdie(_(\"replaying merge commits is not supported yet!\"));\n-\n-\t\tlast_commit = pick_regular_commit(repo, commit, replayed_commits,\n-\t\t\t\t\t\t  onto, &merge_opt, &result);\n-\t\tif (!last_commit)\n-\t\t\tbreak;\n-\n-\t\t/* Record commit -> last_commit mapping */\n-\t\tpos = kh_put_oid_map(replayed_commits, commit->object.oid, &hr);\n-\t\tif (hr == 0)\n-\t\t\tBUG(\"Duplicate rewritten commit: %s\\n\",\n-\t\t\t    oid_to_hex(&commit->object.oid));\n-\t\tkh_value(replayed_commits, pos) = last_commit;\n-\n-\t\t/* Update any necessary branches */\n-\t\tif (advance_name)\n-\t\t\tcontinue;\n-\t\tdecoration = get_name_decoration(&commit->object);\n-\t\tif (!decoration)\n-\t\t\tcontinue;\n-\t\twhile (decoration) {\n-\t\t\tif (decoration->type == DECORATION_REF_LOCAL &&\n-\t\t\t    (contained || strset_contains(update_refs,\n-\t\t\t\t\t\t\t  decoration->name))) {\n-\t\t\t\tif (handle_ref_update(ref_mode, transaction,\n-\t\t\t\t\t\t      decoration->name,\n-\t\t\t\t\t\t      &last_commit->object.oid,\n-\t\t\t\t\t\t      &commit->object.oid,\n-\t\t\t\t\t\t      reflog_msg.buf,\n-\t\t\t\t\t\t      &transaction_err) < 0) {\n-\t\t\t\t\tret = error(_(\"failed to update ref '%s': %s\"),\n-\t\t\t\t\t\t    decoration->name, transaction_err.buf);\n-\t\t\t\t\tgoto cleanup;\n-\t\t\t\t}\n-\t\t\t}\n-\t\t\tdecoration = decoration->next;\n-\t\t}\n-\t}\n-\n-\t/* In --advance mode, advance the target ref */\n-\tif (result.clean == 1 && advance_name) {\n-\t\tif (handle_ref_update(ref_mode, transaction, advance_name,\n-\t\t\t\t      &last_commit->object.oid,\n-\t\t\t\t      &onto->object.oid,\n-\t\t\t\t      reflog_msg.buf,\n-\t\t\t\t      &transaction_err) < 0) {\n+\tfor (size_t i = 0; i < result.updates_nr; i++) {\n+\t\tret = handle_ref_update(ref_mode, transaction, result.updates[i].refname,\n+\t\t\t\t\t&result.updates[i].new_oid, &result.updates[i].old_oid,\n+\t\t\t\t\treflog_msg.buf, &transaction_err);\n+\t\tif (ret) {\n \t\t\tret = error(_(\"failed to update ref '%s': %s\"),\n-\t\t\t\t    advance_name, transaction_err.buf);\n+\t\t\t\t    result.updates[i].refname, transaction_err.buf);\n \t\t\tgoto cleanup;\n \t\t}\n \t}\n \n \t/* Commit the ref transaction if we have one */\n-\tif (transaction && result.clean == 1) {\n+\tif (transaction) {\n \t\tif (ref_transaction_commit(transaction, &transaction_err)) {\n \t\t\tret = error(_(\"failed to commit ref transaction: %s\"),\n \t\t\t\t    transaction_err.buf);\n@@ -517,24 +581,19 @@ int cmd_replay(int argc,\n \t\t}\n \t}\n \n-\tmerge_finalize(&merge_opt, &result);\n-\tkh_destroy_oid_map(replayed_commits);\n-\tif (update_refs) {\n-\t\tstrset_clear(update_refs);\n-\t\tfree(update_refs);\n-\t}\n-\tret = result.clean;\n-\n cleanup:\n \tif (transaction)\n \t\tref_transaction_free(transaction);\n+\treplay_result_release(&result);\n \tstrbuf_release(&transaction_err);\n \tstrbuf_release(&reflog_msg);\n \trelease_revisions(&revs);\n-\tfree(advance_name);\n \n-\t/* Return */\n-\tif (ret < 0)\n-\t\texit(128);\n-\treturn ret ? 0 : 1;\n+\tif (ret) {\n+\t\tif (result.merge_conflict)\n+\t\t\treturn 1;\n+\t\treturn 128;\n+\t}\n+\n+\treturn 0;\n }\n\n-- \n2.52.0.590.g1f87b77810.dirty\n\n"},{"id":"533625","messageId":"20260112-b4-pks-history-builtin-v10-2-e3c6aa5b4cec@pks.im","threadId":"64783","inReplyTo":"20260112-b4-pks-history-builtin-v10-0-e3c6aa5b4cec@pks.im","subject":"[PATCH v10 2/8] builtin/replay: move core logic into \"libgit.a\"","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-01-12T14:15:14Z","receivedAt":"2026-01-12T14:17:10Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"Move the core logic used to replay commits into \"libgit.a\" so that it\ncan be easily reused by other commands. It will be used in a subsequent\ncommit where we're about to introduce a new git-history(1) command.\n\nNote that with this change we have no sign-comparison warnings anymore,\nand neither do we depend on `the_repository`.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n Makefile         |   1 +\n builtin/replay.c | 369 +------------------------------------------------------\n meson.build      |   1 +\n replay.c         | 355 ++++++++++++++++++++++++++++++++++++++++++++++++++++\n replay.h         |  64 ++++++++++\n 5 files changed, 422 insertions(+), 368 deletions(-)\n\ndiff --git a/Makefile b/Makefile\nindex b7eba509c6..1c64a5d2ae 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -1285,6 +1285,7 @@ LIB_OBJS += repack-geometry.o\n LIB_OBJS += repack-midx.o\n LIB_OBJS += repack-promisor.o\n LIB_OBJS += replace-object.o\n+LIB_OBJS += replay.o\n LIB_OBJS += repo-settings.o\n LIB_OBJS += repository.o\n LIB_OBJS += rerere.o\ndiff --git a/builtin/replay.c b/builtin/replay.c\nindex df0dc04d02..e27889d536 100644\n--- a/builtin/replay.c\n+++ b/builtin/replay.c\n@@ -2,389 +2,22 @@\n  * \"git replay\" builtin command\n  */\n \n-#define USE_THE_REPOSITORY_VARIABLE\n-#define DISABLE_SIGN_COMPARE_WARNINGS\n-\n #include \"git-compat-util.h\"\n \n #include \"builtin.h\"\n #include \"config.h\"\n-#include \"environment.h\"\n #include \"hex.h\"\n-#include \"lockfile.h\"\n-#include \"merge-ort.h\"\n #include \"object-name.h\"\n #include \"parse-options.h\"\n #include \"refs.h\"\n+#include \"replay.h\"\n #include \"revision.h\"\n-#include \"strmap.h\"\n-#include <oidset.h>\n-#include <tree.h>\n \n enum ref_action_mode {\n \tREF_ACTION_UPDATE,\n \tREF_ACTION_PRINT,\n };\n \n-static const char *short_commit_name(struct repository *repo,\n-\t\t\t\t     struct commit *commit)\n-{\n-\treturn repo_find_unique_abbrev(repo, &commit->object.oid,\n-\t\t\t\t       DEFAULT_ABBREV);\n-}\n-\n-static struct commit *peel_committish(struct repository *repo,\n-\t\t\t\t      const char *name,\n-\t\t\t\t      const char *mode)\n-{\n-\tstruct object *obj;\n-\tstruct object_id oid;\n-\n-\tif (repo_get_oid(repo, name, &oid))\n-\t\tdie(_(\"'%s' is not a valid commit-ish for %s\"), name, mode);\n-\tobj = parse_object_or_die(repo, &oid, name);\n-\treturn (struct commit *)repo_peel_to_type(repo, name, 0, obj,\n-\t\t\t\t\t\t  OBJ_COMMIT);\n-}\n-\n-static char *get_author(const char *message)\n-{\n-\tsize_t len;\n-\tconst char *a;\n-\n-\ta = find_commit_header(message, \"author\", &len);\n-\tif (a)\n-\t\treturn xmemdupz(a, len);\n-\n-\treturn NULL;\n-}\n-\n-static struct commit *create_commit(struct repository *repo,\n-\t\t\t\t    struct tree *tree,\n-\t\t\t\t    struct commit *based_on,\n-\t\t\t\t    struct commit *parent)\n-{\n-\tstruct object_id ret;\n-\tstruct object *obj = NULL;\n-\tstruct commit_list *parents = NULL;\n-\tchar *author;\n-\tchar *sign_commit = NULL; /* FIXME: cli users might want to sign again */\n-\tstruct commit_extra_header *extra = NULL;\n-\tstruct strbuf msg = STRBUF_INIT;\n-\tconst char *out_enc = get_commit_output_encoding();\n-\tconst char *message = repo_logmsg_reencode(repo, based_on,\n-\t\t\t\t\t\t   NULL, out_enc);\n-\tconst char *orig_message = NULL;\n-\tconst char *exclude_gpgsig[] = { \"gpgsig\", \"gpgsig-sha256\", NULL };\n-\n-\tcommit_list_insert(parent, &parents);\n-\textra = read_commit_extra_headers(based_on, exclude_gpgsig);\n-\tfind_commit_subject(message, &orig_message);\n-\tstrbuf_addstr(&msg, orig_message);\n-\tauthor = get_author(message);\n-\treset_ident_date();\n-\tif (commit_tree_extended(msg.buf, msg.len, &tree->object.oid, parents,\n-\t\t\t\t &ret, author, NULL, sign_commit, extra)) {\n-\t\terror(_(\"failed to write commit object\"));\n-\t\tgoto out;\n-\t}\n-\n-\tobj = parse_object(repo, &ret);\n-\n-out:\n-\trepo_unuse_commit_buffer(the_repository, based_on, message);\n-\tfree_commit_extra_headers(extra);\n-\tfree_commit_list(parents);\n-\tstrbuf_release(&msg);\n-\tfree(author);\n-\treturn (struct commit *)obj;\n-}\n-\n-struct ref_info {\n-\tstruct commit *onto;\n-\tstruct strset positive_refs;\n-\tstruct strset negative_refs;\n-\tint positive_refexprs;\n-\tint negative_refexprs;\n-};\n-\n-static void get_ref_information(struct repository *repo,\n-\t\t\t\tstruct rev_cmdline_info *cmd_info,\n-\t\t\t\tstruct ref_info *ref_info)\n-{\n-\tint i;\n-\n-\tref_info->onto = NULL;\n-\tstrset_init(&ref_info->positive_refs);\n-\tstrset_init(&ref_info->negative_refs);\n-\tref_info->positive_refexprs = 0;\n-\tref_info->negative_refexprs = 0;\n-\n-\t/*\n-\t * When the user specifies e.g.\n-\t *   git replay origin/main..mybranch\n-\t *   git replay ^origin/next mybranch1 mybranch2\n-\t * we want to be able to determine where to replay the commits.  In\n-\t * these examples, the branches are probably based on an old version\n-\t * of either origin/main or origin/next, so we want to replay on the\n-\t * newest version of that branch.  In contrast we would want to error\n-\t * out if they ran\n-\t *   git replay ^origin/master ^origin/next mybranch\n-\t *   git replay mybranch~2..mybranch\n-\t * the first of those because there's no unique base to choose, and\n-\t * the second because they'd likely just be replaying commits on top\n-\t * of the same commit and not making any difference.\n-\t */\n-\tfor (i = 0; i < cmd_info->nr; i++) {\n-\t\tstruct rev_cmdline_entry *e = cmd_info->rev + i;\n-\t\tstruct object_id oid;\n-\t\tconst char *refexpr = e->name;\n-\t\tchar *fullname = NULL;\n-\t\tint can_uniquely_dwim = 1;\n-\n-\t\tif (*refexpr == '^')\n-\t\t\trefexpr++;\n-\t\tif (repo_dwim_ref(repo, refexpr, strlen(refexpr), &oid, &fullname, 0) != 1)\n-\t\t\tcan_uniquely_dwim = 0;\n-\n-\t\tif (e->flags & BOTTOM) {\n-\t\t\tif (can_uniquely_dwim)\n-\t\t\t\tstrset_add(&ref_info->negative_refs, fullname);\n-\t\t\tif (!ref_info->negative_refexprs)\n-\t\t\t\tref_info->onto = lookup_commit_reference_gently(repo,\n-\t\t\t\t\t\t\t\t\t\t&e->item->oid, 1);\n-\t\t\tref_info->negative_refexprs++;\n-\t\t} else {\n-\t\t\tif (can_uniquely_dwim)\n-\t\t\t\tstrset_add(&ref_info->positive_refs, fullname);\n-\t\t\tref_info->positive_refexprs++;\n-\t\t}\n-\n-\t\tfree(fullname);\n-\t}\n-}\n-\n-static void set_up_replay_mode(struct repository *repo,\n-\t\t\t       struct rev_cmdline_info *cmd_info,\n-\t\t\t       const char *onto_name,\n-\t\t\t       char **advance_name,\n-\t\t\t       struct commit **onto,\n-\t\t\t       struct strset **update_refs)\n-{\n-\tstruct ref_info rinfo;\n-\n-\tget_ref_information(repo, cmd_info, &rinfo);\n-\tif (!rinfo.positive_refexprs)\n-\t\tdie(_(\"need some commits to replay\"));\n-\n-\tif (!(!!onto_name ^ !!*advance_name))\n-\t\tBUG(\"expected either onto_name or *advance_name in this function\");\n-\n-\tif (onto_name) {\n-\t\t*onto = peel_committish(repo, onto_name, \"--onto\");\n-\t\tif (rinfo.positive_refexprs <\n-\t\t    strset_get_size(&rinfo.positive_refs))\n-\t\t\tdie(_(\"all positive revisions given must be references\"));\n-\t\t*update_refs = xcalloc(1, sizeof(**update_refs));\n-\t\t**update_refs = rinfo.positive_refs;\n-\t\tmemset(&rinfo.positive_refs, 0, sizeof(**update_refs));\n-\t} else {\n-\t\tstruct object_id oid;\n-\t\tchar *fullname = NULL;\n-\n-\t\tif (!*advance_name)\n-\t\t\tBUG(\"expected either onto_name or *advance_name in this function\");\n-\n-\t\tif (repo_dwim_ref(repo, *advance_name, strlen(*advance_name),\n-\t\t\t     &oid, &fullname, 0) == 1) {\n-\t\t\tfree(*advance_name);\n-\t\t\t*advance_name = fullname;\n-\t\t} else {\n-\t\t\tdie(_(\"argument to --advance must be a reference\"));\n-\t\t}\n-\t\t*onto = peel_committish(repo, *advance_name, \"--advance\");\n-\t\tif (rinfo.positive_refexprs > 1)\n-\t\t\tdie(_(\"cannot advance target with multiple sources because ordering would be ill-defined\"));\n-\t}\n-\tstrset_clear(&rinfo.negative_refs);\n-\tstrset_clear(&rinfo.positive_refs);\n-}\n-\n-static struct commit *mapped_commit(kh_oid_map_t *replayed_commits,\n-\t\t\t\t    struct commit *commit,\n-\t\t\t\t    struct commit *fallback)\n-{\n-\tkhint_t pos = kh_get_oid_map(replayed_commits, commit->object.oid);\n-\tif (pos == kh_end(replayed_commits))\n-\t\treturn fallback;\n-\treturn kh_value(replayed_commits, pos);\n-}\n-\n-static struct commit *pick_regular_commit(struct repository *repo,\n-\t\t\t\t\t  struct commit *pickme,\n-\t\t\t\t\t  kh_oid_map_t *replayed_commits,\n-\t\t\t\t\t  struct commit *onto,\n-\t\t\t\t\t  struct merge_options *merge_opt,\n-\t\t\t\t\t  struct merge_result *result)\n-{\n-\tstruct commit *base, *replayed_base;\n-\tstruct tree *pickme_tree, *base_tree;\n-\n-\tbase = pickme->parents->item;\n-\treplayed_base = mapped_commit(replayed_commits, base, onto);\n-\n-\tresult->tree = repo_get_commit_tree(repo, replayed_base);\n-\tpickme_tree = repo_get_commit_tree(repo, pickme);\n-\tbase_tree = repo_get_commit_tree(repo, base);\n-\n-\tmerge_opt->branch1 = short_commit_name(repo, replayed_base);\n-\tmerge_opt->branch2 = short_commit_name(repo, pickme);\n-\tmerge_opt->ancestor = xstrfmt(\"parent of %s\", merge_opt->branch2);\n-\n-\tmerge_incore_nonrecursive(merge_opt,\n-\t\t\t\t  base_tree,\n-\t\t\t\t  result->tree,\n-\t\t\t\t  pickme_tree,\n-\t\t\t\t  result);\n-\n-\tfree((char*)merge_opt->ancestor);\n-\tmerge_opt->ancestor = NULL;\n-\tif (!result->clean)\n-\t\treturn NULL;\n-\treturn create_commit(repo, result->tree, pickme, replayed_base);\n-}\n-\n-struct replay_revisions_options {\n-\tconst char *advance;\n-\tconst char *onto;\n-\tint contained;\n-};\n-\n-struct replay_result {\n-\tstruct replay_ref_update {\n-\t\tchar *refname;\n-\t\tstruct object_id old_oid;\n-\t\tstruct object_id new_oid;\n-\t} *updates;\n-\tsize_t updates_nr, updates_alloc;\n-\n-\tbool merge_conflict;\n-};\n-\n-static void replay_result_release(struct replay_result *result)\n-{\n-\tfor (size_t i = 0; i < result->updates_nr; i++)\n-\t\tfree(result->updates[i].refname);\n-\tfree(result->updates);\n-}\n-\n-static void replay_result_queue_update(struct replay_result *result,\n-\t\t\t\t       const char *refname,\n-\t\t\t\t       const struct object_id *old_oid,\n-\t\t\t\t       const struct object_id *new_oid)\n-{\n-\tALLOC_GROW(result->updates, result->updates_nr + 1, result->updates_alloc);\n-\tresult->updates[result->updates_nr].refname = xstrdup(refname);\n-\tresult->updates[result->updates_nr].old_oid = *old_oid;\n-\tresult->updates[result->updates_nr].new_oid = *new_oid;\n-\tresult->updates_nr++;\n-}\n-\n-static int replay_revisions(struct rev_info *revs,\n-\t\t\t    struct replay_revisions_options *opts,\n-\t\t\t    struct replay_result *out)\n-{\n-\tkh_oid_map_t *replayed_commits = NULL;\n-\tstruct strset *update_refs = NULL;\n-\tstruct commit *last_commit = NULL;\n-\tstruct commit *commit;\n-\tstruct commit *onto = NULL;\n-\tstruct merge_options merge_opt;\n-\tstruct merge_result result;\n-\tchar *advance;\n-\tint ret;\n-\n-\tadvance = xstrdup_or_null(opts->advance);\n-\tset_up_replay_mode(revs->repo, &revs->cmdline, opts->onto, &advance,\n-\t\t\t   &onto, &update_refs);\n-\n-\t/* FIXME: Should allow replaying commits with the first as a root commit */\n-\n-\tif (prepare_revision_walk(revs) < 0) {\n-\t\tret = error(_(\"error preparing revisions\"));\n-\t\tgoto out;\n-\t}\n-\n-\tinit_basic_merge_options(&merge_opt, revs->repo);\n-\tmemset(&result, 0, sizeof(result));\n-\tmerge_opt.show_rename_progress = 0;\n-\tlast_commit = onto;\n-\treplayed_commits = kh_init_oid_map();\n-\twhile ((commit = get_revision(revs))) {\n-\t\tconst struct name_decoration *decoration;\n-\t\tkhint_t pos;\n-\t\tint hr;\n-\n-\t\tif (!commit->parents)\n-\t\t\tdie(_(\"replaying down from root commit is not supported yet!\"));\n-\t\tif (commit->parents->next)\n-\t\t\tdie(_(\"replaying merge commits is not supported yet!\"));\n-\n-\t\tlast_commit = pick_regular_commit(revs->repo, commit, replayed_commits,\n-\t\t\t\t\t\t  onto, &merge_opt, &result);\n-\t\tif (!last_commit)\n-\t\t\tbreak;\n-\n-\t\t/* Record commit -> last_commit mapping */\n-\t\tpos = kh_put_oid_map(replayed_commits, commit->object.oid, &hr);\n-\t\tif (hr == 0)\n-\t\t\tBUG(\"Duplicate rewritten commit: %s\\n\",\n-\t\t\t    oid_to_hex(&commit->object.oid));\n-\t\tkh_value(replayed_commits, pos) = last_commit;\n-\n-\t\t/* Update any necessary branches */\n-\t\tif (advance)\n-\t\t\tcontinue;\n-\t\tdecoration = get_name_decoration(&commit->object);\n-\t\tif (!decoration)\n-\t\t\tcontinue;\n-\t\twhile (decoration) {\n-\t\t\tif (decoration->type == DECORATION_REF_LOCAL &&\n-\t\t\t    (opts->contained || strset_contains(update_refs,\n-\t\t\t\t\t\t\t\tdecoration->name))) {\n-\t\t\t\treplay_result_queue_update(out, decoration->name,\n-\t\t\t\t\t\t\t   &commit->object.oid,\n-\t\t\t\t\t\t\t   &last_commit->object.oid);\n-\t\t\t}\n-\t\t\tdecoration = decoration->next;\n-\t\t}\n-\t}\n-\n-\tif (!result.clean) {\n-\t\tout->merge_conflict = true;\n-\t\tret = -1;\n-\t\tgoto out;\n-\t}\n-\n-\t/* In --advance mode, advance the target ref */\n-\tif (advance)\n-\t\treplay_result_queue_update(out, advance,\n-\t\t\t\t\t   &onto->object.oid,\n-\t\t\t\t\t   &last_commit->object.oid);\n-\n-\tret = 0;\n-\n-out:\n-\tif (update_refs) {\n-\t\tstrset_clear(update_refs);\n-\t\tfree(update_refs);\n-\t}\n-\tkh_destroy_oid_map(replayed_commits);\n-\tmerge_finalize(&merge_opt, &result);\n-\tfree(advance);\n-\treturn ret;\n-}\n-\n static enum ref_action_mode parse_ref_action_mode(const char *ref_action, const char *source)\n {\n \tif (!ref_action || !strcmp(ref_action, \"update\"))\ndiff --git a/meson.build b/meson.build\nindex dd52efd1c8..a5a4e99b25 100644\n--- a/meson.build\n+++ b/meson.build\n@@ -471,6 +471,7 @@ libgit_sources = [\n   'repack-midx.c',\n   'repack-promisor.c',\n   'replace-object.c',\n+  'replay.c',\n   'repo-settings.c',\n   'repository.c',\n   'rerere.c',\ndiff --git a/replay.c b/replay.c\nnew file mode 100644\nindex 0000000000..f7c61dd83f\n--- /dev/null\n+++ b/replay.c\n@@ -0,0 +1,355 @@\n+#define USE_THE_REPOSITORY_VARIABLE\n+#define DISABLE_SIGN_COMPARE_WARNINGS\n+\n+#include \"git-compat-util.h\"\n+#include \"environment.h\"\n+#include \"hex.h\"\n+#include \"merge-ort.h\"\n+#include \"object-name.h\"\n+#include \"refs.h\"\n+#include \"replay.h\"\n+#include \"revision.h\"\n+#include \"strmap.h\"\n+#include \"tree.h\"\n+\n+static const char *short_commit_name(struct repository *repo,\n+\t\t\t\t     struct commit *commit)\n+{\n+\treturn repo_find_unique_abbrev(repo, &commit->object.oid,\n+\t\t\t\t       DEFAULT_ABBREV);\n+}\n+\n+static struct commit *peel_committish(struct repository *repo,\n+\t\t\t\t      const char *name,\n+\t\t\t\t      const char *mode)\n+{\n+\tstruct object *obj;\n+\tstruct object_id oid;\n+\n+\tif (repo_get_oid(repo, name, &oid))\n+\t\tdie(_(\"'%s' is not a valid commit-ish for %s\"), name, mode);\n+\tobj = parse_object_or_die(repo, &oid, name);\n+\treturn (struct commit *)repo_peel_to_type(repo, name, 0, obj,\n+\t\t\t\t\t\t  OBJ_COMMIT);\n+}\n+\n+static char *get_author(const char *message)\n+{\n+\tsize_t len;\n+\tconst char *a;\n+\n+\ta = find_commit_header(message, \"author\", &len);\n+\tif (a)\n+\t\treturn xmemdupz(a, len);\n+\n+\treturn NULL;\n+}\n+\n+static struct commit *create_commit(struct repository *repo,\n+\t\t\t\t    struct tree *tree,\n+\t\t\t\t    struct commit *based_on,\n+\t\t\t\t    struct commit *parent)\n+{\n+\tstruct object_id ret;\n+\tstruct object *obj = NULL;\n+\tstruct commit_list *parents = NULL;\n+\tchar *author;\n+\tchar *sign_commit = NULL; /* FIXME: cli users might want to sign again */\n+\tstruct commit_extra_header *extra = NULL;\n+\tstruct strbuf msg = STRBUF_INIT;\n+\tconst char *out_enc = get_commit_output_encoding();\n+\tconst char *message = repo_logmsg_reencode(repo, based_on,\n+\t\t\t\t\t\t   NULL, out_enc);\n+\tconst char *orig_message = NULL;\n+\tconst char *exclude_gpgsig[] = { \"gpgsig\", \"gpgsig-sha256\", NULL };\n+\n+\tcommit_list_insert(parent, &parents);\n+\textra = read_commit_extra_headers(based_on, exclude_gpgsig);\n+\tfind_commit_subject(message, &orig_message);\n+\tstrbuf_addstr(&msg, orig_message);\n+\tauthor = get_author(message);\n+\treset_ident_date();\n+\tif (commit_tree_extended(msg.buf, msg.len, &tree->object.oid, parents,\n+\t\t\t\t &ret, author, NULL, sign_commit, extra)) {\n+\t\terror(_(\"failed to write commit object\"));\n+\t\tgoto out;\n+\t}\n+\n+\tobj = parse_object(repo, &ret);\n+\n+out:\n+\trepo_unuse_commit_buffer(the_repository, based_on, message);\n+\tfree_commit_extra_headers(extra);\n+\tfree_commit_list(parents);\n+\tstrbuf_release(&msg);\n+\tfree(author);\n+\treturn (struct commit *)obj;\n+}\n+\n+struct ref_info {\n+\tstruct commit *onto;\n+\tstruct strset positive_refs;\n+\tstruct strset negative_refs;\n+\tint positive_refexprs;\n+\tint negative_refexprs;\n+};\n+\n+static void get_ref_information(struct repository *repo,\n+\t\t\t\tstruct rev_cmdline_info *cmd_info,\n+\t\t\t\tstruct ref_info *ref_info)\n+{\n+\tint i;\n+\n+\tref_info->onto = NULL;\n+\tstrset_init(&ref_info->positive_refs);\n+\tstrset_init(&ref_info->negative_refs);\n+\tref_info->positive_refexprs = 0;\n+\tref_info->negative_refexprs = 0;\n+\n+\t/*\n+\t * When the user specifies e.g.\n+\t *   git replay origin/main..mybranch\n+\t *   git replay ^origin/next mybranch1 mybranch2\n+\t * we want to be able to determine where to replay the commits.  In\n+\t * these examples, the branches are probably based on an old version\n+\t * of either origin/main or origin/next, so we want to replay on the\n+\t * newest version of that branch.  In contrast we would want to error\n+\t * out if they ran\n+\t *   git replay ^origin/master ^origin/next mybranch\n+\t *   git replay mybranch~2..mybranch\n+\t * the first of those because there's no unique base to choose, and\n+\t * the second because they'd likely just be replaying commits on top\n+\t * of the same commit and not making any difference.\n+\t */\n+\tfor (i = 0; i < cmd_info->nr; i++) {\n+\t\tstruct rev_cmdline_entry *e = cmd_info->rev + i;\n+\t\tstruct object_id oid;\n+\t\tconst char *refexpr = e->name;\n+\t\tchar *fullname = NULL;\n+\t\tint can_uniquely_dwim = 1;\n+\n+\t\tif (*refexpr == '^')\n+\t\t\trefexpr++;\n+\t\tif (repo_dwim_ref(repo, refexpr, strlen(refexpr), &oid, &fullname, 0) != 1)\n+\t\t\tcan_uniquely_dwim = 0;\n+\n+\t\tif (e->flags & BOTTOM) {\n+\t\t\tif (can_uniquely_dwim)\n+\t\t\t\tstrset_add(&ref_info->negative_refs, fullname);\n+\t\t\tif (!ref_info->negative_refexprs)\n+\t\t\t\tref_info->onto = lookup_commit_reference_gently(repo,\n+\t\t\t\t\t\t\t\t\t\t&e->item->oid, 1);\n+\t\t\tref_info->negative_refexprs++;\n+\t\t} else {\n+\t\t\tif (can_uniquely_dwim)\n+\t\t\t\tstrset_add(&ref_info->positive_refs, fullname);\n+\t\t\tref_info->positive_refexprs++;\n+\t\t}\n+\n+\t\tfree(fullname);\n+\t}\n+}\n+\n+static void set_up_replay_mode(struct repository *repo,\n+\t\t\t       struct rev_cmdline_info *cmd_info,\n+\t\t\t       const char *onto_name,\n+\t\t\t       char **advance_name,\n+\t\t\t       struct commit **onto,\n+\t\t\t       struct strset **update_refs)\n+{\n+\tstruct ref_info rinfo;\n+\n+\tget_ref_information(repo, cmd_info, &rinfo);\n+\tif (!rinfo.positive_refexprs)\n+\t\tdie(_(\"need some commits to replay\"));\n+\n+\tif (!(!!onto_name ^ !!*advance_name))\n+\t\tBUG(\"expected either onto_name or *advance_name in this function\");\n+\n+\tif (onto_name) {\n+\t\t*onto = peel_committish(repo, onto_name, \"--onto\");\n+\t\tif (rinfo.positive_refexprs <\n+\t\t    strset_get_size(&rinfo.positive_refs))\n+\t\t\tdie(_(\"all positive revisions given must be references\"));\n+\t\t*update_refs = xcalloc(1, sizeof(**update_refs));\n+\t\t**update_refs = rinfo.positive_refs;\n+\t\tmemset(&rinfo.positive_refs, 0, sizeof(**update_refs));\n+\t} else {\n+\t\tstruct object_id oid;\n+\t\tchar *fullname = NULL;\n+\n+\t\tif (!*advance_name)\n+\t\t\tBUG(\"expected either onto_name or *advance_name in this function\");\n+\n+\t\tif (repo_dwim_ref(repo, *advance_name, strlen(*advance_name),\n+\t\t\t     &oid, &fullname, 0) == 1) {\n+\t\t\tfree(*advance_name);\n+\t\t\t*advance_name = fullname;\n+\t\t} else {\n+\t\t\tdie(_(\"argument to --advance must be a reference\"));\n+\t\t}\n+\t\t*onto = peel_committish(repo, *advance_name, \"--advance\");\n+\t\tif (rinfo.positive_refexprs > 1)\n+\t\t\tdie(_(\"cannot advance target with multiple sources because ordering would be ill-defined\"));\n+\t}\n+\tstrset_clear(&rinfo.negative_refs);\n+\tstrset_clear(&rinfo.positive_refs);\n+}\n+\n+static struct commit *mapped_commit(kh_oid_map_t *replayed_commits,\n+\t\t\t\t    struct commit *commit,\n+\t\t\t\t    struct commit *fallback)\n+{\n+\tkhint_t pos = kh_get_oid_map(replayed_commits, commit->object.oid);\n+\tif (pos == kh_end(replayed_commits))\n+\t\treturn fallback;\n+\treturn kh_value(replayed_commits, pos);\n+}\n+\n+static struct commit *pick_regular_commit(struct repository *repo,\n+\t\t\t\t\t  struct commit *pickme,\n+\t\t\t\t\t  kh_oid_map_t *replayed_commits,\n+\t\t\t\t\t  struct commit *onto,\n+\t\t\t\t\t  struct merge_options *merge_opt,\n+\t\t\t\t\t  struct merge_result *result)\n+{\n+\tstruct commit *base, *replayed_base;\n+\tstruct tree *pickme_tree, *base_tree;\n+\n+\tbase = pickme->parents->item;\n+\treplayed_base = mapped_commit(replayed_commits, base, onto);\n+\n+\tresult->tree = repo_get_commit_tree(repo, replayed_base);\n+\tpickme_tree = repo_get_commit_tree(repo, pickme);\n+\tbase_tree = repo_get_commit_tree(repo, base);\n+\n+\tmerge_opt->branch1 = short_commit_name(repo, replayed_base);\n+\tmerge_opt->branch2 = short_commit_name(repo, pickme);\n+\tmerge_opt->ancestor = xstrfmt(\"parent of %s\", merge_opt->branch2);\n+\n+\tmerge_incore_nonrecursive(merge_opt,\n+\t\t\t\t  base_tree,\n+\t\t\t\t  result->tree,\n+\t\t\t\t  pickme_tree,\n+\t\t\t\t  result);\n+\n+\tfree((char*)merge_opt->ancestor);\n+\tmerge_opt->ancestor = NULL;\n+\tif (!result->clean)\n+\t\treturn NULL;\n+\treturn create_commit(repo, result->tree, pickme, replayed_base);\n+}\n+\n+void replay_result_release(struct replay_result *result)\n+{\n+\tfor (size_t i = 0; i < result->updates_nr; i++)\n+\t\tfree(result->updates[i].refname);\n+\tfree(result->updates);\n+}\n+\n+static void replay_result_queue_update(struct replay_result *result,\n+\t\t\t\t       const char *refname,\n+\t\t\t\t       const struct object_id *old_oid,\n+\t\t\t\t       const struct object_id *new_oid)\n+{\n+\tALLOC_GROW(result->updates, result->updates_nr + 1, result->updates_alloc);\n+\tresult->updates[result->updates_nr].refname = xstrdup(refname);\n+\tresult->updates[result->updates_nr].old_oid = *old_oid;\n+\tresult->updates[result->updates_nr].new_oid = *new_oid;\n+\tresult->updates_nr++;\n+}\n+\n+int replay_revisions(struct rev_info *revs,\n+\t\t     struct replay_revisions_options *opts,\n+\t\t     struct replay_result *out)\n+{\n+\tkh_oid_map_t *replayed_commits = NULL;\n+\tstruct strset *update_refs = NULL;\n+\tstruct commit *last_commit = NULL;\n+\tstruct commit *commit;\n+\tstruct commit *onto = NULL;\n+\tstruct merge_options merge_opt;\n+\tstruct merge_result result;\n+\tchar *advance;\n+\tint ret;\n+\n+\tadvance = xstrdup_or_null(opts->advance);\n+\tset_up_replay_mode(revs->repo, &revs->cmdline, opts->onto, &advance,\n+\t\t\t   &onto, &update_refs);\n+\n+\t/* FIXME: Should allow replaying commits with the first as a root commit */\n+\n+\tif (prepare_revision_walk(revs) < 0) {\n+\t\tret = error(_(\"error preparing revisions\"));\n+\t\tgoto out;\n+\t}\n+\n+\tinit_basic_merge_options(&merge_opt, revs->repo);\n+\tmemset(&result, 0, sizeof(result));\n+\tmerge_opt.show_rename_progress = 0;\n+\tlast_commit = onto;\n+\treplayed_commits = kh_init_oid_map();\n+\twhile ((commit = get_revision(revs))) {\n+\t\tconst struct name_decoration *decoration;\n+\t\tkhint_t pos;\n+\t\tint hr;\n+\n+\t\tif (!commit->parents)\n+\t\t\tdie(_(\"replaying down from root commit is not supported yet!\"));\n+\t\tif (commit->parents->next)\n+\t\t\tdie(_(\"replaying merge commits is not supported yet!\"));\n+\n+\t\tlast_commit = pick_regular_commit(revs->repo, commit, replayed_commits,\n+\t\t\t\t\t\t  onto, &merge_opt, &result);\n+\t\tif (!last_commit)\n+\t\t\tbreak;\n+\n+\t\t/* Record commit -> last_commit mapping */\n+\t\tpos = kh_put_oid_map(replayed_commits, commit->object.oid, &hr);\n+\t\tif (hr == 0)\n+\t\t\tBUG(\"Duplicate rewritten commit: %s\\n\",\n+\t\t\t    oid_to_hex(&commit->object.oid));\n+\t\tkh_value(replayed_commits, pos) = last_commit;\n+\n+\t\t/* Update any necessary branches */\n+\t\tif (advance)\n+\t\t\tcontinue;\n+\t\tdecoration = get_name_decoration(&commit->object);\n+\t\tif (!decoration)\n+\t\t\tcontinue;\n+\t\twhile (decoration) {\n+\t\t\tif (decoration->type == DECORATION_REF_LOCAL &&\n+\t\t\t    (opts->contained || strset_contains(update_refs,\n+\t\t\t\t\t\t\t\tdecoration->name))) {\n+\t\t\t\treplay_result_queue_update(out, decoration->name,\n+\t\t\t\t\t\t\t   &commit->object.oid,\n+\t\t\t\t\t\t\t   &last_commit->object.oid);\n+\t\t\t}\n+\t\t\tdecoration = decoration->next;\n+\t\t}\n+\t}\n+\n+\tif (!result.clean) {\n+\t\tout->merge_conflict = true;\n+\t\tret = -1;\n+\t\tgoto out;\n+\t}\n+\n+\t/* In --advance mode, advance the target ref */\n+\tif (advance)\n+\t\treplay_result_queue_update(out, advance,\n+\t\t\t\t\t   &onto->object.oid,\n+\t\t\t\t\t   &last_commit->object.oid);\n+\n+\tret = 0;\n+\n+out:\n+\tif (update_refs) {\n+\t\tstrset_clear(update_refs);\n+\t\tfree(update_refs);\n+\t}\n+\tkh_destroy_oid_map(replayed_commits);\n+\tmerge_finalize(&merge_opt, &result);\n+\tfree(advance);\n+\treturn ret;\n+}\ndiff --git a/replay.h b/replay.h\nnew file mode 100644\nindex 0000000000..09e0a4e640\n--- /dev/null\n+++ b/replay.h\n@@ -0,0 +1,64 @@\n+#ifndef REPLAY_H\n+#define REPLAY_H\n+\n+#include \"hash.h\"\n+\n+struct repository;\n+struct rev_info;\n+\n+/*\n+ * A set of options that can be passed to `replay_revisions()`.\n+ */\n+struct replay_revisions_options {\n+\t/*\n+\t * Starting point at which to create the new commits; must be a branch\n+\t * name. The branch will be updated to point to the rewritten commits.\n+\t * This option is mutually exclusive with `onto`.\n+\t */\n+\tconst char *advance;\n+\n+\t/*\n+\t * Starting point at which to create the new commits; must be a\n+\t * committish. References pointing at decendants of `onto` will be\n+\t * updated to point to the new commits.\n+\t */\n+\t const char *onto;\n+\n+\t/*\n+\t * Update branches that point at commits in the given revision range.\n+\t * Requires `onto` to be set.\n+\t */\n+\tint contained;\n+};\n+\n+/* This struct is used as an out-parameter by `replay_revisions()`. */\n+struct replay_result {\n+\t/*\n+\t * The set of reference updates that are caused by replaying the\n+\t * commits.\n+\t */\n+\tstruct replay_ref_update {\n+\t\tchar *refname;\n+\t\tstruct object_id old_oid;\n+\t\tstruct object_id new_oid;\n+\t} *updates;\n+\tsize_t updates_nr, updates_alloc;\n+\n+\t/* Set to true in case the replay failed with a merge conflict. */\n+\tbool merge_conflict;\n+};\n+\n+void replay_result_release(struct replay_result *result);\n+\n+/*\n+ * Replay a set of commits onto a new location. Leaves both the working tree,\n+ * index and references untouched. Reference updates caused by the replay will\n+ * be recorded in the `updates` out pointer.\n+ *\n+ * Returns 0 on success, a negative error code otherwise.\n+ */\n+int replay_revisions(struct rev_info *revs,\n+\t\t     struct replay_revisions_options *opts,\n+\t\t     struct replay_result *out);\n+\n+#endif\n\n-- \n2.52.0.590.g1f87b77810.dirty\n\n"},{"id":"533624","messageId":"20260112-b4-pks-history-builtin-v10-3-e3c6aa5b4cec@pks.im","threadId":"64783","inReplyTo":"20260112-b4-pks-history-builtin-v10-0-e3c6aa5b4cec@pks.im","subject":"[PATCH v10 3/8] replay: small set of cleanups","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-01-12T14:15:15Z","receivedAt":"2026-01-12T14:17:13Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"Perform a small set of cleanups so that the \"replay\" logic compiles with\n\"-Wsign-compare\" and doesn't use `the_repository` anymore. Note that\nthere are still some implicit dependencies on `the_repository`, e.g.\nbecause we use `get_commit_output_encoding()`.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n replay.c | 11 ++++-------\n 1 file changed, 4 insertions(+), 7 deletions(-)\n\ndiff --git a/replay.c b/replay.c\nindex f7c61dd83f..1e660171d2 100644\n--- a/replay.c\n+++ b/replay.c\n@@ -1,5 +1,4 @@\n #define USE_THE_REPOSITORY_VARIABLE\n-#define DISABLE_SIGN_COMPARE_WARNINGS\n \n #include \"git-compat-util.h\"\n #include \"environment.h\"\n@@ -78,7 +77,7 @@ static struct commit *create_commit(struct repository *repo,\n \tobj = parse_object(repo, &ret);\n \n out:\n-\trepo_unuse_commit_buffer(the_repository, based_on, message);\n+\trepo_unuse_commit_buffer(repo, based_on, message);\n \tfree_commit_extra_headers(extra);\n \tfree_commit_list(parents);\n \tstrbuf_release(&msg);\n@@ -90,16 +89,14 @@ struct ref_info {\n \tstruct commit *onto;\n \tstruct strset positive_refs;\n \tstruct strset negative_refs;\n-\tint positive_refexprs;\n-\tint negative_refexprs;\n+\tsize_t positive_refexprs;\n+\tsize_t negative_refexprs;\n };\n \n static void get_ref_information(struct repository *repo,\n \t\t\t\tstruct rev_cmdline_info *cmd_info,\n \t\t\t\tstruct ref_info *ref_info)\n {\n-\tint i;\n-\n \tref_info->onto = NULL;\n \tstrset_init(&ref_info->positive_refs);\n \tstrset_init(&ref_info->negative_refs);\n@@ -121,7 +118,7 @@ static void get_ref_information(struct repository *repo,\n \t * the second because they'd likely just be replaying commits on top\n \t * of the same commit and not making any difference.\n \t */\n-\tfor (i = 0; i < cmd_info->nr; i++) {\n+\tfor (size_t i = 0; i < cmd_info->nr; i++) {\n \t\tstruct rev_cmdline_entry *e = cmd_info->rev + i;\n \t\tstruct object_id oid;\n \t\tconst char *refexpr = e->name;\n\n-- \n2.52.0.590.g1f87b77810.dirty\n\n"},{"id":"533626","messageId":"20260112-b4-pks-history-builtin-v10-4-e3c6aa5b4cec@pks.im","threadId":"64783","inReplyTo":"20260112-b4-pks-history-builtin-v10-0-e3c6aa5b4cec@pks.im","subject":"[PATCH v10 4/8] replay: support empty commit ranges","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-01-12T14:15:16Z","receivedAt":"2026-01-12T14:17:16Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"In a subsequent commit we're about to introduce a new user of the replay\nsubsystem. With that new user, the range of commits that we'll want to\nreplay will be identified implicitly via \"HEAD\". With such implicit\nranges it becomes likely that the range of revisions that we're asked to\nreplay becomes empty. This case does not make sense with git-replay(1),\nbut with the new command it will.\n\nThis case is not currently supported by `replay_revisions()` though\nbecause we zero-initialize `struct merge_result`. This includes its\n`.clean` member, which indicates whether the merge ran into a conflict\nor not. But given that we don't have any revision to replay, we won't\never perform any merge at all, and consequently that member will never\nbe set to `1`. We thus later think that there's been a merge conflict\nand return an error from `replay_commits()`.\n\nAddress this issue by initializing the `.clean` member to `1`.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n replay.c | 5 +++--\n 1 file changed, 3 insertions(+), 2 deletions(-)\n\ndiff --git a/replay.c b/replay.c\nindex 1e660171d2..a8e6d5b30b 100644\n--- a/replay.c\n+++ b/replay.c\n@@ -266,7 +266,9 @@ int replay_revisions(struct rev_info *revs,\n \tstruct commit *commit;\n \tstruct commit *onto = NULL;\n \tstruct merge_options merge_opt;\n-\tstruct merge_result result;\n+\tstruct merge_result result = {\n+\t\t.clean = 1,\n+\t};\n \tchar *advance;\n \tint ret;\n \n@@ -282,7 +284,6 @@ int replay_revisions(struct rev_info *revs,\n \t}\n \n \tinit_basic_merge_options(&merge_opt, revs->repo);\n-\tmemset(&result, 0, sizeof(result));\n \tmerge_opt.show_rename_progress = 0;\n \tlast_commit = onto;\n \treplayed_commits = kh_init_oid_map();\n\n-- \n2.52.0.590.g1f87b77810.dirty\n\n"},{"id":"533628","messageId":"20260112-b4-pks-history-builtin-v10-5-e3c6aa5b4cec@pks.im","threadId":"64783","inReplyTo":"20260112-b4-pks-history-builtin-v10-0-e3c6aa5b4cec@pks.im","subject":"[PATCH v10 5/8] replay: support updating detached HEAD","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-01-12T14:15:17Z","receivedAt":"2026-01-12T14:17:18Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"In a subsequent commit we're about to introduce a new git-history(1)\ncommand, which will by default work on all local branches and HEAD. This\nis already well-supported by the replay machinery for most of the part:\nupdating branches is one of its prime use cases, and the HEAD ref is\nalso updated in case it points to any of the branches.\n\nHowever, what's not supported yet is to update HEAD in case it is not a\nsymbolic ref. We determine the refs that need to be updated by iterating\nthrough the decorations of the original commit, but we only update those\nrefs that are `DECORATION_REF_LOCAL`, which covers local branches.\n\nAddress this gap by also handling `DECORATION_REF_HEAD`. Note though\nthat this needs to only happen in case we're working on a detached HEAD.\nIf HEAD is pointing to a branch, then we'd already update that branch\nvia `DECORATION_REF_LOCAL`.\n\nRefactor the loop that iterates through the decorations a bit to make\nthe individual conditions easier to understand.\n\nBased-on-patch-by: Elijah Newren <newren@gmail.com>\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n replay.c                 | 47 +++++++++++++++++++++++++++++++++--------------\n t/t3650-replay-basics.sh |  9 +++++++++\n 2 files changed, 42 insertions(+), 14 deletions(-)\n\ndiff --git a/replay.c b/replay.c\nindex a8e6d5b30b..6a8cd2f72d 100644\n--- a/replay.c\n+++ b/replay.c\n@@ -150,11 +150,17 @@ static void get_ref_information(struct repository *repo,\n static void set_up_replay_mode(struct repository *repo,\n \t\t\t       struct rev_cmdline_info *cmd_info,\n \t\t\t       const char *onto_name,\n+\t\t\t       bool *detached_head,\n \t\t\t       char **advance_name,\n \t\t\t       struct commit **onto,\n \t\t\t       struct strset **update_refs)\n {\n \tstruct ref_info rinfo;\n+\tint head_flags = 0;\n+\n+\trefs_read_ref_full(get_main_ref_store(repo), \"HEAD\",\n+\t\t\t   RESOLVE_REF_NO_RECURSE, NULL, &head_flags);\n+\t*detached_head = !(head_flags & REF_ISSYMREF);\n \n \tget_ref_information(repo, cmd_info, &rinfo);\n \tif (!rinfo.positive_refexprs)\n@@ -269,12 +275,13 @@ int replay_revisions(struct rev_info *revs,\n \tstruct merge_result result = {\n \t\t.clean = 1,\n \t};\n+\tbool detached_head;\n \tchar *advance;\n \tint ret;\n \n \tadvance = xstrdup_or_null(opts->advance);\n-\tset_up_replay_mode(revs->repo, &revs->cmdline, opts->onto, &advance,\n-\t\t\t   &onto, &update_refs);\n+\tset_up_replay_mode(revs->repo, &revs->cmdline, opts->onto,\n+\t\t\t   &detached_head, &advance, &onto, &update_refs);\n \n \t/* FIXME: Should allow replaying commits with the first as a root commit */\n \n@@ -312,18 +319,30 @@ int replay_revisions(struct rev_info *revs,\n \t\t/* Update any necessary branches */\n \t\tif (advance)\n \t\t\tcontinue;\n-\t\tdecoration = get_name_decoration(&commit->object);\n-\t\tif (!decoration)\n-\t\t\tcontinue;\n-\t\twhile (decoration) {\n-\t\t\tif (decoration->type == DECORATION_REF_LOCAL &&\n-\t\t\t    (opts->contained || strset_contains(update_refs,\n-\t\t\t\t\t\t\t\tdecoration->name))) {\n-\t\t\t\treplay_result_queue_update(out, decoration->name,\n-\t\t\t\t\t\t\t   &commit->object.oid,\n-\t\t\t\t\t\t\t   &last_commit->object.oid);\n-\t\t\t}\n-\t\t\tdecoration = decoration->next;\n+\n+\t\tfor (decoration = get_name_decoration(&commit->object);\n+\t\t     decoration;\n+\t\t     decoration = decoration->next)\n+\t\t{\n+\t\t\tif (decoration->type != DECORATION_REF_LOCAL &&\n+\t\t\t    decoration->type != DECORATION_REF_HEAD)\n+\t\t\t\tcontinue;\n+\n+\t\t\t/*\n+\t\t\t * We only need to update HEAD separately in case it's\n+\t\t\t * detached. If it's not we'd already update the branch\n+\t\t\t * it is pointing to.\n+\t\t\t */\n+\t\t\tif (decoration->type == DECORATION_REF_HEAD && !detached_head)\n+\t\t\t\tcontinue;\n+\n+\t\t\tif (!opts->contained &&\n+\t\t\t    !strset_contains(update_refs, decoration->name))\n+\t\t\t\tcontinue;\n+\n+\t\t\treplay_result_queue_update(out, decoration->name,\n+\t\t\t\t\t\t   &commit->object.oid,\n+\t\t\t\t\t\t   &last_commit->object.oid);\n \t\t}\n \t}\n \ndiff --git a/t/t3650-replay-basics.sh b/t/t3650-replay-basics.sh\nindex 307101eeb9..c862aa39f3 100755\n--- a/t/t3650-replay-basics.sh\n+++ b/t/t3650-replay-basics.sh\n@@ -249,6 +249,15 @@ test_expect_success 'using replay on bare repo to rebase multiple divergent bran\n \tdone\n '\n \n+test_expect_success 'using replay to update detached HEAD' '\n+\tcurrent_head=$(git branch --show-current) &&\n+\ttest_when_finished git switch \"$current_head\" &&\n+\tgit switch --detach &&\n+\ttest_commit something &&\n+\tgit replay --ref-action=print --onto HEAD~2 --ref-action=print HEAD~..HEAD >updates &&\n+\ttest_grep \"update HEAD \" updates\n+'\n+\n test_expect_success 'merge.directoryRenames=false' '\n \t# create a test case that stress-tests the rename caching\n \tgit switch -c rename-onto &&\n\n-- \n2.52.0.590.g1f87b77810.dirty\n\n"},{"id":"533627","messageId":"20260112-b4-pks-history-builtin-v10-6-e3c6aa5b4cec@pks.im","threadId":"64783","inReplyTo":"20260112-b4-pks-history-builtin-v10-0-e3c6aa5b4cec@pks.im","subject":"[PATCH v10 6/8] wt-status: provide function to expose status for trees","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-01-12T14:15:18Z","receivedAt":"2026-01-12T14:17:22Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"The \"wt-status\" subsystem is responsible for printing status information\naround the current state of the working tree. This most importantly\nincludes information around whether the working tree or the index have\nany changes.\n\nWe're about to introduce a new command where the changes in neither of\nthem are actually relevant to us. Instead, what we want is to format the\nchanges between two different trees. While it is a little bit of a\nstretch to add this as functionality to _working tree_ status, it\ndoesn't make any sense to open-code this functionality, either.\n\nImplement a new function `wt_status_collect_changes_trees()` that diffs\ntwo trees and formats the status accordingly. This function is not yet\nused, but will be in a subsequent commit.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n wt-status.c | 24 ++++++++++++++++++++++++\n wt-status.h |  9 +++++++++\n 2 files changed, 33 insertions(+)\n\ndiff --git a/wt-status.c b/wt-status.c\nindex e12adb26b9..95942399f8 100644\n--- a/wt-status.c\n+++ b/wt-status.c\n@@ -612,6 +612,30 @@ static void wt_status_collect_updated_cb(struct diff_queue_struct *q,\n \t}\n }\n \n+void wt_status_collect_changes_trees(struct wt_status *s,\n+\t\t\t\t     const struct object_id *old_treeish,\n+\t\t\t\t     const struct object_id *new_treeish)\n+{\n+\tstruct diff_options opts = { 0 };\n+\n+\trepo_diff_setup(s->repo, &opts);\n+\topts.output_format = DIFF_FORMAT_CALLBACK;\n+\topts.format_callback = wt_status_collect_updated_cb;\n+\topts.format_callback_data = s;\n+\topts.detect_rename = s->detect_rename >= 0 ? s->detect_rename : opts.detect_rename;\n+\topts.rename_limit = s->rename_limit >= 0 ? s->rename_limit : opts.rename_limit;\n+\topts.rename_score = s->rename_score >= 0 ? s->rename_score : opts.rename_score;\n+\topts.flags.recursive = 1;\n+\tdiff_setup_done(&opts);\n+\n+\tdiff_tree_oid(old_treeish, new_treeish, \"\", &opts);\n+\tdiffcore_std(&opts);\n+\tdiff_flush(&opts);\n+\twt_status_get_state(s->repo, &s->state, 0);\n+\n+\tdiff_free(&opts);\n+}\n+\n static void wt_status_collect_changes_worktree(struct wt_status *s)\n {\n \tstruct rev_info rev;\ndiff --git a/wt-status.h b/wt-status.h\nindex e40a27214a..e9fe32e98c 100644\n--- a/wt-status.h\n+++ b/wt-status.h\n@@ -153,6 +153,15 @@ void wt_status_add_cut_line(struct wt_status *s);\n void wt_status_prepare(struct repository *r, struct wt_status *s);\n void wt_status_print(struct wt_status *s);\n void wt_status_collect(struct wt_status *s);\n+\n+/*\n+ * Collect all changes between the two trees. Changes will be displayed as if\n+ * they were staged into the index.\n+ */\n+void wt_status_collect_changes_trees(struct wt_status *s,\n+\t\t\t\t     const struct object_id *old_treeish,\n+\t\t\t\t     const struct object_id *new_treeish);\n+\n /*\n  * Frees the buffers allocated by wt_status_collect.\n  */\n\n-- \n2.52.0.590.g1f87b77810.dirty\n\n"},{"id":"533629","messageId":"20260112-b4-pks-history-builtin-v10-7-e3c6aa5b4cec@pks.im","threadId":"64783","inReplyTo":"20260112-b4-pks-history-builtin-v10-0-e3c6aa5b4cec@pks.im","subject":"[PATCH v10 7/8] builtin: add new \"history\" command","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-01-12T14:15:19Z","receivedAt":"2026-01-12T14:17:29Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"When rewriting history via git-rebase(1) there are a few very common use\ncases:\n\n  - The ordering of two commits should be reversed.\n\n  - A commit should be split up into two commits.\n\n  - A commit should be dropped from the history completely.\n\n  - Multiple commits should be squashed into one.\n\n  - Editing an existing commit that is not the tip of the current\n    branch.\n\nWhile these operations are all doable, it often feels needlessly kludgey\nto do so by doing an interactive rebase, using the editor to say what\none wants, and then perform the actions. Also, some operations like\nsplitting up a commit into two are way more involved than that and\nrequire a whole series of commands.\n\nRebases also do not update dependent branches. The use of stacked\nbranches has grown quite common with competing version control systems\nlike Jujutsu though, so it clearly is a need that users have. While\nrebases _can_ serve this use case if one always works on the latest\nstacked branch, it is somewhat awkward and very easy to get wrong.\n\nAdd a new \"history\" command to plug these gaps. This command will have\nseveral different subcommands to imperatively rewrite history for common\nuse cases like the above.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n .gitignore                     |  1 +\n Documentation/git-history.adoc | 57 ++++++++++++++++++++++++++++++++++++++++++\n Documentation/meson.build      |  1 +\n Makefile                       |  1 +\n builtin.h                      |  1 +\n builtin/history.c              | 22 ++++++++++++++++\n command-list.txt               |  1 +\n git.c                          |  1 +\n meson.build                    |  1 +\n t/meson.build                  |  1 +\n t/t3450-history.sh             | 17 +++++++++++++\n 11 files changed, 104 insertions(+)\n\ndiff --git a/.gitignore b/.gitignore\nindex 78a45cb5be..24635cf2d6 100644\n--- a/.gitignore\n+++ b/.gitignore\n@@ -79,6 +79,7 @@\n /git-grep\n /git-hash-object\n /git-help\n+/git-history\n /git-hook\n /git-http-backend\n /git-http-fetch\ndiff --git a/Documentation/git-history.adoc b/Documentation/git-history.adoc\nnew file mode 100644\nindex 0000000000..68c35f36b9\n--- /dev/null\n+++ b/Documentation/git-history.adoc\n@@ -0,0 +1,57 @@\n+git-history(1)\n+==============\n+\n+NAME\n+----\n+git-history - EXPERIMENTAL: Rewrite history\n+\n+SYNOPSIS\n+--------\n+[synopsis]\n+git history [<options>]\n+\n+DESCRIPTION\n+-----------\n+\n+Rewrite history by rearranging or modifying specific commits in the\n+history.\n+\n+THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.\n+\n+This command is related to linkgit:git-rebase[1] in that both commands can be\n+used to rewrite history. There are a couple of major differences though:\n+\n+* linkgit:git-history[1] can work in a bare repository as it does not need to\n+  touch either the index or the worktree.\n+* linkgit:git-history[1] does not execute any linkgit:githooks[5] at the\n+  current point in time. This may change in the future.\n+* linkgit:git-history[1] by default updates all branches that are descendants\n+  of the original commit to point to the rewritten commit.\n+\n+Overall, linkgit:git-history[1] aims to provide a more opinionated way to modify\n+your commit history that is simpler to use compared to linkgit:git-rebase[1] in\n+general.\n+\n+Use linkgit:git-rebase[1] if you want to reapply a range of commits onto a\n+different base, or interactive rebases if you want to edit a range of commits\n+at once.\n+\n+LIMITATIONS\n+-----------\n+\n+This command does not (yet) work with histories that contain merges. You\n+should use linkgit:git-rebase[1] with the `--rebase-merges` flag instead.\n+\n+Furthermore, the command does not support operations that can result in merge\n+conflicts. This limitation is by design as history rewrites are not intended to\n+be stateful operations. The limitation can be lifted once (if) Git learns about\n+first-class conflicts.\n+\n+COMMANDS\n+--------\n+\n+No commands are supported yet.\n+\n+GIT\n+---\n+Part of the linkgit:git[1] suite\ndiff --git a/Documentation/meson.build b/Documentation/meson.build\nindex f02dbc20cb..fd2e8cc02d 100644\n--- a/Documentation/meson.build\n+++ b/Documentation/meson.build\n@@ -64,6 +64,7 @@ manpages = {\n   'git-gui.adoc' : 1,\n   'git-hash-object.adoc' : 1,\n   'git-help.adoc' : 1,\n+  'git-history.adoc' : 1,\n   'git-hook.adoc' : 1,\n   'git-http-backend.adoc' : 1,\n   'git-http-fetch.adoc' : 1,\ndiff --git a/Makefile b/Makefile\nindex 1c64a5d2ae..c0569ed8e4 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -1418,6 +1418,7 @@ BUILTIN_OBJS += builtin/get-tar-commit-id.o\n BUILTIN_OBJS += builtin/grep.o\n BUILTIN_OBJS += builtin/hash-object.o\n BUILTIN_OBJS += builtin/help.o\n+BUILTIN_OBJS += builtin/history.o\n BUILTIN_OBJS += builtin/hook.o\n BUILTIN_OBJS += builtin/index-pack.o\n BUILTIN_OBJS += builtin/init-db.o\ndiff --git a/builtin.h b/builtin.h\nindex 1b35565fbd..93c91d07d4 100644\n--- a/builtin.h\n+++ b/builtin.h\n@@ -172,6 +172,7 @@ int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix, struc\n int cmd_grep(int argc, const char **argv, const char *prefix, struct repository *repo);\n int cmd_hash_object(int argc, const char **argv, const char *prefix, struct repository *repo);\n int cmd_help(int argc, const char **argv, const char *prefix, struct repository *repo);\n+int cmd_history(int argc, const char **argv, const char *prefix, struct repository *repo);\n int cmd_hook(int argc, const char **argv, const char *prefix, struct repository *repo);\n int cmd_index_pack(int argc, const char **argv, const char *prefix, struct repository *repo);\n int cmd_init_db(int argc, const char **argv, const char *prefix, struct repository *repo);\ndiff --git a/builtin/history.c b/builtin/history.c\nnew file mode 100644\nindex 0000000000..f6fe32610b\n--- /dev/null\n+++ b/builtin/history.c\n@@ -0,0 +1,22 @@\n+#include \"builtin.h\"\n+#include \"gettext.h\"\n+#include \"parse-options.h\"\n+\n+int cmd_history(int argc,\n+\t\tconst char **argv,\n+\t\tconst char *prefix,\n+\t\tstruct repository *repo UNUSED)\n+{\n+\tconst char * const usage[] = {\n+\t\tN_(\"git history [<options>]\"),\n+\t\tNULL,\n+\t};\n+\tstruct option options[] = {\n+\t\tOPT_END(),\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options, usage, 0);\n+\tif (argc)\n+\t\tusagef(\"unrecognized argument: %s\", argv[0]);\n+\treturn 0;\n+}\ndiff --git a/command-list.txt b/command-list.txt\nindex accd3d0c4b..f9005cf459 100644\n--- a/command-list.txt\n+++ b/command-list.txt\n@@ -115,6 +115,7 @@ git-grep                                mainporcelain           info\n git-gui                                 mainporcelain\n git-hash-object                         plumbingmanipulators\n git-help                                ancillaryinterrogators          complete\n+git-history                             mainporcelain           history\n git-hook                                purehelpers\n git-http-backend                        synchingrepositories\n git-http-fetch                          synchelpers\ndiff --git a/git.c b/git.c\nindex c5fad56813..744cb6527e 100644\n--- a/git.c\n+++ b/git.c\n@@ -586,6 +586,7 @@ static struct cmd_struct commands[] = {\n \t{ \"grep\", cmd_grep, RUN_SETUP_GENTLY },\n \t{ \"hash-object\", cmd_hash_object },\n \t{ \"help\", cmd_help },\n+\t{ \"history\", cmd_history, RUN_SETUP },\n \t{ \"hook\", cmd_hook, RUN_SETUP },\n \t{ \"index-pack\", cmd_index_pack, RUN_SETUP_GENTLY | NO_PARSEOPT },\n \t{ \"init\", cmd_init_db },\ndiff --git a/meson.build b/meson.build\nindex a5a4e99b25..3a1d12caa4 100644\n--- a/meson.build\n+++ b/meson.build\n@@ -610,6 +610,7 @@ builtin_sources = [\n   'builtin/grep.c',\n   'builtin/hash-object.c',\n   'builtin/help.c',\n+  'builtin/history.c',\n   'builtin/hook.c',\n   'builtin/index-pack.c',\n   'builtin/init-db.c',\ndiff --git a/t/meson.build b/t/meson.build\nindex 459c52a489..73006b095a 100644\n--- a/t/meson.build\n+++ b/t/meson.build\n@@ -387,6 +387,7 @@ integration_tests = [\n   't3436-rebase-more-options.sh',\n   't3437-rebase-fixup-options.sh',\n   't3438-rebase-broken-files.sh',\n+  't3450-history.sh',\n   't3500-cherry.sh',\n   't3501-revert-cherry-pick.sh',\n   't3502-cherry-pick-merge.sh',\ndiff --git a/t/t3450-history.sh b/t/t3450-history.sh\nnew file mode 100755\nindex 0000000000..417c343d43\n--- /dev/null\n+++ b/t/t3450-history.sh\n@@ -0,0 +1,17 @@\n+#!/bin/sh\n+\n+test_description='tests for git-history command'\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'does nothing without any arguments' '\n+\tgit history >out 2>&1 &&\n+\ttest_must_be_empty out\n+'\n+\n+test_expect_success 'raises an error with unknown argument' '\n+\ttest_must_fail git history garbage 2>err &&\n+\ttest_grep \"unrecognized argument: garbage\" err\n+'\n+\n+test_done\n\n-- \n2.52.0.590.g1f87b77810.dirty\n\n"},{"id":"533630","messageId":"20260112-b4-pks-history-builtin-v10-8-e3c6aa5b4cec@pks.im","threadId":"64783","inReplyTo":"20260112-b4-pks-history-builtin-v10-0-e3c6aa5b4cec@pks.im","subject":"[PATCH v10 8/8] builtin/history: implement \"reword\" subcommand","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-01-12T14:15:20Z","receivedAt":"2026-01-12T14:17:30Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"Implement a new \"reword\" subcommand for git-history(1). This subcommand\nis similar to the user performing an interactive rebase with a single\ncommit changed to use the \"reword\" instruction.\n\nThe \"reword\" subcommand is built on top of the replay subsystem\ninstead of the sequencer. This leads to some major differences compared\nto git-rebase(1):\n\n  - We do not check out the commit that is to be reworded and instead\n    perform the operation in-memory. This has the obvious benefit of\n    being significantly faster compared to git-rebase(1), but even more\n    importantly it allows the user to rewrite history even if there are\n    local changes in the working tree or in the index.\n\n  - We do not execute any hooks, even though we leave some room for\n    changing this in the future.\n\n  - By default, all local branches that contain the commit will be\n    rewritten. This especially helps with workflows that use stacked\n    branches.\n\nHelped-by: Elijah Newren <newren@gmail.com>\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n Documentation/git-history.adoc |  20 +-\n builtin/history.c              | 415 ++++++++++++++++++++++++++++++++++++++++-\n t/meson.build                  |   1 +\n t/t3450-history.sh             |   6 +-\n t/t3451-history-reword.sh      | 391 ++++++++++++++++++++++++++++++++++++++\n 5 files changed, 823 insertions(+), 10 deletions(-)\n\ndiff --git a/Documentation/git-history.adoc b/Documentation/git-history.adoc\nindex 68c35f36b9..154e262b76 100644\n--- a/Documentation/git-history.adoc\n+++ b/Documentation/git-history.adoc\n@@ -8,7 +8,7 @@ git-history - EXPERIMENTAL: Rewrite history\n SYNOPSIS\n --------\n [synopsis]\n-git history [<options>]\n+git history reword <commit> [--ref-action=(branches|head|print)]\n \n DESCRIPTION\n -----------\n@@ -50,7 +50,23 @@ first-class conflicts.\n COMMANDS\n --------\n \n-No commands are supported yet.\n+The following commands are available to rewrite history in different ways:\n+\n+`reword <commit>`::\n+\tRewrite the commit message of the specified commit. All the other\n+\tdetails of this commit remain unchanged. This command will spawn an\n+\teditor with the current message of that commit.\n+\n+OPTIONS\n+-------\n+\n+`--ref-action=(branches|head|print)`::\n+\tControl which references will be updated by the command, if any. With\n+\t`branches`, all local branches that point to commits which are\n+\tdescendants of the original commit will be rewritten. With `head`, only\n+\tthe current `HEAD` reference will be rewritten. With `print`, all\n+\tupdates as they would be performed with `branches` are printed in a\n+\tformat that can be consumed by linkgit:git-update-ref[1].\n \n GIT\n ---\ndiff --git a/builtin/history.c b/builtin/history.c\nindex f6fe32610b..77ad0294dd 100644\n--- a/builtin/history.c\n+++ b/builtin/history.c\n@@ -1,22 +1,427 @@\n+#define USE_THE_REPOSITORY_VARIABLE\n+\n #include \"builtin.h\"\n+#include \"commit.h\"\n+#include \"commit-reach.h\"\n+#include \"config.h\"\n+#include \"editor.h\"\n+#include \"environment.h\"\n #include \"gettext.h\"\n+#include \"hex.h\"\n #include \"parse-options.h\"\n+#include \"refs.h\"\n+#include \"replay.h\"\n+#include \"revision.h\"\n+#include \"sequencer.h\"\n+#include \"strvec.h\"\n+#include \"tree.h\"\n+#include \"wt-status.h\"\n+\n+#define GIT_HISTORY_REWORD_USAGE \\\n+\tN_(\"git history reword <commit> [--ref-action=(branches|head|print)]\")\n+\n+static void change_data_free(void *util, const char *str UNUSED)\n+{\n+\tstruct wt_status_change_data *d = util;\n+\tfree(d->rename_source);\n+\tfree(d);\n+}\n+\n+static int fill_commit_message(struct repository *repo,\n+\t\t\t       const struct object_id *old_tree,\n+\t\t\t       const struct object_id *new_tree,\n+\t\t\t       const char *default_message,\n+\t\t\t       const char *action,\n+\t\t\t       struct strbuf *out)\n+{\n+\tconst char *path = git_path_commit_editmsg();\n+\tconst char *hint =\n+\t\t_(\"Please enter the commit message for the %s changes.\"\n+\t\t  \" Lines starting\\nwith '%s' will be ignored, and an\"\n+\t\t  \" empty message aborts the commit.\\n\");\n+\tstruct wt_status s;\n+\n+\tstrbuf_addstr(out, default_message);\n+\tstrbuf_addch(out, '\\n');\n+\tstrbuf_commented_addf(out, comment_line_str, hint, action, comment_line_str);\n+\twrite_file_buf(path, out->buf, out->len);\n+\n+\twt_status_prepare(repo, &s);\n+\tFREE_AND_NULL(s.branch);\n+\ts.ahead_behind_flags = AHEAD_BEHIND_QUICK;\n+\ts.commit_template = 1;\n+\ts.colopts = 0;\n+\ts.display_comment_prefix = 1;\n+\ts.hints = 0;\n+\ts.use_color = 0;\n+\ts.whence = FROM_COMMIT;\n+\ts.committable = 1;\n+\n+\ts.fp = fopen(git_path_commit_editmsg(), \"a\");\n+\tif (!s.fp)\n+\t\treturn error_errno(_(\"could not open '%s'\"), git_path_commit_editmsg());\n+\n+\twt_status_collect_changes_trees(&s, old_tree, new_tree);\n+\twt_status_print(&s);\n+\twt_status_collect_free_buffers(&s);\n+\tstring_list_clear_func(&s.change, change_data_free);\n+\n+\tstrbuf_reset(out);\n+\tif (launch_editor(path, out, NULL)) {\n+\t\tfprintf(stderr, _(\"Aborting commit as launching the editor failed.\\n\"));\n+\t\treturn -1;\n+\t}\n+\tstrbuf_stripspace(out, comment_line_str);\n+\n+\tcleanup_message(out, COMMIT_MSG_CLEANUP_ALL, 0);\n+\n+\tif (!out->len) {\n+\t\tfprintf(stderr, _(\"Aborting commit due to empty commit message.\\n\"));\n+\t\treturn -1;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+static int commit_tree_with_edited_message(struct repository *repo,\n+\t\t\t\t\t   const char *action,\n+\t\t\t\t\t   struct commit *original,\n+\t\t\t\t\t   struct commit **out)\n+{\n+\tconst char *exclude_gpgsig[] = {\n+\t\t/* We reencode the message, so the encoding needs to be stripped. */\n+\t\t\"encoding\",\n+\t\t/* We need to strip signatures as those will become invalid. */\n+\t\t\"gpgsig\",\n+\t\t\"gpgsig-sha256\",\n+\t\tNULL,\n+\t};\n+\tconst char *original_message, *original_body, *ptr;\n+\tstruct commit_extra_header *original_extra_headers = NULL;\n+\tstruct strbuf commit_message = STRBUF_INIT;\n+\tstruct object_id rewritten_commit_oid;\n+\tstruct object_id original_tree_oid;\n+\tstruct object_id parent_tree_oid;\n+\tchar *original_author = NULL;\n+\tstruct commit *parent;\n+\tsize_t len;\n+\tint ret;\n+\n+\toriginal_tree_oid = repo_get_commit_tree(repo, original)->object.oid;\n+\n+\tparent = original->parents ? original->parents->item : NULL;\n+\tif (parent) {\n+\t\tif (repo_parse_commit(repo, parent)) {\n+\t\t\tret = error(_(\"unable to parse parent commit %s\"),\n+\t\t\t\t    oid_to_hex(&parent->object.oid));\n+\t\t\tgoto out;\n+\t\t}\n+\n+\t\tparent_tree_oid = repo_get_commit_tree(repo, parent)->object.oid;\n+\t} else {\n+\t\toidcpy(&parent_tree_oid, repo->hash_algo->empty_tree);\n+\t}\n+\n+\t/* We retain authorship of the original commit. */\n+\toriginal_message = repo_logmsg_reencode(repo, original, NULL, NULL);\n+\tptr = find_commit_header(original_message, \"author\", &len);\n+\tif (ptr)\n+\t\toriginal_author = xmemdupz(ptr, len);\n+\tfind_commit_subject(original_message, &original_body);\n+\n+\tret = fill_commit_message(repo, &parent_tree_oid, &original_tree_oid,\n+\t\t\t\t  original_body, action, &commit_message);\n+\tif (ret < 0)\n+\t\tgoto out;\n+\n+\toriginal_extra_headers = read_commit_extra_headers(original, exclude_gpgsig);\n+\n+\tret = commit_tree_extended(commit_message.buf, commit_message.len, &original_tree_oid,\n+\t\t\t\t   original->parents, &rewritten_commit_oid, original_author,\n+\t\t\t\t   NULL, NULL, original_extra_headers);\n+\tif (ret < 0)\n+\t\tgoto out;\n+\n+\t*out = lookup_commit_or_die(&rewritten_commit_oid, \"rewritten commit\");\n+\n+out:\n+\tfree_commit_extra_headers(original_extra_headers);\n+\tstrbuf_release(&commit_message);\n+\tfree(original_author);\n+\treturn ret;\n+}\n+\n+enum ref_action {\n+\tREF_ACTION_DEFAULT,\n+\tREF_ACTION_BRANCHES,\n+\tREF_ACTION_HEAD,\n+\tREF_ACTION_PRINT,\n+};\n+\n+static int parse_ref_action(const struct option *opt, const char *value, int unset)\n+{\n+\tenum ref_action *action = opt->value;\n+\n+\tBUG_ON_OPT_NEG_NOARG(unset, value);\n+\tif (!strcmp(value, \"branches\")) {\n+\t\t*action = REF_ACTION_BRANCHES;\n+\t} else if (!strcmp(value, \"head\")) {\n+\t\t*action = REF_ACTION_HEAD;\n+\t} else if (!strcmp(value, \"print\")) {\n+\t\t*action = REF_ACTION_PRINT;\n+\t} else {\n+\t\treturn error(_(\"%s expects one of 'branches', 'head' or 'print'\"),\n+\t\t\t     opt->long_name);\n+\t}\n+\n+\treturn 0;\n+}\n+\n+static int handle_reference_updates(enum ref_action action,\n+\t\t\t\t    struct repository *repo,\n+\t\t\t\t    struct commit *original,\n+\t\t\t\t    struct commit *rewritten,\n+\t\t\t\t    const char *reflog_msg)\n+{\n+\tconst struct name_decoration *decoration;\n+\tstruct replay_revisions_options opts = { 0 };\n+\tstruct replay_result result = { 0 };\n+\tstruct ref_transaction *transaction = NULL;\n+\tstruct strvec args = STRVEC_INIT;\n+\tstruct strbuf err = STRBUF_INIT;\n+\tstruct commit *head = NULL;\n+\tstruct rev_info revs;\n+\tchar hex[GIT_MAX_HEXSZ + 1];\n+\tbool detached_head;\n+\tint head_flags = 0;\n+\tint ret;\n+\n+\trefs_read_ref_full(get_main_ref_store(repo), \"HEAD\",\n+\t\t\t   RESOLVE_REF_NO_RECURSE, NULL, &head_flags);\n+\tdetached_head = !(head_flags & REF_ISSYMREF);\n+\n+\trepo_init_revisions(repo, &revs, NULL);\n+\tstrvec_push(&args, \"ignored\");\n+\tstrvec_push(&args, \"--reverse\");\n+\tstrvec_push(&args, \"--topo-order\");\n+\tstrvec_push(&args, \"--full-history\");\n+\n+\t/* We only want to see commits that are descendants of the old commit. */\n+\tstrvec_pushf(&args, \"--ancestry-path=%s\",\n+\t\t     oid_to_hex(&original->object.oid));\n+\n+\t/*\n+\t * Ancestry path may also show ancestors of the old commit, but we\n+\t * don't want to see those, either.\n+\t */\n+\tstrvec_pushf(&args, \"^%s\", oid_to_hex(&original->object.oid));\n+\n+\t/*\n+\t * When we're asked to update HEAD we need to verify that the commit\n+\t * that we want to rewrite is actually an ancestor of it and, if so,\n+\t * update it. Otherwise we'll update (or print) all descendant\n+\t * branches.\n+\t */\n+\tif (action == REF_ACTION_HEAD) {\n+\t\tstruct commit_list *from_list = NULL;\n+\n+\t\thead = lookup_commit_reference_by_name(\"HEAD\");\n+\t\tif (!head) {\n+\t\t\tret = error(_(\"cannot look up HEAD\"));\n+\t\t\tgoto out;\n+\t\t}\n+\n+\t\tcommit_list_insert(original, &from_list);\n+\t\tret = repo_is_descendant_of(repo, head, from_list);\n+\t\tfree_commit_list(from_list);\n+\n+\t\tif (ret < 0) {\n+\t\t\tret = error(_(\"cannot determine descendance\"));\n+\t\t\tgoto out;\n+\t\t} else if (!ret) {\n+\t\t\tret = error(_(\"rewritten commit must be an ancestor \"\n+\t\t\t\t      \"of HEAD when using --ref-action=head\"));\n+\t\t\tgoto out;\n+\t\t}\n+\n+\t\tstrvec_push(&args, \"HEAD\");\n+\t} else {\n+\t\tstrvec_push(&args, \"--branches\");\n+\t\tstrvec_push(&args, \"HEAD\");\n+\t}\n+\n+\tsetup_revisions_from_strvec(&args, &revs, NULL);\n+\tif (revs.nr)\n+\t\tBUG(\"revisions were set up with invalid argument '%s'\", args.v[0]);\n+\n+\topts.onto = oid_to_hex_r(hex, &rewritten->object.oid);\n+\n+\tret = replay_revisions(&revs, &opts, &result);\n+\tif (ret)\n+\t\tgoto out;\n+\n+\tswitch (action) {\n+\tcase REF_ACTION_BRANCHES:\n+\tcase REF_ACTION_HEAD:\n+\t\ttransaction = ref_store_transaction_begin(get_main_ref_store(repo), 0, &err);\n+\t\tif (!transaction) {\n+\t\t\tret = error(_(\"failed to begin ref transaction: %s\"), err.buf);\n+\t\t\tgoto out;\n+\t\t}\n+\n+\t\tfor (size_t i = 0; i < result.updates_nr; i++) {\n+\t\t\tret = ref_transaction_update(transaction,\n+\t\t\t\t\t\t     result.updates[i].refname,\n+\t\t\t\t\t\t     &result.updates[i].new_oid,\n+\t\t\t\t\t\t     &result.updates[i].old_oid,\n+\t\t\t\t\t\t     NULL, NULL, 0, reflog_msg, &err);\n+\t\t\tif (ret) {\n+\t\t\t\tret = error(_(\"failed to update ref '%s': %s\"),\n+\t\t\t\t\t    result.updates[i].refname, err.buf);\n+\t\t\t\tgoto out;\n+\t\t\t}\n+\t\t}\n+\n+\t\t/*\n+\t\t * `replay_revisions()` only updates references that are\n+\t\t * ancestors of `rewritten`, so we need to manually\n+\t\t * handle updating references that point to `original`.\n+\t\t */\n+\t\tfor (decoration = get_name_decoration(&original->object);\n+\t\t     decoration;\n+\t\t     decoration = decoration->next)\n+\t\t{\n+\t\t\tif (decoration->type != DECORATION_REF_LOCAL &&\n+\t\t\t    decoration->type != DECORATION_REF_HEAD)\n+\t\t\t\tcontinue;\n+\n+\t\t\tif (action == REF_ACTION_HEAD &&\n+\t\t\t    decoration->type != DECORATION_REF_HEAD)\n+\t\t\t\tcontinue;\n+\n+\t\t\t/*\n+\t\t\t * We only need to update HEAD separately in case it's\n+\t\t\t * detached. If it's not we'd already update the branch\n+\t\t\t * it is pointing to.\n+\t\t\t */\n+\t\t\tif (action == REF_ACTION_BRANCHES &&\n+\t\t\t    decoration->type == DECORATION_REF_HEAD &&\n+\t\t\t    !detached_head)\n+\t\t\t\tcontinue;\n+\n+\t\t\tret = ref_transaction_update(transaction,\n+\t\t\t\t\t\t     decoration->name,\n+\t\t\t\t\t\t     &rewritten->object.oid,\n+\t\t\t\t\t\t     &original->object.oid,\n+\t\t\t\t\t\t     NULL, NULL, 0, reflog_msg, &err);\n+\t\t\tif (ret) {\n+\t\t\t\tret = error(_(\"failed to update ref '%s': %s\"),\n+\t\t\t\t\t    decoration->name, err.buf);\n+\t\t\t\tgoto out;\n+\t\t\t}\n+\t\t}\n+\n+\t\tif (ref_transaction_commit(transaction, &err)) {\n+\t\t\tret = error(_(\"failed to commit ref transaction: %s\"), err.buf);\n+\t\t\tgoto out;\n+\t\t}\n+\n+\t\tbreak;\n+\tcase REF_ACTION_PRINT:\n+\t\tfor (size_t i = 0; i < result.updates_nr; i++)\n+\t\t\tprintf(\"update %s %s %s\\n\",\n+\t\t\t       result.updates[i].refname,\n+\t\t\t       oid_to_hex(&result.updates[i].new_oid),\n+\t\t\t       oid_to_hex(&result.updates[i].old_oid));\n+\t\tbreak;\n+\tdefault:\n+\t\tBUG(\"unsupported ref action %d\", action);\n+\t}\n+\n+\tret = 0;\n+\n+out:\n+\tref_transaction_free(transaction);\n+\treplay_result_release(&result);\n+\trelease_revisions(&revs);\n+\tstrbuf_release(&err);\n+\tstrvec_clear(&args);\n+\treturn ret;\n+}\n+\n+static int cmd_history_reword(int argc,\n+\t\t\t      const char **argv,\n+\t\t\t      const char *prefix,\n+\t\t\t      struct repository *repo)\n+{\n+\tconst char * const usage[] = {\n+\t\tGIT_HISTORY_REWORD_USAGE,\n+\t\tNULL,\n+\t};\n+\tenum ref_action action = REF_ACTION_DEFAULT;\n+\tstruct option options[] = {\n+\t\tOPT_CALLBACK_F(0, \"ref-action\", &action, N_(\"<action>\"),\n+\t\t\t       N_(\"control ref update behavior (branches|head|print)\"),\n+\t\t\t       PARSE_OPT_NONEG, parse_ref_action),\n+\t\tOPT_END(),\n+\t};\n+\tstruct strbuf reflog_msg = STRBUF_INIT;\n+\tstruct commit *original, *rewritten;\n+\tint ret;\n+\n+\targc = parse_options(argc, argv, prefix, options, usage, 0);\n+\tif (argc != 1) {\n+\t\tret = error(_(\"command expects a single revision\"));\n+\t\tgoto out;\n+\t}\n+\trepo_config(repo, git_default_config, NULL);\n+\n+\tif (action == REF_ACTION_DEFAULT)\n+\t\taction = REF_ACTION_BRANCHES;\n+\n+\toriginal = lookup_commit_reference_by_name(argv[0]);\n+\tif (!original) {\n+\t\tret = error(_(\"commit cannot be found: %s\"), argv[0]);\n+\t\tgoto out;\n+\t}\n+\n+\tret = commit_tree_with_edited_message(repo, \"reworded\", original, &rewritten);\n+\tif (ret < 0) {\n+\t\tret = error(_(\"failed writing reworded commit\"));\n+\t\tgoto out;\n+\t}\n+\n+\tstrbuf_addf(&reflog_msg, \"reword: updating %s\", argv[0]);\n+\n+\tret = handle_reference_updates(action, repo, original, rewritten,\n+\t\t\t\t       reflog_msg.buf);\n+\tif (ret < 0) {\n+\t\tret = error(_(\"failed replaying descendants\"));\n+\t\tgoto out;\n+\t}\n+\n+\tret = 0;\n+\n+out:\n+\tstrbuf_release(&reflog_msg);\n+\treturn ret;\n+}\n \n int cmd_history(int argc,\n \t\tconst char **argv,\n \t\tconst char *prefix,\n-\t\tstruct repository *repo UNUSED)\n+\t\tstruct repository *repo)\n {\n \tconst char * const usage[] = {\n-\t\tN_(\"git history [<options>]\"),\n+\t\tGIT_HISTORY_REWORD_USAGE,\n \t\tNULL,\n \t};\n+\tparse_opt_subcommand_fn *fn = NULL;\n \tstruct option options[] = {\n+\t\tOPT_SUBCOMMAND(\"reword\", &fn, cmd_history_reword),\n \t\tOPT_END(),\n \t};\n \n \targc = parse_options(argc, argv, prefix, options, usage, 0);\n-\tif (argc)\n-\t\tusagef(\"unrecognized argument: %s\", argv[0]);\n-\treturn 0;\n+\treturn fn(argc, argv, prefix, repo);\n }\ndiff --git a/t/meson.build b/t/meson.build\nindex 73006b095a..c9f92450dc 100644\n--- a/t/meson.build\n+++ b/t/meson.build\n@@ -388,6 +388,7 @@ integration_tests = [\n   't3437-rebase-fixup-options.sh',\n   't3438-rebase-broken-files.sh',\n   't3450-history.sh',\n+  't3451-history-reword.sh',\n   't3500-cherry.sh',\n   't3501-revert-cherry-pick.sh',\n   't3502-cherry-pick-merge.sh',\ndiff --git a/t/t3450-history.sh b/t/t3450-history.sh\nindex 417c343d43..f513463b92 100755\n--- a/t/t3450-history.sh\n+++ b/t/t3450-history.sh\n@@ -5,13 +5,13 @@ test_description='tests for git-history command'\n . ./test-lib.sh\n \n test_expect_success 'does nothing without any arguments' '\n-\tgit history >out 2>&1 &&\n-\ttest_must_be_empty out\n+\ttest_must_fail git history 2>err &&\n+\ttest_grep \"need a subcommand\" err\n '\n \n test_expect_success 'raises an error with unknown argument' '\n \ttest_must_fail git history garbage 2>err &&\n-\ttest_grep \"unrecognized argument: garbage\" err\n+\ttest_grep \"unknown subcommand: .garbage.\" err\n '\n \n test_done\ndiff --git a/t/t3451-history-reword.sh b/t/t3451-history-reword.sh\nnew file mode 100755\nindex 0000000000..3594421b68\n--- /dev/null\n+++ b/t/t3451-history-reword.sh\n@@ -0,0 +1,391 @@\n+#!/bin/sh\n+\n+test_description='tests for git-history reword subcommand'\n+\n+. ./test-lib.sh\n+. \"$TEST_DIRECTORY/lib-log-graph.sh\"\n+\n+reword_with_message () {\n+\tcat >message &&\n+\twrite_script fake-editor.sh <<-\\EOF &&\n+\tcp message \"$1\"\n+\tEOF\n+\ttest_set_editor \"$(pwd)\"/fake-editor.sh &&\n+\tgit history reword \"$@\" &&\n+\trm fake-editor.sh message\n+}\n+\n+expect_graph () {\n+\tcat >expect &&\n+\tlib_test_cmp_graph --graph --format=%s \"$@\"\n+}\n+\n+expect_log () {\n+\tgit log --format=\"%s\" \"$@\" >actual &&\n+\tcat >expect &&\n+\ttest_cmp expect actual\n+}\n+\n+test_expect_success 'can reword tip of a branch' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\n+\t\tgit symbolic-ref HEAD >expect &&\n+\t\treword_with_message HEAD <<-EOF &&\n+\t\tthird reworded\n+\t\tEOF\n+\t\tgit symbolic-ref HEAD >actual &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\texpect_log <<-\\EOF &&\n+\t\tthird reworded\n+\t\tsecond\n+\t\tfirst\n+\t\tEOF\n+\n+\t\tgit reflog >reflog &&\n+\t\ttest_grep \"reword: updating HEAD\" reflog\n+\t)\n+'\n+\n+test_expect_success 'can reword commit in the middle' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\n+\t\tgit symbolic-ref HEAD >expect &&\n+\t\treword_with_message HEAD~ <<-EOF &&\n+\t\tsecond reworded\n+\t\tEOF\n+\t\tgit symbolic-ref HEAD >actual &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\texpect_log <<-\\EOF\n+\t\tthird\n+\t\tsecond reworded\n+\t\tfirst\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'can reword commit in the middle even on detached head' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third_on_main &&\n+\t\tgit checkout --detach HEAD^ &&\n+\t\ttest_commit third_on_head &&\n+\n+\t\treword_with_message HEAD~ <<-EOF &&\n+\t\tsecond reworded\n+\t\tEOF\n+\n+\t\texpect_graph HEAD --branches <<-\\EOF\n+\t\t* third_on_head\n+\t\t| * third_on_main\n+\t\t|/\n+\t\t* second reworded\n+\t\t* first\n+\t\tEOF\n+       )\n+'\n+\n+test_expect_success 'can reword the detached head' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\tgit checkout --detach HEAD &&\n+\t\ttest_commit third &&\n+\n+\t\treword_with_message HEAD <<-EOF &&\n+\t\tthird reworded\n+\t\tEOF\n+\n+\t\texpect_log <<-\\EOF\n+\t\tthird reworded\n+\t\tsecond\n+\t\tfirst\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'can reword root commit' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\t\treword_with_message HEAD~2 <<-EOF &&\n+\t\tfirst reworded\n+\t\tEOF\n+\n+\t\texpect_log <<-\\EOF\n+\t\tthird\n+\t\tsecond\n+\t\tfirst reworded\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'can reword in a bare repo' '\n+\ttest_when_finished \"rm -rf repo repo.git\" &&\n+\tgit init repo &&\n+\ttest_commit -C repo first &&\n+\tgit clone --bare repo repo.git &&\n+\t(\n+\t\tcd repo.git &&\n+\t\treword_with_message HEAD <<-EOF &&\n+\t\treworded\n+\t\tEOF\n+\n+\t\texpect_log <<-\\EOF\n+\t\treworded\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'can reword a commit on a different branch' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\tgit branch theirs &&\n+\t\ttest_commit ours &&\n+\t\tgit switch theirs &&\n+\t\ttest_commit theirs &&\n+\n+\t\tgit rev-parse ours >ours-before &&\n+\t\treword_with_message theirs <<-EOF &&\n+\t\tReworded theirs\n+\t\tEOF\n+\t\tgit rev-parse ours >ours-after &&\n+\t\ttest_cmp ours-before ours-after &&\n+\n+\t\texpect_graph --branches <<-\\EOF\n+\t\t* Reworded theirs\n+\t\t| * ours\n+\t\t|/\n+\t\t* base\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'can reword a merge commit' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\tgit branch branch &&\n+\t\ttest_commit ours &&\n+\t\tgit switch branch &&\n+\t\ttest_commit theirs &&\n+\t\tgit switch - &&\n+\t\tgit merge theirs &&\n+\n+\t\t# It is not possible to replay merge commits embedded in the\n+\t\t# history (yet).\n+\t\ttest_must_fail git history reword HEAD~ 2>err &&\n+\t\ttest_grep \"replaying merge commits is not supported yet\" err &&\n+\n+\t\t# But it is possible to reword a merge commit directly.\n+\t\treword_with_message HEAD <<-EOF &&\n+\t\tReworded merge commit\n+\t\tEOF\n+\t\texpect_graph <<-\\EOF\n+\t\t*   Reworded merge commit\n+\t\t|\\\n+\t\t| * theirs\n+\t\t* | ours\n+\t\t|/\n+\t\t* base\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--ref-action=print prints ref updates without modifying repo' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\tgit branch branch &&\n+\t\ttest_commit ours &&\n+\t\tgit switch branch &&\n+\t\ttest_commit theirs &&\n+\n+\t\tgit refs list >refs-expect &&\n+\t\treword_with_message --ref-action=print base >updates <<-\\EOF &&\n+\t\treworded commit\n+\t\tEOF\n+\t\tgit refs list >refs-actual &&\n+\t\ttest_cmp refs-expect refs-actual &&\n+\n+\t\ttest_grep \"update refs/heads/branch\" updates &&\n+\t\ttest_grep \"update refs/heads/main\" updates &&\n+\t\tgit update-ref --stdin <updates &&\n+\t\texpect_log --branches <<-\\EOF\n+\t\ttheirs\n+\t\tours\n+\t\treworded commit\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--ref-action=head updates only HEAD' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\tgit branch branch &&\n+\t\ttest_commit theirs &&\n+\t\tgit switch branch &&\n+\t\ttest_commit ours &&\n+\n+\t\t# When told to update HEAD, only, the command will refuse to\n+\t\t# rewrite commits that are not an ancestor of HEAD.\n+\t\ttest_must_fail git history reword --ref-action=head theirs 2>err &&\n+\t\ttest_grep \"rewritten commit must be an ancestor of HEAD\" err &&\n+\n+\t\treword_with_message --ref-action=head base >updates <<-\\EOF &&\n+\t\treworded base\n+\t\tEOF\n+\t\texpect_log HEAD <<-\\EOF &&\n+\t\tours\n+\t\treworded base\n+\t\tEOF\n+\t\texpect_log main <<-\\EOF\n+\t\ttheirs\n+\t\tbase\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'editor shows proper status' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\n+\t\twrite_script fake-editor.sh <<-\\EOF &&\n+\t\tcp \"$1\" . &&\n+\t\tprintf \"\\namend a comment\\n\" >>\"$1\"\n+\t\tEOF\n+\t\ttest_set_editor \"$(pwd)\"/fake-editor.sh &&\n+\t\tgit history reword HEAD &&\n+\n+\t\tcat >expect <<-EOF &&\n+\t\tfirst\n+\n+\t\t# Please enter the commit message for the reworded changes. Lines starting\n+\t\t# with ${SQ}#${SQ} will be ignored, and an empty message aborts the commit.\n+\t\t# Changes to be committed:\n+\t\t#\tnew file:   first.t\n+\t\t#\n+\t\tEOF\n+\t\ttest_cmp expect COMMIT_EDITMSG &&\n+\n+\t\ttest_commit_message HEAD <<-\\EOF\n+\t\tfirst\n+\n+\t\tamend a comment\n+\t\tEOF\n+\t)\n+'\n+\n+# For now, git-history(1) does not yet execute any hooks. This is subject to\n+# change in the future, and if it does this test here is expected to start\n+# failing. In other words, this test is not an endorsement of the current\n+# status quo.\n+test_expect_success 'hooks are not executed for rewritten commits' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\n+\t\tORIG_PATH=\"$(pwd)\" &&\n+\t\texport ORIG_PATH &&\n+\t\tfor hook in prepare-commit-msg pre-commit post-commit post-rewrite commit-msg\n+\t\tdo\n+\t\t\twrite_script .git/hooks/$hook <<-\\EOF || exit 1\n+\t\t\ttouch \"$ORIG_PATH/hooks.log\n+\t\t\tEOF\n+\t\tdone &&\n+\n+\t\treword_with_message HEAD~ <<-EOF &&\n+\t\tsecond reworded\n+\t\tEOF\n+\n+\t\tcat >expect <<-EOF &&\n+\t\tthird\n+\t\tsecond reworded\n+\t\tfirst\n+\t\tEOF\n+\t\tgit log --format=%s >actual &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\ttest_path_is_missing hooks.log\n+\t)\n+'\n+\n+test_expect_success 'aborts with empty commit message' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\n+\t\t! reword_with_message HEAD 2>err </dev/null &&\n+\t\ttest_grep \"Aborting commit due to empty commit message.\" err\n+\t)\n+'\n+\n+test_expect_success 'retains changes in the worktree and index' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttouch a b &&\n+\t\tgit add . &&\n+\t\tgit commit -m \"initial commit\" &&\n+\t\techo foo >a &&\n+\t\techo bar >b &&\n+\t\tgit add b &&\n+\t\treword_with_message HEAD <<-EOF &&\n+\t\tmessage\n+\t\tEOF\n+\t\tcat >expect <<-\\EOF &&\n+\t\t M a\n+\t\tM  b\n+\t\t?? actual\n+\t\t?? expect\n+\t\tEOF\n+\t\tgit status --porcelain >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_done\n\n-- \n2.52.0.590.g1f87b77810.dirty\n\n"},{"id":"533643","messageId":"xmqqv7h6na0s.fsf@gitster.g","threadId":"64783","inReplyTo":"20260112-b4-pks-history-builtin-v10-1-e3c6aa5b4cec@pks.im","subject":"Re: [PATCH v10 1/8] builtin/replay: extract core logic to replay revisions","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-01-12T15:08:51Z","receivedAt":"2026-01-12T15:08:56Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Patrick Steinhardt <ps@pks.im> writes:\n\n> -\tdie_for_incompatible_opt2(!!onto_name, \"--onto\",\n> -\t\t\t\t  !!*advance_name, \"--advance\");\n> +\tif (!(!!onto_name ^ !!*advance_name))\n> +\t\tBUG(\"expected either onto_name or *advance_name in this function\");\n\nThis brings our crypticness to a whole new level.  onto_name not\nbeing NULL is a sign that \"--onto\" was given, while *advance_name\npointer points at the string that \"--advance\" option has received.\nWe are saying that only one of these two must be non-NULL, and the\nother must be NULL.\n\nI know !!VAR is an idiom to turn any pointer into 0 (=NULL) or 1\n(!=NULL), but isn't the latter (i.e., normalizing all non-NULL\npointer to 1) a bit overkill, which becomes only necessary because\nthe construction wants to use \"^\" as \"sides of this operator are\ndifferent Boolean values\" operator.  And then to add on top, the\nwhole thing is !(negated).  I wonder if\n\n\tif (!onto_name != !*advance_name)\n\t\tBUG(\"one and only one of --onto/--advance must be given\");\n\nis easier to follow without being overly cute?\n\nThanks.\n\n\n"},{"id":"533649","messageId":"aWUVHKMfG0vRb8_G@pks.im","threadId":"64783","inReplyTo":"xmqqv7h6na0s.fsf@gitster.g","subject":"Re: [PATCH v10 1/8] builtin/replay: extract core logic to replay revisions","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-01-12T15:37:00Z","receivedAt":"2026-01-12T15:37:08Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Mon, Jan 12, 2026 at 07:08:51AM -0800, Junio C Hamano wrote:\n> Patrick Steinhardt <ps@pks.im> writes:\n> \n> > -\tdie_for_incompatible_opt2(!!onto_name, \"--onto\",\n> > -\t\t\t\t  !!*advance_name, \"--advance\");\n> > +\tif (!(!!onto_name ^ !!*advance_name))\n> > +\t\tBUG(\"expected either onto_name or *advance_name in this function\");\n> \n> This brings our crypticness to a whole new level.  onto_name not\n> being NULL is a sign that \"--onto\" was given, while *advance_name\n> pointer points at the string that \"--advance\" option has received.\n> We are saying that only one of these two must be non-NULL, and the\n> other must be NULL.\n> \n> I know !!VAR is an idiom to turn any pointer into 0 (=NULL) or 1\n> (!=NULL), but isn't the latter (i.e., normalizing all non-NULL\n> pointer to 1) a bit overkill, which becomes only necessary because\n> the construction wants to use \"^\" as \"sides of this operator are\n> different Boolean values\" operator.  And then to add on top, the\n> whole thing is !(negated).  I wonder if\n> \n> \tif (!onto_name != !*advance_name)\n> \t\tBUG(\"one and only one of --onto/--advance must be given\");\n> \n> is easier to follow without being overly cute?\n\nIt should be:\n\n    if (!onto_name == !*advance_name)\n        BUG(...);\n\nBut other than that it reads way better indeed. Fixed up locally,\nthanks!\n\nPatrick\n"},{"id":"533696","messageId":"CABPp-BGOcMRerGpH5HGkUR4-DKPx+VmkWzqRt8qideZoJBrvHg@mail.gmail.com","threadId":"64783","inReplyTo":"20260112-b4-pks-history-builtin-v10-1-e3c6aa5b4cec@pks.im","subject":"Re: [PATCH v10 1/8] builtin/replay: extract core logic to replay revisions","fromName":"Elijah Newren","fromEmail":"newren@gmail.com","sentAt":"2026-01-13T06:00:06Z","receivedAt":"2026-01-13T06:00:20Z","isPatch":true,"sender":{"key":"newren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/5455730?v=4"},"body":"On Mon, Jan 12, 2026 at 6:17 AM Patrick Steinhardt <ps@pks.im> wrote:\n[...]\n> -       die_for_incompatible_opt2(!!onto_name, \"--onto\",\n> -                                 !!*advance_name, \"--advance\");\n> +       if (!(!!onto_name ^ !!*advance_name))\n> +               BUG(\"expected either onto_name or *advance_name in this function\");\n> +\n\nI liked Junio's comments on this section and your response.\n\n> @@ -253,6 +254,137 @@ static struct commit *pick_regular_commit(struct repository *repo,\n[...]\n> +       if (!result.clean) {\n> +               out->merge_conflict = true;\n> +               ret = -1;\n\nEven if you keep the special merge_conflict field for other purposes,\nsetting ret to -1 here still feels very wrong.  Negative return codes,\nand especially -1, is used throughout the merge machinery to signal\nunexpected errors like failure to read/write to disk.  Further, it's\ninconsistent with how builtin/merge-tree.c works, where both in code\nand in documentation merge result code is 0 == clean, 1 == merge\nconflicts.  I'm worried using -1 here could cause some nasty future\nmaintenance headaches trying to understand the field if left this way,\nat least for me.  As mentioned in the last round, the ret value here\nshould be 1.\n\n> @@ -306,21 +438,11 @@ int cmd_replay(int argc,\n[...]\n> -       die_for_incompatible_opt2(!!advance_name_opt, \"--advance\",\n> -                                 contained, \"--contained\");\n> +       die_for_incompatible_opt2(!!opts.advance, \"--advance\",\n> +                                 opts.contained, \"--contained\");\n\nThis predates your patch, but I'm wondering if there's anything we\nshould do to clarify and/or simplify the first check.  The original\nform of the check\n     +       die_for_incompatible_opt2(!!opts.advance, \"--advance\",\n     +                                 opts.contained, \"--contained\");\nwas created because (a) I had code that allowed --onto to be implicit\nin some cases, and (b) I was thinking only of --onto and --advance\nmodes.\n\nHowever: (a) we got rid of the implicit mode selection from my private\nbranch, and (b) Siddharth added patches which added a --revert mode.\nThose patches caused confusion around the interplay of --contained\nwith the new mode\n(https://lore.kernel.org/git/xmqq3460ocv7.fsf@gitster.g/).  I thought\nthe synopsis:\n           \"([--contained] --onto <newbase> | --advance <branch>) \"\nimplied clearly enough that --contained is a sub-mode of --onto, but\napparently that wasn't the case.  Perhaps we can strengthen that\nunderstanding if we change the check here to instead be something like\n   if (opts.contained && !opts.onto)\n      die(\"--onto must be specified if --contained is\")\n\nDefinitely not critical; but might be a nice cleanup.\n\n> +       die_for_incompatible_opt2(!!opts.advance, \"--advance\",\n> +                                 !!opts.onto, \"--onto\");\n\nYeah, and Siddharth can convert this to a die_for_incompatible_opt3()\ncall, adding \"--revert\" to it when he rerolls his series on top of\nyours.\n\n> -       /* Return */\n> -       if (ret < 0)\n> -               exit(128);\n> -       return ret ? 0 : 1;\n> +       if (ret) {\n> +               if (result.merge_conflict)\n> +                       return 1;\n> +               return 128;\n> +       }\n> +\n> +       return 0;\n>  }\n\nYou mentioned that you wanted to keep the merge_conflict field due to\nsome future patches beyond the currently submitted series.  I wonder\nif it'd make more sense to introduce that field once you introduce the\nnew patches, but i don't feel too strongly about that.  I do feel\nstrongly that the place where you set ret to -1 is problematic and\nshould be changed to 1.\n"},{"id":"533698","messageId":"CABPp-BGhtPyiVT=32NXz3k8m=+ZgPziXueM4Y8+g4dAUtN9osw@mail.gmail.com","threadId":"64783","inReplyTo":"20260112-b4-pks-history-builtin-v10-4-e3c6aa5b4cec@pks.im","subject":"Re: [PATCH v10 4/8] replay: support empty commit ranges","fromName":"Elijah Newren","fromEmail":"newren@gmail.com","sentAt":"2026-01-13T06:00:27Z","receivedAt":"2026-01-13T06:00:40Z","isPatch":true,"sender":{"key":"newren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/5455730?v=4"},"body":"On Mon, Jan 12, 2026 at 6:17 AM Patrick Steinhardt <ps@pks.im> wrote:\n>\n> In a subsequent commit we're about to introduce a new user of the replay\n> subsystem. With that new user, the range of commits that we'll want to\n> replay will be identified implicitly via \"HEAD\". With such implicit\n> ranges it becomes likely that the range of revisions that we're asked to\n> replay becomes empty. This case does not make sense with git-replay(1),\n> but with the new command it will.\n\nI think I know what you were trying to say, but this feels misleading;\nit could be the commit at the tip of any branch, not just HEAD.\nPerhaps:\n\nIn a subsequent commit we're about to introduce a new user of the replay\nsubsystem. With that new user, the range of commits that we'll want to\nreplay will be identified implicitly via a single commit, and will\ninclude all descendants of that commit to any branch. If that commit has\nno descendants (because it's the tip of some branch), then the range of\nrevisions that we're asked to replay becomes empty. This case does not\nmake sense with git-replay(1), but with the new command it will.\n\n> This case is not currently supported by `replay_revisions()` though\n> because we zero-initialize `struct merge_result`. This includes its\n> `.clean` member, which indicates whether the merge ran into a conflict\n> or not. But given that we don't have any revision to replay, we won't\n> ever perform any merge at all, and consequently that member will never\n> be set to `1`. We thus later think that there's been a merge conflict\n> and return an error from `replay_commits()`.\n>\n> Address this issue by initializing the `.clean` member to `1`.\n>\n> Signed-off-by: Patrick Steinhardt <ps@pks.im>\n> ---\n>  replay.c | 5 +++--\n>  1 file changed, 3 insertions(+), 2 deletions(-)\n>\n> diff --git a/replay.c b/replay.c\n> index 1e660171d2..a8e6d5b30b 100644\n> --- a/replay.c\n> +++ b/replay.c\n> @@ -266,7 +266,9 @@ int replay_revisions(struct rev_info *revs,\n>         struct commit *commit;\n>         struct commit *onto = NULL;\n>         struct merge_options merge_opt;\n> -       struct merge_result result;\n> +       struct merge_result result = {\n> +               .clean = 1,\n> +       };\n>         char *advance;\n>         int ret;\n>\n> @@ -282,7 +284,6 @@ int replay_revisions(struct rev_info *revs,\n>         }\n>\n>         init_basic_merge_options(&merge_opt, revs->repo);\n> -       memset(&result, 0, sizeof(result));\n>         merge_opt.show_rename_progress = 0;\n>         last_commit = onto;\n>         replayed_commits = kh_init_oid_map();\n>\n> --\n> 2.52.0.590.g1f87b77810.dirty\n\nLooks good otherwise; thanks for splitting this out.\n"},{"id":"533699","messageId":"CABPp-BFvi0xmpP1jcU0qc7c-KhYXgzpCVbXyto3FMSFSSwrKoA@mail.gmail.com","threadId":"64783","inReplyTo":"20260112-b4-pks-history-builtin-v10-5-e3c6aa5b4cec@pks.im","subject":"Re: [PATCH v10 5/8] replay: support updating detached HEAD","fromName":"Elijah Newren","fromEmail":"newren@gmail.com","sentAt":"2026-01-13T06:00:53Z","receivedAt":"2026-01-13T06:01:06Z","isPatch":true,"sender":{"key":"newren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/5455730?v=4"},"body":"On Mon, Jan 12, 2026 at 6:17 AM Patrick Steinhardt <ps@pks.im> wrote:\n[...]\n> +       refs_read_ref_full(get_main_ref_store(repo), \"HEAD\",\n> +                          RESOLVE_REF_NO_RECURSE, NULL, &head_flags);\n> +       *detached_head = !(head_flags & REF_ISSYMREF);\n\nAh, I like this simpler way to determine if we have a detached HEAD.\nOut of curiosity, though, is there a need to specify\nRESOLVE_REF_NO_RECURSE?  I waffled on that for a bit and just left it\nout.\n\n> @@ -312,18 +319,30 @@ int replay_revisions(struct rev_info *revs,\n[...]\n> +               for (decoration = get_name_decoration(&commit->object);\n> +                    decoration;\n> +                    decoration = decoration->next)\n> +               {\n> +                       if (decoration->type != DECORATION_REF_LOCAL &&\n> +                           decoration->type != DECORATION_REF_HEAD)\n> +                               continue;\n> +\n> +                       /*\n> +                        * We only need to update HEAD separately in case it's\n> +                        * detached. If it's not we'd already update the branch\n> +                        * it is pointing to.\n> +                        */\n> +                       if (decoration->type == DECORATION_REF_HEAD && !detached_head)\n> +                               continue;\n> +\n> +                       if (!opts->contained &&\n> +                           !strset_contains(update_refs, decoration->name))\n> +                               continue;\n\nI like this refactoring to make the conditions clearer; very nice.\n\n> diff --git a/t/t3650-replay-basics.sh b/t/t3650-replay-basics.sh\n> index 307101eeb9..c862aa39f3 100755\n> --- a/t/t3650-replay-basics.sh\n> +++ b/t/t3650-replay-basics.sh\n> @@ -249,6 +249,15 @@ test_expect_success 'using replay on bare repo to rebase multiple divergent bran\n>         done\n>  '\n>\n> +test_expect_success 'using replay to update detached HEAD' '\n> +       current_head=$(git branch --show-current) &&\n> +       test_when_finished git switch \"$current_head\" &&\n> +       git switch --detach &&\n> +       test_commit something &&\n> +       git replay --ref-action=print --onto HEAD~2 --ref-action=print HEAD~..HEAD >updates &&\n> +       test_grep \"update HEAD \" updates\n> +'\n> +\n>  test_expect_success 'merge.directoryRenames=false' '\n>         # create a test case that stress-tests the rename caching\n>         git switch -c rename-onto &&\n>\n> --\n> 2.52.0.590.g1f87b77810.dirty\n\nGood extra test too.\n"},{"id":"533700","messageId":"CABPp-BHq35ySWhud-_53BXE1ed=V7MqjrpWDzgWkcs7o_Lum-A@mail.gmail.com","threadId":"64783","inReplyTo":"20260112-b4-pks-history-builtin-v10-8-e3c6aa5b4cec@pks.im","subject":"Re: [PATCH v10 8/8] builtin/history: implement \"reword\" subcommand","fromName":"Elijah Newren","fromEmail":"newren@gmail.com","sentAt":"2026-01-13T06:01:03Z","receivedAt":"2026-01-13T06:01:16Z","isPatch":true,"sender":{"key":"newren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/5455730?v=4"},"body":"On Mon, Jan 12, 2026 at 6:17 AM Patrick Steinhardt <ps@pks.im> wrote:\n>\n> Implement a new \"reword\" subcommand for git-history(1). This subcommand\n> is similar to the user performing an interactive rebase with a single\n> commit changed to use the \"reword\" instruction.\n>\n> The \"reword\" subcommand is built on top of the replay subsystem\n> instead of the sequencer. This leads to some major differences compared\n> to git-rebase(1):\n>\n>   - We do not check out the commit that is to be reworded and instead\n>     perform the operation in-memory. This has the obvious benefit of\n>     being significantly faster compared to git-rebase(1), but even more\n>     importantly it allows the user to rewrite history even if there are\n>     local changes in the working tree or in the index.\n>\n>   - We do not execute any hooks, even though we leave some room for\n>     changing this in the future.\n>\n>   - By default, all local branches that contain the commit will be\n>     rewritten. This especially helps with workflows that use stacked\n>     branches.\n>\n> Helped-by: Elijah Newren <newren@gmail.com>\n> Signed-off-by: Patrick Steinhardt <ps@pks.im>\n\nThis patch is now a mixture of my patch and yours; all the changes\nsince your v9 & my patch look good to me.\n"},{"id":"533701","messageId":"CABPp-BEDpjfxLrB5nWS+DkkTBKR+Le+jJhQiJCio=WkruchioA@mail.gmail.com","threadId":"64783","inReplyTo":"20260112-b4-pks-history-builtin-v10-0-e3c6aa5b4cec@pks.im","subject":"Re: [PATCH v10 0/8] Introduce git-history(1) command for easy history editing","fromName":"Elijah Newren","fromEmail":"newren@gmail.com","sentAt":"2026-01-13T06:01:23Z","receivedAt":"2026-01-13T06:01:36Z","isPatch":true,"sender":{"key":"newren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/5455730?v=4"},"body":"On Mon, Jan 12, 2026 at 6:17 AM Patrick Steinhardt <ps@pks.im> wrote:\n[...]\n> Changes in v10:\n>   - Split out changes that make `replay_revisions()` handle empty\n>     revision ranges properly.\n>   - Drop redundant `struct repository` parameter in\n>     `replay_revisions()`.\n>   - Move around `die_for_incompatible_opt2()`.\n>   - Fix up some of the headers in \"replay.c\".\n>   - Split out the changes that make `replay_revisions()` handle empty\n>     revision ranges properly.\n>   - Create a separate commit that fixes updating detached HEAD.\n>   - Strip \"encoding\" header as we're reencoding commit messages.\n>   - Merge the logic to update refs in in `REF_ACTION_HEAD` and\n>     `REF_ACTION_BRANCHES` modes.\n>   - Link to v9: https://lore.kernel.org/r/20260109-b4-pks-history-builtin-v9-0-8766101814c6@pks.im\n\nBoth Junio and I highlighted some changes in patch 1 (you already\nindicated your agreement with Junio), and I had some minor rewording\nsuggestions for the commit message of patch 4.  All other patches in\nv10 look good to me.\n"},{"id":"533708","messageId":"aWX0nsiqdOJMwGHH@pks.im","threadId":"64783","inReplyTo":"CABPp-BFvi0xmpP1jcU0qc7c-KhYXgzpCVbXyto3FMSFSSwrKoA@mail.gmail.com","subject":"Re: [PATCH v10 5/8] replay: support updating detached HEAD","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-01-13T07:30:38Z","receivedAt":"2026-01-13T07:30:50Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Mon, Jan 12, 2026 at 10:00:53PM -0800, Elijah Newren wrote:\n> On Mon, Jan 12, 2026 at 6:17 AM Patrick Steinhardt <ps@pks.im> wrote:\n> [...]\n> > +       refs_read_ref_full(get_main_ref_store(repo), \"HEAD\",\n> > +                          RESOLVE_REF_NO_RECURSE, NULL, &head_flags);\n> > +       *detached_head = !(head_flags & REF_ISSYMREF);\n> \n> Ah, I like this simpler way to determine if we have a detached HEAD.\n> Out of curiosity, though, is there a need to specify\n> RESOLVE_REF_NO_RECURSE?  I waffled on that for a bit and just left it\n> out.\n\nThere isn't, but I felt like it clarifies that we really only care about\nthe immediate ref's value. It's also more efficient, even though that\ndoes not matter at all in the grand scheme of things.\n\nPatrick\n"},{"id":"533709","messageId":"aWX0qcDrBoeN64WI@pks.im","threadId":"64783","inReplyTo":"CABPp-BGhtPyiVT=32NXz3k8m=+ZgPziXueM4Y8+g4dAUtN9osw@mail.gmail.com","subject":"Re: [PATCH v10 4/8] replay: support empty commit ranges","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-01-13T07:30:49Z","receivedAt":"2026-01-13T07:30:55Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Mon, Jan 12, 2026 at 10:00:27PM -0800, Elijah Newren wrote:\n> On Mon, Jan 12, 2026 at 6:17 AM Patrick Steinhardt <ps@pks.im> wrote:\n> >\n> > In a subsequent commit we're about to introduce a new user of the replay\n> > subsystem. With that new user, the range of commits that we'll want to\n> > replay will be identified implicitly via \"HEAD\". With such implicit\n> > ranges it becomes likely that the range of revisions that we're asked to\n> > replay becomes empty. This case does not make sense with git-replay(1),\n> > but with the new command it will.\n> \n> I think I know what you were trying to say, but this feels misleading;\n> it could be the commit at the tip of any branch, not just HEAD.\n> Perhaps:\n> \n> In a subsequent commit we're about to introduce a new user of the replay\n> subsystem. With that new user, the range of commits that we'll want to\n> replay will be identified implicitly via a single commit, and will\n> include all descendants of that commit to any branch. If that commit has\n> no descendants (because it's the tip of some branch), then the range of\n> revisions that we're asked to replay becomes empty. This case does not\n> make sense with git-replay(1), but with the new command it will.\n\nThat reads better indeed, thanks!\n\nPatrick\n"},{"id":"533711","messageId":"aWX0055xT2PKzgfa@pks.im","threadId":"64783","inReplyTo":"CABPp-BGOcMRerGpH5HGkUR4-DKPx+VmkWzqRt8qideZoJBrvHg@mail.gmail.com","subject":"Re: [PATCH v10 1/8] builtin/replay: extract core logic to replay revisions","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-01-13T07:31:31Z","receivedAt":"2026-01-13T07:31:37Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Mon, Jan 12, 2026 at 10:00:06PM -0800, Elijah Newren wrote:\n> On Mon, Jan 12, 2026 at 6:17 AM Patrick Steinhardt <ps@pks.im> wrote:\n> > @@ -253,6 +254,137 @@ static struct commit *pick_regular_commit(struct repository *repo,\n> [...]\n> > +       if (!result.clean) {\n> > +               out->merge_conflict = true;\n> > +               ret = -1;\n> \n> Even if you keep the special merge_conflict field for other purposes,\n> setting ret to -1 here still feels very wrong.  Negative return codes,\n> and especially -1, is used throughout the merge machinery to signal\n> unexpected errors like failure to read/write to disk.  Further, it's\n> inconsistent with how builtin/merge-tree.c works, where both in code\n> and in documentation merge result code is 0 == clean, 1 == merge\n> conflicts.  I'm worried using -1 here could cause some nasty future\n> maintenance headaches trying to understand the field if left this way,\n> at least for me.  As mentioned in the last round, the ret value here\n> should be 1.\n\nTo expand a bit why I prefer my style: it's extremely easy to get the\nreturn code wrong. We have a bunch of callsites that set the return code\ndirectly, and in theory we now have to vet every single such call site\nto verify that it will never return an error code of 1. If any function\ndoes, then exiting with 1 now becomes ambiguous. This is an error\nwaiting to happen.\n\nWith being explicit about the error code and the specific condition we\ncare about we make that issue go away. Furthermore, it also helps to\ndocument intent in the code as it is very explicit now.\n\nIn any case, I don't want this disagreement to block the patch series,\nso I'll adapt to make it return a positive value on a conflict.\n\n> > @@ -306,21 +438,11 @@ int cmd_replay(int argc,\n> [...]\n> > -       die_for_incompatible_opt2(!!advance_name_opt, \"--advance\",\n> > -                                 contained, \"--contained\");\n> > +       die_for_incompatible_opt2(!!opts.advance, \"--advance\",\n> > +                                 opts.contained, \"--contained\");\n> \n> This predates your patch, but I'm wondering if there's anything we\n> should do to clarify and/or simplify the first check.  The original\n> form of the check\n>      +       die_for_incompatible_opt2(!!opts.advance, \"--advance\",\n>      +                                 opts.contained, \"--contained\");\n> was created because (a) I had code that allowed --onto to be implicit\n> in some cases, and (b) I was thinking only of --onto and --advance\n> modes.\n> \n> However: (a) we got rid of the implicit mode selection from my private\n> branch, and (b) Siddharth added patches which added a --revert mode.\n> Those patches caused confusion around the interplay of --contained\n> with the new mode\n> (https://lore.kernel.org/git/xmqq3460ocv7.fsf@gitster.g/).  I thought\n> the synopsis:\n>            \"([--contained] --onto <newbase> | --advance <branch>) \"\n> implied clearly enough that --contained is a sub-mode of --onto, but\n> apparently that wasn't the case.  Perhaps we can strengthen that\n> understanding if we change the check here to instead be something like\n>    if (opts.contained && !opts.onto)\n>       die(\"--onto must be specified if --contained is\")\n> \n> Definitely not critical; but might be a nice cleanup.\n> \n> > +       die_for_incompatible_opt2(!!opts.advance, \"--advance\",\n> > +                                 !!opts.onto, \"--onto\");\n> \n> Yeah, and Siddharth can convert this to a die_for_incompatible_opt3()\n> call, adding \"--revert\" to it when he rerolls his series on top of\n> yours.\n\nFair, but this feels somewhat unrelated to my changes. So I'll not touch\nthis part and leave it for a future patch series to clean up, if that's\nalright with you.\n\n> > -       /* Return */\n> > -       if (ret < 0)\n> > -               exit(128);\n> > -       return ret ? 0 : 1;\n> > +       if (ret) {\n> > +               if (result.merge_conflict)\n> > +                       return 1;\n> > +               return 128;\n> > +       }\n> > +\n> > +       return 0;\n> >  }\n> \n> You mentioned that you wanted to keep the merge_conflict field due to\n> some future patches beyond the currently submitted series.  I wonder\n> if it'd make more sense to introduce that field once you introduce the\n> new patches, but i don't feel too strongly about that.  I do feel\n> strongly that the place where you set ret to -1 is problematic and\n> should be changed to 1.\n\nI'll drop the field for now.\n\nPatrick\n"}]}