Skip to content

Instantly share code, notes, and snippets.

@aeris
Created July 7, 2012 16:45
Show Gist options
  • Save aeris/3067164 to your computer and use it in GitHub Desktop.
Save aeris/3067164 to your computer and use it in GitHub Desktop.
Git prompt for ZSH
autoload -U colors
colors
# Allow for functions in the prompt.
setopt prompt_subst
autoload -U add-zsh-hook
add-zsh-hook chpwd chpwd_update_git_vars
add-zsh-hook preexec preexec_update_git_vars
add-zsh-hook precmd precmd_update_git_vars
function preexec_update_git_vars() {
case "$2" in
git*)
__EXECUTED_GIT_COMMAND=1
;;
esac
}
function precmd_update_git_vars() {
if [ -n "$__EXECUTED_GIT_COMMAND" ]; then
update_current_git_vars
unset __EXECUTED_GIT_COMMAND
fi
}
function chpwd_update_git_vars() {
update_current_git_vars
}
function update_current_git_vars() {
unset GIT_BRANCH
if git rev-parse --git-dir &> /dev/null; then
GIT_BRANCH=$(git branch | sed -n 's/\*\s*\(.*\)/\1/p')
GIT_CHANGED=$(git diff --name-only | wc -l)
GIT_STAGED=$(git diff --staged --name-only | wc -l)
GIT_CHANGED=$(($GIT_CHANGED-$GIT_STAGED))
GIT_UNTRACKED=$(git ls-files --others --exclude-standard | wc -l)
GIT_STATUS="$(git status)"
GIT_AHEAD=$(echo $GIT_STATUS | sed -n "s/# Your branch is ahead of '.*' by \([0-9]*\) commit.*/\1/p")
GIT_BEHIND=$(echo $GIT_STATUS | sed -n "s/# Your branch is behind '.*' by \([0-9]*\) commit.*/\1/p")
if [ "$(($GIT_CHANGED+$GIT_STAGED+$GIT_UNTRACKED))" -eq "0" ]; then
GIT_CLEAN=1
else
GIT_CLEAN=0
fi
fi
}
GIT_PROMPT_PREFIX="("
GIT_PROMPT_SUFFIX=")"
GIT_PROMPT_SEPARATOR="|"
GIT_PROMPT_BRANCH="%{$fg_bold[magenta]%}"
GIT_PROMPT_STAGED="%{$fg_bold[cyan]%}●"
GIT_PROMPT_CHANGED="%{$fg_bold[yellow]%}+"
GIT_PROMPT_UNTRACKED="%{$fg_bold[red]%}…"
GIT_PROMPT_AHEAD="%{$fg_bold[green]%}↑"
GIT_PROMPT_BEHIND="%{$fg_bold[yellow]%}↓"
GIT_PROMPT_REMOTE=""
GIT_PROMPT_DIRTY="%{$fg_bold[red]%}⚡%{${reset_color}%"
GIT_PROMPT_CLEAN="%{$fg_bold[green]%}✔%{${reset_color}%"
git_status() {
update_current_git_vars
if [ -n "$GIT_BRANCH" ]; then
STATUS="$GIT_PROMPT_PREFIX$GIT_PROMPT_BRANCH$GIT_BRANCH%{${reset_color}%}"
if [ "$GIT_BEHIND" -ne "0" ]; then
STATUS="$STATUS$GIT_PROMPT_SEPARATOR$GIT_PROMPT_BEHIND$GIT_BEHIND%{${reset_color}%}"
fi
if [ "$GIT_AHEAD" -ne "0" ]; then
STATUS="$STATUS$GIT_PROMPT_SEPARATOR$GIT_PROMPT_AHEAD$GIT_AHEAD%{${reset_color}%}"
fi
if [ "$GIT_STAGED" -ne "0" ]; then
STATUS="$STATUS$GIT_PROMPT_SEPARATOR$GIT_PROMPT_STAGED$GIT_STAGED%{${reset_color}%}"
fi
if [ "$GIT_CHANGED" -ne "0" ]; then
STATUS="$STATUS$GIT_PROMPT_SEPARATOR$GIT_PROMPT_CHANGED$GIT_CHANGED%{${reset_color}%}"
fi
if [ "$GIT_UNTRACKED" -ne "0" ]; then
STATUS="$STATUS$GIT_PROMPT_SEPARATOR$GIT_PROMPT_UNTRACKED$GIT_UNTRACKED%{${reset_color}%}"
fi
if [ "$GIT_CLEAN" -eq "1" ]; then
STATUS="$STATUS$GIT_PROMPT_SEPARATOR$GIT_PROMPT_CLEAN"
else
STATUS="$STATUS$GIT_PROMPT_SEPARATOR$GIT_PROMPT_DIRTY"
fi
STATUS="$STATUS$GIT_PROMPT_SUFFIX"
echo "$STATUS"
fi
}
if [ "`id -u`" -eq 0 ]; then
PROMPT="%{$fg_bold[red]%}%n%{$fg[yellow]%}@%{$fg[white]%}%m %{$fg[green]%}%~
%{${reset_color}%}$(git_status)%{$fg_bold[yellow]%}#%{${reset_color}%} "
else
PROMPT="%{$fg_bold[cyan]%}%n%{$fg[yellow]%}@%{$fg[white]%}%m %{$fg[green]%}%~
%{${reset_color}%}$(git_status)%{$fg_bold[yellow]%}$%{${reset_color}%} "
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment