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 <pclouds@xxxxxxxxx> --- worktree.c | 21 +++++++++++++++++++++ worktree.h | 6 ++++++ 2 files changed, 27 insertions(+) diff --git a/worktree.c b/worktree.c index 6b3015b..cb30af3 100644 --- a/worktree.c +++ b/worktree.c @@ -231,6 +231,27 @@ int is_main_worktree(const struct worktree *wt) return !wt->id; } +const char *is_worktree_locked(const struct worktree *wt) +{ + static struct strbuf sb = STRBUF_INIT; + struct strbuf path = STRBUF_INIT; + + strbuf_git_common_path(&path, "worktrees/%s/locked", wt->id); + + if (!file_exists(path.buf)) { + strbuf_release(&path); + return NULL; + } + + strbuf_reset(&sb); + if (strbuf_read_file(&sb, path.buf, 0) < 0) + die_errno(_("failed to read '%s'"), path.buf); + strbuf_release(&path); + + strbuf_trim(&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 39f1aa2..3a780c2 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. Return value is only valid until the next invocation. + */ +extern const char *is_worktree_locked(const struct worktree *wt); + /* * Free up the memory for worktree(s) */ -- 2.8.2.524.g6ff3d78 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html