From 71166274a22f65be8f345bcf0f2d23f4136c36c2 Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Tue, 13 Apr 2021 15:58:55 +0200 Subject: [PATCH] git prompt: Respect status_order even without informative status Fixes #7926. Also switches the default status order for non-informative to the informative one: stagedstate invalidstate dirtystate untrackedfiles stashstate instead of dirty staged stash untracked --- share/functions/fish_git_prompt.fish | 56 ++++++++++++++-------------- tests/checks/git.fish | 27 +++++++++++++- 2 files changed, 55 insertions(+), 28 deletions(-) diff --git a/share/functions/fish_git_prompt.fish b/share/functions/fish_git_prompt.fish index 746b53e8c..f461a53e4 100644 --- a/share/functions/fish_git_prompt.fish +++ b/share/functions/fish_git_prompt.fish @@ -188,14 +188,18 @@ function fish_git_prompt --description "Prompt function for Git" set -l r $rbc[1] # current operation set -l b $rbc[2] # current branch set -l detached $rbc[3] - set -l w #dirty working directory - set -l i #staged changes - set -l s #stashes - set -l u #untracked + set -l dirtystate #dirty working directory + set -l stagedstate #staged changes + set -l invalidstate #staged changes + set -l stashstate #stashes + set -l untrackedfiles #untracked set -l c $rbc[4] # bare repository set -l p #upstream set -l informative_status + set -q __fish_git_prompt_status_order + or set -g __fish_git_prompt_status_order stagedstate invalidstate dirtystate untrackedfiles stashstate + if not set -q ___fish_git_prompt_init # This takes a while, so it only needs to be done once, # and then whenever the configuration changes. @@ -242,21 +246,21 @@ function fish_git_prompt --description "Prompt function for Git" else # This has to be set explicitly. if test "$dirty" = true - set w (__fish_git_prompt_dirty) + set dirtystate (__fish_git_prompt_dirty) if test -n "$sha" - set i (__fish_git_prompt_staged) + set stagedstate (__fish_git_prompt_staged) else - set i $___fish_git_prompt_char_invalidstate + set invalidstate 1 end end if set -q __fish_git_prompt_showstashstate and test -r $git_dir/logs/refs/stash - set s $___fish_git_prompt_char_stashstate + set stashstate 1 end if test "$untracked" = true - set u (__fish_git_prompt_untracked) + set untrackedfiles (__fish_git_prompt_untracked) end end @@ -275,17 +279,19 @@ function fish_git_prompt --description "Prompt function for Git" end end - if test -n "$w" - set w "$___fish_git_prompt_color_dirtystate$w$___fish_git_prompt_color_dirtystate_done" - end - if test -n "$i" - set i "$___fish_git_prompt_color_stagedstate$i$___fish_git_prompt_color_stagedstate_done" - end - if test -n "$s" - set s "$___fish_git_prompt_color_stashstate$s$___fish_git_prompt_color_stashstate_done" - end - if test -n "$u" - set u "$___fish_git_prompt_color_untrackedfiles$u$___fish_git_prompt_color_untrackedfiles_done" + set -l f "" + for i in $__fish_git_prompt_status_order + if test -n "$$i" + set -l color_var ___fish_git_prompt_color_$i + set -l color_done_var ___fish_git_prompt_color_{$i}_done + set -l symbol_var ___fish_git_prompt_char_$i + + set -l color $$color_var + set -l color_done $$color_done_var + set -l symbol $$symbol_var + + set f "$f$color$symbol$color_done" + end end set b (string replace refs/heads/ '' -- $b) @@ -309,7 +315,6 @@ function fish_git_prompt --description "Prompt function for Git" end # Formatting - set -l f "$w$i$s$u" if test -n "$f" set f "$space$f" end @@ -328,23 +333,20 @@ function __fish_git_prompt_staged --description "fish_git_prompt helper, tells w # but we want to return 0 if there are staged changes. # So we invert the status. not command git diff-index --cached --quiet HEAD -- 2>/dev/null - and echo $___fish_git_prompt_char_stagedstate + and echo 1 end function __fish_git_prompt_untracked --description "fish_git_prompt helper, tells whether or not the current repository has untracked files" command git ls-files --others --exclude-standard --directory --no-empty-directory --error-unmatch -- :/ >/dev/null 2>&1 - and echo $___fish_git_prompt_char_untrackedfiles + and echo 1 end function __fish_git_prompt_dirty --description "fish_git_prompt helper, tells whether or not the current branch has tracked, modified files" # Like staged, invert the status because we want 0 to mean there are dirty files. not command git diff --no-ext-diff --quiet --exit-code 2>/dev/null - and echo $___fish_git_prompt_char_dirtystate + and echo 1 end -set -q __fish_git_prompt_status_order -or set -g __fish_git_prompt_status_order stagedstate invalidstate dirtystate untrackedfiles stashstate - function __fish_git_prompt_informative_status set -l stashstate 0 set -l stashfile "$argv[1]/logs/refs/stash" diff --git a/tests/checks/git.fish b/tests/checks/git.fish index 1e3c8f3fb..43b6d8b78 100644 --- a/tests/checks/git.fish +++ b/tests/checks/git.fish @@ -40,11 +40,36 @@ fish_git_prompt echo # the git prompt doesn't print a newline #CHECK: (newbranch) -__fish_git_prompt_show_informative_status=1 fish_git_prompt +set -g __fish_git_prompt_show_informative_status 1 +fish_git_prompt echo #CHECK: (newbranch|…1) +set -e __fish_git_prompt_show_informative_status # Confirm the mode changes back fish_git_prompt echo #CHECK: (newbranch) + +# (for some reason stagedstate is only shown with showdirtystate?) +set -g __fish_git_prompt_showdirtystate 1 +git add foo +fish_git_prompt +echo +#CHECK: (newbranch +) + +set -g __fish_git_prompt_showuntrackedfiles 1 +touch bananan +fish_git_prompt +echo +#CHECK: (newbranch +%) + +set -g __fish_git_prompt_status_order untrackedfiles stagedstate +fish_git_prompt +echo +#CHECK: (newbranch %+) + +set -g __fish_git_prompt_status_order untrackedfiles +fish_git_prompt +echo +#CHECK: (newbranch %)