#! /bin/bash

USAGE="[--snapshot] [--shortlog] [--skip=<n>] [<git-log options>] [until [since]]"

no_snapshot=yes
skip=0
shortlog=
opts=

while :
do
	case "$1" in
	--shortlog)
		shortlog=yes ;;
	--snapshot)
		no_snapshot= ;;
	--skip=*)
		skip=${1#*=} ;;
	-h|--help)
		usage ;;
	-*)
		opts+=" $1";;
	*)
		break
	esac
	shift
done

case $# in
0|1|2)
	until=$1
	since=$2
	;;
*)
	usage
esac

#
# log command assumes to be in a kernel repository (being tracked by
# kdist or not).
#
kdist__cd_kernel_topdir || exit

#
# If 'since' is not specified, use the closest previous tag.
#
if ! test $since; then
	tmp=${until:-HEAD}
	while :; do
		since=$(git__describe --abbrev=0 $tmp~1) || exit
		tmp=$since
		#
		# By default we don't consider the snapshot tags
		#
		if test $no_snapshot &&
		   expr $since : "-[1-9][0-9]\+\.git[1-9][0.9]*" >/dev/null
		then
			continue
		fi
		if test $skip -gt 0
		then
			let skip-=1
			continue
		fi
		break
	done
fi

#
# keep it simple for now
#
echo >&2 "Logging $since..$until"

if test $shortlog
then
	git log $opts --first-parent $since..$until | git shortlog
else
	git log $opts --first-parent $since..$until
fi
