[PATCH v2 1/5] worktree.c: add find_worktree_by_path()
- From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
- Date
- May 22, 2016, 10:43 UTC
- Message-ID
- <20160522104341.656-2-pclouds@gmail.com>
- In-Reply-To
- <20160522104341.656-1-pclouds@gmail.com>
So far we haven't needed to identify an existing worktree from command line. Future commands such as lock or move will need it. There are of course other options for identifying a worktree, for example by branch or even by internal id. They may be added later if proved useful.
Helped-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> --- worktree.c | 12 ++++++++++++ worktree.h | 6 ++++++ 2 files changed, 18 insertions(+)
diff --git a/worktree.c b/worktree.c index f4a4f38..c5d45a6 100644 --- a/worktree.c +++ b/worktree.c @@ -214,6 +214,18 @@ const char *get_worktree_git_dir(const struct worktree *wt) return git_common_path("worktrees/%s", wt->id); } +struct worktree *find_worktree_by_path(struct worktree **list, + const char *path_) +{ + char *path = xstrdup(real_path(path_)); + + for (; *list; list++) + if (!fspathcmp(path, real_path((*list)->path))) + break; + free(path); + return *list; +} + int is_worktree_being_rebased(const struct worktree *wt, const char *target) { diff --git a/worktree.h b/worktree.h index 1394909..91627fd 100644 --- a/worktree.h +++ b/worktree.h @@ -29,6 +29,12 @@ extern struct worktree **get_worktrees(void); */ extern const char *get_worktree_git_dir(const struct worktree *wt); +/* + * Search a worktree by its path. Paths are normalized internally. + */ +extern struct worktree *find_worktree_by_path(struct worktree **list, + const char *path_); + /* * Free up the memory for worktree(s) */
-- 2.8.2.524.g6ff3d78