Skip to content

Instantly share code, notes, and snippets.

@alexy
Created June 15, 2011 18:11
Show Gist options
  • Save alexy/1027702 to your computer and use it in GitHub Desktop.
Save alexy/1027702 to your computer and use it in GitHub Desktop.
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
# zsh colors -- http://spiralofhope.wordpress.com/2009/04/23/zsh-ansi-prompt/
function precmd {
# let's get the current get branch that we are under
# ripped from /etc/bash_completion.d/git from the git devs
git_ps1 () {
if which git &> /dev/null; then
local g="$(git rev-parse --git-dir 2>/dev/null)"
if [ -n "$g" ]; then
local r
local b
if [ -d "$g/rebase-apply" ]; then
if test -f "$g/rebase-apply/rebasing"; then
r="|REBASE"
elif test -f "$g/rebase-apply/applying"; then
r="|AM"
else
r="|AM/REBASE"
fi
b="$(git symbolic-ref HEAD 2>/dev/null)"
elif [ -f "$g/rebase-merge/interactive" ]; then
r="|REBASE-i"
b="$(cat "$g/rebase-merge/head-name")"
elif [ -d "$g/rebase-merge" ]; then
r="|REBASE-m"
b="$(cat "$g/rebase-merge/head-name")"
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
if [ -n "$1" ]; then
printf "$1" "${b##refs/heads/}$r"
else
printf "%s" " G:${b##refs/heads/}$r"
fi
fi
else
printf ""
fi
}
#PR_GIT="$(git_ps1)"
PR_GIT="$(parse_git_branch)"
}
# needed so zsh prompt will work
setprompt () {
setopt PROMPT_SUBST
PROMPT="%{$fg[blue]%}%m %{$fg[yellow]%}%~ %{$fg[green]%}${PR_GIT}%{$reset_color%}%% "
}
# select a prompt version based on the shell
if [ "$ZSH_VERSION" ]
then
echo "we use the superior ZSH!"
autoload -U colors && colors
setprompt
else
proml_bash
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment