Skip to content

Commit 0cc4dca

Browse files
committed
Merge branch 'en/sparse-status'
"git status" learned to report the status of sparse checkout. * en/sparse-status: git-prompt: include sparsity state as well git-prompt: document how in-progress operations affect the prompt wt-status: show sparse checkout status as well
2 parents 33a22c1 + afda36d commit 0cc4dca

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

contrib/completion/git-prompt.sh

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@
7070
# state symbols by setting GIT_PS1_STATESEPARATOR. The default separator
7171
# is SP.
7272
#
73+
# When there is an in-progress operation such as a merge, rebase,
74+
# revert, cherry-pick, or bisect, the prompt will include information
75+
# related to the operation, often in the form "|<OPERATION-NAME>".
76+
#
77+
# When the repository has a sparse-checkout, a notification of the form
78+
# "|SPARSE" will be included in the prompt. This can be shortened to a
79+
# single '?' character by setting GIT_PS1_COMPRESSSPARSESTATE, or omitted
80+
# by setting GIT_PS1_OMITSPARSESTATE.
81+
#
7382
# By default, __git_ps1 will compare HEAD to your SVN upstream if it can
7483
# find one, or @{upstream} otherwise. Once you have set
7584
# GIT_PS1_SHOWUPSTREAM, you can override it on a per-repository basis by
@@ -421,6 +430,13 @@ __git_ps1 ()
421430
return $exit
422431
fi
423432

433+
local sparse=""
434+
if [ -z "${GIT_PS1_COMPRESSSPARSESTATE}" ] &&
435+
[ -z "${GIT_PS1_OMITSPARSESTATE}" ] &&
436+
[ "$(git config --bool core.sparseCheckout)" == "true" ]; then
437+
sparse="|SPARSE"
438+
fi
439+
424440
local r=""
425441
local b=""
426442
local step=""
@@ -492,6 +508,7 @@ __git_ps1 ()
492508
local i=""
493509
local s=""
494510
local u=""
511+
local h=""
495512
local c=""
496513
local p=""
497514

@@ -524,6 +541,11 @@ __git_ps1 ()
524541
u="%${ZSH_VERSION+%}"
525542
fi
526543

544+
if [ -n "${GIT_PS1_COMPRESSSPARSESTATE}" ] &&
545+
[ "$(git config --bool core.sparseCheckout)" == "true" ]; then
546+
h="?"
547+
fi
548+
527549
if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
528550
__git_ps1_show_upstream
529551
fi
@@ -542,8 +564,8 @@ __git_ps1 ()
542564
b="\${__git_ps1_branch_name}"
543565
fi
544566

545-
local f="$w$i$s$u"
546-
local gitstring="$c$b${f:+$z$f}$r$p"
567+
local f="$h$w$i$s$u"
568+
local gitstring="$c$b${f:+$z$f}${sparse}$r$p"
547569

548570
if [ $pcmode = yes ]; then
549571
if [ "${__git_printf_supports_v-}" != yes ]; then

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)