env/utils/shgit

60 lines
1.5 KiB
Plaintext
Raw Permalink Normal View History

2019-12-09 12:03:41 +01:00
# shgit
2023-06-20 03:00:31 +02:00
# Written in 2019-2021 by Lucas
2019-09-11 04:11:40 +02:00
# CC0 1.0 Universal/Public domain - No rights reserved
#
# To the extent possible under law, the author(s) have dedicated all
# copyright and related and neighboring rights to this software to the
# public domain worldwide. This software is distributed without any
# warranty. You should have received a copy of the CC0 Public Domain
# Dedication along with this software. If not, see
# <http://creativecommons.org/publicdomain/zero/1.0/>.
2019-12-09 12:03:41 +01:00
shgit()
{
: ${SHGIT_BEGIN:=} ${SHGIT_END:= } ${SHGIT_SEP:=:}
2019-12-14 01:58:19 +01:00
args=$(git rev-parse --git-dir --is-inside-work-tree 2>/dev/null) ||
2019-12-09 12:03:41 +01:00
return
2019-12-14 01:58:19 +01:00
set -- $args
gitdir=${1:-}
worktree=${2:-}
2019-09-11 04:11:40 +02:00
if ! name=$(git symbolic-ref -q --short HEAD); then
2019-12-10 12:30:20 +01:00
name=$(git rev-parse --short HEAD 2>/dev/null)
2021-01-21 04:06:17 +01:00
name=${name:-"?"}
2019-12-09 12:03:41 +01:00
fi
2019-09-11 04:11:40 +02:00
rb=
2019-12-10 12:30:20 +01:00
rbdir=$gitdir/rebase-merge
2019-09-11 04:11:40 +02:00
if [ -d "$rbdir" ]; then
2019-12-10 12:30:20 +01:00
rb=${SHGIT_SEP}REBASE
2019-09-11 04:11:40 +02:00
read -r name <"$rbdir/head-name"
name=${name##*/}
[ -f "$rbdir/interactive" ] && rb="$rb-i"
rbcount=$(wc -l <"$rbdir/done")
read -r rbtotal <"$rbdir/end"
rb="$rb ${rbcount##* }/$rbtotal"
fi
status=
2019-12-14 01:58:19 +01:00
if [ "$worktree" = "true" ]; then
if [ -n "$(git status --porcelain)" ]; then
2021-01-21 04:06:17 +01:00
status=$SHGIT_SEP"*"
2019-12-14 01:58:19 +01:00
fi
else
2021-01-21 04:06:17 +01:00
status=$SHGIT_SEP"?"
2019-12-14 01:58:19 +01:00
fi
2019-09-11 04:11:40 +02:00
2020-05-03 16:55:57 +02:00
oldIFS=$IFS
IFS=" "
set -- $(git rev-list --left-right --count @{upstream}...HEAD \
2>/dev/null)
IFS=$oldIFS
commits=v${1:-0}^${2:-0}
commits=${commits#v0}
commits=${commits%^0}
commits=${commits:+$SHGIT_SEP$commits}
2019-09-11 04:11:40 +02:00
2019-12-09 12:03:41 +01:00
printf "%s%s%s" "$SHGIT_BEGIN" "$name$rb$status$commits" "$SHGIT_END"
2019-09-11 04:11:40 +02:00
}