#
# HubFlow - a fork of the git-flow tools to apply Vincent Driessen's
# branching model to working with GitHub
#
# Original blog post presenting this model is found at:
#    http://nvie.com/git-model
#
# The HubFlow documentation is found at:
#    http://datasift.github.com/gitflow/
#
# Feel free to contribute to this project at:
#    http://github.com/datasift/gitflow
#
# Copyright 2010 Vincent Driessen. All rights reserved.
# Copyright 2012 MediaSift Ltd. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#    1. Redistributions of source code must retain the above copyright notice,
#       this list of conditions and the following disclaimer.
#
#    2. Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY VINCENT DRIESSEN ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL VINCENT DRIESSEN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation are
# those of the authors and should not be interpreted as representing official
# policies, either expressed or implied, of Vincent Driessen.
#

require_git_repo
require_hubflow_initialized
hubflow_load_settings
VERSION_PREFIX=$(eval "echo `git config --get hubflow.prefix.versiontag`")
PREFIX=$(git config --get hubflow.prefix.hotfix)

usage() {
	echo "usage: git hf hotfix [list] [-v]"
	echo "       git hf hotfix start <version> [<base>]"
	echo "       git hf hotfix finish [-sumpk] <version>"
	echo "       git hf hotfix track <name>"
	echo "       git hf hotfix pull [-r] [<remote> [<version>]]"
	echo "       git hf hotfix push [<remote> [<version>]]"
	echo "       git hf hotfix cancel [-f] <version>"
}

cmd_default() {
	cmd_list "$@"
}

cmd_list() {
	DEFINE_boolean verbose false 'verbose (more) output' v
	parse_args "$@"

	local hotfix_branches
	local current_branch
	local short_names
	hotfix_branches=$(echo "$(git_local_branches)" | grep "^$PREFIX")
	if [ -z "$hotfix_branches" ]; then
		warn "No hotfix branches exist."
                warn ""
                warn "You can start a new hotfix branch:"
                warn ""
                warn "    git hf hotfix start <version> [<base>]"
                warn ""
		exit 0
	fi
	current_branch=$(git branch --no-color | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
	short_names=$(echo "$hotfix_branches" | sed "s ^$PREFIX  g")

	# determine column width first
	local width=0
	local branch
	for branch in $short_names; do
		local len=${#branch}
		width=$(max $width $len)
	done
	width=$(($width+3))

	local branch
	for branch in $short_names; do
		local fullname=$PREFIX$branch
		local base=$(git merge-base "$fullname" "$MASTER_BRANCH")
		local master_sha=$(git rev-parse "$MASTER_BRANCH")
		local branch_sha=$(git rev-parse "$fullname")
		if [ "$fullname" = "$current_branch" ]; then
			printf "* "
		else
			printf "  "
		fi
		if flag verbose; then
			printf "%-${width}s" "$branch"
			if [ "$branch_sha" = "$master_sha" ]; then
				printf "(no commits yet)"
			else
				local tagname=$(git name-rev --tags --no-undefined --name-only "$base")
				local nicename
				if [ "$tagname" != "" ]; then
					nicename=$tagname
				else
					nicename=$(git rev-parse --short "$base")
				fi
				printf "(based on $nicename)"
			fi
		else
			printf "%s" "$branch"
		fi
		echo
	done
}

cmd_help() {
	usage
	exit 0
}

name_or_current() {
	if [ -z "$NAME" ]; then
		use_current_hotfix_branch_name
	fi
}

parse_args() {
	# parse options
	FLAGS "$@" || exit $?
	eval set -- "${FLAGS_ARGV}"

	# read arguments into global variables
	VERSION=$1
	BRANCH=$PREFIX$VERSION
}

parse_remote_name() {
	# parse options
	FLAGS "$@" || exit $?
	eval set -- "${FLAGS_ARGV}"

	# read arguments into global variables
	REMOTE=$1
	NAME=$2
	BRANCH=$PREFIX$VERSION
}

require_version_arg() {
	if [ "$VERSION" == "" ]; then
		warn "Missing argument <version>"
		usage
		exit 1
	fi
}

require_base_is_on_master() {
	if ! git branch --no-color --contains "$BASE" 2>/dev/null \
			| sed 's/[* ] //g' \
	  		| grep -q "^$MASTER_BRANCH\$"; then
		die "fatal: Given base '$BASE' is not a valid commit on '$MASTER_BRANCH'."
	fi
}

require_no_existing_hotfix_branches() {
	local hotfix_branches=$(echo "$(git_local_branches)" | grep "^$PREFIX")
	local first_branch=$(echo ${hotfix_branches} | head -n1)
	first_branch=${first_branch#$PREFIX}
	[ -z "$hotfix_branches" ] || \
		die "There is an existing hotfix branch ($first_branch). Finish that one first."
}

use_current_hotfix_branch_name() {
	local current_branch=$(git_current_branch)
	if startswith "$current_branch" "$PREFIX"; then
		BRANCH=$current_branch
		NAME=${BRANCH#$PREFIX}
	else
		warn "The current HEAD is not a hotfix branch."
		warn "Please specify a <name> argument."
		exit 1
	fi
}

cmd_start() {
	DEFINE_boolean fetch true "fetch from $ORIGIN before creating the new branch" F
	parse_args "$@"
	BASE=${2:-$MASTER_BRANCH}
	require_version_arg

	# sanity checks
	require_clean_working_tree
	require_remote_available
	if flag fetch ; then
		hubflow_fetch_latest_changes_from_origin
	fi
	require_base_is_on_master
	require_no_existing_hotfix_branches
	require_branch_absent "$BRANCH"
	require_tag_absent "$VERSION_PREFIX$VERSION"
	if has "$ORIGIN/$MASTER_BRANCH" $(git_remote_branches); then
		require_branches_equal "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH"
	fi

	# create branch
	git checkout -b "$BRANCH" "$BASE"

	# push it back up to remote repo
	hubflow_push_latest_changes_to_origin

	echo
	echo "Summary of actions:"
	echo "- A new branch '$BRANCH' was created, based on '$BASE'"
	echo "- The branch '$BRANCH' has been pushed up to '$ORIGIN/$BRANCH'"
	echo "- You are now on branch '$BRANCH'"
	echo
	echo "Follow-up actions:"
	echo "- Bump the version number now!"
	echo "- Start committing your hot fixes"
	echo "- When done, run:"
	echo
	echo "     git hf hotfix finish '$VERSION'"
	echo
}

cmd_finish() {
	DEFINE_boolean fetch true "fetch from $ORIGIN before performing finish" F
	DEFINE_boolean sign false "sign the release tag cryptographically" s
	DEFINE_string signingkey "" "use the given GPG-key for the digital signature (implies -s)" u
	DEFINE_string message "" "use the given tag message" m
	DEFINE_boolean push true "push to $ORIGIN after performing finish" p
	DEFINE_boolean keep false "keep branch after performing finish" k
	DEFINE_boolean notag false "don't tag this release" n
	DEFINE_boolean nobackmerge false "don't back-merge $MASTER_BRANCH to be a parent of $DEVELOP_BRANCH (using tag if applicable)" b	parse_args "$@"
	parse_args "$@"
	require_version_arg

	# handle flags that imply other flags
	if [ "$FLAGS_signingkey" != "" ]; then
		FLAGS_sign=$FLAGS_TRUE
	fi

	# sanity checks
	require_branch "$BRANCH"
	require_clean_working_tree
	require_remote_available

	# update local repo with remote changes first, if asked
	if flag fetch; then
		# fetch and merge the latest changes from origin
		hubflow_merge_latest_changes_from_origin
	fi

	# we must be up to date before continuing
	if has "$ORIGIN/$MASTER_BRANCH" $(git_remote_branches); then
		require_branches_equal "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH"
	fi
	if has "$ORIGIN/$DEVELOP_BRANCH" $(git_remote_branches); then
		require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH"
	fi

	# try to merge into master
	# in case a previous attempt to finish this release branch has failed,
	# but the merge into master was successful, we skip it now
	if ! git_is_branch_merged_into "$BRANCH" "$MASTER_BRANCH"; then
		git checkout "$MASTER_BRANCH" || \
		  die "Could not check out $MASTER_BRANCH."
		git merge --no-ff -srecursive -Xtheirs "$BRANCH" || \
		  die "There were merge conflicts."
		  # TODO: What do we do now?
	fi

	if noflag notag; then
		# try to tag the release
		# in case a previous attempt to finish this release branch has failed,
		# but the tag was set successful, we skip it now
		local tagname=$VERSION_PREFIX$VERSION
		if ! git_tag_exists "$tagname"; then
			local opts="-a"
			flag sign && opts="$opts -s"
			[ "$FLAGS_signingkey" != "" ] && opts="$opts -u '$FLAGS_signingkey'"
			[ "$FLAGS_message" != "" ] && opts="$opts -m '$FLAGS_message'"
			eval git tag $opts "$VERSION_PREFIX$VERSION" || \
			die "Tagging failed. Please run finish again to retry."
		fi
	fi

	# try to merge into develop
	if noflag nobackmerge; then
		# in case a previous attempt to finish this hotfix branch has failed,
		# but the merge into develop was successful, we skip it now
		if ! git_is_branch_merged_into "$MASTER_BRANCH" "$DEVELOP_BRANCH"; then
			git checkout "$DEVELOP_BRANCH" || \
				die "Could not check out $DEVELOP_BRANCH."
			# merge the master branch back into develop; this makes the master
			# branch - and the new tag (if provided) - a parent of the development
			# branch, which in turn lets you use 'git describe' on either branch
			if noflag notag; then
				git merge --no-ff "$tagname" || \
					die "There were merge conflicts."
			else
				git merge --no-ff "$MASTER_BRANCH" || \
					die "There were merge conflicts."
			fi
		fi
	else
		# in case a previous attempt to finish this hotfix branch has failed,
		# but the merge into develop was successful, we skip it now
		if ! git_is_branch_merged_into "$BRANCH" "$DEVELOP_BRANCH"; then
			git checkout "$DEVELOP_BRANCH" || \
				die "Could not check out $DEVELOP_BRANCH."
			# just merge the release branch into the development branch
			git merge --no-ff "$BRANCH" || \
				die "There were merge conflicts."
		fi
	fi

	# delete branch
	if noflag keep; then
		# delete the remote branch first
		if flag push ; then
			git push "$ORIGIN" :"$BRANCH" || \
				die "Could not delete remote branch"
		fi

		# delete the local branch afterwards
		git branch -d "$BRANCH"
	fi

	if flag push; then
		git push "$ORIGIN" "$DEVELOP_BRANCH" || \
			die "Could not push to $DEVELOP_BRANCH from $ORIGIN."
		git push "$ORIGIN" "$MASTER_BRANCH" || \
			die "Could not push to $MASTER_BRANCH from $ORIGIN."
		if noflag notag; then
			git push --tags "$ORIGIN" || \
				die "Could not push tags to $ORIGIN."
		fi
	fi

	echo
	echo "Summary of actions:"
	echo "- Latest objects have been fetched from '$ORIGIN'"
	echo "- Hotfix branch has been merged into '$MASTER_BRANCH'"
	if noflag notag; then
		echo "- The hotfix was tagged '$VERSION_PREFIX$VERSION'"
	fi
	echo "- Hotfix branch has been back-merged into '$DEVELOP_BRANCH'"
	if flag keep; then
		echo "- Hotfix branch '$BRANCH' is still available"
	else
		echo "- Hotfix branch '$BRANCH' has been deleted"
	fi
	if flag push; then
		echo "- '$DEVELOP_BRANCH', '$MASTER_BRANCH' and tags have been pushed to '$ORIGIN'"
	fi
	echo
}

cmd_cancel() {
	DEFINE_boolean fetch true "fetch from $ORIGIN before performing cancel" F
	DEFINE_string message "" "use the given tag message" m
	DEFINE_boolean push true "push to $ORIGIN after performing cancel" p
	DEFINE_boolean keep false "keep branch after performing cancel" k
	DEFINE_boolean force false "safety feature; cannot cancel a hotfix without this flag" f
	DEFINE_boolean discard true "drop the changes in this hotfix; do not merge back into develop" d

	parse_args "$@"
	require_version_arg

	# has the user chosen the force flag?
	if noflag force ; then
		warn "To prevent you accidentally cancelling a hotfix, you _must_ use the -f flag"
		warn "with this command"
		exit 1
	fi

	# sanity checks
	require_branch "$BRANCH"
	require_clean_working_tree
	if flag push ; then
		git push "$ORIGIN" "$BRANCH" || die "Could not push hotfix branch up to $ORIGIN"
	fi

	# we only merge into develop if the user hasn't selected the -d flag
	if noflag discard ; then
		if flag fetch ; then
			git fetch -q "$ORIGIN" "$DEVELOP_BRANCH" || \
			  die "Could not fetch $DEVELOP_BRANCH from $ORIGIN."
		fi
		if has "$ORIGIN/$DEVELOP_BRANCH" $(git_remote_branches); then
			require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH"
		fi

		# try to merge into develop
		#
		# in case a previous attempt to finish this release branch has failed,
		# but the merge into develop was successful, we skip it now
		if ! git_is_branch_merged_into "$BRANCH" "$DEVELOP_BRANCH"; then
			git checkout "$DEVELOP_BRANCH" || \
				die "Could not check out $DEVELOP_BRANCH."
			# just merge the release branch into the development branch
			git merge --no-ff "$BRANCH" || \
				die "There were merge conflicts."
		fi

		# push back to remote repo
		if flag push ; then
			git push "$ORIGIN" "$DEVELOP_BRANCH" || \
				die "Could not push to $DEVELOP_BRANCH from $ORIGIN."
		fi
	fi

	# delete the remote branch
	if flag push ; then
		git push "$ORIGIN" :"$BRANCH" || \
			die "Could not delete the remote $BRANCH in $ORIGIN."
	fi

	# delete local branch
	if noflag keep ; then
		if [ "$BRANCH" = "$(git_current_branch)" ]; then
			git checkout "$DEVELOP_BRANCH"
		fi

		git branch -D "$BRANCH"
	fi

	echo
	echo "Summary of actions:"
	if flag push ; then
		echo "- Latest objects have been fetched from '$ORIGIN'"
	fi
	if noflag discard ; then
		echo "- Hotfix branch has been merged into '$DEVELOP_BRANCH'"
	fi
	if noflag discard && flag push ; then
		echo "- '$DEVELOP_BRANCH' has been pushed to '$ORIGIN'"
	fi
	if flag push ; then
		echo "- Hotfix branch '$BRANCH' in '$ORIGIN' has been deleted."
	fi
	if flag keep ; then
		echo "- Hotfix branch '$BRANCH' is still available locally"
	else
		echo "- Hotfix branch '$BRANCH' has been deleted locally"
	fi

	echo
}

cmd_pull() {
	git hf pull "$@"
}

cmd_push() {
	git hf push "$@"
}
