git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 4/4] Make "git branch -d" prune missing worktrees automatically.

From
Peter Jones <pjones@redhat.com>
Date
Oct 18, 2019, 19:45 UTC
Message-ID
<20191018194542.1316981-4-pjones@redhat.com>
In-Reply-To
<20191018194542.1316981-1-pjones@redhat.com>
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 <pjones@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
Previous: Junio C HamanoNext: Peter Jones
Message 8 of 15 in “Make die_if_checked_out() ignore missing worktree checkouts.”
  1. 1/2 Make die_if_checked_out() ignore missing worktree checkouts.Peter Jones, Oct 17, 2019
  2. 2/2 Make "git branch -d" prune missing worktrees automatically.Peter Jones, Oct 17, 2019
  3. Eric SunshineOct 17, 2019
  4. Peter JonesOct 18, 2019
  5. 1/4 libgit: Add a read-only helper to test the worktree lockPeter Jones, Oct 18, 2019
  6. 2/4 libgit: Expose more worktree functionality.Peter Jones, Oct 18, 2019
  7. Junio C HamanoOct 21, 2019
  8. 4/4 Make "git branch -d" prune missing worktrees automatically.Peter Jones, Oct 18, 2019
  9. 3/4 Make die_if_checked_out() prune missing checkouts of unlocked worktrees.Peter Jones, Oct 18, 2019
  10. Junio C HamanoOct 21, 2019
  11. Junio C HamanoOct 21, 2019
  12. Eric SunshineNov 8, 2019
  13. Phillip WoodNov 8, 2019
  14. Eric SunshineNov 9, 2019
  15. SZEDER GáborOct 17, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.