OSDN Git Service

worktree.c: find_worktree() search by path suffix
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Mon, 13 Jun 2016 12:18:26 +0000 (19:18 +0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 8 Jul 2016 22:31:04 +0000 (15:31 -0700)
This allows the user to do something like "worktree lock foo" or
"worktree lock to/foo" instead of "worktree lock /long/path/to/foo" if
it's unambiguous.

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.

Suggested-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>
Documentation/git-worktree.txt
worktree.c

index 27330c5..7850dee 100644 (file)
@@ -129,6 +129,11 @@ OPTIONS
 <worktree>::
        Working trees can be identified by path, either relative or
        absolute.
++
+If the last path components in the working tree's path is unique among
+working trees, it can be used to identify worktrees. For example if
+you only have to working trees at "/abc/def/ghi" and "/abc/def/ggg",
+then "ghi" or "def/ghi" is enough to point to the former working tree.
 
 DETAILS
 -------
index 2bcfff3..2107c06 100644 (file)
@@ -219,12 +219,41 @@ const char *get_worktree_git_dir(const struct worktree *wt)
                return git_common_path("worktrees/%s", wt->id);
 }
 
+static struct worktree *find_worktree_by_suffix(struct worktree **list,
+                                               const char *suffix)
+{
+       struct worktree *found = NULL;
+       int nr_found = 0, suffixlen;
+
+       suffixlen = strlen(suffix);
+       if (!suffixlen)
+               return NULL;
+
+       for (; *list && nr_found < 2; list++) {
+               const char      *path    = (*list)->path;
+               int              pathlen = strlen(path);
+               int              start   = pathlen - suffixlen;
+
+               /* suffix must start at directory boundary */
+               if ((!start || (start > 0 && is_dir_sep(path[start - 1]))) &&
+                   !fspathcmp(suffix, path + start)) {
+                       found = *list;
+                       nr_found++;
+               }
+       }
+       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_suffix(list, arg)))
+               return wt;
+
        arg = prefix_filename(prefix, strlen(prefix), arg);
        path = xstrdup(real_path(arg));
        for (; *list; list++)