]> git.ipfire.org Git - thirdparty/git.git/commitdiff
wt-status: show sparse checkout status as well
authorElijah Newren <newren@gmail.com>
Thu, 18 Jun 2020 20:49:57 +0000 (20:49 +0000)
committerJunio C Hamano <gitster@pobox.com>
Thu, 18 Jun 2020 21:12:28 +0000 (14:12 -0700)
Some of the early feedback of folks trying out sparse-checkouts at
$dayjob is that sparse checkouts can sometimes be disorienting; users
can forget that they had a sparse-checkout and then wonder where files
went.  Add some output to 'git status' in the form of a simple line that
states:

    You are in a sparse checkout with 35% of files present.

where, obviously, the exact figure changes depending on what percentage
of files from the index do not have the SKIP_WORKTREE bit set.

Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
wt-status.c
wt-status.h

index 98dfa6f73f9d7cd41867f97fdae41bd4dc5ec2a1..c560cbe860a42daa136e16195535ef10e2ab0a9f 100644 (file)
@@ -1484,6 +1484,18 @@ static void show_bisect_in_progress(struct wt_status *s,
        wt_longstatus_print_trailer(s);
 }
 
+static void show_sparse_checkout_in_use(struct wt_status *s,
+                                       const char *color)
+{
+       if (s->state.sparse_checkout_percentage == SPARSE_CHECKOUT_DISABLED)
+               return;
+
+       status_printf_ln(s, color,
+                        _("You are in a sparse checkout with %d%% of tracked files present."),
+                        s->state.sparse_checkout_percentage);
+       wt_longstatus_print_trailer(s);
+}
+
 /*
  * Extract branch information from rebase/bisect
  */
@@ -1623,6 +1635,31 @@ int wt_status_check_bisect(const struct worktree *wt,
        return 0;
 }
 
+static void wt_status_check_sparse_checkout(struct repository *r,
+                                           struct wt_status_state *state)
+{
+       int skip_worktree = 0;
+       int i;
+
+       if (!core_apply_sparse_checkout || r->index->cache_nr == 0) {
+               /*
+                * Don't compute percentage of checked out files if we
+                * aren't in a sparse checkout or would get division by 0.
+                */
+               state->sparse_checkout_percentage = SPARSE_CHECKOUT_DISABLED;
+               return;
+       }
+
+       for (i = 0; i < r->index->cache_nr; i++) {
+               struct cache_entry *ce = r->index->cache[i];
+               if (ce_skip_worktree(ce))
+                       skip_worktree++;
+       }
+
+       state->sparse_checkout_percentage =
+               100 - (100 * skip_worktree)/r->index->cache_nr;
+}
+
 void wt_status_get_state(struct repository *r,
                         struct wt_status_state *state,
                         int get_detached_from)
@@ -1658,6 +1695,7 @@ void wt_status_get_state(struct repository *r,
        }
        if (get_detached_from)
                wt_status_get_detached_from(r, state);
+       wt_status_check_sparse_checkout(r, state);
 }
 
 static void wt_longstatus_print_state(struct wt_status *s)
@@ -1681,6 +1719,9 @@ static void wt_longstatus_print_state(struct wt_status *s)
                show_revert_in_progress(s, state_color);
        if (state->bisect_in_progress)
                show_bisect_in_progress(s, state_color);
+
+       if (state->sparse_checkout_percentage != SPARSE_CHECKOUT_DISABLED)
+               show_sparse_checkout_in_use(s, state_color);
 }
 
 static void wt_longstatus_print(struct wt_status *s)
index 73ab5d4da1c0b4766cb63501b9db06c0a7fea934..f1fa0ec1a750a92967c35cafe2fdb25a32873d5a 100644 (file)
@@ -79,6 +79,7 @@ enum wt_status_format {
 
 #define HEAD_DETACHED_AT _("HEAD detached at ")
 #define HEAD_DETACHED_FROM _("HEAD detached from ")
+#define SPARSE_CHECKOUT_DISABLED -1
 
 struct wt_status_state {
        int merge_in_progress;
@@ -90,6 +91,7 @@ struct wt_status_state {
        int bisect_in_progress;
        int revert_in_progress;
        int detached_at;
+       int sparse_checkout_percentage; /* SPARSE_CHECKOUT_DISABLED if not sparse */
        char *branch;
        char *onto;
        char *detached_from;