darkrc/bin/gitprompt

124 lines
2.7 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() {
2020-05-20 08:14:36 +00:00
if [ -f "$1/.git" ]
then dir="$(cat $top/.git | sed -e 's/^gitdir: //')"
else dir="$1/.git"
fi
2020-01-08 13:45:27 +00:00
if [ -n "$BASH_AUTOFETCH_TIMEOUT" ]; then
timeout=$BASH_AUTOFETCH_TIMEOUT
else
timeout=60
fi
2020-05-20 08:14:36 +00:00
if [ -f "$dir/FETCH_HEAD" ]; then
diff=$(($(date +%s) - $(stat -c %Y $dir/FETCH_HEAD)))
2020-01-08 13:45:27 +00:00
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-05-20 08:14:36 +00:00
touch $dir/FETCH_HEAD
2020-01-08 13:45:27 +00:00
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
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-02-04 14:27:13 +00:00
gray='\033[00;37m'
2020-01-08 13:45:27 +00:00
blue='\033[01;34m'
yellow='\033[01;33m'
red='\033[01;31m'
green='\033[01;32m'
purple='\033[01;35m'
2020-02-04 14:27:13 +00:00
/bin/echo -ne "\033[00;3${f}mδ\033[00;33m "
/bin/echo -ne "${purple}$(basename $top) "
2020-01-08 13:45:27 +00:00
# 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-02-04 14:27:13 +00:00
/bin/echo -ne "$blue"
2020-01-13 13:59:22 +00:00
elif [ $(echo "$branch" | grep '^#' ) ]
2020-01-08 13:45:27 +00:00
then
2020-02-04 14:27:13 +00:00
/bin/echo -ne "$red"
2020-01-08 13:45:27 +00:00
else
2020-02-04 14:27:13 +00:00
/bin/echo -ne "$yellow"
fi
/bin/echo -ne "$branch\033[00;36m"
# SYNC
if [ -z "$ahead" ] && [ -z "$behind" ]
then
/bin/echo -ne "" # Nothing to do here
elif [ -z "$ahead" ]
then
/bin/echo -ne " ${yellow}↓${behind}"
elif [ -z "$behind" ]
then
/bin/echo -ne " ${green}↑${ahead}"
else
/bin/echo -ne " ${red}↓${behind}${red}↑${ahead}"
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-02-05 08:41:58 +00:00
/bin/echo -ne " \033[01;36m≠$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-02-04 14:27:13 +00:00
/bin/echo -ne " ${purple}+$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-02-04 14:27:13 +00:00
/bin/echo -ne " ${purple}~$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-02-05 08:41:58 +00:00
/bin/echo -ne " ${yellow}+$untracked"
2020-01-08 13:45:27 +00:00
fi
else
exit 1
2020-01-08 13:45:27 +00:00
fi
}
git__prompt