Skip to content

Commit 346ef53

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 984ad9e commit 346ef53

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

worktree.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ void free_worktrees(struct worktree **worktrees)
1313
free(worktrees[i]->path);
1414
free(worktrees[i]->id);
1515
free(worktrees[i]->head_ref);
16+
free(worktrees[i]->lock_reason);
1617
free(worktrees[i]);
1718
}
1819
free (worktrees);
@@ -98,6 +99,8 @@ static struct worktree *get_main_worktree(void)
9899
worktree->is_detached = is_detached;
99100
worktree->is_current = 0;
100101
add_head_info(&head_ref, worktree);
102+
worktree->lock_reason = NULL;
103+
worktree->lock_reason_valid = 0;
101104

102105
done:
103106
strbuf_release(&path);
@@ -143,6 +146,8 @@ static struct worktree *get_linked_worktree(const char *id)
143146
worktree->is_detached = is_detached;
144147
worktree->is_current = 0;
145148
add_head_info(&head_ref, worktree);
149+
worktree->lock_reason = NULL;
150+
worktree->lock_reason_valid = 0;
146151

147152
done:
148153
strbuf_release(&path);
@@ -234,6 +239,29 @@ int is_main_worktree(const struct worktree *wt)
234239
return !wt->id;
235240
}
236241

242+
const char *is_worktree_locked(struct worktree *wt)
243+
{
244+
assert(!is_main_worktree(wt));
245+
246+
if (!wt->lock_reason_valid) {
247+
struct strbuf path = STRBUF_INIT;
248+
249+
strbuf_addstr(&path, worktree_git_path(wt, "locked"));
250+
if (file_exists(path.buf)) {
251+
struct strbuf lock_reason = STRBUF_INIT;
252+
if (strbuf_read_file(&lock_reason, path.buf, 0) < 0)
253+
die_errno(_("failed to read '%s'"), path.buf);
254+
strbuf_trim(&lock_reason);
255+
wt->lock_reason = strbuf_detach(&lock_reason, NULL);
256+
} else
257+
wt->lock_reason = NULL;
258+
wt->lock_reason_valid = 1;
259+
strbuf_release(&path);
260+
}
261+
262+
return wt->lock_reason;
263+
}
264+
237265
int is_worktree_being_rebased(const struct worktree *wt,
238266
const char *target)
239267
{

worktree.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ struct worktree {
55
char *path;
66
char *id;
77
char *head_ref;
8+
char *lock_reason; /* internal use */
89
unsigned char head_sha1[20];
910
int is_detached;
1011
int is_bare;
1112
int is_current;
13+
int lock_reason_valid;
1214
};
1315

1416
/* Functions for acting on the information about worktrees. */
@@ -42,6 +44,12 @@ extern struct worktree *find_worktree(struct worktree **list,
4244
*/
4345
extern int is_main_worktree(const struct worktree *wt);
4446

47+
/*
48+
* Return the reason string if the given worktree is locked or NULL
49+
* otherwise.
50+
*/
51+
extern const char *is_worktree_locked(struct worktree *wt);
52+
4553
/*
4654
* Free up the memory for worktree(s)
4755
*/

0 commit comments

Comments
 (0)