darkrc/bin/gitprompt

118 lines
2.5 KiB
Text
Raw Normal View History

2020-01-09 14:49:45 +00:00
#!/bin/sh
2020-01-08 13:45:27 +00:00
git__fetch() {
if [ -n "$BASH_AUTOFETCH_TIMEOUT" ]; then
timeout=$BASH_AUTOFETCH_TIMEOUT
else
timeout=60
fi
2020-01-09 14:49:45 +00:00
if [ -f "$1/.git/FETCH_HEAD" ]; then
2020-01-08 13:45:27 +00:00
diff=$(($(date +%s) - $(stat -c %Y $1/.git/FETCH_HEAD)))
else
diff=9999
fi
2020-01-09 14:49:45 +00:00
if [ "$diff" -gt "$timeout" ]
2020-01-08 13:45:27 +00:00
then run=1
else run=0
fi
2020-01-09 14:49:45 +00:00
if [ "$run" -gt 0 ]; then
2020-01-08 13:45:27 +00:00
touch $1/.git/FETCH_HEAD
nohup git fetch > /dev/null 2>&1 &
fi
echo $run
}
git__prompt () {
top=$(git rev-parse --show-toplevel 2>/dev/null)
if [ -n "$top" ]
then
2020-01-09 14:49:45 +00:00
if [ "$BASH_AUTOFETCH" -gt 0 ]; then
2020-01-08 13:45:27 +00:00
autofetch=$(git__fetch $top)
f=$((2-$autofetch))
else
f=3
fi
2020-01-09 14:49:45 +00:00
/bin/echo -ne "\033[00;3${f}mδ\033[00;33m"
status=$(git status --short 2>/dev/null)
branch=$(git branch | grep -Po '(?<=\* )[[:alnum:]_.-]*')
modif=$(echo "$status" | grep -Po '^\s*M' | wc -l)
untracked=$(echo "$status" | grep -Po '^\?\?' | wc -l)
added=$(echo "$status" | grep -Po '^\s*[A]' | wc -l)
deleted=$(echo "$status" | grep -Po '^\s*[D]' | wc -l)
renamed=$(echo "$status" | grep -Eo '^\s*R' | wc -l)
stat=$(git branch -vv | grep -P '^\*' | grep -Po '\[.*\]')
ahead=$(echo $stat | grep -Po '(?<=ahead )\d*')
behind=$(echo $stat | grep -Po '(?<=behind )\d*')
2020-01-08 13:45:27 +00:00
gray='\033[01;30m'
blue='\033[01;34m'
yellow='\033[01;33m'
red='\033[01;31m'
green='\033[01;32m'
purple='\033[01;35m'
# SYNC
2020-01-09 14:49:45 +00:00
if [ -z "$ahead" ] && [ -z "$behind" ]
2020-01-08 13:45:27 +00:00
then
2020-01-09 14:49:45 +00:00
/bin/echo -ne "" # Nothing to do here
elif [ -z "$ahead" ]
2020-01-08 13:45:27 +00:00
then
2020-01-09 14:49:45 +00:00
/bin/echo -ne " ${yellow}↓${behind}"
elif [ -z "$behind" ]
2020-01-08 13:45:27 +00:00
then
2020-01-09 14:49:45 +00:00
/bin/echo -ne " ${green}↑${ahead}"
2020-01-08 13:45:27 +00:00
else
2020-01-09 14:49:45 +00:00
/bin/echo -ne " ${red}↓${behind}${red}↑${ahead}"
2020-01-08 13:45:27 +00:00
fi
# BRANCH
2020-01-09 14:49:45 +00:00
if [ -z "$branch" ]
2020-01-08 13:45:27 +00:00
then
2020-01-09 14:49:45 +00:00
head=$(git rev-parse --short HEAD 2>&1)
2020-01-15 13:35:35 +00:00
if [ "$(echo "$head" | grep '^fatal')" ]
2020-01-08 13:45:27 +00:00
then
branch='{no commits}'
else
branch='#'"$head"
fi
fi
if [ "$branch" = 'master' ]
then
2020-01-09 14:49:45 +00:00
/bin/echo -ne " $blue$branch"
2020-01-13 13:59:22 +00:00
elif [ $(echo "$branch" | grep '^#' ) ]
2020-01-08 13:45:27 +00:00
then
2020-01-09 14:49:45 +00:00
/bin/echo -ne " $red$branch"
2020-01-08 13:45:27 +00:00
else
2020-01-09 14:49:45 +00:00
/bin/echo -ne " $yellow$branch"
2020-01-08 13:45:27 +00:00
fi
# CHANGES
2020-01-09 14:49:45 +00:00
if [ "$modif" = 0 ]
2020-01-08 13:45:27 +00:00
then
2020-01-09 14:49:45 +00:00
/bin/echo -ne # "${gray}:\033[01;36m$modif" # No modified files
2020-01-08 13:45:27 +00:00
else
2020-01-09 14:49:45 +00:00
/bin/echo -ne "${gray}:\033[01;33m$modif" # Modified files
2020-01-08 13:45:27 +00:00
fi
2020-01-09 14:49:45 +00:00
if [ "$added" -ne 0 ]
2020-01-08 13:45:27 +00:00
then
2020-01-09 14:49:45 +00:00
/bin/echo -ne " ${green}+$added"
2020-01-08 13:45:27 +00:00
fi
2020-01-09 14:49:45 +00:00
if [ "$deleted" -ne 0 ]
2020-01-08 13:45:27 +00:00
then
2020-01-09 14:49:45 +00:00
/bin/echo -ne " ${red}-$deleted"
2020-01-08 13:45:27 +00:00
fi
2020-01-09 14:49:45 +00:00
if [ "$renamed" -ne 0 ]
2020-01-08 13:45:27 +00:00
then
2020-01-09 14:49:45 +00:00
/bin/echo -ne " ${yellow}$renamed→$renamed"
2020-01-08 13:45:27 +00:00
fi
2020-01-09 14:49:45 +00:00
if [ "$untracked" -ne 0 ]
2020-01-08 13:45:27 +00:00
then
2020-01-09 14:49:45 +00:00
/bin/echo -ne " ${purple}+$untracked"
2020-01-08 13:45:27 +00:00
fi
else
exit 1
2020-01-08 13:45:27 +00:00
fi
}
git__prompt
echo