]> git.ipfire.org Git - thirdparty/git.git/commitdiff
worktree: teach worktree to lazy-load "prunable" reason
authorRafael Silva <rafaeloliveira.cs@gmail.com>
Tue, 19 Jan 2021 21:27:34 +0000 (22:27 +0100)
committerJunio C Hamano <gitster@pobox.com>
Sat, 30 Jan 2021 17:57:16 +0000 (09:57 -0800)
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 <sunshine@sunshineco.com>
Signed-off-by: Rafael Silva <rafaeloliveira.cs@gmail.com>
Reviewed-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
worktree.c
worktree.h

index 8ae019af79bb5d17ce993745d996cbde42289f10..fb3e286996d1e6102e7b008bd0fbff73e52552d0 100644 (file)
@@ -15,6 +15,7 @@ void free_worktrees(struct worktree **worktrees)
                free(worktrees[i]->id);
                free(worktrees[i]->head_ref);
                free(worktrees[i]->lock_reason);
+               free(worktrees[i]->prune_reason);
                free(worktrees[i]);
        }
        free (worktrees);
@@ -245,6 +246,25 @@ const char *worktree_lock_reason(struct worktree *wt)
        return wt->lock_reason;
 }
 
+const char *worktree_prune_reason(struct worktree *wt, timestamp_t expire)
+{
+       struct strbuf reason = STRBUF_INIT;
+       char *path = NULL;
+
+       if (is_main_worktree(wt))
+               return NULL;
+       if (wt->prune_reason_valid)
+               return wt->prune_reason;
+
+       if (should_prune_worktree(wt->id, &reason, &path, expire))
+               wt->prune_reason = strbuf_detach(&reason, NULL);
+       wt->prune_reason_valid = 1;
+
+       strbuf_release(&reason);
+       free(path);
+       return wt->prune_reason;
+}
+
 /* convenient wrapper to deal with NULL strbuf */
 static void strbuf_addf_gently(struct strbuf *buf, const char *fmt, ...)
 {
index 818e1491c7ec331647cfa3886877c1fc3a33a123..8b7c408132d1f7c7a81824f635488955a2df3cd6 100644 (file)
@@ -11,11 +11,13 @@ struct worktree {
        char *id;
        char *head_ref;         /* NULL if HEAD is broken or detached */
        char *lock_reason;      /* private - use worktree_lock_reason */
+       char *prune_reason;     /* private - use worktree_prune_reason */
        struct object_id head_oid;
        int is_detached;
        int is_bare;
        int is_current;
        int lock_reason_valid; /* private */
+       int prune_reason_valid; /* private */
 };
 
 /*
@@ -73,6 +75,13 @@ int is_main_worktree(const struct worktree *wt);
  */
 const char *worktree_lock_reason(struct worktree *wt);
 
+/*
+ * Return the reason string if the given worktree should be pruned, otherwise
+ * NULL if it should not be pruned. `expire` defines a grace period to prune
+ * the worktree when its path does not exist.
+ */
+const char *worktree_prune_reason(struct worktree *wt, timestamp_t expire);
+
 /*
  * Return true if worktree entry should be pruned, along with the reason for
  * pruning. Otherwise, return false and the worktree's path in `wtpath`, or