Skip to content

Instantly share code, notes, and snippets.

@wence-
Created November 14, 2014 11:26
Show Gist options
  • Select an option

  • Save wence-/1f5d77b0c856b78033eb to your computer and use it in GitHub Desktop.

Select an option

Save wence-/1f5d77b0c856b78033eb to your computer and use it in GitHub Desktop.
bzr_branch=''
function find_bzr_branch {
local dir=. dirty head nick
# Parse all the bzr config stuff by hand, rather than calling out
# to the binary, cos it's so damn slow.
until [ "$dir" -ef / ]; do
if [ -d "$dir/.bzr/checkout" ]; then
head=$(readlink -f $dir)
dirty=''
bzr_branch=" ${red}b${green}"
# horrendously slow
bzr status -SV 2>/dev/null | grep "^ M" >/dev/null 2>&1 && dirty="yes"
nick=''
while read line; do
if [[ $line == nickname\ =\ * ]]; then
nick=${line/nickname\ =\ /}
fi
done < $dir/.bzr/branch/branch.conf
if [[ $nick != '' ]]; then
bzr_branch="$bzr_branch ${nick}"
else
bzr_branch="$bzr_branch ${head##*/}"
fi
if [[ $dirty != '' ]]; then
bzr_branch="$bzr_branch ${red}d"
fi
return
fi
dir="../$dir"
done
bzr_branch=''
}
hg_branch=''
function find_hg_branch {
local dir=. head dirty bookmark all
declare -a all
until [ "$dir" -ef / ]; do
if [ -d "$dir/.hg" ]; then
all=( $(hg prompt 'x{branch} x{status|modified} x{bookmark}') )
head=${all[0]:1}
dirty=${all[1]:1}
bookmark=${all[2]:1}
hg_branch=" ${red}h${green}"
if [[ $bookmark != '' ]]; then
hg_branch="$hg_branch ${bookmark}"
else
hg_branch="$hg_branch b(${head})"
fi
if [[ $dirty != '' ]]; then
hg_branch="$hg_branch ${red}d"
fi
return
fi
dir="../$dir"
done
hg_branch=''
}
git_branch=''
function find_git_branch {
local dir=. head staged dirty stash
until [ "$dir" -ef / ]; do
if [ -f "$dir/.git/HEAD" ]; then
git_branch=" ${red}g${green}"
head=$(< "$dir/.git/HEAD")
dirty=''
git --git-dir="${dir}git" --work-tree="${dir}" diff --no-ext-diff \
--quiet --exit-code || \
dirty="yes"
staged=''
if git rev-parse --quiet --verify HEAD > /dev/null; then
git diff-index --cached --quiet HEAD -- || \
staged="yes"
fi
stash=''
# stash is just a file in refs/stash
if [[ -f "$dir/.git/refs/stash" ]]; then
stash="yes"
fi
if [[ $head == ref:\ refs/heads/* ]]; then
git_branch="$git_branch ${head#*/*/}"
elif [[ $head != '' ]]; then
git_branch="$git_branch (detached: ${head:0:6})"
else
git_branch='$git_branch (unknown)'
fi
if [[ $staged != '' ]]; then
git_branch="$git_branch ${yellow}s"
fi
if [[ $dirty != '' ]]; then
git_branch="$git_branch ${red}d"
fi
if [[ $stash != '' ]]; then
git_branch="$git_branch ${magenta}st"
fi
return
fi
dir="../$dir"
done
git_branch=''
}
blue=$'\e[1;34m'
magenta=$'\e[1;35m'
red=$'\e[1;31m'
normal=$'\e[m'
green=$'\e[1;32m'
yellow=$'\e[1;33m'
export PROMPT_COMMAND="find_git_branch; find_bzr_branch; find_hg_branch"
export PS1='\u@\h:\[$blue\]\w\[$green\]$git_branch$bzr_branch$hg_branch\[$normal\]\n\$ '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment