Subject: [PATCH v3 4/6] worktree.c: retrieve lock status (and optionally reason) in get_worktrees()

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Mon, 30 May 2016 17:49:37 +0700

We need this later to avoid double locking a worktree, or unlocking one
when it's not even locked.

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 worktree.c | 12 ++++++++++++
 worktree.h |  1 +
 2 files changed, 13 insertions(+)

diff --git a/worktree.c b/worktree.c
index 6432eec..c6a64b1 100644
--- a/worktree.c
+++ b/worktree.c
@@ -98,6 +98,7 @@ static struct worktree *get_main_worktree(void)
 	worktree->is_detached = is_detached;
 	worktree->is_current = 0;
 	add_head_info(&head_ref, worktree);
+	worktree->lock_reason = NULL;
 
 done:
 	strbuf_release(&path);
@@ -144,6 +145,17 @@ static struct worktree *get_linked_worktree(const char *id)
 	worktree->is_current = 0;
 	add_head_info(&head_ref, worktree);
 
+	strbuf_reset(&path);
+	strbuf_addf(&path, "%s/worktrees/%s/locked", get_git_common_dir(), id);
+	if (file_exists(path.buf)) {
+		struct strbuf lock_reason = STRBUF_INIT;
+		if (strbuf_read_file(&lock_reason, path.buf, 0) < 0)
+			die_errno(_("failed to read '%s'"), path.buf);
+		strbuf_trim(&lock_reason);
+		worktree->lock_reason = strbuf_detach(&lock_reason, NULL);
+	} else
+		worktree->lock_reason = NULL;
+
 done:
 	strbuf_release(&path);
 	strbuf_release(&worktree_path);
diff --git a/worktree.h b/worktree.h
index e1c4715..9932710 100644
--- a/worktree.h
+++ b/worktree.h
@@ -5,6 +5,7 @@ struct worktree {
 	char *path;
 	char *id;
 	char *head_ref;
+	char *lock_reason;	/* NULL means not locked */
 	unsigned char head_sha1[20];
 	int is_detached;
 	int is_bare;
-- 
2.8.2.524.g6ff3d78