Currently, if you do:

$ git branch zonk origin/master
$ git worktree add zonk zonk
$ rm -rf zonk
$ git branch -d zonk

You get the following error:

$ git branch -d zonk
error: Cannot delete branch 'zonk' checked out at 
'/home/pjones/devel/kernel.org/git/zonk'

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, deleting the branch will automatically prune it.

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

diff --git a/builtin/branch.c b/builtin/branch.c
index 2ef214632f0..a2a1e89c66b 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -236,13 +236,17 @@ static int delete_branches(int argc, const char **argv, 
int force, int kinds,
                if (kinds == FILTER_REFS_BRANCHES) {
                        const struct worktree *wt =
                                find_shared_symref("HEAD", name);
-                       if (wt) {
+                       int rc = -1;
+
+                       if (wt && (rc = prune_worktree_if_missing(wt)) < 0) {
                                error(_("Cannot delete branch '%s' "
                                        "checked out at '%s'"),
                                      bname.buf, wt->path);
                                ret = 1;
                                continue;
                        }
+                       if (rc >= 0)
+                               delete_worktrees_dir_if_empty();
                }
 
                target = resolve_refdup(name,
-- 
2.23.0

Reply via email to