Skip to content

Instantly share code, notes, and snippets.

@alecthegeek
Forked from elecnix/bash-git-prompt
Created March 4, 2010 03:36
Show Gist options
  • Save alecthegeek/321372 to your computer and use it in GitHub Desktop.
Save alecthegeek/321372 to your computer and use it in GitHub Desktop.
Displays the current git branch name and the dirty state in your Bash shell
#!/bin/bash
#
# Displays the current git branch name and the dirty state in your Bash shell
# prompt. Add a line like this to your ~/.bashrc file:
#
# . ~/path/to/gist/bash-git-prompt
#
# To use this, you must enable "enable programmable completion features".
# Look at your ~/.bashrc for something like:
#
# if [ -f /etc/bash_completion ]; then
# . /etc/bash_completion
# fi
#
# Since Bash 3.1, Debian has moved the file bash_completion to the bash-completion
# package, so: apt-get install bash-completion
#
# Otherwise, you will get an error for __git_ps1 not being found.
if [ -r /etc/bash_completion.d/git ] ; then
function __repo_branch ()
{
__git_ps1
}
function __git_prompt_enabled ()
{
# Search for .git-prompt-disable file in parent directories
dir=`pwd`
while [ `dirname "$dir"` != "$dir" ] ; do
if [ -f "$dir/.git-prompt-enable" ] ; then return 0 ; fi
if [ -f "$dir/.git-prompt-disable" ] ; then return 1 ; fi
dir=`dirname "$dir"`
done
}
function __repo_dirty ()
{
__git_prompt_enabled || ( perl -e 'print "? "' ; exit 1 ) || return
status=`git status 2>&1`
if [[ "$status" =~ "Not a git repository" ]] ; then return ; fi
if [[ "$status" =~ "Changed but not updated" ]] ; then perl -e 'print "C "' ; return ; fi
if [[ "$status" =~ "Untracked files" ]] ; then perl -e 'print "u "' ; return ; fi
if [[ "$status" =~ "Changes to be committed" ]] ; then perl -e 'print "i "' ; return ; fi
if [[ "$status" =~ "Your branch is ahead" ]] ; then perl -e 'print "a "' ; return ; fi
}
PS1='${debian_chroot:+($debian_chroot)}\[\033[00;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\033[01;33m\]$(__repo_branch)\[\033[01;31m\]$(__repo_dirty)\[\033[00m\]\$ '
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment