@matheusbsbs / matheus-bagliano / commits / bb4995fc3f
Fork 0

worktree: add utility to find worktree by pathname

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>

Eric Sunshine committed Feb 24, 2020 at 04:08 UTC bb4995fc3fab35b56f1ca48cda9187bcf340e643
2 files changed +16 -6
worktree.c
+10 -6
@@ -215,7 +215,6 @@ struct worktree *find_worktree(struct worktree **list,
215 const char *arg)
216 {
217 struct worktree *wt;
218 - char *path;
218 char *to_free = NULL;
219
220 if ((wt = find_worktree_by_suffix(list, arg)))
@@ -223,11 +222,17 @@ struct worktree *find_worktree(struct worktree **list,
222
223 if (prefix)
224 arg = to_free = prefix_filename(prefix, arg);
226 - path = real_pathdup(arg, 0);
227 - if (!path) {
228 - free(to_free);
225 + wt = find_worktree_by_path(list, arg);
226 + free(to_free);
227 + return wt;
228 +}
229 +
230 +struct worktree *find_worktree_by_path(struct worktree **list, const char *p)
231 +{
232 + char *path = real_pathdup(p, 0);
233 +
234 + if (!path)
235 return NULL;
230 - }
236 for (; *list; list++) {
237 const char *wt_path = real_path_if_valid((*list)->path);
238
@@ -235,7 +240,6 @@ struct worktree *find_worktree(struct worktree **list,
240 break;
241 }
242 free(path);
238 - free(to_free);
243 return *list;
244 }
245
worktree.h
+6
@@ -61,6 +61,12 @@ struct worktree *find_worktree(struct worktree **list,
61 const char *prefix,
62 const char *arg);
63
64 +/*
65 + * Return the worktree corresponding to `path`, or NULL if no such worktree
66 + * exists.
67 + */
68 +struct worktree *find_worktree_by_path(struct worktree **, const char *path);
69 +
70 /*
71 * Return true if the given worktree is the main one.
72 */