#!/usr/bin/env bash # # 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". die() { echo '-------------------------' 1>&2 echo 'pre-commit check failure' 1>&2 echo '-------------------------' 1>&2 echo "$@" 1>&2 if [ -f /tmp/capp_lint.tmp ]; then rm -f /tmp/capp_lint.tmp fi exit 1 } #----------------------------------------------------------------------------- # Check for committer identity. advice=' Use the commands: git config --global user.name '\''Your Name'\'' git config --global user.email '\''you@yourdomain.com'\'' to introduce yourself to Git before committing.' # Ensure name and email are available. git config --get user.name > /dev/null && git config --get user.email > /dev/null || die 'Identity not configured!' "$advice" # Validate the name and email. git config --get user.name | grep ' ' > /dev/null || die 'Please set user.name to your Real Name (with a space), not a userid.' "$advice" git config --get user.email | grep '^[^@]*@[^@]*$' > /dev/null || die 'Please set user.email to an email address (userid@validdomain.com).' "$advice" #----------------------------------------------------------------------------- # Check content that will be added by this commit. if git rev-parse --verify -q HEAD > /dev/null; then against=HEAD else # Initial commit: diff against an empty tree object against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 fi # Disallow non-ascii file names. The printable range starts at the # space character and ends with tilde. if test "$(git diff --cached --name-only --diff-filter=A -z $against | LC_ALL=C tr -d '[ -~]\0')"; then die 'Non-ascii file names may not be added: '"$(git diff --cached --name-only --diff-filter=A $against)" fi # Check for trailing whitespaces, indents that uses a space before a tab and leftover conflict markers if test "$(git diff-index --check --cached $against --)"; then die 'The following whitespace errors or unresolved conflicts have been found: '"$(git diff-index --check --cached $against --)" fi #----------------------------------------------------------------------------- # Check for inadvertent globals and dangling variable declarations. fileCount=`git diff --cached --name-only --diff-filter=AM $against | wc -l` if [[ fileCount -ne "0" ]]; then lintPath='' if [[ `which capp_lint.py` ]]; then lintPath="`which capp_lint.py`" fi if [[ -z "$lintPath" && `which capp_lint` ]]; then lintPath="`which capp_lint`" fi if [[ -z "$lintPath" ]]; then path=`dirname $0`/capp_lint.py if [[ -x "$path" ]]; then lintPath="$path" fi fi if [[ -z "$lintPath" ]]; then die 'Could not find capp_lint or capp_lint.py in your PATH or in .git/hooks.' fi git diff --cached --name-only --diff-filter=AM $against | "$lintPath" - > /tmp/capp_lint.tmp if [ -s /tmp/capp_lint.tmp ]; then die 'The following lint errors were found: '"$(cat /tmp/capp_lint.tmp)" else rm -f /tmp/capp_lint.tmp fi fi exit 0