#!/bin/sh
# .git/hooks/pre-commit
# Called by "git commit" with no arguments, on failure it exits with a non-zero
# status code.
# Author: Lekensteyn <lekensteyn@gmail.com>

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

fail=false

# Prevent whitespace in file names from breaking the loop
IFS='
'
for file in $(git diff --cached --name-only --diff-filter=ACM $against); do
    # Get the MIME type of a file
    case "$(file -bi "$file")" in
      'text/x-shellscript;'*)
        bash -n "$file" 2>&1 || fail=true
        ;;
    esac
done
if $fail; then
    echo "Error: Syntax errors have been detected in some files."
	exit 1
fi

exec git diff-index --check --cached $against --
