You can switch to any branch you like, unless it's already checked out by another worktree. You can run git-bisect. You can rebase.
But... you should not switch branch if you are, in your current worktree, in the middle of a bisect or rebase.
And that was not enforced before Git 2.38.
With Git 2.38 (Q3 2022), introduce a helper to see if a branch is already being worked on (hence should not be newly checked out in a working tree), which performs much better than the existing find_shared_symref() to replace many uses of the latter.
See commit 4b6e18f, commit b489b9d, commit 12d47e3, commit d2ba271, commit 31ad6b6 (14 Jun 2022) by Derrick Stolee (derrickstolee).
See commit 9bef0b1, commit b2463fc (18 Jun 2022) by Jeff King (peff).
(Merged by Junio C Hamano -- gitster -- in commit c2d0109, 11 Jul 2022)
branch: check for bisects and rebases
Signed-off-by: Derrick Stolee
The branch_checked_out() helper was added by the previous change, but it used an over-simplified view to check if a branch is checked out.
It only focused on the HEAD symref, but ignored whether a bisect or rebase was happening.
Teach branch_checked_out() to check for these things, and also add tests to ensure that we do not lose this functionality in the future.
Now that this test coverage exists, we can safely refactor validate_new_branchname() to use branch_checked_out().
Note that we need to prepend "refs/heads/" to the 'state.branch' after calling wt_status_check_*().
We also need to duplicate wt->path so the value is not freed at the end of the call.
Changing branch in a worktree which is being rebased (or bisected) would result in:
cannot force update the branch <abranch> checked out at <worktree path>