Re: [PATCH v3 4/6] worktree.c: retrieve lock status (and optionally reason) in get_worktrees()
- From
Junio C Hamano <gitster@pobox.com>
- Date
- May 31, 2016, 17:55 UTC
- Message-ID
- <xmqq8tyq5czn.fsf@gitster.mtv.corp.google.com>
- In-Reply-To
- <20160530104939.28407-5-pclouds@gmail.com>
Nguyễn Thái Ngọc Duy <pclouds@gmail.com> writes:
> We need this later to avoid double locking a worktree, or unlocking one > when it's not even locked.
Shouldn't this be done lazily?
If a user is working in worktree B and is not doing anything funky, she would not care why worktree A and C are locked, even though she might care the fact that they are locked.
Show 48 quoted lines
> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
> ---
> 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;