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
This commit is contained in:
Junio C Hamano 2020-07-06 22:09:13 -07:00
commit 0cc4dcacb3
3 changed files with 67 additions and 2 deletions

View file

@ -70,6 +70,15 @@
# state symbols by setting GIT_PS1_STATESEPARATOR. The default separator
# is SP.
#
# When there is an in-progress operation such as a merge, rebase,
# revert, cherry-pick, or bisect, the prompt will include information
# related to the operation, often in the form "|<OPERATION-NAME>".
#
# When the repository has a sparse-checkout, a notification of the form
# "|SPARSE" will be included in the prompt. This can be shortened to a
# single '?' character by setting GIT_PS1_COMPRESSSPARSESTATE, or omitted
# by setting GIT_PS1_OMITSPARSESTATE.
#
# By default, __git_ps1 will compare HEAD to your SVN upstream if it can
# find one, or @{upstream} otherwise. Once you have set
# GIT_PS1_SHOWUPSTREAM, you can override it on a per-repository basis by
@ -421,6 +430,13 @@ __git_ps1 ()
return $exit
fi
local sparse=""
if [ -z "${GIT_PS1_COMPRESSSPARSESTATE}" ] &&
[ -z "${GIT_PS1_OMITSPARSESTATE}" ] &&
[ "$(git config --bool core.sparseCheckout)" == "true" ]; then
sparse="|SPARSE"
fi
local r=""
local b=""
local step=""
@ -492,6 +508,7 @@ __git_ps1 ()
local i=""
local s=""
local u=""
local h=""
local c=""
local p=""
@ -524,6 +541,11 @@ __git_ps1 ()
u="%${ZSH_VERSION+%}"
fi
if [ -n "${GIT_PS1_COMPRESSSPARSESTATE}" ] &&
[ "$(git config --bool core.sparseCheckout)" == "true" ]; then
h="?"
fi
if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
__git_ps1_show_upstream
fi
@ -542,8 +564,8 @@ __git_ps1 ()
b="\${__git_ps1_branch_name}"
fi
local f="$w$i$s$u"
local gitstring="$c$b${f:+$z$f}$r$p"
local f="$h$w$i$s$u"
local gitstring="$c$b${f:+$z$f}${sparse}$r$p"
if [ $pcmode = yes ]; then
if [ "${__git_printf_supports_v-}" != yes ]; then

View 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)

View 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;