]> git.ipfire.org Git - thirdparty/git.git/commit - branch.c
branch: update the message to refuse touching a branch in-use
authorJunio C Hamano <gitster@pobox.com>
Fri, 21 Jul 2023 21:53:12 +0000 (14:53 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 21 Jul 2023 22:30:57 +0000 (15:30 -0700)
commit4970bedef2e077f78baac5c6e825d17786be735b
treeda5e442f341eed31aa45a152b71c2e3598add47f
parentaa9166bcc0ba654fc21f198a30647ec087f733ed
branch: update the message to refuse touching a branch in-use

The "git branch -f" command can refuse to force-update a branch that
is used by another worktree.  The original rationale for this
behaviour was that updating a branch that is checked out in another
worktree, without making a matching change to the index and the
working tree files in that worktree, will lead to a very confused
user.  "git diff HEAD" will no longer give a useful patch, because
HEAD is a commit unrelated to what the index and the working tree in
the worktree were based on, for example.

These days, the same mechanism also protects branches that are being
rebased or bisected, and the same machanism is expected to be the
right place to add more checks, when we decide to protect branches
undergoing other kinds of operations.  We however forgot to rethink
the messaging, which originally said that we are refusing to touch
the branch because it is "checked out" elsewhere, when d2ba271a
(branch: check for bisects and rebases, 2022-06-14) started to
protect branches that are being rebased or bisected.

The spirit of the check has always been that we do not want to
disrupt the use of the same branch in other worktrees.  Let's reword
the message slightly to say that the branch is "used by" another
worktree, instead of "checked out".

We could teach the branch.c:prepare_checked_out_branches() function
to remember why it decided that a particular branch needs protecting
(i.e. was it because it was checked out?  being bisected?  something
else?) in addition to which worktree the branch was in use, and use
that in the error message to say "you cannot force update this
branch because it is being bisected in the worktree X", etc., but it
is dubious that such extra complexity is worth it.  The message
already tells which directory the worktree in question is, and it
should be just a "chdir" away for the user to find out what state it
is in, if the user felt curious enough.  So let's not go there yet.

Helped-by: Josh Sref <jsoref@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
branch.c
t/t2407-worktree-heads.sh