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 <sunshine@sunshineco.com> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Jun 3, 2016 at 19:19 UTC 6835314459794831a1b88bed56549653710e910c
2 files changed +23
worktree.c
+15
@@ -214,6 +214,21 @@ const char *get_worktree_git_dir(const struct worktree *wt)
214 return git_common_path("worktrees/%s", wt->id);
215 }
216
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 +
232 int is_worktree_being_rebased(const struct worktree *wt,
233 const char *target)
234 {
worktree.h
+8
@@ -29,6 +29,14 @@ extern struct worktree **get_worktrees(void);
29 */
30 extern const char *get_worktree_git_dir(const struct worktree *wt);
31
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 +
40 /*
41 * Free up the memory for worktree(s)
42 */