#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments.  The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".

echo "begin pre-commit"
branch=`git rev-parse --abbrev-ref HEAD`

if test "$branch" == "master"
then
    echo "$(tput setaf 1)ERROR: Committing directly to master is not allowed. Commit to develop or a feature branch instead.$(tput sgr0)"
    exit 1
fi

if test "${branch#*feature/}" != "$branch"
then
   if test \! -e README.md
   then
       echo "$(tput setaf 1)ERROR: Feature branches must have a README.md in the root IMP directory describing them. Make sure you remove the README.md commit before merging into develop."
       echo "ERROR: current branch is" $branch "$(tput sgr0)"
       exit 1
   fi
fi

# Redirect output to stderr.
exec 1>&2
files=`git diff --cached --name-only --diff-filter=ACM`
if test -n "$files"
then
  echo "checking standards on" $files
  if python `git rev-parse --show-toplevel`/tools/check-standards.py $files
  then
    echo "Standards checks OK."
  else
    exit 1
  fi
fi

echo "checking for untracked files"
untracked=`git status -u --porcelain | grep '^??'`
if test -n "$untracked"
then
  echo "$(tput setaf 1)ERROR: There are untracked files in your repository.$(tput sgr0)"
  echo "If you want to keep them around you can add them to your .git/info/exclude file: " `echo $untracked | cut -f2 -d\ `
  echo "You can use --no-verify to skip checks if you are sure that is what you want."
  exit 1
fi

echo "end pre-commit"