git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Rubén Justo" <rjusto@gmail.com>
To: Git List <git@vger.kernel.org>
Cc: Eric Sunshine <sunshine@sunshineco.com>
Subject: [PATCH] worktree: teach find_shared_symref to ignore current worktree
Date: Tue, 17 Jan 2023 01:36:41 +0100	[thread overview]
Message-ID: <eeb0c778-af0a-235c-f009-bca3abafdb15@gmail.com> (raw)

We prevent some operations from being executed on a branch checked out
in a worktree other than the current one.  An example of this was
introduced in b5cabb4 (rebase: refuse to switch to branch already
checked out elsewhere, 2020-02-23).

"find_shared_symref()" is sometimes used to find the worktree in which a
branch is checked out.  It performs its search starting with the current
worktree.

As we allow to have the same branch checked out in multiple worktrees
simultaneously...

	$ git worktree add foo
	$ git worktree add -f bar foo
	$ git checkout --ignore-other-worktrees foo

... if the branch checked out in the current worktree is also checked
out in another worktree, with "find_shared_symref()" we will not notice
this "other" working tree.

Let's teach "find_shared_symref()" to ignore the current worktree in the
search, based on the caller's needs.

Signed-off-by: Rubén Justo <rjusto@gmail.com>
---
 branch.c               | 4 ++--
 builtin/notes.c        | 2 +-
 builtin/receive-pack.c | 2 +-
 t/t3400-rebase.sh      | 3 +++
 worktree.c             | 6 +++++-
 worktree.h             | 3 ++-
 6 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/branch.c b/branch.c
index d182756827..2508e94add 100644
--- a/branch.c
+++ b/branch.c
@@ -822,8 +822,8 @@ void die_if_checked_out(const char *branch, int ignore_current_worktree)
 	struct worktree **worktrees = get_worktrees();
 	const struct worktree *wt;
 
-	wt = find_shared_symref(worktrees, "HEAD", branch);
-	if (wt && (!ignore_current_worktree || !wt->is_current)) {
+	wt = find_shared_symref(worktrees, "HEAD", branch, ignore_current_worktree);
+	if (wt) {
 		skip_prefix(branch, "refs/heads/", &branch);
 		die(_("'%s' is already checked out at '%s'"), branch, wt->path);
 	}
diff --git a/builtin/notes.c b/builtin/notes.c
index 80d9dfd25c..80326bdaab 100644
--- a/builtin/notes.c
+++ b/builtin/notes.c
@@ -869,7 +869,7 @@ static int merge(int argc, const char **argv, const char *prefix)
 		/* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
 		worktrees = get_worktrees();
 		wt = find_shared_symref(worktrees, "NOTES_MERGE_REF",
-					default_notes_ref());
+					default_notes_ref(), 0);
 		if (wt)
 			die(_("a notes merge into %s is already in-progress at %s"),
 			    default_notes_ref(), wt->path);
diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index a90af30363..18d400101c 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -1460,7 +1460,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
 	int do_update_worktree = 0;
 	struct worktree **worktrees = get_worktrees();
 	const struct worktree *worktree =
-		find_shared_symref(worktrees, "HEAD", name);
+		find_shared_symref(worktrees, "HEAD", name, 0);
 
 	/* only refs/... are allowed */
 	if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
diff --git a/t/t3400-rebase.sh b/t/t3400-rebase.sh
index d5a8ee39fc..874cfff8fe 100755
--- a/t/t3400-rebase.sh
+++ b/t/t3400-rebase.sh
@@ -407,6 +407,9 @@ test_expect_success 'refuse to switch to branch checked out elsewhere' '
 	git checkout main &&
 	git worktree add wt &&
 	test_must_fail git -C wt rebase main main 2>err &&
+	test_i18ngrep "already checked out" err &&
+	git worktree add --force wt2 main &&
+	test_must_fail git rebase main main &&
 	test_i18ngrep "already checked out" err
 '
 
diff --git a/worktree.c b/worktree.c
index aa43c64119..3d686137ef 100644
--- a/worktree.c
+++ b/worktree.c
@@ -405,7 +405,8 @@ int is_worktree_being_bisected(const struct worktree *wt,
  */
 const struct worktree *find_shared_symref(struct worktree **worktrees,
 					  const char *symref,
-					  const char *target)
+					  const char *target,
+					  int ignore_current_worktree)
 {
 	const struct worktree *existing = NULL;
 	int i = 0;
@@ -416,6 +417,9 @@ const struct worktree *find_shared_symref(struct worktree **worktrees,
 		struct ref_store *refs;
 		int flags;
 
+		if (wt->is_current && ignore_current_worktree)
+			continue;
+
 		if (wt->is_bare)
 			continue;
 
diff --git a/worktree.h b/worktree.h
index 9dcea6fc8c..a9f35ee990 100644
--- a/worktree.h
+++ b/worktree.h
@@ -147,7 +147,8 @@ void free_worktrees(struct worktree **);
  */
 const struct worktree *find_shared_symref(struct worktree **worktrees,
 					  const char *symref,
-					  const char *target);
+					  const char *target,
+					  int ignore_current_worktree);
 
 /*
  * Similar to head_ref() for all HEADs _except_ one from the current
-- 
2.39.0

             reply	other threads:[~2023-01-17  0:36 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-17  0:36 Rubén Justo [this message]
2023-01-17 23:27 ` [PATCH] worktree: teach find_shared_symref to ignore current worktree Junio C Hamano
2023-01-18 23:50   ` Rubén Justo
2023-01-19 10:48     ` Phillip Wood
2023-01-19 23:18       ` Rubén Justo
2023-01-22  1:20 ` [PATCH v2 0/3] fix die_if_checked_out() when ignore_current_worktree Rubén Justo
2023-01-22  1:23   ` [PATCH v2 1/3] branch: " Rubén Justo
2023-01-22  1:50     ` Junio C Hamano
2023-01-22 11:51       ` Rubén Justo
2023-01-22 19:58         ` Junio C Hamano
2023-01-22 23:21           ` Rubén Justo
2023-01-24 10:35             ` Phillip Wood
2023-01-26  3:07               ` Rubén Justo
2023-01-22  1:28   ` [PATCH v2 2/3] rebase: refuse to switch to a branch already checked out elsewhere (test) Rubén Justo
2023-01-22  1:28   ` [PATCH v2 3/3] switch: reject if the branch is " Rubén Justo
2023-02-04 23:19   ` [PATCH v3 0/4] fix die_if_checked_out() when ignore_current_worktree Rubén Justo
2023-02-04 23:25     ` [PATCH v3 1/4] worktree: introduce is_shared_symref() Rubén Justo
2023-02-07 10:44       ` Phillip Wood
2023-02-04 23:25     ` [PATCH v3 2/4] branch: fix die_if_checked_out() when ignore_current_worktree Rubén Justo
2023-02-06 16:56       ` Ævar Arnfjörð Bjarmason
2023-02-06 23:09         ` Rubén Justo
2023-02-07 10:50         ` Phillip Wood
2023-02-07 12:58           ` Ævar Arnfjörð Bjarmason
2023-02-04 23:26     ` [PATCH v3 3/4] rebase: refuse to switch to a branch already checked out elsewhere (test) Rubén Justo
2023-02-06 16:59       ` Ævar Arnfjörð Bjarmason
2023-02-06 23:16         ` Rubén Justo
2023-02-07 10:52           ` Phillip Wood
2023-02-08  0:43             ` Rubén Justo
2023-02-08  5:19               ` Junio C Hamano
2023-02-08 22:09                 ` Rubén Justo
2023-02-04 23:26     ` [PATCH v3 4/4] switch: reject if the branch is " Rubén Justo
2023-02-15  4:17       ` Eric Sunshine
2023-02-15 22:17         ` Rubén Justo
2023-02-25 14:14   ` [PATCH v4 0/4] fix die_if_checked_out() when ignore_current_worktree Rubén Justo
2023-02-25 14:21     ` [PATCH v4 1/4] worktree: introduce is_shared_symref() Rubén Justo
2023-02-25 14:22     ` [PATCH v4 2/4] branch: fix die_if_checked_out() when ignore_current_worktree Rubén Justo
2023-02-25 14:22     ` [PATCH v4 3/4] rebase: refuse to switch to a branch already checked out elsewhere (test) Rubén Justo
2023-02-25 14:22     ` [PATCH v4 4/4] switch: reject if the branch is " Rubén Justo
2023-02-25 22:50     ` [PATCH v4 0/4] fix die_if_checked_out() when ignore_current_worktree Junio C Hamano
2023-02-27  0:00       ` Rubén Justo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=eeb0c778-af0a-235c-f009-bca3abafdb15@gmail.com \
    --to=rjusto@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=sunshine@sunshineco.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).