Skip to content

Commit b8a846b

Browse files
pks-tgitster
authored andcommitted
worktree: expose interface to look up worktree by name
Our worktree interfaces do not provide a way to look up a worktree by its name. Expose `get_linked_worktree()` to allow for this usecase. As callers are responsible for freeing this worktree, introduce a new function `free_worktree()` that does so. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 84f0ea9 commit b8a846b

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

worktree.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,23 @@
1212
#include "wt-status.h"
1313
#include "config.h"
1414

15+
void free_worktree(struct worktree *worktree)
16+
{
17+
if (!worktree)
18+
return;
19+
free(worktree->path);
20+
free(worktree->id);
21+
free(worktree->head_ref);
22+
free(worktree->lock_reason);
23+
free(worktree->prune_reason);
24+
free(worktree);
25+
}
26+
1527
void free_worktrees(struct worktree **worktrees)
1628
{
1729
int i = 0;
18-
19-
for (i = 0; worktrees[i]; i++) {
20-
free(worktrees[i]->path);
21-
free(worktrees[i]->id);
22-
free(worktrees[i]->head_ref);
23-
free(worktrees[i]->lock_reason);
24-
free(worktrees[i]->prune_reason);
25-
free(worktrees[i]);
26-
}
30+
for (i = 0; worktrees[i]; i++)
31+
free_worktree(worktrees[i]);
2732
free (worktrees);
2833
}
2934

@@ -75,8 +80,8 @@ static struct worktree *get_main_worktree(int skip_reading_head)
7580
return worktree;
7681
}
7782

78-
static struct worktree *get_linked_worktree(const char *id,
79-
int skip_reading_head)
83+
struct worktree *get_linked_worktree(const char *id,
84+
int skip_reading_head)
8085
{
8186
struct worktree *worktree = NULL;
8287
struct strbuf path = STRBUF_INIT;

worktree.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ struct worktree *find_worktree(struct worktree **list,
5757
const char *prefix,
5858
const char *arg);
5959

60+
/*
61+
* Look up the worktree corresponding to `id`, or NULL of no such worktree
62+
* exists.
63+
*/
64+
struct worktree *get_linked_worktree(const char *id,
65+
int skip_reading_head);
66+
6067
/*
6168
* Return the worktree corresponding to `path`, or NULL if no such worktree
6269
* exists.
@@ -134,6 +141,11 @@ void repair_worktrees(worktree_repair_fn, void *cb_data);
134141
*/
135142
void repair_worktree_at_path(const char *, worktree_repair_fn, void *cb_data);
136143

144+
/*
145+
* Free up the memory for a worktree.
146+
*/
147+
void free_worktree(struct worktree *);
148+
137149
/*
138150
* Free up the memory for worktree(s)
139151
*/

0 commit comments

Comments
 (0)