#!/bin/bash

source /usr/lib/djwong-git/djwong-git

# Run the checkpatch script on every commit in the list.

if [ "$#" -gt 1 ] || [ "$1" = "--help" ]; then
	echo "Usage: $0 <commit range>" 1>&2
	exit 1
fi
commit_range="$(commit_range_from_arg "$1")" || exit 1

readarray -t commits < <(git rev-list --reverse --no-merges "${commit_range}")
if [ "${#commits[@]}" -eq 0 ]; then
	echo 'No commits?'
	exit 1
fi

debug=0
test -n "${DBG}" && debug=1
bad=0
for commit_id in "${commits[@]}"; do
	range="${commit_id}^1..${commit_id}"
	oneline="$(git log --oneline "${range}")"

	# Dump commit in full format so we get committer
	git log --format=full "${range}" | \
		awk -f "/usr/lib/djwong-git/checkpatch.awk" \
				-v "commit_id=${commit_id}" \
				-v "debug=${debug}" \
				-v "oneline=${oneline}"
	test $? -eq 0 || bad=1
done

test "${bad}" -eq 0
