All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peter Jones <pjones@redhat.com>
To: git@vger.kernel.org
Cc: "Eric Sunshine" <sunshine@sunshineco.com>,
	"SZEDER Gábor" <szeder.dev@gmail.com>,
	"Peter Jones" <pjones@redhat.com>
Subject: [PATCH v2 1/4] libgit: Add a read-only helper to test the worktree lock
Date: Fri, 18 Oct 2019 15:45:39 -0400	[thread overview]
Message-ID: <20191018194542.1316981-1-pjones@redhat.com> (raw)
In-Reply-To: <20191018194317.wvqphshpkfskvkyh@redhat.com>

Add the function is_worktree_locked(), which is a helper to tell if a
worktree is locked without having to be able to modify it.

Signed-off-by: Peter Jones <pjones@redhat.com>
---
 builtin/worktree.c |  2 +-
 worktree.c         | 16 ++++++++++++++++
 worktree.h         |  5 +++++
 3 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/builtin/worktree.c b/builtin/worktree.c
index 4de44f579af..86305cc1fe1 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -245,7 +245,7 @@ static void validate_worktree_add(const char *path, const struct add_opts *opts)
 	if (!wt)
 		goto done;
 
-	locked = !!worktree_lock_reason(wt);
+	locked = is_worktree_locked(wt);
 	if ((!locked && opts->force) || (locked && opts->force > 1)) {
 		if (delete_git_dir(wt->id))
 		    die(_("unable to re-add worktree '%s'"), path);
diff --git a/worktree.c b/worktree.c
index 5b4793caa34..4924805c389 100644
--- a/worktree.c
+++ b/worktree.c
@@ -244,6 +244,22 @@ int is_main_worktree(const struct worktree *wt)
 	return !wt->id;
 }
 
+int is_worktree_locked(const struct worktree *wt)
+{
+	struct strbuf path = STRBUF_INIT;
+	int locked = 0;
+
+	if (wt->lock_reason_valid && wt->lock_reason)
+		return 1;
+
+	strbuf_addstr(&path, worktree_git_path(wt, "locked"));
+	if (file_exists(path.buf))
+		locked = 1;
+
+	strbuf_release(&path);
+	return locked;
+}
+
 const char *worktree_lock_reason(struct worktree *wt)
 {
 	assert(!is_main_worktree(wt));
diff --git a/worktree.h b/worktree.h
index caecc7a281c..5ff16c414b5 100644
--- a/worktree.h
+++ b/worktree.h
@@ -56,6 +56,11 @@ struct worktree *find_worktree(struct worktree **list,
  */
 int is_main_worktree(const struct worktree *wt);
 
+/*
+ * Return true if the given worktree is locked
+ */
+int is_worktree_locked(const struct worktree *wt);
+
 /*
  * Return the reason string if the given worktree is locked or NULL
  * otherwise.
-- 
2.23.0


  reply	other threads:[~2019-10-18 19:46 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-17 16:28 [PATCH 1/2] Make die_if_checked_out() ignore missing worktree checkouts Peter Jones
2019-10-17 16:28 ` [PATCH 2/2] Make "git branch -d" prune missing worktrees automatically Peter Jones
2019-10-17 17:28   ` Eric Sunshine
2019-10-18 19:43     ` Peter Jones
2019-10-18 19:45       ` Peter Jones [this message]
2019-10-18 19:45         ` [PATCH v2 2/4] libgit: Expose more worktree functionality Peter Jones
2019-10-21  1:59           ` Junio C Hamano
2019-10-18 19:45         ` [PATCH v2 3/4] Make die_if_checked_out() prune missing checkouts of unlocked worktrees Peter Jones
2019-10-21  2:09           ` Junio C Hamano
2019-10-18 19:45         ` [PATCH v2 4/4] Make "git branch -d" prune missing worktrees automatically Peter Jones
2019-10-21  1:36         ` [PATCH v2 1/4] libgit: Add a read-only helper to test the worktree lock Junio C Hamano
2019-11-08 10:14       ` [PATCH 2/2] Make "git branch -d" prune missing worktrees automatically Eric Sunshine
2019-11-08 14:56         ` Phillip Wood
2019-11-09 11:34           ` Eric Sunshine
2019-10-17 16:44 ` [PATCH 1/2] Make die_if_checked_out() ignore missing worktree checkouts SZEDER Gábor

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=20191018194542.1316981-1-pjones@redhat.com \
    --to=pjones@redhat.com \
    --cc=git@vger.kernel.org \
    --cc=sunshine@sunshineco.com \
    --cc=szeder.dev@gmail.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.