#!/bin/sh -

gitatG=git@github.com
repo_g="$gitatG":umich-arc
gitatB=git@bitbucket.org
repo_b="$gitatB":umarcts
gitatM=git@git.umms.med.umich.edu
repo_p="$gitatM":platform-services
repo_d="$gitatM":devops

up="$HOME/l/arcts/git/ansible"
dflt='platform-services/ansible-dev platform-services/ansible-prod'
dflt="$dflt platform-services/medinfo-conf"

# gitit - personal convenience front-end for git
# Steve Kinzler, steve@kinzler.com, Apr 23/Apr 26
# https://kinzler.com/me/home.html#unix

case "$1" in
-h)	echo "usage: $0 [ verb ] [ arg ... ]" 1>&2
	$0 -l | sed 's/^/	/'	      1>&2; exit 1;;
-l)	exec sed -n 's/^\([a-z][^) ]*\)).*$/\1/p' `which $0`;;
esac

case "$1" in
up)		for dir in $up
		do	echo "Updating $dir ..."
			(cd "$dir" && gitit pull)
		done; exit;;
cloneG)		exec git clone "$gitatG:$2";;
cloneg|clone)	exec git clone "$repo_g/$2";;
cloneB)		exec git clone "$gitatB:$2";;
cloneb)		exec git clone "$repo_b/$2";;
cloneM)		exec git clone "$gitatM:$2";;
clonep)		exec git clone "$repo_p/$2";;
cloned)		exec git clone "$repo_d/$2";;
dflt)		for repo in $dflt
		do
			$0 clone "$repo"
		done; exit;;
esac

top=`git rev-parse --show-toplevel`			   || exit $?
cd "$top"						   || exit
test -d .git || { echo "$0: not in a repo (`/bin/pwd`)" 1>&2; exit 2; }

case "$1" in
info)		git remote -v;;
status)		git status;;
branch)		git branch -a;;
add)		git add .;;
diff)		git diff	 HEAD 2> /dev/null;;
diffc)		git diff --color HEAD 2> /dev/null;;

commit)		git commit -a -e;;
commitmsg)	shift; echo git commit -a -m "$*";;
amend)		git commit --amend;;
push)		echo 'RUN: gitit merge'
		echo ' OR: gitit PUSH';;
PUSH)		git push;;
merge)		git switch -c "$USER$$" && git push origin "$USER$$";;

log)		git log;;
gc)		git repack -a -d -f --depth=250 --window=250;;
pull)		git pull | grep -v '^Already'; true;;

del)		rm -fr "$top";;

# adapted from https://piechowski.io/post/git-commands-before-reading-code
about)
	echo WHAT CHANGES THE MOST;		    $0 about_changes
	echo WHO BUILT THIS;			    $0 about_who
	echo WHERE DO BUGS CLUSTER;		    $0 about_bugs
	echo IS THIS PROJECT ACCELERATING OR DYING; $0 about_active
	echo HOW OFTEN IS THE TEAM FIREFIGHTING;    $0 about_revert;;
about_changes)
	git log --format=format: --name-only --since='1 year ago' | grep . |
		sort | uniq -c | sort -nr | head -20;;
about_who)
	git shortlog -sn --no-merges | cat;;
about_bugs)
	git log -i -E --grep='fix|bug|broken' --name-only --format='' |
		sort | uniq -c | sort -nr | head -20;;
about_active)
	git log --format='%ad' --date=format:'%Y-%m' |
		sort | uniq -c;;
about_revert)
	git log --oneline --since='1 year ago' |
		grep -iE 'revert|hotfix|emergency|rollback' | sed 's/^/  /';;

*)		$0 diffc | cat; echo --------; $0 status;;
esac
