#!/bin/bash
###############################################################################
# File:          tbd-git-pre-commit-hook.sh
#
# Description:   Git pre-commit that checks your code
#
# Instructions:  Place this file in your project's .git/hooks renamed to "pre-commit"
#                run: "chmod +x .git/hooks/pre-commit"
# 
# -- For all projects
# mkdir -p ~/.git-templates/hooks
# git config --global init.templatedir '~/.git-templates'
# cp tbd-git-pre-commit-hook.sh ~/.git-templates/hooks/pre-commit
# chmod +x ~/.git-templates/hooks/pre-commit
# cd <YOUR_PROJECT_DIR>
# rm .git/hooks/pre-commit && git init
#
#
# -- For a specific project
# cd <YOUR_PROJECT_DIR>
# cp tbd-git-pre-commit-hook.sh .git/hooks/pre-commit
# chmod +x .git/hooks/pre-commit
#
#
# Author: Vincent Pietri
###############################################################################

# Colors
end="\033[0m"
red="\033[0;31m"
yellowb="\033[1;33m"
green="\033[0;32m"

function red {
  echo -e "${red}$@${end}"
}

function green {
  echo -e "${green}$@${end}"
}

function yellowb {
  echo -e "${yellowb}$@${end}"
}


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

ERROR_COUNT=0
WARNING_COUNT=0

# Show files about to be commited that do not match filters
# FILES=$(git diff-index --name-only --cached --diff-filter=ACMRTX ${against} -- | grep -v -f .jshintignore | grep \.js$ -i`)

# Show all php and phtml files about to be commited
FILES=$(git diff-index --name-only --cached --diff-filter=ACMRTX ${against} --  | egrep "\.php|\.phtml")

echo "== pre-commit hook =="
echo "-- Check errors"
for FILE in $FILES
do
    # Php syntax check only (lint)
    ERRORS=$(php -l -ddisplay_errors\=1 -derror_reporting\=E_ALL -dlog_errrors\=0 "$FILE" 2>&1 | grep "^PHP Parse error")
    
    if [ "$ERRORS" != "" ]; then 
        red $ERRORS
        ERROR_COUNT=`expr ${ERROR_COUNT} + 1`
    fi
done


if [[ ${ERROR_COUNT} -ne 0 ]]; then
    echo ""
    red " -- COMMIT ABORTED :-("
    exit $((${ERROR_COUNT}))
fi

echo "-- Check warnings"
for FILE in $FILES
do
    # Check log functions    
    for TBDCALL in $(grep -En '\\TBD\\Core\\Helper\\Debug::|Mage::log|var_dump'  $FILE |  grep -v "^[0-9]:\s*\/" | sed 's/ //g')
    do
        yellowb $FILE : $TBDCALL
        WARNING_COUNT=`expr ${WARNING_COUNT} + 1`
    done    
    
done


if [[ ${WARNING_COUNT} -ne 0 ]]; then
    # Allows us to read user input below, assigns stdin to keyboard
    exec < /dev/tty

    while true; do
      read -p "Ignore warning and continue? (y/N) " yn
      if [ "$yn" = "" ]; then
        yn='N'
      fi
      case $yn in
          [Yy] ) break;;
          [Nn] ) red " -- COMMIT HALTED"; exit $((${WARNING_COUNT}));;
          * ) echo "Please answer y or n for yes or no.";;
      esac
    done
fi 

echo "== commit PASSED =="
echo "== You can push your commit with the command:"
green "git push origin "$(git rev-parse --abbrev-ref HEAD)

