worktree.c: add is_worktree_locked()
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 <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
984ad9e56c
commit
346ef53058
28
worktree.c
28
worktree.c
@ -13,6 +13,7 @@ void free_worktrees(struct worktree **worktrees)
|
|||||||
free(worktrees[i]->path);
|
free(worktrees[i]->path);
|
||||||
free(worktrees[i]->id);
|
free(worktrees[i]->id);
|
||||||
free(worktrees[i]->head_ref);
|
free(worktrees[i]->head_ref);
|
||||||
|
free(worktrees[i]->lock_reason);
|
||||||
free(worktrees[i]);
|
free(worktrees[i]);
|
||||||
}
|
}
|
||||||
free (worktrees);
|
free (worktrees);
|
||||||
@ -98,6 +99,8 @@ static struct worktree *get_main_worktree(void)
|
|||||||
worktree->is_detached = is_detached;
|
worktree->is_detached = is_detached;
|
||||||
worktree->is_current = 0;
|
worktree->is_current = 0;
|
||||||
add_head_info(&head_ref, worktree);
|
add_head_info(&head_ref, worktree);
|
||||||
|
worktree->lock_reason = NULL;
|
||||||
|
worktree->lock_reason_valid = 0;
|
||||||
|
|
||||||
done:
|
done:
|
||||||
strbuf_release(&path);
|
strbuf_release(&path);
|
||||||
@ -143,6 +146,8 @@ static struct worktree *get_linked_worktree(const char *id)
|
|||||||
worktree->is_detached = is_detached;
|
worktree->is_detached = is_detached;
|
||||||
worktree->is_current = 0;
|
worktree->is_current = 0;
|
||||||
add_head_info(&head_ref, worktree);
|
add_head_info(&head_ref, worktree);
|
||||||
|
worktree->lock_reason = NULL;
|
||||||
|
worktree->lock_reason_valid = 0;
|
||||||
|
|
||||||
done:
|
done:
|
||||||
strbuf_release(&path);
|
strbuf_release(&path);
|
||||||
@ -234,6 +239,29 @@ int is_main_worktree(const struct worktree *wt)
|
|||||||
return !wt->id;
|
return !wt->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char *is_worktree_locked(struct worktree *wt)
|
||||||
|
{
|
||||||
|
assert(!is_main_worktree(wt));
|
||||||
|
|
||||||
|
if (!wt->lock_reason_valid) {
|
||||||
|
struct strbuf path = STRBUF_INIT;
|
||||||
|
|
||||||
|
strbuf_addstr(&path, worktree_git_path(wt, "locked"));
|
||||||
|
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);
|
||||||
|
wt->lock_reason = strbuf_detach(&lock_reason, NULL);
|
||||||
|
} else
|
||||||
|
wt->lock_reason = NULL;
|
||||||
|
wt->lock_reason_valid = 1;
|
||||||
|
strbuf_release(&path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return wt->lock_reason;
|
||||||
|
}
|
||||||
|
|
||||||
int is_worktree_being_rebased(const struct worktree *wt,
|
int is_worktree_being_rebased(const struct worktree *wt,
|
||||||
const char *target)
|
const char *target)
|
||||||
{
|
{
|
||||||
|
@ -5,10 +5,12 @@ struct worktree {
|
|||||||
char *path;
|
char *path;
|
||||||
char *id;
|
char *id;
|
||||||
char *head_ref;
|
char *head_ref;
|
||||||
|
char *lock_reason; /* internal use */
|
||||||
unsigned char head_sha1[20];
|
unsigned char head_sha1[20];
|
||||||
int is_detached;
|
int is_detached;
|
||||||
int is_bare;
|
int is_bare;
|
||||||
int is_current;
|
int is_current;
|
||||||
|
int lock_reason_valid;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Functions for acting on the information about worktrees. */
|
/* Functions for acting on the information about worktrees. */
|
||||||
@ -42,6 +44,12 @@ extern struct worktree *find_worktree(struct worktree **list,
|
|||||||
*/
|
*/
|
||||||
extern int is_main_worktree(const struct worktree *wt);
|
extern int is_main_worktree(const struct worktree *wt);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Return the reason string if the given worktree is locked or NULL
|
||||||
|
* otherwise.
|
||||||
|
*/
|
||||||
|
extern const char *is_worktree_locked(struct worktree *wt);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Free up the memory for worktree(s)
|
* Free up the memory for worktree(s)
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user