Skip to content

Instantly share code, notes, and snippets.

@trodrigues
Created April 13, 2011 09:08
Show Gist options
  • Save trodrigues/917242 to your computer and use it in GitHub Desktop.
Save trodrigues/917242 to your computer and use it in GitHub Desktop.
# showing git branch
export __CURRENT_GIT_BRANCH=
export __CURRENT_GIT_VARS_INVALID=1
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}
zsh_git_invalidate_vars() {
export __CURRENT_GIT_VARS_INVALID=1
prompt
}
zsh_git_compute_vars() {
export __CURRENT_GIT_BRANCH="$(parse_git_branch)"
export __CURRENT_GIT_VARS_INVALID=
}
chpwd_functions+='zsh_git_chpwd_update_vars'
zsh_git_chpwd_update_vars() {
zsh_git_invalidate_vars
}
preexec_functions+='zsh_git_preexec_update_vars'
zsh_git_preexec_update_vars() {
case "$(history $HISTCMD)" in
*git*)
zsh_git_invalidate_vars
;;
esac
}
precmd_functions+=prompt
get_git_prompt_info() {
test -n "$__CURRENT_GIT_VARS_INVALID" && zsh_git_compute_vars
echo $__CURRENT_GIT_BRANCH
}
# prompt config
function prompt {
if [ "$(get_git_prompt_info)" = "" ] ; then
GIT_START=""; GIT_END="";
else
GIT_START="["; GIT_END="]";
fi
PS1=""
PS1=$PS1"%F{green}%4~"
PS1=$PS1"%f%F{yellow}$GIT_START"
PS1=$PS1"$(get_git_prompt_info)$GIT_END%f "
if [ "$SSH_TTY" != "" ] ; then
PS1=$PS1"%F{magenta}%m%f"
fi
PS1=$PS1"%F{green}%#%f "
}
prompt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment