public
Created

Git info in PS1

  • Download Gist
git_info.bash
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
git_info() {
# colors
local RESET="\033[0m"
local GRAY="\033[1;30m"
local RED="\033[1;31m"
local GREEN="\033[1;32m"
local BLUE="\033[1;34m"
local PINK="\033[1;35m"
local CYAN="\033[1;36m"
 
local GOOD="\xe2\x88\x99" # ∙ circle
local AHEAD="\xe2\x86\x91" # ↑ up arrow
local BEHIND="\xe2\x86\x93" # ↓ down arrow
local CHANGED="\xcf\x9f" # ϟ lightning
local DIVERGED="\xe2\x86\x95" # ↕ up-down
local UNTRACKED="\xe0\xb2\xa0_\xe0\xb2\xa0" # ಠ_ಠ disapproval
 
git_place() {
local place=$(git status 2> /dev/null | head -n2 | tail -n1)
if [[ $( echo $place | grep "Your branch is ahead of" ) != "" ]]; then
echo -e "$PINK$AHEAD"
elif [[ $( echo $place | grep "Your branch is behind" ) != "" ]]; then
echo -e "$BLUE$BEHIND"
elif [[ $( echo $place | grep "Your branch and .* have diverged" ) != "" ]]; then
echo -e "$CYAN$DIVERGED"
else
echo -e "$GREEN$GOOD"
fi
}
 
git_status() {
local status=$(git status 2> /dev/null | tail -n1)
if [[ $status != "nothing to commit (working directory clean)" ]]; then
if [[ $status == 'nothing added to commit but untracked files present (use "git add" to track)' ]]; then
echo -e "$GRAY$UNTRACKED"
else
echo -e "$RED$CHANGED"
fi
fi
}
 
local branch=$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/\1/")
 
if [[ $branch != "" ]]; then
echo -e "[$branch$(git_place)$(git_status)$RESET]"
fi
}
 
# Example usage:
# $ source git_info.bash
# $ PS1="$(git_info) $PS1"

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.