@matheusbsbs / matheus-bagliano / commits / 051df3cfe8
Fork 0

wt-status: show sparse checkout status as well

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>

Elijah Newren committed Jun 18, 2020 at 20:49 UTC 051df3cfe8ed7d113197636f860edea14f283037
2 files changed +43
wt-status.c
+41
@@ -1484,6 +1484,18 @@ static void show_bisect_in_progress(struct wt_status *s,
1484 wt_longstatus_print_trailer(s);
1485 }
1486
1487 +static void show_sparse_checkout_in_use(struct wt_status *s,
1488 + const char *color)
1489 +{
1490 + if (s->state.sparse_checkout_percentage == SPARSE_CHECKOUT_DISABLED)
1491 + return;
1492 +
1493 + status_printf_ln(s, color,
1494 + _("You are in a sparse checkout with %d%% of tracked files present."),
1495 + s->state.sparse_checkout_percentage);
1496 + wt_longstatus_print_trailer(s);
1497 +}
1498 +
1499 /*
1500 * Extract branch information from rebase/bisect
1501 */
@@ -1623,6 +1635,31 @@ int wt_status_check_bisect(const struct worktree *wt,
1635 return 0;
1636 }
1637
1638 +static void wt_status_check_sparse_checkout(struct repository *r,
1639 + struct wt_status_state *state)
1640 +{
1641 + int skip_worktree = 0;
1642 + int i;
1643 +
1644 + if (!core_apply_sparse_checkout || r->index->cache_nr == 0) {
1645 + /*
1646 + * Don't compute percentage of checked out files if we
1647 + * aren't in a sparse checkout or would get division by 0.
1648 + */
1649 + state->sparse_checkout_percentage = SPARSE_CHECKOUT_DISABLED;
1650 + return;
1651 + }
1652 +
1653 + for (i = 0; i < r->index->cache_nr; i++) {
1654 + struct cache_entry *ce = r->index->cache[i];
1655 + if (ce_skip_worktree(ce))
1656 + skip_worktree++;
1657 + }
1658 +
1659 + state->sparse_checkout_percentage =
1660 + 100 - (100 * skip_worktree)/r->index->cache_nr;
1661 +}
1662 +
1663 void wt_status_get_state(struct repository *r,
1664 struct wt_status_state *state,
1665 int get_detached_from)
@@ -1658,6 +1695,7 @@ void wt_status_get_state(struct repository *r,
1695 }
1696 if (get_detached_from)
1697 wt_status_get_detached_from(r, state);
1698 + wt_status_check_sparse_checkout(r, state);
1699 }
1700
1701 static void wt_longstatus_print_state(struct wt_status *s)
@@ -1681,6 +1719,9 @@ static void wt_longstatus_print_state(struct wt_status *s)
1719 show_revert_in_progress(s, state_color);
1720 if (state->bisect_in_progress)
1721 show_bisect_in_progress(s, state_color);
1722 +
1723 + if (state->sparse_checkout_percentage != SPARSE_CHECKOUT_DISABLED)
1724 + show_sparse_checkout_in_use(s, state_color);
1725 }
1726
1727 static void wt_longstatus_print(struct wt_status *s)
wt-status.h
+2
@@ -79,6 +79,7 @@ enum wt_status_format {
79
80 #define HEAD_DETACHED_AT _("HEAD detached at ")
81 #define HEAD_DETACHED_FROM _("HEAD detached from ")
82 +#define SPARSE_CHECKOUT_DISABLED -1
83
84 struct wt_status_state {
85 int merge_in_progress;
@@ -90,6 +91,7 @@ struct wt_status_state {
91 int bisect_in_progress;
92 int revert_in_progress;
93 int detached_at;
94 + int sparse_checkout_percentage; /* SPARSE_CHECKOUT_DISABLED if not sparse */
95 char *branch;
96 char *onto;
97 char *detached_from;