#!/bin/bash

# Support routines for git commands

commit_range_from_arg() {
	local arg="$1"

	if [ -z "${arg}" ]; then
		# If we didn't get a commit range argument and this is a
		# tracking branch, pick all the commits since this branch
		# deviated.
		base="$(git branch-remote 2>/dev/null)"
		if [ -n "${base}" ]; then
			echo "${base}..HEAD"
			return 0
		fi

		return 1
	fi

	# If the argument already has a range in it, we're done.
	if echo "${arg}" | grep -q '\.\.'; then
		echo "${arg}"
		return 0
	fi


	# If the argument is a tracking branch, pick all the commits since that
	# branch deviated.
	base="$(git branch-remote "${arg}" 2>/dev/null)"
	if [ -n "${base}" ]; then
		echo "${base}..${arg}"
		return 0
	fi

	# If we didn't get something that looks like a range, only select one
	# commit because git rev-list run against a non-range lists everything
	# from whatever it finds all the way back to the beginning.
	echo "${arg}^1..${arg}"
	return 0
}
