adding git details to bash prompt. fixes #2

This commit is contained in:
Andrew Davidson 2023-01-10 22:23:20 -08:00
parent 57d749b04a
commit 9d30e171e4

View file

@ -66,9 +66,41 @@ sh_yellow="\[\033[1;33m\]"
sh_light_gray="\[\033[0;37m\]"
sh_white="\[\033[1;37m\]"
function git_prompt {
PCHANGED=""
PAHEAD=""
PBEHIND=""
local FULL_BRANCH="$(git symbolic-ref -q HEAD 2>/dev/null || git name-rev --name-only --no-undefined --always HEAD 2>/dev/null)"
PBRANCH="$(echo $FULL_BRANCH | sed 's/refs\/heads\///')"
if [ -n $PBRANCH ]; then
echo -n "${PBRANCH} "
# Are there unstaged changes in the working directory
local CHANGED=$(git status --porcelain --ignore-submodule -unormal 2>/dev/null | wc -l)
if [ $CHANGED -gt 0 ]; then
PCHANGED="◊"
fi
# Are we behind the origin?
local NUM_BEHIND="$(git log --oneline ..@{u} 2>/dev/null | wc -l | tr -d ' ' )"
if [ "$NUM_BEHIND" -gt 0 ]; then
PBEHIND="↓"
fi
# Are we ahead of the origin?
local NUM_AHEAD="$(git log --oneline @{u}.. 2>/dev/null | wc -l | tr -d ' ')"
if [ "$NUM_AHEAD" -gt 0 ]; then
PAHEAD="↑"
fi
if [[ -n $PCHANGED || -n $PAHEAD || -n $PBEHIND ]]; then
echo -n "${PCHANGED}${PBEHIND}${PAHEAD}"
fi
fi
}
# Make a pretty prompt.
export PROMPT_COMMAND='history -a; if [ $? -ne 0 ];then ERROR_FLAG=1;else ERROR_FLAG=;fi;'
export PS1=${HOSTCOLOUR}${sh_light_gray}'\t | \h'${sh_green}':\w'${sh_light_gray}' '${sh_light_gray}'${ERROR_FLAG:+'${sh_light_red}'}\$ '${sh_norm}
export PS1=${HOSTCOLOUR}${sh_light_gray}'\t | \h'${sh_green}':\w'${sh_light_gray}' $(git_prompt)'${sh_light_gray}'${ERROR_FLAG:+'${sh_light_red}'}\$ '${sh_norm}
# New files and folders should not be world readable
umask 0027