Skip to content

Commit 6835314

Browse files
pcloudsgitster
authored andcommitted
worktree.c: add find_worktree()
So far we haven't needed to identify an existing worktree from command line. Future commands such as lock or move will need it. The current implementation identifies worktrees by path (*). In future, the function could learn to identify by $(basename $path) or tags... (*) We could probably go cheaper with comparing inode number (and probably more reliable than paths when unicode enters the game). But not all systems have good inode that so let's stick to something simple for now. Helped-by: Eric Sunshine <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0409e0b commit 6835314

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

worktree.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,21 @@ const char *get_worktree_git_dir(const struct worktree *wt)
214214
return git_common_path("worktrees/%s", wt->id);
215215
}
216216

217+
struct worktree *find_worktree(struct worktree **list,
218+
const char *prefix,
219+
const char *arg)
220+
{
221+
char *path;
222+
223+
arg = prefix_filename(prefix, strlen(prefix), arg);
224+
path = xstrdup(real_path(arg));
225+
for (; *list; list++)
226+
if (!fspathcmp(path, real_path((*list)->path)))
227+
break;
228+
free(path);
229+
return *list;
230+
}
231+
217232
int is_worktree_being_rebased(const struct worktree *wt,
218233
const char *target)
219234
{

worktree.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ extern struct worktree **get_worktrees(void);
2929
*/
3030
extern const char *get_worktree_git_dir(const struct worktree *wt);
3131

32+
/*
33+
* Search a worktree that can be unambiguously identified by
34+
* "arg". "prefix" must not be NULL.
35+
*/
36+
extern struct worktree *find_worktree(struct worktree **list,
37+
const char *prefix,
38+
const char *arg);
39+
3240
/*
3341
* Free up the memory for worktree(s)
3442
*/

0 commit comments

Comments
 (0)