#!/usr/local/plan9/bin/rc

git=git
show=false
fn gitshow {
	echo '%' git $*
	git $*
}
if(! ~ $#* 0 && ~ $1 -v) {
	git=gitshow
	show=true
	shift
}
	
if(~ $#* 0) {
	echo 'usage: codereview <command> <args>' >[1=2]
	exit usage
}

if(~ $#PLAN9 0) {
	PLAN9=/usr/local/plan9
}
if(! test -d $PLAN9/lib/git) {
	echo 'codereview: cannot find $PLAN9/lib/git' >[1=2]
	exit git
}

if(! test -e $PLAN9/.git/hooks/commit-msg) {
	if($show) {
		echo '% ln -s ../../lib/git/commit-msg.hook $PLAN9/.git/hooks/commit-msg'
	}
	ln -s ../../lib/git/commit-msg.hook $PLAN9/.git/hooks/commit-msg
}

switch($1) {
case help
	9 man 1 codereview

case pending
	shift
	if(! ~ $#* 0) {
		echo 'usage: codereview pending' >[1=2]
		exit usage
	}
	$git branch -v

case create
	shift
	if(~ $#* 0) {
		echo 'usage: codereview create branchname' >[1=2]
		exit usage
	}
	branch=$1
	shift
	if(! git branch -l | 9 grep '\* master$' >/dev/null) {
		echo 'codereview: create not on master branch; use codereview commit' >[1=2]
		exit master
	}
	if($show) {
		echo '% git branch '$branch' && git commit || git branch -d '$branch >[1=2]
	}
	git branch $branch && git checkout $branch && git commit -a $* || git branch -d $branch

case commit
	shift
	if(git branch -l | 9 grep '\* master$' >/dev/null) {
		echo 'codereview: commit on master branch; use codereview create <branchname>' >[1=2]
		exit master
	}
	if(~ `{git merge-base HEAD HEAD} `{git merge-base HEAD master}) {
		# first commit on branch, somehow.
		$git commit $*
		exit $status
	}
	$git commit --amend -a $*
	exit $status

case upload
	if(git branch -l | 9 grep '\* master$' >/dev/null) {
		echo 'codereview: upload on master branch' >[1=2]
		exit master
	}
	if(~ `{git merge-base HEAD HEAD} `{git merge-base HEAD master}) {
		# no commit on branch
		echo 'codereview: no commits yet on this feature branch' >[1=2]
		exit commit
	}
	if(! 9 grep 'machine plan9port-review.googlesource.com' $HOME/.netrc >/dev/null >[2=1]) {
		echo 'codereview: warning: cannot find plan9port-review in netrc' >[1=2]
	}
	if(! git status | 9 grep 'nothing to commit, working directory clean' >/dev/null) {
		echo 'codereview: warning: local changes not yet committed' >[1=2]
		git status
	}
	$git push https://plan9port-review.googlesource.com/plan9 HEAD:refs/for/master >[2=1] | 9 sed 's/.*
//'

case sync
	shift
	if(! ~ $#* 0) {
		echo 'usage: codereview sync' >[1=2]
		exit usage
	}
	$git fetch -q
	
	branch=`{git branch -l | 9 sed -n 's/^\* //p'}
	if(~ $branch master) {
		$git merge -q --ff-only origin/master
		exit $status
	}

	if(~ `{git merge-base HEAD HEAD} `{git merge-base HEAD master}) {
		# no commit on branch
		git merge -q --ff-only origin/master
		exit $status
	}

	# Exactly this commit in master. Fast-forward from master and delete this branch.
	if(git branch -r --contains HEAD | 9 grep '^ *origin/master$' >/dev/null) {
		$git checkout -q master
		$git merge -q --ff-only origin/master
		$git branch -q -d $branch
		exit $status
	}
	
	changeid=`{git log -n 1 | 9 sed -n 's/^    Change-Id: //p'}
	if(~ $#changeid 0) {
		echo 'codereview: cannot find change id'
		exit changeid
	}

	if(git log --grep 'Change-Id: '$changeid origin/master | 9 grep . >/dev/null) {
		# Something like this got submitted.
		$git checkout -q master
		$git merge -q --ff-only origin/master
		echo 'Change submitted but perhaps not identical to your copy.' >[1=2]
		echo 'To remove old copy:' git branch -d $branch >[1=2]
		exit 1
	}
	
	if(git branch -r --contains master | 9 grep '^ *origin/master$' >/dev/null)
		$git branch -f master origin/master
	$git rebase -q origin/master

case *
	echo 'codereview: unrecognized command '$1 >[1=2]
	exit usage
}