]> git.ipfire.org Git - thirdparty/git.git/commitdiff
worktree: add utility to find worktree by pathname
authorEric Sunshine <sunshine@sunshineco.com>
Mon, 24 Feb 2020 09:08:47 +0000 (04:08 -0500)
committerJunio C Hamano <gitster@pobox.com>
Mon, 24 Feb 2020 21:04:30 +0000 (13:04 -0800)
find_worktree() employs heuristics to match user provided input -- which
may be a pathname or some sort of shorthand -- with an actual worktree.
Although this convenience allows a user to identify a worktree with
minimal typing, the black-box nature of these heuristics makes it
potentially difficult for callers which already know the exact path of a
worktree to be confident that the correct worktree will be returned for
any specific pathname (particularly a relative one), especially as the
heuristics are enhanced and updated.

Therefore, add a companion function, find_worktree_by_path(), which
deterministically identifies a worktree strictly by pathname with no
interpretation and no magic matching.

Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
worktree.c
worktree.h

index 5b4793caa34e3ab981268662efdc1d840ac53390..43c6685d4ea452a14271b271efc5712b4c161e49 100644 (file)
@@ -215,7 +215,6 @@ struct worktree *find_worktree(struct worktree **list,
                               const char *arg)
 {
        struct worktree *wt;
-       char *path;
        char *to_free = NULL;
 
        if ((wt = find_worktree_by_suffix(list, arg)))
@@ -223,11 +222,17 @@ struct worktree *find_worktree(struct worktree **list,
 
        if (prefix)
                arg = to_free = prefix_filename(prefix, arg);
-       path = real_pathdup(arg, 0);
-       if (!path) {
-               free(to_free);
+       wt = find_worktree_by_path(list, arg);
+       free(to_free);
+       return wt;
+}
+
+struct worktree *find_worktree_by_path(struct worktree **list, const char *p)
+{
+       char *path = real_pathdup(p, 0);
+
+       if (!path)
                return NULL;
-       }
        for (; *list; list++) {
                const char *wt_path = real_path_if_valid((*list)->path);
 
@@ -235,7 +240,6 @@ struct worktree *find_worktree(struct worktree **list,
                        break;
        }
        free(path);
-       free(to_free);
        return *list;
 }
 
index b8a851b92b3e13d3cdb157246aec2f457190dfee..d242a6e71c0333f80b76e44976f0c4356f99bec3 100644 (file)
@@ -61,6 +61,12 @@ struct worktree *find_worktree(struct worktree **list,
                               const char *prefix,
                               const char *arg);
 
+/*
+ * Return the worktree corresponding to `path`, or NULL if no such worktree
+ * exists.
+ */
+struct worktree *find_worktree_by_path(struct worktree **, const char *path);
+
 /*
  * Return true if the given worktree is the main one.
  */