Skip to content

Instantly share code, notes, and snippets.

@gwoo
Created November 28, 2012 21:17
Show Gist options
  • Save gwoo/4164592 to your computer and use it in GitHub Desktop.
Save gwoo/4164592 to your computer and use it in GitHub Desktop.
add git info to terminal prompt
parse_git_path() {
repo=$(git rev-parse --git-dir 2>/dev/null)
if [ -n $repo ]
then
repo=$(echo $repo | sed 's/.git$//')
if [ -z $repo ]
then
repo=$PWD
else
home=$HOME
repo=$(echo $repo | sed 's/\/$//')
fi
fi
repo=$(echo $repo | sed 's,'"$HOME"',~,')
echo ${repo##*/}
}
parse_git_branch () {
repo=$(git rev-parse --git-dir 2>/dev/null)
if [ -n $repo ]
then
if git rev-parse --git-dir >/dev/null 2>&1
then
branch=$(git branch 2>/dev/null| sed -n '/^\*/s/^\* //p')
if git diff --quiet 2>/dev/null >&2
then
branch=$branch
else
branch=$branch'*'
fi
else
return 0
fi
#branch=$branch
fi
echo "("$branch")"
}
parse_git_subdir() {
local subdir=""
repo=$(git rev-parse --git-dir 2>/dev/null)
if [ -n $repo ]
then
repo=$(echo $repo | sed 's/.git$//')
if [ -z $repo ]
then
return 0
else
home=$HOME
subdir="/"$(echo $PWD | sed 's,'"$repo"',,')
fi
fi
echo $subdir
}
BASE="\[\033[0;37m\]"
MAGENTA="\[\033[0;35m\]"
GREEN="\[\033[0;32m\]"
BLUE="\[\033[0;36m\]"
PS1="$BLUE\$(parse_git_path)$MAGENTA\$(parse_git_branch)$BASE\$(parse_git_subdir)$BASE: "
export PROMPT_COMMAND='echo -ne "\033]0;${PWD/#$HOME/~} `git branch 2>&1 | grep "*" | awk -F" " "{print $2}"`"; echo -ne "\007"'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment