Re: [PATCH 1/5] worktree.c: add find_worktree_by_path()
- From
Eric Sunshine <sunshine@sunshineco.com>
- Date
- May 13, 2016, 16:29 UTC
- Message-ID
- <CAPig+cSu08BaEu22iGouM_7q6yrKbVtqNrfJsWo6aYuPAeWq0w@mail.gmail.com>
- 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 <pclouds@gmail.com> wrote:
Show 24 quoted lines
> 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 <pclouds@gmail.com>
> ---
> 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;