functions/fish_git_prompt: Let helper functions return useful status

If you use these to figure out if there _are_ staged files, or dirty
or whatever, you currently need to check the output, which relies on
the configured character.

Instead, we let them also return a useful status.
Notably, this is *not* simply the status of the git call.

__fish_git_prompt_X returns 0 if the repo is X.

This works for untracked, but the "diff" things return 1 if there is a
diff, so we invert the status for them.

See #5748.

[ci skip]
This commit is contained in:
Fabian Homborg 2019-03-19 10:05:39 +01:00
parent f56bce3f97
commit 05ef157757

View File

@ -485,36 +485,45 @@ end
function __fish_git_prompt_staged --description "fish_git_prompt helper, tells whether or not the current branch has staged files"
set -l sha $argv[1]
set -l staged
set -l ret 0
if test -n "$sha"
command git diff-index --cached --quiet HEAD -- 2>/dev/null
or set staged $___fish_git_prompt_char_stagedstate
# The "diff" functions all return > 0 if there _is_ a diff,
# 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 set staged $___fish_git_prompt_char_stagedstate
set ret $status
else
set staged $___fish_git_prompt_char_invalidstate
set ret 2
end
echo $staged
return $ret
end
function __fish_git_prompt_untracked --description "fish_git_prompt helper, tells whether or not the current repository has untracked files"
set -l untracked
set -l ret 1
if command git ls-files --others --exclude-standard --directory --no-empty-directory --error-unmatch -- :/ >/dev/null 2>&1
set ret $status
set untracked $___fish_git_prompt_char_untrackedfiles
end
echo $untracked
return $ret
end
function __fish_git_prompt_dirty --description "fish_git_prompt helper, tells whether or not the current branch has tracked, modified files"
set -l dirty
set -l os
command git diff --no-ext-diff --quiet --exit-code 2>/dev/null
set os $status
if test $os -ne 0
# 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
set -l os $status
if test $os -eq 0
set dirty $___fish_git_prompt_char_dirtystate
end
echo $dirty
return $os
end
set -g ___fish_git_prompt_status_order stagedstate invalidstate dirtystate untrackedfiles