Skip to content

Commit fc0c7d5

Browse files
raffsgitster
authored andcommitted
worktree: teach worktree to lazy-load "prunable" reason
Add worktree_prune_reason() to allow a caller to discover whether a worktree is prunable and the reason that it is, much like worktree_lock_reason() indicates whether a worktree is locked and the reason for the lock. As with worktree_lock_reason(), retrieve the prunable reason lazily and cache it in the `worktree` structure. Helped-by: Eric Sunshine <[email protected]> Signed-off-by: Rafael Silva <[email protected]> Reviewed-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a29a8b7 commit fc0c7d5

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

worktree.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ void free_worktrees(struct worktree **worktrees)
1515
free(worktrees[i]->id);
1616
free(worktrees[i]->head_ref);
1717
free(worktrees[i]->lock_reason);
18+
free(worktrees[i]->prune_reason);
1819
free(worktrees[i]);
1920
}
2021
free (worktrees);
@@ -245,6 +246,25 @@ const char *worktree_lock_reason(struct worktree *wt)
245246
return wt->lock_reason;
246247
}
247248

249+
const char *worktree_prune_reason(struct worktree *wt, timestamp_t expire)
250+
{
251+
struct strbuf reason = STRBUF_INIT;
252+
char *path = NULL;
253+
254+
if (is_main_worktree(wt))
255+
return NULL;
256+
if (wt->prune_reason_valid)
257+
return wt->prune_reason;
258+
259+
if (should_prune_worktree(wt->id, &reason, &path, expire))
260+
wt->prune_reason = strbuf_detach(&reason, NULL);
261+
wt->prune_reason_valid = 1;
262+
263+
strbuf_release(&reason);
264+
free(path);
265+
return wt->prune_reason;
266+
}
267+
248268
/* convenient wrapper to deal with NULL strbuf */
249269
static void strbuf_addf_gently(struct strbuf *buf, const char *fmt, ...)
250270
{

worktree.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ struct worktree {
1111
char *id;
1212
char *head_ref; /* NULL if HEAD is broken or detached */
1313
char *lock_reason; /* private - use worktree_lock_reason */
14+
char *prune_reason; /* private - use worktree_prune_reason */
1415
struct object_id head_oid;
1516
int is_detached;
1617
int is_bare;
1718
int is_current;
1819
int lock_reason_valid; /* private */
20+
int prune_reason_valid; /* private */
1921
};
2022

2123
/*
@@ -73,6 +75,13 @@ int is_main_worktree(const struct worktree *wt);
7375
*/
7476
const char *worktree_lock_reason(struct worktree *wt);
7577

78+
/*
79+
* Return the reason string if the given worktree should be pruned, otherwise
80+
* NULL if it should not be pruned. `expire` defines a grace period to prune
81+
* the worktree when its path does not exist.
82+
*/
83+
const char *worktree_prune_reason(struct worktree *wt, timestamp_t expire);
84+
7685
/*
7786
* Return true if worktree entry should be pruned, along with the reason for
7887
* pruning. Otherwise, return false and the worktree's path in `wtpath`, or

0 commit comments

Comments
 (0)