Short post, long command…
I’ve decided to start showing the current git branch in my PS1. However, since I don’t want to know when I’m on master, I had to write a new PS1 that I haven’t yet seen anywhere. Add the following to your .bashrc:
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
if [ -e /usr/share/git-core/contrib/completion/git-prompt.sh ]; then
. /usr/share/git-core/contrib/completion/git-prompt.sh
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$([ "$(__git_ps1 %s)" != "" -a "$(__git_ps1 %s)" != "master" ] && (echo -e " (\[33[32m\]"$(__git_ps1 "%s")"\[33[0m\])") || echo "")\$ '
fi
This keeps my PS1 short for when I’m hacking on personal repositories that only have a single branch. Keep in mind that you might have to change the path to git-prompt.sh depending on what OS you’re using.
Example:
james@computer:~/code/puppet$ cd puppet-gluster
james@computer:~/code/puppet/puppet-gluster$ git checkout -b cool-new-feature
Switched to a new branch 'cool-new-feature'
james@computer:~/code/puppet/puppet-gluster (cool-new-feature)$ # tada !
The branch name is coloured to match the default colours that git uses to colour branches.
Happy hacking,
James
The `git-prompt.sh` you test for is sourced by bash completion anyway (at least on my machine), so testing for existence of `__git_ps1` removes the platform fragility; see https://github.com/jhermann/ruby-slippers/blob/master/home/.bashrc.d/git.sh
The problem was that my platform seemed to not automatically source the git-prompt.sh ! My (fragile) sourcing of it was really just a hack to get it to work for now. If $PLATFORM starts behaving better in the future, I can remove that -e test.
I like your use of ‘type -t’.
The right thing to do is to probably combine both techniques:
1) Source that file if __git_ps1 isn’t a function. (compatibility hack for a broken? os)
2) Then, test and continue if __git_ps1 is a function.