Skip to content

Commit 051df3c

Browse files
newrengitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 101b320 commit 051df3c

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

wt-status.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,18 @@ static void show_bisect_in_progress(struct wt_status *s,
14841484
wt_longstatus_print_trailer(s);
14851485
}
14861486

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+
14871499
/*
14881500
* Extract branch information from rebase/bisect
14891501
*/
@@ -1623,6 +1635,31 @@ int wt_status_check_bisect(const struct worktree *wt,
16231635
return 0;
16241636
}
16251637

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+
16261663
void wt_status_get_state(struct repository *r,
16271664
struct wt_status_state *state,
16281665
int get_detached_from)
@@ -1658,6 +1695,7 @@ void wt_status_get_state(struct repository *r,
16581695
}
16591696
if (get_detached_from)
16601697
wt_status_get_detached_from(r, state);
1698+
wt_status_check_sparse_checkout(r, state);
16611699
}
16621700

16631701
static void wt_longstatus_print_state(struct wt_status *s)
@@ -1681,6 +1719,9 @@ static void wt_longstatus_print_state(struct wt_status *s)
16811719
show_revert_in_progress(s, state_color);
16821720
if (state->bisect_in_progress)
16831721
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);
16841725
}
16851726

16861727
static void wt_longstatus_print(struct wt_status *s)

wt-status.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ enum wt_status_format {
7979

8080
#define HEAD_DETACHED_AT _("HEAD detached at ")
8181
#define HEAD_DETACHED_FROM _("HEAD detached from ")
82+
#define SPARSE_CHECKOUT_DISABLED -1
8283

8384
struct wt_status_state {
8485
int merge_in_progress;
@@ -90,6 +91,7 @@ struct wt_status_state {
9091
int bisect_in_progress;
9192
int revert_in_progress;
9293
int detached_at;
94+
int sparse_checkout_percentage; /* SPARSE_CHECKOUT_DISABLED if not sparse */
9395
char *branch;
9496
char *onto;
9597
char *detached_from;

0 commit comments

Comments
 (0)