From 051df3cfe8ed7d113197636f860edea14f283037 Mon Sep 17 00:00:00 2001 From: Elijah Newren Date: Thu, 18 Jun 2020 20:49:57 +0000 Subject: [PATCH 1/3] 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 Signed-off-by: Junio C Hamano --- wt-status.c | 41 +++++++++++++++++++++++++++++++++++++++++ wt-status.h | 2 ++ 2 files changed, 43 insertions(+) diff --git a/wt-status.c b/wt-status.c index 98dfa6f73f..c560cbe860 100644 --- a/wt-status.c +++ b/wt-status.c @@ -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) diff --git a/wt-status.h b/wt-status.h index 73ab5d4da1..f1fa0ec1a7 100644 --- a/wt-status.h +++ b/wt-status.h @@ -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; From 30b00f009c28d595fba967bbf33a9c4031b3292b Mon Sep 17 00:00:00 2001 From: Elijah Newren Date: Sun, 21 Jun 2020 05:21:26 +0000 Subject: [PATCH 2/3] git-prompt: document how in-progress operations affect the prompt Signed-off-by: Elijah Newren Signed-off-by: Junio C Hamano --- contrib/completion/git-prompt.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh index 014cd7c3cf..179b96e493 100644 --- a/contrib/completion/git-prompt.sh +++ b/contrib/completion/git-prompt.sh @@ -70,6 +70,10 @@ # 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 "|". +# # 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 From afda36dbf3b4f5a489ab44c00d5210c1fa894a40 Mon Sep 17 00:00:00 2001 From: Elijah Newren Date: Sun, 21 Jun 2020 05:21:27 +0000 Subject: [PATCH 3/3] git-prompt: include sparsity state as well git-prompt includes the current branch, a bunch of single character mini-state displayers, and some much longer in-progress state notifications. The current branch is always shown. The single character mini-state displayers are all off by default (they are not self explanatory) but each has an environment variable for turning it on. The in-progress state notifications provide no configuration options for turning them off, and can be up to 15 characters long (e.g. "|REBASE (12/18)" or "|CHERRY-PICKING"). The single character mini-state tends to be used for things like "Do you have any stashes in refs/stash?" or "Are you ahead or behind of upstream?". These are things which users can take advantage of but do not affect most normal git operations. The in-progress states, by contrast, suggest the user needs to interact differently and may also prevent some normal operations from succeeding (e.g. git switch may show an error instead of switching branches). Sparsity is like the in-progress states in that it suggests a fundamental different interaction with the repository (many of the files from the repository are not present in your working copy!). A few commits ago added sparsity information to wt_longstatus_print_state(), grouping it with other in-progress state displays. We do similarly here with the prompt and show the extra state, by default, with an extra |SPARSE This state can be present simultaneously with the in-progress states, in which case it will appear before the other states; for example, (branchname|SPARSE|REBASE 6/10) The reason for showing the "|SPARSE" substring before other states is to emphasize those other states. Sparsity is probably not going to change much within a repository, while temporary operations will. So we want the state changes related to temporary operations to be listed last, to make them appear closer to where the user types and make them more likely to be noticed. The fact that sparsity isn't just cached metadata or additional information is what leads us to show it more similarly to the in-progress states, but the fact that sparsity is not transient like the in-progress states might cause some users to want an abbreviated notification of sparsity state or perhaps even be able to turn it off. Allow GIT_PS1_COMPRESSSPARSESTATE to be set to request that it be shortened to a single character ('?'), and GIT_PS1_OMITSPARSESTATE to be set to request that sparsity state be omitted from the prompt entirely. Signed-off-by: Elijah Newren Signed-off-by: Junio C Hamano --- contrib/completion/git-prompt.sh | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh index 179b96e493..e6cd5464e5 100644 --- a/contrib/completion/git-prompt.sh +++ b/contrib/completion/git-prompt.sh @@ -74,6 +74,11 @@ # revert, cherry-pick, or bisect, the prompt will include information # related to the operation, often in the form "|". # +# 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 @@ -425,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="" @@ -496,6 +508,7 @@ __git_ps1 () local i="" local s="" local u="" + local h="" local c="" local p="" @@ -528,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 @@ -546,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