Skip to content

Instantly share code, notes, and snippets.

@tonylegrone
Created August 10, 2017 19:52
Show Gist options
  • Save tonylegrone/7ea0a70ff78bb3f566f1577203df4ce6 to your computer and use it in GitHub Desktop.
Save tonylegrone/7ea0a70ff78bb3f566f1577203df4ce6 to your computer and use it in GitHub Desktop.
# vim: set ft=sh sw=2 ts=2 expandtab:
# -polka-
# git_prompt_info accepts 0 or 1 arguments (i.e., format string)
# returns text to add to bash PS1 prompt (includes branch name)
git_prompt_info () {
local g="$(git rev-parse --git-dir 2>/dev/null)"
if [ -n "$g" ]; then
local r
local b
local d
local s
# Rebasing
if [ -d "$g/rebase-apply" ] ; then
if test -f "$g/rebase-apply/rebasing" ; then
r="|REBASE"
fi
b="$(git symbolic-ref HEAD 2>/dev/null)"
# Interactive rebase
elif [ -f "$g/rebase-merge/interactive" ] ; then
r="|REBASE-i"
b="$(cat "$g/rebase-merge/head-name")"
# Merging
elif [ -f "$g/MERGE_HEAD" ] ; then
r="|MERGING"
b="$(git symbolic-ref HEAD 2>/dev/null)"
else
if [ -f "$g/BISECT_LOG" ] ; then
r="|BISECTING"
fi
if ! b="$(git symbolic-ref HEAD 2>/dev/null)" ; then
if ! b="$(git describe --exact-match HEAD 2>/dev/null)" ; then
b="$(cut -c1-7 "$g/HEAD")..."
fi
fi
fi
# Dirty Branch
local newfile='?? '
if [ -n "$ZSH_VERSION" ]; then
newfile='\?\? '
fi
if [ -n "${1-}" ]; then
d=''
s=$(git status --porcelain 2> /dev/null)
[[ $s =~ "$newfile" ]] && d+="+"
[[ $s =~ "M " ]] && d+="*"
[[ $s =~ "D " ]] && d+="-"
printf "$1" "${b##refs/heads/}${r}${d}"
else
d=''
s=$(git status --porcelain 2> /dev/null)
[[ $s =~ "$newfile" ]] && d+="%{$fg[green]%} ⊕%{$reset_color%}"
[[ $s =~ "M " ]] && d+="%{$fg[yellow]%} ⊙%{$reset_color%}"
[[ $s =~ "D " ]] && d+="%{$fg[red]%} ⊝%{$reset_color%}"
printf "%s " "%{$fg[magenta]%}${b##refs/heads/}%{$reset_color%}%{$fg[yellow]%}${r}%{$reset_color%}${d}"
fi
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment