For a while now I’ve been using the __git_ps1 function in my bash’s PS1 prompt
(with PS1='\w$(__git_ps1)'). Now I want to color it depending on branch
status.
I wrote a bash function that checks if the current branch is modified, and
colors red or white depending on the status. The problem is that it uses git to check the status (it’s the only way I know off), and that’s several
status
times slower than __git_ps1, which is enough to cause an annoying delay when
I’m using the prompt (I’m on a very weak netbook).
So I ask: is there a faster way to check status of the current git folder?
__git_ps1 is a lot faster than manually parsing git branch, so I’m thinking
there might be some other hidden git function.
Not entirely your answer, but bash-completion has this built-in.
If you set the bash ENV GIT_PS1_SHOWDIRTYSTATE to a nonempty value, unstaged (*) and staged (+) changes will be shown next to the branch name. You can configure this per-repository ith the bash.showDirtyState variable, which defaults to true once GIT_PS1_SHOWDIRTYSTATE is enabled.
You can also see if currently something is stashed, by setting GIT_PS1_SHOWSTASHSTATE to a nonempty value. If something is stashed, then a ‘$’ will be shown next to the branch name.
If you would like to see if there are untracked files, then you can set GIT_PS1_SHOWUNTRACKEDFILES to a nonempty value. If there are untracked files, then a ‘%’ will be shown next to the branch name.
Not sure about the speed degrade when you enable this though.
If you want to do the coloring:
Staged files:
Stashed files
Untracked files
The above snippets come from the bash-completion script.