From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.21 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796038308; b=5mybDP03lTth33ffQDy1/WR2jxg9etRLD71v3A6+rl7yKRxNr/S3SuSYSgzYvq9M8Jz6 H73dBvwr5IwvSBeaNsebQnNQ/o3YaGoEd36drQbAanh17JabeAcTnDDjIs+m0cK+Q7FxL Vly5LIK9KLgi+9XhJOzgb02jzNov9xyjNTdg2pusBLTfoP8rM/eESyLFfJ6VO2SzmPJjH XZdkSFGkUTjsyi682/gYglYV1N2Y4i8cMVDU9nxRJbbkedTr+SH83aVxMmfvBzhu4Ym6X CFEBW7rJ7DB/jMO07Qmr4F+YB/Z/amyZABx7g9glNwxAdiMQHoR7hDqKV9fCbXMnHSA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796038308; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:In-Reply-To: References:Date:Message-ID:MIME-Version:Content-Type; bh=3a2holzb8jt1IXHt3hya1p/KvvaR5h2eWUgMwkNObVI=; b=Mim7L4AG49ceUQIVWQPgB7pGZlDirVtTl5hccyhfQHnRrdVsdDs7LVxMUluWyfk3pa2y WoK7TUC+0mHXTymjU1hbokD3lKyO0wfN4A/gzBPYZhug5k9x2YKAJbuVJa4unSHb5oW0b zTnermtJhYU0UxsDt4P9JRPZ9hKF+HDAZcn04XihyUFVZOya2kSj+C5vlJuvdMIWTfqed InoX77fjAmh803Wdmkosjj/XmHnTvX6tcMtO69mSJmbkDO4IuBOJiIlzKH7MaOmSZLFxc Q7I9XjGUOpe/6iV2RXTKJLTwg6pYn26cOzzQ9KFkCe4/jxJfxbUIC1BLws1gLSGcMsA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=iotcl.com; spf=fail smtp.mailfrom=iotcl.com; dkim=pass header.d=iotcl.com header.i=@iotcl.com header.a=rsa-sha256 header.s=key1 header.b=pvVO4yHb; arc=none smtp.remote-ip=95.215.58.21 Received: from mta1.migadu.com (out-21.mta1.migadu.com [95.215.58.21]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 56635606A31C for ; Mon, 30 Nov 2026 11:31:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 56635606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=iotcl.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=iotcl.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=iotcl.com header.i=@iotcl.com header.a=rsa-sha256 header.s=key1 header.b=pvVO4yHb X-Envelope-To: git@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=G3BkH5RzLdGyM17VAopb1WFe5LucvCcCH7VkvhpKrAM=; c=simple/simple; d=iotcl.com; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796038304; v=1; x=1796643104; b=pvVO4yHbXAHl6Y0mawykloyBZQJKKil5LVg0IVurIzU+etTOHM4yC7dw2eDEXCDoijmkn4Dy NbbS5ADgy6QXly5gyE/feEUR4Rl7RNoDZGUVxABrBUIQeUkBKBEnpaDUhnCTAQ9XRzZpwvLmQS8 aZ8idoqdSzY6UuurGPQmlBGk= X-Envelope-To: git@vger.kernel.org Received: by mta12.migadu.com with ESMTPS id f05fde52d3f14f7a; Mon, 30 Nov 2026 11:31:44 +0000 X-Mizu-Trace-ID: f05fde52d3f14f7a X-Migadu-Flow: FLOW_OUT From: Toon Claes To: Aniket Gupta , git@vger.kernel.org Cc: Patrick Steinhardt , Junio C Hamano , Phillip Wood , Nikita Bobko Subject: Re: [PATCH v2] history: refuse to rewrite branches used by other worktrees In-Reply-To: <20261127173252.7794-1-aniketgupta3001@gmail.com> References: <20261123103911.98860-1-aniketgupta3001@gmail.com> <20261127173252.7794-1-aniketgupta3001@gmail.com> Date: Mon, 30 Nov 2026 12:31:40 +0100 Message-ID: <87qzg2r1wj.fsf@dev.null.iotcl.com.invalid> X-Mailing-List: git@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain Aniket Gupta writes: > By default "git history" rewrites every local branch that descends from > the commit it changes. If another worktree has such a branch checked > out, the branch moves but the index and working tree of that worktree > stay behind, so it shows the rewrite as staged changes. If the current > worktree is in the middle of rebasing the branch, "git rebase > --continue" later fails because the branch is not where the rebase > expects it. > > "git branch -f" refuses to move such branches and "git rebase > --update-refs" skips them. Do the same here: before replaying anything > look for local branches that contain the commit and are used by a > worktree and refuse to go on if there is one. Use branch_checked_out() > so that a branch that is being rebased or bisected, in any worktree, > counts as used. Updating clean worktrees would be another way, but > refusing is simpler. Refuse with --dry-run as well because it will show > what a real run will do. > > The branch that the current worktree has checked out is fine, since the > command already takes care of the index and working tree of that > worktree: "drop" updates them, "reword", "split" and "squash" keep the > tree of the tip, and "fixup" takes the staged changes. A bare repository > has no such worktree, and branch_checked_out() records only one worktree > per branch, so ask the other worktrees whether they use the branch > instead of skipping it by name. --update-refs=head refuses in the same > cases. > > Refuse to rewrite HEAD while a rebase is in progress, with either > --update-refs mode, since "git rebase --abort" would throw the result > away. > > Mention --update-refs=head, which leaves the other branches alone, with > the existing advice.historyUpdateRefs hint. > > Reported-by: Toon Claes > Reported-by: Nikita Bobko > Helped-by: Phillip Wood > Signed-off-by: Aniket Gupta > --- > Changes in v2: > > - Check the branch of the current worktree against the other > worktrees instead of skipping it by name. This fixes a bare > repository and a branch that two worktrees have checked out (Junio, > Phillip). > - Find the branches with the decorations of the revision walk instead > of a second walk (Phillip). > - Refuse to rewrite HEAD while a rebase is in progress (Phillip). > - Reword the log message and advice.adoc. > > Documentation/config/advice.adoc | 7 +- > Documentation/git-history.adoc | 7 + > builtin/history.c | 144 ++++++++++++++++- > t/t3450-history.sh | 265 +++++++++++++++++++++++++++++++ > 4 files changed, 413 insertions(+), 10 deletions(-) > > diff --git a/Documentation/config/advice.adoc b/Documentation/config/advice.adoc > index 3d91c90eda..65f3ff0e02 100644 > --- a/Documentation/config/advice.adoc > +++ b/Documentation/config/advice.adoc > @@ -60,9 +60,10 @@ all advice messages. > Shown when the user tries to delete a not fully merged > branch without the force option set. > historyUpdateRefs:: > - Shown when `git history squash` refuses because a local branch > - cannot be rewritten as a descendant of the squashed commit, to > - tell the user about `--update-refs=head`. > + Shown when `git history` refuses to run because a local branch is > + used by another worktree, or, for `squash`, because it cannot be > + rewritten as a descendant of the squashed commit, to tell the > + user about `--update-refs=head`. > ignoredHook:: > Shown when a hook is ignored because the hook is not > set as executable. > diff --git a/Documentation/git-history.adoc b/Documentation/git-history.adoc > index 1fcc30150a..243817d28a 100644 > --- a/Documentation/git-history.adoc > +++ b/Documentation/git-history.adoc > @@ -215,6 +215,13 @@ empty. > `branches`, all local branches that point to commits which are > descendants of the original commit will be rewritten. With `head`, only > the current `HEAD` reference will be rewritten. Defaults to `branches`. > ++ > +A branch that is used by another worktree, or that the current worktree is > +in the middle of rebasing or bisecting, cannot be rewritten, as the index and > +working tree of that worktree would not match the rewritten branch. With > +`branches` the command refuses in that case, so use `head` to rewrite only > +the current branch. The command refuses to rewrite `HEAD` while a rebase > +is in progress in the current worktree. > > EXAMPLES > -------- > diff --git a/builtin/history.c b/builtin/history.c > index 43ba76f5a1..d0a3287c1d 100644 > --- a/builtin/history.c > +++ b/builtin/history.c > @@ -2,6 +2,7 @@ > > #include "builtin.h" > #include "advice.h" > +#include "branch.h" > #include "cache-tree.h" > #include "commit.h" > #include "commit-reach.h" > @@ -11,6 +12,7 @@ > #include "gettext.h" > #include "hex.h" > #include "lockfile.h" > +#include "log-tree.h" > #include "merge-ort.h" > #include "oidmap.h" > #include "parse-options.h" > @@ -27,6 +29,7 @@ > #include "tree.h" > #include "tree-walk.h" > #include "unpack-trees.h" > +#include "worktree.h" > #include "wt-status.h" > > #define GIT_HISTORY_DROP_USAGE \ > @@ -260,15 +263,100 @@ static int parse_ref_action(const struct option *opt, const char *value, int uns > return 0; > } > > -static int revwalk_contains_merges(struct repository *repo, > - const struct strvec *revwalk_args) > +/* > + * Return the path of the worktree that uses "refname", if rewriting it would > + * leave the index and working tree of that worktree behind. The branch that > + * the current worktree has checked out is fine, unless another worktree has > + * it checked out as well. > + */ > +static char *branch_used_elsewhere(struct repository *repo, > + const char *refname, > + const char *head_target) > +{ > + char *path = NULL; > + > + if (head_target && !strcmp(refname, head_target)) { > + struct worktree **worktrees = get_worktrees(repo); > + > + for (int i = 0; worktrees[i]; i++) { > + if (!worktrees[i]->is_current && > + is_shared_symref(worktrees[i], "HEAD", refname)) { By reading the comment above is_shared_symref() I've noticed this leaves a gap open. When a branch is checked out in another worktree, but has an ongoing rebase or bisect, it is not detected. You can reproduce with: test_expect_success '--update-refs=head refuses a branch that has ongoing rebase in another worktree' ' test_when_finished "rm -rf repo wt" && git init repo --initial-branch=main && ( cd repo && test_commit first && test_commit second && test_commit third && test_commit fourth && git branch other third && git worktree add ../wt -b topic && test_must_fail git -C ../wt rebase --update-refs -x false first && git checkout other && git refs list >refs-expect && test_must_fail git history drop --update-refs=head second 2>err && test_grep "^error: cannot rewrite branch .other. used by worktree at .*wt" err && git refs list >refs-actual && test_cmp refs-expect refs-actual ) ' Currently the `git history drop` command doesn't fail, and this causes the rebase to fail once completed: error: update_ref failed for ref 'refs/heads/X': cannot lock ref 'refs/heads/X': is at 602027c... but expected 8087f28... If you'd try `git branch -f X second` during the ongoing rebase, you'd see it would reject it, while `git history` doesn't. The function branch_checked_out(), used by other branches than HEAD, was changed in aa7f2fd150 (branch: consider refs under 'update-refs', 2022-07-19) to learn about this situation, but is_shared_symref() isn't. I realize this is pretty much an edge case, and I'm not saying you should address that gap in is_shared_symref() in this series, but I think it's worthwhile to mention that in the commit message. > + path = xstrdup(worktrees[i]->path); > + break; > + } > + } > + free_worktrees(worktrees); > + } else { > + const char *checked_out = branch_checked_out(refname); > + > + if (checked_out) > + path = xstrdup(checked_out); > + } > + > + return path; > +} > + > +/* "git rebase --abort" would throw away a rewrite of the detached HEAD. */ > +static bool rebase_in_progress(struct repository *repo) > +{ > + struct worktree **worktrees = get_worktrees(repo); > + struct wt_status_state state = { 0 }; > + bool ret = false; > + > + for (int i = 0; worktrees[i]; i++) { > + if (worktrees[i]->is_current) { > + wt_status_check_rebase(worktrees[i], &state); > + ret = state.rebase_in_progress || > + state.rebase_interactive_in_progress; > + break; > + } > + } > + > + wt_status_state_free_buffers(&state); > + free_worktrees(worktrees); > + return ret; > +} > + > +static void collect_used_branches(struct repository *repo, > + const struct commit *commit, > + const char *head_target, > + struct string_list *used, > + bool *rewrites_head) > { > + const struct name_decoration *decoration; > + > + for (decoration = get_name_decoration(&commit->object); > + decoration; > + decoration = decoration->next) { > + char *path; > + > + if (decoration->type == DECORATION_REF_HEAD) > + *rewrites_head = true; > + if (decoration->type != DECORATION_REF_LOCAL) > + continue; > + path = branch_used_elsewhere(repo, decoration->name, head_target); > + if (path) > + string_list_append(used, decoration->name)->util = path; > + } > +} > + > +static int validate_revwalk(struct repository *repo, > + enum ref_action action, > + struct commit *original, > + const struct strvec *revwalk_args) > +{ > + struct string_list used = STRING_LIST_INIT_DUP; > struct strvec args = STRVEC_INIT; > struct rev_info revs; > + struct commit *commit; > + bool rewrites_head = action == REF_ACTION_HEAD; > + char *head_target; > int ret; > > + head_target = refs_resolve_refdup(get_main_ref_store(repo), "HEAD", > + 0, NULL, NULL); > + > strvec_pushv(&args, revwalk_args->v); > - strvec_push(&args, "--min-parents=2"); > > repo_init_revisions(repo, &revs, NULL); > > @@ -281,17 +369,59 @@ static int revwalk_contains_merges(struct repository *repo, > goto out; > } > > - if (get_revision(&revs)) { > - ret = error(_("replaying merge commits is not supported yet!")); > + if (action == REF_ACTION_BRANCHES) > + collect_used_branches(repo, original, head_target, &used, > + &rewrites_head); > + > + while ((commit = get_revision(&revs))) { > + if (commit->parents && commit->parents->next) { > + ret = error(_("replaying merge commits is not supported yet!")); > + goto out; > + } > + if (action == REF_ACTION_BRANCHES) > + collect_used_branches(repo, commit, head_target, &used, > + &rewrites_head); > + } > + > + if (action == REF_ACTION_HEAD && head_target && > + starts_with(head_target, "refs/heads/")) { > + char *path = branch_used_elsewhere(repo, head_target, head_target); > + if (path) > + string_list_append(&used, head_target)->util = path; > + } > + > + if (used.nr) { > + string_list_sort(&used); > + for (size_t i = 0; i < used.nr; i++) { > + const char *branch; > + > + if (!skip_prefix(used.items[i].string, "refs/heads/", &branch)) > + BUG("branch decoration without refs/heads/ prefix"); > + error(_("cannot rewrite branch '%s' used by worktree at '%s'"), > + branch, (const char *)used.items[i].util); > + } > + if (action == REF_ACTION_BRANCHES) > + advise_if_enabled(ADVICE_HISTORY_UPDATE_REFS, > + _("Use --update-refs=head to rewrite only " > + "the current branch and leave such branches " > + "untouched.")); There are situations where using '--update-refs=head' would not help. For example when a branch is checked out in another worktree (using --force), or when you're operating on a bare repository. > + ret = -1; > + goto out; > + } > + > + if (rewrites_head && rebase_in_progress(repo)) { > + ret = error(_("cannot rewrite HEAD while a rebase is in progress")); > goto out; > } > > - reset_revision_walk(); > ret = 0; > > out: > + reset_revision_walk(); > release_revisions(&revs); > + string_list_clear(&used, 1); > strvec_clear(&args); > + free(head_target); > return ret; > } > > @@ -354,7 +484,7 @@ static int setup_revwalk(struct repository *repo, > strvec_push(&args, "HEAD"); > } > > - ret = revwalk_contains_merges(repo, &args); > + ret = validate_revwalk(repo, action, original, &args); > if (ret < 0) > goto out; -- Laters, Toon