Skip to content

Instantly share code, notes, and snippets.

@greneholt
Last active November 3, 2017 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greneholt/3faf901461305eecf028438652e2e0c2 to your computer and use it in GitHub Desktop.
Save greneholt/3faf901461305eecf028438652e2e0c2 to your computer and use it in GitHub Desktop.
#!/bin/bash
while getopts "c" opt; do
case "$opt" in
c)
color=1
;;
?)
exit 1
esac
done
if [[ $color ]]; then
Reset="\e[0m"
Bold="\e[1m"
Dim="\e[2m"
Underline="\e[4m"
Reverse="\e[7m"
Default="\e[39m"
Black="\e[30m"
Red="\e[31m"
Green="\e[32m"
Yellow="\e[33m"
Blue="\e[34m"
Magenta="\e[35m"
Cyan="\e[36m"
White="\e[37m"
fi
in_git="$(git rev-parse --is-inside-work-tree 2>/dev/null)"
git_exit="$?"
svn_info="$(svn info 2>/dev/null)"
svn_exit="$?"
if [[ $git_exit -eq 0 && $in_git == true ]]; then
# In a git repo.
if read rem_rev loc_rev < <(git rev-list --count --left-right @{upstream}...HEAD 2>/dev/null); then
if [[ $rem_rev -gt 0 && $loc_rev -gt 0 ]]; then
upstream="<>" # diverged from upstream
elif [[ $rem_rev -gt 0 ]]; then
upstream="<" # behind upstream
elif [[ $loc_rev -gt 0 ]]; then
upstream=">" # ahead of upstream
else
upstream="=" # equal with upstream
fi
else
upstream="" # no upstream
fi
if git diff-index --quiet HEAD -- &>/dev/null; then
clean=""
else
clean="*"
fi
wc_root="$(git rev-parse --show-toplevel 2>/dev/null)"
echo -e "$Black$(dirname "$wc_root")/$Blue$Bold$(basename "$wc_root") [$(git rev-parse --abbrev-ref HEAD 2>/dev/null)$upstream$clean]$Reset $Cyan$Reverse/$(git rev-parse --show-prefix 2>/dev/null)$Reset"
elif [[ $svn_exit -eq 0 ]]; then
# In a svn repo.
# This strange sequence is necessary because svn resolves symlinks when reporting wc_root. So
# we resolve symlinks in PWD, then remove the wc_root from it to get the path relative to
# wc_root. Finally, we remove rel_cur_dir from PWD to get wc_root without symlinks resolved.
wc_root="$(echo "$svn_info" | sed -n 's/Working Copy Root Path: \(.*\)/\1/p')"
abs_cur_dir="$(readlink -e $PWD)"
rel_cur_dir="${abs_cur_dir#$wc_root}"
wc_root="${PWD%$rel_cur_dir}"
[[ -z $rel_cur_dir ]] && rel_cur_dir=/ # Force the rel_cur_dir to be a slash in the root diretory.
wc_rel_url="$(echo "$svn_info" | sed -n 's/Relative URL: \(.*\)/\1/p')"
wc_rel_url="${wc_rel_url%$rel_cur_dir}"
echo -e "$Black$(dirname "$wc_root")/$Magenta$Bold$(basename "$wc_root") [$wc_rel_url]$Reset $Cyan$Reverse$rel_cur_dir$Reset"
else
echo -e "$Cyan$Reverse$PWD$Reset"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment