mirror of
https://github.com/fish-shell/fish-shell.git
synced 2024-11-26 10:43:47 +08:00
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:
parent
f56bce3f97
commit
05ef157757
|
@ -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"
|
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 sha $argv[1]
|
||||||
|
|
||||||
set -l staged
|
set -l staged
|
||||||
|
set -l ret 0
|
||||||
|
|
||||||
if test -n "$sha"
|
if test -n "$sha"
|
||||||
command git diff-index --cached --quiet HEAD -- 2>/dev/null
|
# The "diff" functions all return > 0 if there _is_ a diff,
|
||||||
or set staged $___fish_git_prompt_char_stagedstate
|
# 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
|
else
|
||||||
set staged $___fish_git_prompt_char_invalidstate
|
set staged $___fish_git_prompt_char_invalidstate
|
||||||
|
set ret 2
|
||||||
end
|
end
|
||||||
echo $staged
|
echo $staged
|
||||||
|
return $ret
|
||||||
end
|
end
|
||||||
|
|
||||||
function __fish_git_prompt_untracked --description "fish_git_prompt helper, tells whether or not the current repository has untracked files"
|
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
|
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
|
set untracked $___fish_git_prompt_char_untrackedfiles
|
||||||
end
|
end
|
||||||
echo $untracked
|
echo $untracked
|
||||||
|
return $ret
|
||||||
end
|
end
|
||||||
|
|
||||||
function __fish_git_prompt_dirty --description "fish_git_prompt helper, tells whether or not the current branch has tracked, modified files"
|
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 dirty
|
||||||
|
|
||||||
set -l os
|
# Like staged, invert the status because we want 0 to mean there are dirty files.
|
||||||
command git diff --no-ext-diff --quiet --exit-code 2>/dev/null
|
not command git diff --no-ext-diff --quiet --exit-code 2>/dev/null
|
||||||
set os $status
|
set -l os $status
|
||||||
if test $os -ne 0
|
if test $os -eq 0
|
||||||
set dirty $___fish_git_prompt_char_dirtystate
|
set dirty $___fish_git_prompt_char_dirtystate
|
||||||
end
|
end
|
||||||
echo $dirty
|
echo $dirty
|
||||||
|
return $os
|
||||||
end
|
end
|
||||||
|
|
||||||
set -g ___fish_git_prompt_status_order stagedstate invalidstate dirtystate untrackedfiles
|
set -g ___fish_git_prompt_status_order stagedstate invalidstate dirtystate untrackedfiles
|
||||||
|
|
Loading…
Reference in New Issue
Block a user