Re: [PATCH v3 2/6] worktree.c: find_worktree() learns to identify worktrees by basename
- From
Junio C Hamano <gitster@pobox.com>
- Date
- May 31, 2016, 17:51 UTC
- Message-ID
- <xmqqh9de5d6e.fsf@gitster.mtv.corp.google.com>
- In-Reply-To
- <20160530104939.28407-3-pclouds@gmail.com>
Nguyễn Thái Ngọc Duy <pclouds@gmail.com> writes:
> This allows the user to do something like "worktree lock foo" instead of > "worktree lock <path/to/foo>". With completion support it could be quite > convenient. While this base name search can be done in the same worktree > iteration loop, the code is split into a separate function for clarity.
Makes me wonder if you want to do this without calling basename(3) function at all. I do not think such a feature would cost that much over what we see in this patch.
That is, wouldn't you rather see "worktree lock to/foo" work when 'foo' is ambiguous but 'to/foo' is not?
Show 45 quoted lines
>
> Suggested-by: Eric Sunshine <sunshine@sunshineco.com>
> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
> ---
> worktree.c | 21 +++++++++++++++++++++
> 1 file changed, 21 insertions(+)
>
> diff --git a/worktree.c b/worktree.c
> index 0782e00..4dd7b77 100644
> --- a/worktree.c
> +++ b/worktree.c
> @@ -214,12 +214,33 @@ const char *get_worktree_git_dir(const struct worktree *wt)
> return git_common_path("worktrees/%s", wt->id);
> }
>
> +static struct worktree *find_worktree_by_basename(struct worktree **list,
> + const char *base_name)
> +{
> + struct worktree *found = NULL;
> + int nr_found = 0;
> +
> + for (; *list && nr_found < 2; list++) {
> + char *path = xstrdup((*list)->path);
> + if (!fspathcmp(base_name, basename(path))) {
> + found = *list;
> + nr_found++;
> + }
> + free(path);
> + }
> + return nr_found == 1 ? found : NULL;
> +}
> +
> struct worktree *find_worktree(struct worktree **list,
> const char *prefix,
> const char *arg)
> {
> + struct worktree *wt;
> char *path;
>
> + if ((wt = find_worktree_by_basename(list, arg)))
> + return wt;
> +
> arg = prefix_filename(prefix, strlen(prefix), arg);
> path = xstrdup(real_path(arg));
> for (; *list; list++)