#
# git-flow -- A collection of Git extensions to provide high-level
# repository operations for Vincent Driessen's branching model.
#
# Original blog post presenting this model is found at:
#    http://nvie.com/archives/323
#
# Feel free to contribute to this project at:
#    http://github.com/nvie/gitflow
#
# Copyright (c) 2010 by Vincent Driessen
# Copyright (c) 2010 by Benedikt Böhm
#

VERSION_PREFIX=$(git config --get gitflow.prefix.versiontag)
PREFIX=$(git config --get gitflow.prefix.release || echo release/)

usage() {
	echo "usage: git flow release [list] [-v]"
	echo "       git flow release start <version>"
	echo "       git flow release finish <version>"
	# TODO
	#echo ""
	#echo "options:"
	#echo "--option    Explanation"
	#echo ""
	#echo "start-only options:"
	#echo "--bump <script>"
	#echo "            Run the given script to auto-update the version number"
	#echo ""
	#echo "finish-only options:"
	#echo "--push      Push to the origin repo when finished"
}

cmd_default() {
	cmd_list "$@"
}

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

	typeset release_branches
	typeset current_branch
	typeset short_names
	release_branches="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX")"
	if [ -z "$release_branches" ]; then
		warn "No release branches exist."
		exit 0
	fi

	current_branch=$(git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
	short_names=$(echo "$release_branches" | sed "s?^$PREFIX??g")

	# determine column width first
	typeset -i width=0
	typeset branch
	for branch in $short_names; do
		typeset -i len=$(($(echo "$branch" | wc -c) - 1))
		width=$(max $width $len)
	done
	width=width+3

	typeset branch
	for branch in $short_names; do
		typeset fullname="$PREFIX$branch"
		typeset base=$(git merge-base "$fullname" "$DEVELOP_BRANCH")
		typeset develop_sha=$(git rev-parse "$DEVELOP_BRANCH")
		typeset 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" = "$develop_sha" ]; then
				printf "(no commits yet)"
			else
				typeset nicename="$(git rev-parse --short $base)"
				printf "(based on $nicename)"
			fi
		else
			printf "%s" "$branch"
		fi
		echo
	done
}

cmd_help() {
	usage
	exit 0
}

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

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

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

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

cmd_start() {
	DEFINE_boolean fetch true "fetch from $ORIGIN before performing finish" F
	parse_args "$@"
	BASE="${2:-$DEVELOP_BRANCH}"
	require_version_arg
	require_base_is_on_develop

	# sanity checks
	gitflow_require_clean_working_tree
	gitflow_require_branch_absent $BRANCH
	gitflow_require_tag_absent $VERSION_PREFIX$VERSION
	if flag fetch; then
		git fetch -q $ORIGIN $DEVELOP_BRANCH
	fi
	gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH

	# create branch
	git checkout -b $BRANCH $BASE

	echo
	echo "Summary of actions:"
	echo "- A new branch '$BRANCH' was created, based on '$BASE'"
	echo "- You are now on branch '$BRANCH'"
	echo
	echo "Follow-up actions:"
	echo "- Bump the version number now!"
	echo "- Start committing last-minute fixes in preparing your release"
	echo "- When done, run:"
	echo
	echo "     git flow release 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
	parse_args "$@"
	require_version_arg

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

	# sanity checks
	gitflow_require_branch $BRANCH
	gitflow_require_clean_working_tree
	if flag fetch; then
		git fetch -q $ORIGIN $MASTER_BRANCH || \
		  die "Could not fetch $MASTER_BRANCH from $ORIGIN."
		git fetch -q $ORIGIN $DEVELOP_BRANCH || \
		  die "Could not fetch $DEVELOP_BRANCH from $ORIGIN."
	fi
	gitflow_require_branches_equal $MASTER_BRANCH $ORIGIN/$MASTER_BRANCH
	gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH

	# 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 ! gitflow_is_branch_merged_into $BRANCH $MASTER_BRANCH; then
		git checkout $MASTER_BRANCH || \
		  die "Could not check out $MASTER_BRANCH."
		git merge --no-ff $BRANCH || \
		  die "There were merge conflicts."
		  # TODO: What do we do now?
	fi

	# 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
	typeset tagname=$VERSION_PREFIX$VERSION
	if ! gitflow_tag_exists $tagname; then
		typeset opts="-a"
		flag sign && opts="$opts -s"
		[ "$FLAGS_signingkey" != "" ] && opts="$opts -u '$FLAGS_signingkey'"
		[ "$FLAGS_message" != "" ] && opts="$opts -m '$FLAGS_message'"
		git tag $opts "$tagname" || \
		  die "Tagging failed. Please run finish again to retry."
	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 ! gitflow_is_branch_merged_into $BRANCH $DEVELOP_BRANCH; then
		git checkout $DEVELOP_BRANCH || \
		  die "Could not check out $DEVELOP_BRANCH."

		# TODO: Actually, accounting for 'git describe' pays, so we should
		# ideally git merge --no-ff $tagname here, instead!
		git merge --no-ff $BRANCH || \
		  die "There were merge conflicts."
		  # TODO: What do we do now?
	fi

	# delete branch
	git branch -d $BRANCH

	# TODO: Implement an optional push to master
	# git push origin develop; git push origin master; git push --tags origin

	echo
	echo "Summary of actions:"
	echo "- Latest objects have been fetched from '$ORIGIN'"
	echo "- Release branch has been merged into '$MASTER_BRANCH'"
	echo "- The release was tagged '$tagname'"
	echo "- Release branch has been back-merged into '$DEVELOP_BRANCH'"
	echo "- Release branch '$BRANCH' has been deleted"
	echo
}
