Skip to content

Commit afda36d

Browse files
newrengitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 30b00f0 commit afda36d

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

contrib/completion/git-prompt.sh

+20-2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@
7474
# revert, cherry-pick, or bisect, the prompt will include information
7575
# related to the operation, often in the form "|<OPERATION-NAME>".
7676
#
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+
#
7782
# By default, __git_ps1 will compare HEAD to your SVN upstream if it can
7883
# find one, or @{upstream} otherwise. Once you have set
7984
# GIT_PS1_SHOWUPSTREAM, you can override it on a per-repository basis by
@@ -425,6 +430,13 @@ __git_ps1 ()
425430
return $exit
426431
fi
427432

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+
428440
local r=""
429441
local b=""
430442
local step=""
@@ -496,6 +508,7 @@ __git_ps1 ()
496508
local i=""
497509
local s=""
498510
local u=""
511+
local h=""
499512
local c=""
500513
local p=""
501514

@@ -528,6 +541,11 @@ __git_ps1 ()
528541
u="%${ZSH_VERSION+%}"
529542
fi
530543

544+
if [ -n "${GIT_PS1_COMPRESSSPARSESTATE}" ] &&
545+
[ "$(git config --bool core.sparseCheckout)" == "true" ]; then
546+
h="?"
547+
fi
548+
531549
if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
532550
__git_ps1_show_upstream
533551
fi
@@ -546,8 +564,8 @@ __git_ps1 ()
546564
b="\${__git_ps1_branch_name}"
547565
fi
548566

549-
local f="$w$i$s$u"
550-
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"
551569

552570
if [ $pcmode = yes ]; then
553571
if [ "${__git_printf_supports_v-}" != yes ]; then

0 commit comments

Comments
 (0)