Make git prompt sh compatible

This commit is contained in:
Talia 2020-01-09 15:49:45 +01:00
parent 69d06ad700
commit abe9d47390

View file

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