Created
April 26, 2013 13:19
-
-
Save mmcclimon/5467330 to your computer and use it in GitHub Desktop.
Git info in my bash prompt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# variables for git info in prompt | |
bGIT_PROMPT_OPEN='' | |
bGIT_PROMPT_MIDDLE='' | |
bGIT_PROMPT_CLOSE='' | |
bGIT_CURRENT_BRANCH='' | |
bGIT_CURRENT_SHA1='' | |
bGIT_CURRENT_STATUS='' | |
set_ps1_vars() { | |
local gitdir=`git rev-parse --git-dir 2> /dev/null` | |
if [ -z $gitdir ]; then | |
bGIT_PROMPT_OPEN='' | |
bGIT_PROMPT_MIDDLE='' | |
bGIT_PROMPT_CLOSE='' | |
bGIT_CURRENT_BRANCH='' | |
bGIT_CURRENT_SHA1='' | |
bGIT_CURRENT_STATUS='' | |
return 0 | |
else | |
bGIT_PROMPT_OPEN='[' | |
bGIT_PROMPT_MIDDLE='|' | |
bGIT_PROMPT_CLOSE=']' | |
bGIT_CURRENT_BRANCH=`git symbolic-ref HEAD 2> /dev/null | cut -d '/' -f 3` | |
bGIT_CURRENT_SHA1=`git rev-parse --short HEAD 2> /dev/null` | |
gitstatus=`git status --porcelain 2> /dev/null` | |
if [ -z "$gitstatus" ]; then | |
bGIT_CURRENT_STATUS='' | |
else | |
bGIT_CURRENT_STATUS='*' | |
fi | |
return 0 | |
fi | |
} | |
# colors for prompt | |
NORMAL="\[\e[0m\]" | |
PROMPT_C="\[\e[38;5;074m\]" | |
DIR_C="\[\e[38;5;237m\]" | |
BRANCH_C="\[\e[38;5;023m\]" | |
SHA1_C="\[\e[38;5;022m\]" | |
GIT_STATUS_C="\[\e[38;5;009m\]" | |
export PROMPT_COMMAND=set_ps1_vars # to avoid running rev-parse a bunch of times | |
# fancy prompt, showing git info if in a git repo | |
PS1="\n${PROMPT_C}(\t)${NORMAL} ${DIR_C}\w${NORMAL}" | |
# now add git info | |
PS1="${PS1} ${DIR_C}\$bGIT_PROMPT_OPEN${NORMAL}${BRANCH_C}\$bGIT_CURRENT_BRANCH${NORMAL}${DIR_C}\$bGIT_PROMPT_MIDDLE${NORMAL}${SHA1_C}\$bGIT_CURRENT_SHA1${NORMAL}${DIR_C}\$bGIT_PROMPT_CLOSE${NORMAL}${GIT_STATUS_C}\$bGIT_CURRENT_STATUS${NORMAL}" | |
# now add newline with prompt itself | |
PS1="${PS1}\n${PROMPT_C}\$${NORMAL} " |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment