#!/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".

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

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-index --name-only $against`
if test -n "$files"
then
  if python `git rev-parse --show-toplevel`/tools/check-standards.py `git diff-index --name-only $against`
  then
    echo "Format checks OK."
  else
    exit 1
  fi
fi

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
