Currently if you do, for example:

$ git worktree add path foo

And "foo" has already been checked out at some other path, but the user
has removed it without pruning, you'll get an error that the branch is
already checked out.  It isn't meaningfully checked out, the repo's
data is just stale and no longer reflects reality.

This makes it so that if nothing is present where a worktree is
supposedly checked out, we ignore that the worktree exists, and let it
get cleaned up the next time worktrees are pruned.

(I would prune it instead, but prune isn't available from libgit
currently.)

Signed-off-by: Peter Jones <pjo...@redhat.com>
---
 branch.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/branch.c b/branch.c
index 579494738a7..60322ded953 100644
--- a/branch.c
+++ b/branch.c
@@ -360,6 +360,9 @@ void die_if_checked_out(const char *branch, int 
ignore_current_worktree)
        wt = find_shared_symref("HEAD", branch);
        if (!wt || (ignore_current_worktree && wt->is_current))
                return;
+       if (access(wt->path, F_OK) < 0 &&
+           (errno == ENOENT || errno == ENOTDIR))
+               return;
        skip_prefix(branch, "refs/heads/", &branch);
        die(_("'%s' is already checked out at '%s'"),
            branch, wt->path);
-- 
2.23.0

Reply via email to