#!/bin/sh
#
# A hook script to verify what is about to be committed.
# Called by "git commit" with no arguments.  The hook exits with
# non-zero status if the files being committed do not conform to
# the Mesos style.
#
# To enable this hook, do this from the root of the repo:
#
# $ ln -s ../../support/hooks/pre-commit .git/hooks/pre-commit

if git rev-parse --verify HEAD >/dev/null 2>&1
then
	against=HEAD
else
	# Initial commit: diff against an empty tree object
	against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

# Redirect output to stderr.
exec 1>&2

# If there are whitespace errors, print the offending file names and fail.
git diff-index --check --cached $against -- || exit 1

# Check Mesos style.
ADDED_OR_MODIFIED=$(git diff --cached --name-only --diff-filter=AM)
if [ -n "$ADDED_OR_MODIFIED" ]; then
    # NOTE: We choose to implement this as a conditional rather than a call to
    # `xargs` on purpose. Some implementations of `xargs` will call your script
    # even if the arguments you pass in are empty. In our case, this would
    # cause us to erroneously lint every file in the repository. Additionally,
    # many implementations do not support the `-r` flag, (which instructs
    # `xargs` to not run the script if the arguments are empty), so we also
    # cannot use that.
    ./support/mesos-style.py $ADDED_OR_MODIFIED || exit 1
fi

# Check that the commits are properly split between mesos, libprocess and stout.
git diff --cached --name-only --diff-filter=AMD | xargs ./support/mesos_split.py || exit 1
