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

[PATCH v2 1/3] branch: fix die_if_checked_out() when ignore_current_worktree

From
Rubén Justo <rjusto@gmail.com>
Date
Jan 22, 2023, 01:23 UTC
Message-ID
<17f267b1-7f5e-2fb6-fb14-1c37ec355e65@gmail.com>
In-Reply-To
<f7f45f54-9261-45ea-3399-8ba8dee6832b@gmail.com>

In 8d9fdd7 (worktree.c: check whether branch is rebased in another worktree, 2016-04-22) die_if_checked_out() learned a new option ignore_current_worktree, to modify the operation from "die() if the branch is checked out in any worktree" to "die() if the branch is checked out in any worktree other than the current one".

Unfortunately we implemented it by checking the flag is_current in the worktree that find_shared_symref() returns.

When the same branch is checked out in several worktrees simultaneously, find_shared_symref() will return the first matching worktree in the list composed by get_worktrees(). If one of the worktrees with the checked out branch is the current worktree, find_shared_symref() may or may not return it, depending on the order of the list.

Let's stop using find_shared_symref() in die_if_checked_out(), to handle correctly ignore_current_worktree.

Signed-off-by: Rubén Justo <rjusto@gmail.com>
---
 branch.c   | 16 +++++++++++-----
 worktree.c | 54 +++++++++++++++++++++++++++++-------------------------
 worktree.h |  6 ++++++
 3 files changed, 46 insertions(+), 30 deletions(-)
diff --git a/branch.c b/branch.c
index d182756827..2378368415 100644
--- a/branch.c
+++ b/branch.c
@@ -820,12 +820,18 @@ void remove_branch_state(struct repository *r, int verbose)
 void die_if_checked_out(const char *branch, int ignore_current_worktree)
 {
 	struct worktree **worktrees = get_worktrees();
-	const struct worktree *wt;
+	int i;
+
+	for (i = 0; worktrees[i]; i++)
+	{
+		if (worktrees[i]->is_current && ignore_current_worktree)
+			continue;
 
-	wt = find_shared_symref(worktrees, "HEAD", branch);
-	if (wt && (!ignore_current_worktree || !wt->is_current)) {
-		skip_prefix(branch, "refs/heads/", &branch);
-		die(_("'%s' is already checked out at '%s'"), branch, wt->path);
+		if (is_shared_symref(worktrees[i], "HEAD", branch)) {
+			skip_prefix(branch, "refs/heads/", &branch);
+			die(_("'%s' is already checked out at '%s'"),
+				branch, worktrees[i]->path);
+		}
 	}
 
 	free_worktrees(worktrees);
diff --git a/worktree.c b/worktree.c
index aa43c64119..d500d69e4c 100644
--- a/worktree.c
+++ b/worktree.c
@@ -403,6 +403,33 @@ int is_worktree_being_bisected(const struct worktree *wt,
  * bisect). New commands that do similar things should update this
  * function as well.
  */
+int is_shared_symref(const struct worktree *wt, const char *symref,
+		     const char *target)
+{
+	const char *symref_target;
+	struct ref_store *refs;
+	int flags;
+
+	if (wt->is_bare)
+		return 0;
+
+	if (wt->is_detached && !strcmp(symref, "HEAD")) {
+		if (is_worktree_being_rebased(wt, target))
+			return 1;
+		if (is_worktree_being_bisected(wt, target))
+			return 1;
+	}
+
+	refs = get_worktree_ref_store(wt);
+	symref_target = refs_resolve_ref_unsafe(refs, symref, 0,
+						NULL, &flags);
+	if ((flags & REF_ISSYMREF) &&
+	    symref_target && !strcmp(symref_target, target))
+		return 1;
+
+	return 0;
+}
+
 const struct worktree *find_shared_symref(struct worktree **worktrees,
 					  const char *symref,
 					  const char *target)
@@ -411,31 +438,8 @@ const struct worktree *find_shared_symref(struct worktree **worktrees,
 	int i = 0;
 
 	for (i = 0; worktrees[i]; i++) {
-		struct worktree *wt = worktrees[i];
-		const char *symref_target;
-		struct ref_store *refs;
-		int flags;
-
-		if (wt->is_bare)
-			continue;
-
-		if (wt->is_detached && !strcmp(symref, "HEAD")) {
-			if (is_worktree_being_rebased(wt, target)) {
-				existing = wt;
-				break;
-			}
-			if (is_worktree_being_bisected(wt, target)) {
-				existing = wt;
-				break;
-			}
-		}
-
-		refs = get_worktree_ref_store(wt);
-		symref_target = refs_resolve_ref_unsafe(refs, symref, 0,
-							NULL, &flags);
-		if ((flags & REF_ISSYMREF) &&
-		    symref_target && !strcmp(symref_target, target)) {
-			existing = wt;
+		if (is_shared_symref(worktrees[i], symref, target)) {
+			existing = worktrees[i];
 			break;
 		}
 	}
diff --git a/worktree.h b/worktree.h
index 9dcea6fc8c..7889c4761d 100644
--- a/worktree.h
+++ b/worktree.h
@@ -149,6 +149,12 @@ const struct worktree *find_shared_symref(struct worktree **worktrees,
 					  const char *symref,
 					  const char *target);
 
+/*
+ * Returns true if a symref points to a ref in a worktree.
+ */
+int is_shared_symref(const struct worktree *wt,
+		     const char *symref, const char *target);
+
 /*
  * Similar to head_ref() for all HEADs _except_ one from the current
  * worktree, which is covered by head_ref().
-- 
2.39.0
Previous: Rubén JustoNext: Junio C Hamano
Message 7 of 40 in “worktree: teach find_shared_symref to ignore current worktree”
  1. worktree: teach find_shared_symref to ignore current worktreeRubén Justo, Jan 17, 2023
  2. Junio C HamanoJan 17, 2023
  3. Rubén JustoJan 18, 2023
  4. Phillip WoodJan 19, 2023
  5. Rubén JustoJan 19, 2023
  6. 0/3 fix die_if_checked_out() when ignore_current_worktreeRubén Justo, Jan 22, 2023
  7. 1/3 branch: fix die_if_checked_out() when ignore_current_worktreeRubén Justo, Jan 22, 2023
  8. Junio C HamanoJan 22, 2023
  9. Rubén JustoJan 22, 2023
  10. Junio C HamanoJan 22, 2023
  11. Rubén JustoJan 22, 2023
  12. Phillip WoodJan 24, 2023
  13. Rubén JustoJan 26, 2023
  14. 2/3 rebase: refuse to switch to a branch already checked out elsewhere (test)Rubén Justo, Jan 22, 2023
  15. 3/3 switch: reject if the branch is already checked out elsewhere (test)Rubén Justo, Jan 22, 2023
  16. 0/4 fix die_if_checked_out() when ignore_current_worktreeRubén Justo, Feb 4, 2023
  17. 1/4 worktree: introduce is_shared_symref()Rubén Justo, Feb 4, 2023
  18. Phillip WoodFeb 7, 2023
  19. 2/4 branch: fix die_if_checked_out() when ignore_current_worktreeRubén Justo, Feb 4, 2023
  20. Ævar Arnfjörð BjarmasonFeb 6, 2023
  21. Rubén JustoFeb 6, 2023
  22. Phillip WoodFeb 7, 2023
  23. Ævar Arnfjörð BjarmasonFeb 7, 2023
  24. 3/4 rebase: refuse to switch to a branch already checked out elsewhere (test)Rubén Justo, Feb 4, 2023
  25. Ævar Arnfjörð BjarmasonFeb 6, 2023
  26. Rubén JustoFeb 6, 2023
  27. Phillip WoodFeb 7, 2023
  28. Rubén JustoFeb 8, 2023
  29. Junio C HamanoFeb 8, 2023
  30. Rubén JustoFeb 8, 2023
  31. 4/4 switch: reject if the branch is already checked out elsewhere (test)Rubén Justo, Feb 4, 2023
  32. Eric SunshineFeb 15, 2023
  33. Rubén JustoFeb 15, 2023
  34. 0/4 fix die_if_checked_out() when ignore_current_worktreeRubén Justo, Feb 25, 2023
  35. 1/4 worktree: introduce is_shared_symref()Rubén Justo, Feb 25, 2023
  36. 2/4 branch: fix die_if_checked_out() when ignore_current_worktreeRubén Justo, Feb 25, 2023
  37. 3/4 rebase: refuse to switch to a branch already checked out elsewhere (test)Rubén Justo, Feb 25, 2023
  38. 4/4 switch: reject if the branch is already checked out elsewhere (test)Rubén Justo, Feb 25, 2023
  39. Junio C HamanoFeb 25, 2023
  40. Rubén JustoFeb 27, 2023

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.