Skip to content

Instantly share code, notes, and snippets.

@nickboldt
Created November 11, 2012 16:19
Show Gist options
  • Save nickboldt/4055398 to your computer and use it in GitHub Desktop.
Save nickboldt/4055398 to your computer and use it in GitHub Desktop.
bashrc file which tracks git status in my prompt
# .bashrc
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
source ~/.alias
export GREP_OPTIONS='--color=auto'
norm="\033[0;39m";
grey="\033[1;30m";
green="\033[1;32m";
brown="\033[0;33m";
yellow="\033[1;33m";
blue="\033[1;34m";
cyan="\033[1;36m";
red="\033[1;31m";
purple='\e[0;35m'
yellow_reversed='\e[0;30m\e[43m'
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
BLUE="\[\033[0;34m\]"
LIGHT_RED="\[\033[1;31m\]"
LIGHT_GREEN="\[\033[1;32m\]"
WHITE="\[\033[1;37m\]"
LIGHT_GRAY="\[\033[0;37m\]"
COLOR_NONE="\[\e[0m\]"
hcol=$blue;
ucol=$grey;
rcol=$red;
export VISUAL=vim
export EDITOR=vim
export PATH=$PATH:~/bin
export JAVA_HOME=/opt/sun-java2-6.0/
export MAVEN_OPTS="-Xms512m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=256m"
export GITUSER=nickboldt
#####################################################################################
# based on https://gist.github.com/47267
function parse_git_branch {
git_status=""
gitdir=`git rev-parse --git-dir 2> /dev/null`
#git rev-parse --git-dir &> /dev/null
if [[ -d ${gitdir} ]] && [[ ${gitdir/.git/} != ${gitdir} ]]; then git_status="$(git status 2> /dev/null)"; fi
branch_pattern="^# On branch ([^${IFS}]*)"
remote_pattern="# Your branch is (.*) of"
diverge_pattern="# Your branch and (.*) have diverged"
newfile="new file:"
untracked="Untracked file"
if [[ ! ${git_status} =~ "working directory clean" ]]; then
state="${RED}⚡"
fi
# add an else if or two here if you want to get more specific
if [[ ${git_status} =~ ${remote_pattern} ]]; then
if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then
remote="${YELLOW}↑ "
else
remote="${YELLOW}↓ "
fi
elif [[ ${git_status} =~ ${diverge_pattern} ]]; then
remote="${YELLOW}↕ "
fi
if [[ ${git_status} =~ ${newfile} ]]; then
remote="${remote}${YELLOW}A "
fi
if [[ ${git_status} =~ ${untracked} ]]; then
remote="${remote}${purple}? "
fi
if [[ ${git_status} =~ ${branch_pattern} ]]; then
branch=${BASH_REMATCH[1]}
if [[ $TOPIC ]]; then
if [[ ${branch} == ${TOPIC} ]]; then # we're in the topic branch
echo " ${yellow_reversed}${branch}${norm} = ${GREEN}${TOPIC} ${remote}${state}"
else # we're in the origin branch, not the topic branch
echo " ${yellow_reversed}${branch}${norm} ⇐ ${YELLOW}${TOPIC} ${remote}${state}"
fi
else
echo -n " ${yellow_reversed}${branch}${norm}"
if [[ -f ${gitdir}/svn/.metadata ]]; then
PWD=`pwd`
if [[ ${PWD/\/target/} = ${PWD} ]]; then
# echo " ${BLUE}`git svn info 2> /dev/null | grep URL | sed -e \"s/URL: //g\"`${norm}"
echo " ${BLUE}`cat ${gitdir}/svn/.metadata | grep reposRoot | sed -e \"s/.\+reposRoot = //g\"`${norm}"
else
echo -n " ${BLUE}`cat ${gitdir}/svn/.metadata | grep reposRoot | sed -e \"s/.\+reposRoot = //g\"`${norm}"
echo " ... ${BLUE}target${PWD##*/target}"
fi
else
echo " ${RED}! NO \\\${TOPIC} SET ${remote}${state}"
fi
fi
fi
}
function timestamp() {
echo -ne "[$(date +%H:%M:%S)]\033]0; ${PWD}\007"
}
function prompt_func() {
previous_return_value=$?;
if [ $UID -eq 0 ]; then # root
prompt="$(timestamp) $rcol\u$norm@$hcol\h$norm:\w\[$norm\]$(parse_git_branch)\n$RED#$norm";
else
prompt="$(timestamp) $ucol\u$norm@$hcol\h$norm:\w\[$norm\]$(parse_git_branch)\n$ucol\$$norm";
fi
if test $previous_return_value -eq 0
then
PS1="${prompt}➔ "
else
PS1="${prompt}${RED}➔${COLOR_NONE} "
fi
}
PROMPT_COMMAND=prompt_func
@nickboldt
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment