#!/bin/bash

source git-hub-helpers

# Begin working on a new feature
start(){
	BRANCH=${1:?"Please specify a branch name."}
	git stash
	# Just a semantic `git checkout -b $BRANCH $MASTER`
	git checkout -b $BRANCH $MASTER
	git stash pop
}

# Make the feature branch available on the user's fork.
publish(){
	BRANCH=${1:-$CUR_BRANCH}

	git hub-feature update $BRANCH

	echo "Forcing updates to https://github.com/$GH_ORIGIN/tree/$BRANCH"
	git push $Q --force $ORIGIN $BRANCH
}

# Get all updates from upstream into the current branch.
# Can cause merge conflicts, when rebasing and when poping the stash.
update(){
	_stash
	BRANCH=${1:-$CUR_BRANCH}

	git checkout $MASTER
	echo "Rebasing upstream updates into master..."
	git pull $Q --rebase $UPSTREAM $MASTER
	echo "Rebasing master into $BRANCH..."
	git checkout $Q $BRANCH
	git rebase $Q $MASTER

	if [ $CUR_BRANCH != $BRANCH ] ; then
		git checkout $Q $CUR_BRANCH
	fi

	_stash_pop
}

# Create a pull request to review the feature branch.
review(){
	BRANCH=${1:-$CUR_BRANCH}

	git hub-feature publish $BRANCH

	PULL_NUMBER=$(github_pr_number $BRANCH)

	# Check if the PR already exists
	if [ -z "$PULL_NUMBER" ] ; then
		# Prepare the JSON for the pull request.
		# The title is the most recent commit message at time of starting the review.
		TITLE='"title":"'"$BRANCH: $(git log -1 --pretty=format:'%s')"'"'
		BODY='"body":"'"$(git log --pretty=format:'%h - %s <%an>' $MASTER.. | sed ':a;N;s/\n/\\n/g;b a')"'"'
		BASE='"base":"'"$GH_UPSTREAM_USER:$MASTER"'"'
		BASE_SHA='"base_sha":"'"$(git rev-parse $MASTER)"'"'
		HEAD='"head":"'"$GH_ORIGIN_USER:$BRANCH"'"'
		HEAD_SHA='"head_sha":"'"$(git rev-parse $BRANCH)"'"'

		echo "{$TITLE,$BODY,$BASE,$HEAD,$BASE_SHA,$HEAD_SHA}" >| .tmp_pr_req

		github /repos/$GH_UPSTREAM/pulls .tmp_pr_req >| .tmp_pr_res

		PULL_NUMBER=$(github_pr_number $BRANCH)
	fi

	PULL_URL="http://github.com/$GH_UPSTREAM/pull/$PULL_NUMBER"

	echo "Pull request is at $PULL_URL"

	rm .tmp_pr*
}

# Close the feature branch, if it is mergeable.
finish(){
	BRANCH=${1:-$CUR_BRANCH}

	git hub-feature publish $BRANCH

	PULL_NUMBER=$(github_pr_number $BRANCH)

	github_put /repos/$GH_UPSTREAM/pulls/$PULL_NUMBER/merge "{}" >| .tmp_merge

	if [ "x$(cat .tmp_merge | json merged)" == "xtrue" ] ; then
		echo "Pull request $PULL_NUMBER successfully merged."
		_stash
		git checkout $Q $MASTER >/dev/null
		_stash_pop

		git pull $Q --rebase $UPSTREAM $MASTER
		git push $Q $ORIGIN :$BRANCH
		git branch $Q -D $BRANCH
	else
		cat .tmp_merge | json message
	fi

	rm .tmp_merge
}

usage(){
	echo "$0:	Manage a github flow features."
}

help(){
	usage
	echo ""
	echo "	start <branchname>: Begin working on a new feature."
	echo "	publish [branchname]: Publish branch to origin. Defaults to current branch."
	echo "	update [branchname]: Pull changes from upstream into the development branch,"
	echo "	                     then rebase the branch off development."
	echo "	review [branchname]: Create a pull request for a feature."
	echo "	finish [branchname]: Merge a pull request and clean up branches."
	echo "	help: Show this message."
}

command=${1:-usage}
shift
case "$command" in
	s | start) start $@ ;;
	p | publish) publish $@ ;;
	u | update) update $@ ;;
	r | review) review $@ ;;
	f | finish) finish $@ ;;
	h | help) help ;;
	*) usage ;;
esac