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

[PATCH v2 3/4] Make die_if_checked_out() prune missing checkouts of unlocked worktrees.

From
Peter Jones <pjones@redhat.com>
Date
Oct 18, 2019, 19:45 UTC
Message-ID
<20191018194542.1316981-3-pjones@redhat.com>
In-Reply-To
<20191018194542.1316981-1-pjones@redhat.com>
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 <pjones@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
Previous: Peter JonesNext: Junio C Hamano
Message 9 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.