From: Phillip Wood <phillip.wood123@gmail.com>
To: Patrick Steinhardt <ps@pks.im>, phillip.wood@dunelm.org.uk
Cc: Aniket Gupta <aniketgupta3001@gmail.com>,
git@vger.kernel.org, Toon Claes <toon@iotcl.com>,
Junio C Hamano <gitster@pobox.com>,
Nikita Bobko <nikita@nbobko.com>
Subject: Re: [PATCH v2] history: refuse to rewrite branches used by other worktrees
Date: Wed, 2 Dec 2026 10:59:47 +0000 [thread overview]
Message-ID: <e526719d-7442-4641-bd1e-10529108efd0@gmail.com> (raw)
In-Reply-To: <aw-5UoMbP0WOg25l@pks.im>
On 02/12/2026 06:23, Patrick Steinhardt wrote:
> On Tue, Dec 01, 2026 at 04:37:37PM +0000, Phillip Wood wrote:
>> On 01/12/2026 13:18, Patrick Steinhardt wrote:
>>>
>>> I'm probably missing something, but the way we handle `head_target`
>>> seems weird to me. It is getting resolved to whatever we have checked
>>> out in the current worktree. But do we even care about whether the
>>> currently checked out branch is used anywhere else?
>>
>> It's confusing if HEAD gets updated in one worktree because a branch was
>> rewritten in a different worktree. That's why we have checks in "git rebase"
>> to avoid rewriting branches that are checked out in other worktrees.
>
> Yes, but we resolve `head_target` only for the current worktree, we
> don't ever resolve it for any of the other worktrees. That's what's
> confusing me. Each worktree has its own HEAD, so we'd need to verify
> each of them separately, don't we? After all, rewriting history may
> cause us to modify an arbitrary branch, and if such an arbitrary branch
> is what HEAD points to in the other worktree then we suffer from the
> same problem
If a branch that we want to rewrite isn't checked out in the current
worktree then we call branch_checked_out() which tells us if it is in
use in another worktree. The reason we special case HEAD is the when a
branch is in use in more than one worktree there is guarantee which
worktree it will return, so it could return the current worktree and
we'd miss that it is checked out in another worktree as well.
>>> What I would have expected us to do is that we take the list of pending
>>> ref updates that we compute in `handle_reference_updates()` and verify
>>> that none of those refs that we're updating is checked out or being
>>> rebased/bisected. But as far as I can see, this check here comes before
>>> we have even computed those ref updates in the first place.
>>
>> We need to do the check before rewriting so that a user running "git history
>> reword" does not spend time editing the commit message, only for us to
>> refuse to update any refs.
>
> The only point in time where we _need_ to check is when doing the actual
> update, because this is where we may end up breaking the user's working
> trees. But I agree that we _should_ also check before performing any
> rewrites because that's simply a better UX.
>
>>> So we're verifying the pre-rewrite state, not the state after already
>>> having performed the history rewrite where the ref updates are now
>>> pending. And consequently, I think we don't reject some updates that may
>>> happen in worktrees.
In the current code, we do at least two revision walks (one to look for
merge commits and the other in replay_revisions(), the "squash"
subcommand walks the revs a few more times) with "--branches". There is
no guarantee that those revision walks will see the same set of commits
so that problem already exists. We'd be better to collect all the refs
that need to be updated and all the leaf commits in the first walk, and
then pass the leaf commits to replay_revisions() so that we know we're
replaying the same set of commits that we've just checked. We'd also
need replay_revisions() to give us a mapping of rewritten commits so we
can figure out the ref updates. Having a list of rewritten commits would
make it easy to support note copying as well.
>> Ideally we'd find all the branches that we want to rewrite, write those to
>> "$GIT_DIR/history/ref-updates", then walk all the other worktrees checking
>> which branch is checked out, being rebased etc. and read each worktree's
>> "$GIT_DIR/history/ref-updates" to see if there is an active "git history"
>> command rewriting the same branch. If a branch that we want to rewrite is
>> being rewritten by a command running in another worktree we'd remove
>> "$GIT_DIR/history/ref-updates" and error out. "git checkout", "git rebase"
>> etc. would also check "$GIT_DIR/history/ref-updates" as well. By writing the
>> list of refs we want to update before checking if any other worktree is
>> updating them and removing the file after we've rewritten them, I think we
>> avoid races where two commands started at the same time try and update the
>> same branch.
Unfortunately it is not that simple [1]
> I don't think we necessarily need to implement that all at once
>> though, the checks added in this patch are a good first step.
>
> We can fully avoid the race by doing the checks when the ref transaction
> is prepared though, can't we? Well, with reftables at least its trivial.
> With the files backend it's a bit of a different story though,
> unfortunately.
To prepare a ref transaction you need to know the new object id. If we
want to check we're not going to try and update the same ref as another
process before we rewrite the commits then we cannot prepare a
transaction. If we want to handle this entirely in the ref backend
(which I agree would be ideal) we'd need to be able to say "check this
ref points at this object and lock it. I'll tell you what to update it
to later" before we rewrite the commits and then "here are the new
values for those refs I asked to to lock, please update them now" after
we've rewritten the commits.
Thanks
Phillip
[1] If we have a history that looks like
- A - B - C (topic)
and run "git history drop A" and "git history reword B" at the same time
we can have
git history drop A git history reword B
collect ref updates
collect ref updates
write $GIT_DIR/history/ref-updates
check other worktrees
rewrite commits
update refs
remove $GIT_DIR/history/ref-updates
write "$GIT_DIR/history/ref-
updates"
# At this point the refs we read
# have been updated but there
# are other rewrites in
# progress.
check other worktrees
rewrite commits
update refs
# This will either fail if we
# use the old value we read at
# the start, or overwrite the
# changes made by dropping A. We
# need to re-read the refs after
# checking the other worktrees
# to check they are unchanged,
# before rewriting the commits.
Another thing to think about is what happens if we run
GIT_EDITOR="git branch feature B;:" git history reword A
should "feature" be rewritten?
next prev parent reply other threads:[~2026-12-02 10:59 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 10:39 [PATCH] history: refuse to rewrite branches used by other worktrees Aniket Gupta
2026-11-23 13:10 ` Phillip Wood
2026-11-23 21:54 ` Aniket Gupta
2026-11-24 10:49 ` Phillip Wood
2026-11-24 14:09 ` Aniket Gupta
2026-11-23 20:14 ` Junio C Hamano
2026-11-23 21:54 ` Aniket Gupta
2026-11-27 17:32 ` [PATCH v2] " Aniket Gupta
2026-11-28 0:12 ` Junio C Hamano
2026-11-28 7:37 ` Aniket Gupta
2026-11-30 11:31 ` Toon Claes
2026-11-30 12:15 ` Aniket Gupta
2026-11-30 14:39 ` Phillip Wood
2026-11-30 17:13 ` Aniket Gupta
2026-11-30 14:49 ` Phillip Wood
2026-11-30 17:13 ` Aniket Gupta
2026-12-01 16:39 ` Phillip Wood
2026-12-01 17:36 ` Aniket Gupta
2026-12-01 13:18 ` Patrick Steinhardt
2026-12-01 16:37 ` Phillip Wood
2026-12-02 6:23 ` Patrick Steinhardt
2026-12-02 10:59 ` Phillip Wood [this message]
2026-12-01 18:11 ` Aniket Gupta
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=e526719d-7442-4641-bd1e-10529108efd0@gmail.com \
--to=phillip.wood123@gmail.com \
--cc=aniketgupta3001@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=nikita@nbobko.com \
--cc=phillip.wood@dunelm.org.uk \
--cc=ps@pks.im \
--cc=toon@iotcl.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.