Skip to content

Instantly share code, notes, and snippets.

@olivierverdier
Forked from scelis/chpwd_update_git_vars.sh
Created February 21, 2010 15:16
Show Gist options
  • Save olivierverdier/310361 to your computer and use it in GitHub Desktop.
Save olivierverdier/310361 to your computer and use it in GitHub Desktop.
update_current_git_vars.sh
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from subprocess import Popen, PIPE
output = Popen(['git','status'], stdout=PIPE).communicate()[0]
lines = output.splitlines()
import re
behead_re = re.compile(r"^# Your branch is (ahead of|behind) '(.*)' by (\d+) commit")
diverge_re = re.compile(r"^# and have (\d) and (\d) different")
symbols = {'ahead of': '↑', 'behind': '↓'}
bline = lines[0]
if bline.find('Not currently on any branch') != -1:
branch = Popen(['git','rev-parse','--short','HEAD'], stdout=PIPE).communicate()[0][:-1]
else:
branch = bline.split(' ')[3]
## if branch == 'master':
## branch = '♦'
bstatusline = lines[1]
match = behead_re.match(bstatusline)
if match:
branch += symbols[match.groups()[0]]
branch += match.groups()[2]
elif bstatusline.find('nothing to commit (working directory clean)') != -1:
branch += '⚡'
else:
div_match = diverge_re.match(lines[2])
if div_match:
branch += "|{1}↕{0}".format(*div_match.groups())
print '(%s)' % branch
if [ -n "$__EXECUTED_GIT_COMMAND" ]; then
update_current_git_vars.sh
unset __EXECUTED_GIT_COMMAND
fi
case "$1" in
git*)
__EXECUTED_GIT_COMMAND=1
;;
esac
if [ -n "$__CURRENT_GIT_BRANCH" ]; then
s="$__CURRENT_GIT_BRANCH"
printf " %s%s" "%{${fg[red]}%}" "$s"
fi
unset __CURRENT_GIT_BRANCH
local stat="$(git status 2>/dev/null)"
local gitstatus="${HOME}/.zsh/gitstatus.py"
if [ -n "$stat" ]; then
__CURRENT_GIT_BRANCH=`python ${gitstatus}`
fi
# Initialize colors.
autoload -U colors
colors
# Allow for functions in the prompt.
setopt PROMPT_SUBST
# Autoload zsh functions.
fpath=(~/.zsh/functions $fpath)
autoload -U ~/.zsh/functions/*(:t)
# Enable auto-execution of functions.
typeset -ga preexec_functions
typeset -ga precmd_functions
typeset -ga chpwd_functions
# Append git functions needed for prompt.
preexec_functions+='preexec_update_git_vars'
precmd_functions+='precmd_update_git_vars'
chpwd_functions+='chpwd_update_git_vars'
# Set the prompt.
PROMPT=$'%{${fg[cyan]}%}%B%~%b$(prompt_git_info)%{${fg[default]}%} '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment