diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-08-13 23:54:37 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-08-13 23:54:37 +1200 |
commit | 6e003bfb431e076090ddad55066043dbe8e9e26c (patch) | |
tree | e1a623a89a09d3d162e8d5ada96e155d9846f0a8 /bash/bashrc.d | |
parent | Mod4+g changes to "go", Mod4+i changes to "gimp" (diff) | |
download | dotfiles-6e003bfb431e076090ddad55066043dbe8e9e26c.tar.gz dotfiles-6e003bfb431e076090ddad55066043dbe8e9e26c.zip |
Use porcelain commands for Git prompt
Checking the output of `git status -z` works, but to be correctly
handled requires using a null delimiter for read. Because I want to port
this to OpenBSD pdksh (which doesn't have the -d option for read), this
seems to be a workable alternative.
It also enables me to remove the process substitution (another thing
pdksh doesn't support), and the array of flags.
I haven't yet tested this for speed.
Diffstat (limited to 'bash/bashrc.d')
-rw-r--r-- | bash/bashrc.d/prompt.bash | 36 |
1 files changed, 9 insertions, 27 deletions
diff --git a/bash/bashrc.d/prompt.bash b/bash/bashrc.d/prompt.bash index ba678909..feae7833 100644 --- a/bash/bashrc.d/prompt.bash +++ b/bash/bashrc.d/prompt.bash @@ -112,40 +112,22 @@ prompt() { fi branch=${branch##*/} - # Safely read status with -z --porcelain - local line - local -i ready modified untracked - while IFS= read -rd '' line ; do - if [[ $line == [MADRCT]* ]] ; then - ready=1 - fi - if [[ $line == ?[MADRCT]* ]] ; then - modified=1 - fi - if [[ $line == '??'* ]] ; then - untracked=1 - fi - done < <(git status -z --porcelain 2>/dev/null) - - # Build state array from status output flags - local -a state - if ((ready)) ; then - state[${#state[@]}]='+' + local state + if ! git diff-files --quiet ; then + state=${state}! fi - if ((modified)) ; then - state[${#state[@]}]='!' + if ! git diff-index --cached --quiet HEAD ; then + state=${state}+ fi - if ((untracked)) ; then - state[${#state[@]}]='?' + if [[ -n $(git ls-files --others --exclude-standard) ]] ; then + state=${state}? fi - - # Add another indicator if we have stashed changes if git rev-parse --verify refs/stash >/dev/null 2>&1 ; then - state[${#state[@]}]='^' + state=${state}^ fi # Print the status in brackets with a git: prefix - (IFS= ; printf '(git:%s%s)' "${branch:-unknown}" "${state[*]}") + printf '(git:%s%s)' "${branch:-unknown}" "$state" ;; # Subversion prompt function |