This provides an API for checking if a worktree is locked. We need to
check this to avoid double locking a worktree, or try to unlock one when
it's not even locked.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclo...@gmail.com>
---
 worktree.c | 18 ++++++++++++++++++
 worktree.h |  6 ++++++
 2 files changed, 24 insertions(+)

diff --git a/worktree.c b/worktree.c
index 37fea3d..6805deb 100644
--- a/worktree.c
+++ b/worktree.c
@@ -243,6 +243,24 @@ int is_main_worktree(const struct worktree *wt)
        return wt && !wt->id;
 }
 
+const char *is_worktree_locked(const struct worktree *wt)
+{
+       static struct strbuf sb = STRBUF_INIT;
+
+       if (!file_exists(git_common_path("worktrees/%s/locked", wt->id)))
+               return NULL;
+
+       strbuf_reset(&sb);
+       if (strbuf_read_file(&sb,
+                            git_common_path("worktrees/%s/locked", wt->id),
+                            0) < 0)
+               die_errno(_("failed to read '%s'"),
+                         git_common_path("worktrees/%s/locked", wt->id));
+
+       strbuf_rtrim(&sb);
+       return sb.buf;
+}
+
 int is_worktree_being_rebased(const struct worktree *wt,
                              const char *target)
 {
diff --git a/worktree.h b/worktree.h
index 77a9e09..12906be 100644
--- a/worktree.h
+++ b/worktree.h
@@ -40,6 +40,12 @@ extern struct worktree *find_worktree_by_path(struct 
worktree **list,
  */
 extern int is_main_worktree(const struct worktree *wt);
 
+/*
+ * Return the reason string if the given worktree is locked. Return
+ * NULL otherwise.
+ */
+extern const char *is_worktree_locked(const struct worktree *wt);
+
 /*
  * Free up the memory for worktree
  */
-- 
2.8.2.524.g6ff3d78

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to