Skip to content

Commit bb4995f

Browse files
sunshinecogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a80c4c2 commit bb4995f

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

worktree.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,27 +215,31 @@ struct worktree *find_worktree(struct worktree **list,
215215
const char *arg)
216216
{
217217
struct worktree *wt;
218-
char *path;
219218
char *to_free = NULL;
220219

221220
if ((wt = find_worktree_by_suffix(list, arg)))
222221
return wt;
223222

224223
if (prefix)
225224
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)
229235
return NULL;
230-
}
231236
for (; *list; list++) {
232237
const char *wt_path = real_path_if_valid((*list)->path);
233238

234239
if (wt_path && !fspathcmp(path, wt_path))
235240
break;
236241
}
237242
free(path);
238-
free(to_free);
239243
return *list;
240244
}
241245

worktree.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ struct worktree *find_worktree(struct worktree **list,
6161
const char *prefix,
6262
const char *arg);
6363

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+
6470
/*
6571
* Return true if the given worktree is the main one.
6672
*/

0 commit comments

Comments
 (0)