Snippets

Ben Buchanan BASH: git bling

You are viewing an old version of this snippet. View the current version.
Revised by Ben Buchanan 7c5ae0e
#!/bin/bash

environment=`uname -s`
if [[ "$environment" == *CYGWIN* ]]
then

    function parse_git_branch {
      git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/\1/"
    }
    export PS1='\e[1;30m\][\e[\e[1;30m\]\e[1;33m\] \u@\H \[\e[1;32m\]\w\[\e[0m\] $(parse_git_branch) (check status manually) \e[1;30m\]]\e[37m\]\n> '
    echo "Cygwin prompt formatting loaded - note no git status or stash parsing (too damn slow)"
fi

if [[ "$environment" == *Darwin* ]]
then
    # mac version - git stash handled a little differently
    function parse_git_dirty {
      [[ $(git status 2> /dev/null | tail -n1) != "nothing to commit, working directory clean" ]] && echo "(*)"
    }
    function parse_git_stash {
      [[ $(git stash list 2> /dev/null | tail -n1) != "" ]] && echo "^"
    }
    function parse_git_branch {
      git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/\1$(parse_git_dirty)/"
    }
    
    # Minimal version with standard prompt and git output:
    # export PS1='\u@\H \w $(git branch &>/dev/null;if [ $? -eq 0 ]; then echo "$(parse_git_branch)$(parse_git_stash)"; fi) $ '

    # Slightly more blingy
    export PS1='\e[1;37m\][\e[\e[1;37m\]\e[1;33m\] \u@\H \[\e[1;32m\]\w\[\e[0m\]$(git branch &>/dev/null;if [ $? -eq 0 ];
    then echo " \[\033[01;34m\]\e[1;37m$(parse_git_branch)$(parse_git_stash)"; fi) \e[1;37m\]] \e[37m\]\n\[$PROMPT\]$ '
    echo "Prompt formatting loaded"
fi
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.