#!/bin/sh
#
# Commit hook designed to keep tab characters out of ASDF.
# Written by Daniel Herring, released into the public domain.
# With improvements by Stelian Ionescu

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

tab=$(printf '\t')
tmpfile=$(mktemp)

for f in $(git diff --cached --name-only --diff-filter=AM ${against})
do
  case "${f}" in
  *.asd | *.lisp | *.script )
    IFS=""
    diff=$(git diff --cached -p ${against} "${f}" 2> /dev/null)
    if (egrep -B 3 -C 3 "^\+.{0,}(${tab}+|[[:space:]]+\$)" <<<"${diff}") \
        > "${tmpfile}" 2> /dev/null
    then
      echo "Error: This git repository disallows the #\Tab character in lisp files."
      echo "Only spaces should be used for indenting lisp code."
      echo "In emacs, 'M-x untabify' and (setq indent-tabs-mode nil) may help."
      echo
      echo "First offending file and lines:"
      echo "${f}"
      cat "${tmpfile}"
      rm -f "${tmpfile}"
      exit 1
    fi
    ;;
  esac
done
rm -f "${tmpfile}"

# nothing else to complain about
exit 0
