From: Eric Sunshine Date: Fri, 13 May 2016 16:29:59 GMT Subject: Re: [PATCH 1/5] worktree.c: add find_worktree_by_path() Message-ID: In-Reply-To: <20160510141729.23063-1-pclouds@gmail.com> On Tue, May 10, 2016 at 10:17 AM, Nguyễn Thái Ngọc Duy wrote: > 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. > > Signed-off-by: Nguyễn Thái Ngọc Duy > --- > diff --git a/worktree.c b/worktree.c > @@ -222,6 +222,22 @@ const char *get_worktree_git_dir(const struct worktree *wt) > +struct worktree *find_worktree_by_path(struct worktree **list, > + const char *path_) > +{ > + char *path = xstrdup(real_path(path_)); > + struct worktree *wt = NULL; > + > + while (*list) { > + wt = *list++; > + if (!fspathcmp(path, real_path(wt->path))) > + break; > + wt = NULL; > + } > + free(path); > + return wt; > +} Very slightly shorter and perhaps idiomatic (but not at all worth a re-roll): for (; *list; list++) if (!fspathcmp(path, real_path((*list)->path))) break; free(path); return *list;