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, and the worktree is not locked, 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, and it is not locked, we ignore that the
worktree exists, then it should be cleaned up.

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

diff --git a/branch.c b/branch.c
index 579494738a7..760ef387144 100644
--- a/branch.c
+++ b/branch.c
@@ -360,6 +360,12 @@ 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 (prune_worktree_if_missing(wt) >= 0) {
+               delete_worktrees_dir_if_empty();
+               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