> What follows is a bunch of additional logic to verify whether we'd end > up rewriting any commit that is checked out anywhere. Would it make > sense to maybe split this up into a sequence of commits, where each of > the commits introduces one such new check?
I feel that there are no separate checks left to split now, primarily because the branches that are checked out, rebased, bisected or going to be updated by "rebase --update-refs", all come from one list in branch.c, and history.c only has to check against it. A separate commit for the new function in branch.c would add a function that nothing uses yet, so maybe we can keep it as one patch?
> s/int/size_t/
The check has moved to branch.c, so both worktree loops shall be gone in v3.
> So doesn't this make `rebase_in_progress()` obsolete, at least for > worktrees other than the current one?
I dont think so. It only looks at the current worktree, which is_shared_symref() skips. If the current worktree is rebasing a detached HEAD, there is no branch to refuse, so this function is what stops us from rewriting HEAD in the middle of it. Rebases of a branch in other worktrees will be covered by the list in branch.c in v3.
> What this function doesn't check is whether any of the worktrees is > detached on a specific commit ID that we'd end up rewriting. But I > think that's actually intentional, as it doesn't really matter for our > usecase of git-history(1).
It is intentional. I had tried a worktree that is detached on a rewritten commit: its HEAD stays where it was and its status is clean.
> 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?
Yup. As Phillip says, it is confusing if the HEAD of one worktree changes because a branch was rewritten from another. That is why head mode refuses the current branch when another worktree uses it. In v2, the current branch could still be rewritten while another worktree's "rebase --update-refs" was about to update it.
> 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.
Yes v2 missed the --update-refs case, and v3 will fix it. The refs we check are the same ones that get updated as both come from the same commits. We check early on purpose. If we waited, "reword" and the other commands would first ask for a message and only then refuse.
> But even if we detected all of them, we would now be racy as we should > perform the validation at the point in time where we have already > gathered expected old and new commit IDs for the ref updates, > shouldn't we?
This indeed is a gap. If a worktree starts using a branch while the history is being rewritten, for eg while the editor is open, we will not notice. I repro'd that with a "reword" editor that adds a worktree. Phillip sketched a way to close it with a file that records the refs being rewritten and that other worktrees and commands check. The checks seemed like a good first step. Maybe we can leave that for later?