From: Patrick Steinhardt Date: Wed, 03 Jun 2026 16:13:59 GMT Subject: [PATCH v2 0/9] builtin/history: introduce "drop" subcommand Message-ID: <20260603-b4-pks-history-drop-v2-0-742cb5b5176d@pks.im> In-Reply-To: <20260601-b4-pks-history-drop-v1-0-643e32340d55@pks.im> Hi, this small patch series introduces the new "drop" subcommand for git-history(1). As a reader might guess, the command does exactly that: given a commit, it will drop that commit from the commit history and replay descendant branches on top of it. Changes in v2: - Reworked `update_worktree()` to use `reset_head()`, which required a bunch of changes to `reset_head()`. - Consistently mention the commit that cannot be dropped as part of error messages. - Adapt error message to not use backticks anymore. - Drop redundant "--graph" flag in a test helper. - Link to v1: https://patch.msgid.link/20260601-b4-pks-history-drop-v1-0-643e32340d55@pks.im Thanks! Patrick --- Patrick Steinhardt (9): read-cache: split out function to drop unmerged entries to stage 0 reset: drop `USE_THE_REPOSITORY_VARIABLE` reset: modernize flags passed to `reset_head()` reset: introduce dry-run mode reset: introduce ability to skip reference updates reset: allow the caller to specify the current HEAD object reset: stop assuming that the caller passes in a clean index builtin/history: split handling of ref updates into two phases builtin/history: implement "drop" subcommand Documentation/git-history.adoc | 38 ++- builtin/history.c | 289 ++++++++++++++++++++--- builtin/rebase.c | 2 +- read-cache-ll.h | 1 + read-cache.c | 12 +- reset.c | 91 +++++--- reset.h | 44 +++- sequencer.c | 2 +- t/meson.build | 1 + t/t3454-history-drop.sh | 513 +++++++++++++++++++++++++++++++++++++++++ 10 files changed, 905 insertions(+), 88 deletions(-) Range-diff versus v1: -: ---------- > 1: d6e4f3193d read-cache: split out function to drop unmerged entries to stage 0 -: ---------- > 2: 2eef3d77e4 reset: drop `USE_THE_REPOSITORY_VARIABLE` -: ---------- > 3: cbfd105ca3 reset: modernize flags passed to `reset_head()` -: ---------- > 4: bbb7f3c61c reset: introduce dry-run mode -: ---------- > 5: b3d036cea1 reset: introduce ability to skip reference updates -: ---------- > 6: 7df1787049 reset: allow the caller to specify the current HEAD object -: ---------- > 7: f58254bbb8 reset: stop assuming that the caller passes in a clean index 1: 2a4b683b8c = 8: 9dee781f0a builtin/history: split handling of ref updates into two phases 2: 02712e70d3 ! 9: 2b4e4075e6 builtin/history: implement "drop" subcommand @@ Documentation/git-history.adoc: The staged addition of `unrelated.txt` has been ## builtin/history.c ## @@ + #include "read-cache.h" + #include "refs.h" + #include "replay.h" ++#include "reset.h" + #include "revision.h" #include "sequencer.h" #include "strvec.h" #include "tree.h" @@ builtin/history.c: static int cmd_history_split(int argc, + const struct commit *new_head, + bool dry_run) +{ -+ struct index_state index = INDEX_STATE_INIT(repo); -+ struct unpack_trees_options opts = { 0 }; -+ struct lock_file lock = LOCK_INIT; -+ struct tree_desc desc[2] = { 0 }; -+ char *desc_buf[2] = { 0 }; -+ int ret; -+ -+ if (!dry_run && -+ repo_hold_locked_index(repo, &lock, LOCK_REPORT_ON_ERROR) < 0) -+ return -1; -+ -+ if (read_index_from(&index, repo->index_file, repo->gitdir) < 0) { -+ ret = error(_("unable to read index")); -+ goto out; -+ } -+ -+ setup_unpack_trees_porcelain(&opts, "history drop"); -+ opts.head_idx = 1; -+ opts.src_index = &index; -+ opts.dst_index = &index; -+ opts.fn = twoway_merge; -+ opts.merge = 1; -+ opts.update = !dry_run; -+ opts.dry_run = dry_run; -+ opts.preserve_ignored = 0; -+ init_checkout_metadata(&opts.meta, NULL, &new_head->object.oid, NULL); -+ -+ desc_buf[0] = fill_tree_descriptor(repo, &desc[0], &old_head->object.oid); -+ desc_buf[1] = fill_tree_descriptor(repo, &desc[1], &new_head->object.oid); -+ -+ if (unpack_trees(2, desc, &opts)) { -+ ret = -1; -+ goto out; -+ } -+ -+ if (!dry_run) { -+ cache_tree_free(&index.cache_tree); -+ -+ if (write_locked_index(&index, &lock, COMMIT_LOCK)) { -+ ret = error(_("could not write index")); -+ goto out; -+ } -+ } -+ -+ ret = 0; -+ -+out: -+ clear_unpack_trees_porcelain(&opts); -+ rollback_lock_file(&lock); -+ release_index(&index); -+ free(desc_buf[0]); -+ free(desc_buf[1]); -+ return ret; ++ struct reset_head_opts opts = { ++ .oid_from = &old_head->object.oid, ++ .oid = &new_head->object.oid, ++ .flags = RESET_HEAD_SKIP_REF_UPDATES, ++ }; ++ if (dry_run) ++ opts.flags |= RESET_HEAD_DRY_RUN; ++ return reset_head(repo, &opts); +} + +static int find_head_tree_change(struct repository *repo, @@ builtin/history.c: static int cmd_history_split(int argc, + argv[0]); + goto out; + } else if (original->parents->next) { -+ ret = error(_("cannot drop merge commit")); ++ ret = error(_("cannot drop merge commit: %s"), argv[0]); + goto out; + } + @@ builtin/history.c: static int cmd_history_split(int argc, + } + + if (head_moves && update_worktree(repo, old_head, new_head, false) < 0) { -+ ret = error(_("failed to update working tree; " -+ "run `git checkout HEAD` to sync")); ++ ret = error(_("could not update working tree to new commit %s"), ++ oid_to_hex(&new_head->object.oid)); + goto out; + } + @@ t/t3454-history-drop.sh (new) + +expect_graph () { + cat >expect && -+ lib_test_cmp_graph --graph --format=%s "$@" ++ lib_test_cmp_graph --format=%s "$@" +} + +expect_log () { --- base-commit: 1666c1265231b0bc5f613fbbf3f0a9896cdef76e change-id: 20260601-b4-pks-history-drop-28f6c6399e7b