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

[PATCH v2 1/4] libgit: Add a read-only helper to test the worktree lock

From
Peter Jones <pjones@redhat.com>
Date
Oct 18, 2019, 19:45 UTC
Message-ID
<20191018194542.1316981-1-pjones@redhat.com>
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
Previous: Peter JonesNext: Peter Jones
Message 5 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.