#!/bin/sh

zero=$(git hash-object --stdin </dev/null | tr '0-9a-f' '0')
initial_commit="ed3069f01d4ee023d296212a3521d3354f109194"

while read local_ref local_oid remote_ref remote_oid
do
	if test "$local_oid" = "$zero"
	then
		# Handle delete
		:
	else
		if test "$remote_oid" = "$zero"
		then
			# New branch, examine all commits
			range="$local_oid"
		else
			# Update to existing branch, examine new commits
			range="$remote_oid..$local_oid"
		fi

		commit=$(git rev-list --reverse --extended-regexp --invert-grep --grep '^Signed-off-by: ' --grep '\(#[0-9]+\)$' "$range" | head -n 1)
		# find commits which                                ^ are not            ^ signed off              ^ or merge commits

		if [ -n "$commit" ] && [ "$commit" != "$initial_commit" ]
		then
			rebase_commit=$(git rev-parse --short "$commit"^)
			echo >&2 "Found commit(s) which are not signed-off."
			echo >&2 "You can sign-off the commits using: git rebase --signoff --autostash $rebase_commit"
			echo >&2 "or rerun the push with --no-verify"
			exit 1
		fi
	fi
done

exit 0
