worktree: teach worktree_lock_reason() to gently handle main worktree
authorRafael Silva <redacted>
Tue, 19 Jan 2021 21:27:35 +0000 (22:27 +0100)
committerJunio C Hamano <redacted>
Sat, 30 Jan 2021 17:57:20 +0000 (09:57 -0800)
commiteb36135af7b03fbaab2d3091fa7f5c62a164ff43
treef393effd7a6e69121708c53f98fcf0c262b65c23
parentfc0c7d5e9e9e396afdd669019e7635773b1d1423
worktree: teach worktree_lock_reason() to gently handle main worktree

worktree_lock_reason() aborts with an assertion failure when called on
the main worktree since locking the main worktree is nonsensical. Not
only is this behavior undocumented, thus callers might not even be aware
that the call could potentially crash the program, but it also forces
clients to be extra careful:

    if (!is_main_worktree(wt) && worktree_locked_reason(...))
        ...

Since we know that locking makes no sense in the context of the main
worktree, we can simply return false for the main worktree, thus making
client code less complex by eliminating the need for the callers to have
inside knowledge about the implementation:

    if (worktree_lock_reason(...))
        ...

Helped-by: Eric Sunshine <redacted>
Signed-off-by: Rafael Silva <redacted>
Reviewed-by: Eric Sunshine <redacted>
Signed-off-by: Junio C Hamano <redacted>
builtin/worktree.c
worktree.c
git clone https://git.99rst.org/PROJECT