Skip to content

Instantly share code, notes, and snippets.

@cwhsu1984
Last active August 29, 2015 14:07
Show Gist options
  • Save cwhsu1984/a31ab8077628fd29f0c1 to your computer and use it in GitHub Desktop.
Save cwhsu1984/a31ab8077628fd29f0c1 to your computer and use it in GitHub Desktop.
githook
#!/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
# If you want to allow non-ASCII filenames set this variable to true.
allownonascii=$(git config --bool hooks.allownonascii)
# Redirect output to stderr.
exec 1>&2
# Cross platform projects tend to avoid non-ASCII filenames; prevent
# them from being added to the repository. We exploit the fact that the
# printable range starts at the space character and ends with tilde.
if [ "$allownonascii" != "true" ] &&
# Note that the use of brackets around a tr range is ok here, (it's
# even required, for portability to Solaris 10's /usr/bin/tr), since
# the square bracket bytes happen to fall in the designated range.
test $(git diff --cached --name-only --diff-filter=A -z $against |
LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
then
cat <<\EOF
Error: Attempt to add a non-ASCII file name.
This can cause problems if you want to work with people on other platforms.
To be portable it is advisable to rename the file.
If you know what you are doing you can disable this check using:
git config hooks.allownonascii true
EOF
exit 1
fi
# Check php syntax error if there exists php file in the commit.
if [ -z "$PHP_BIN" ]
then
PHP_BIN=php
fi
if [ "$(echo -e thenest)" = test ]
then
echo_e="echo -e"
else
echo_e="echo"
fi
errors=""
if ! which "$PHP_BIN" >/dev/null 2>&1
then
echo "PHP Syntax check failed:"
echo "PHP binary does not exist or is not in path: $PHP_BIN"
exit 1
fi
# dash does not support $'\n':
# http://forum.soft32.com/linux2/Bug-409179-DASH-Settings-IFS-work-properly-ftopict70039.html
IFS='
'
# get a list of staged files
for line in $(git diff-index --cached --full-index $against)
do
# split needed values
line=$(echo $line | sed 's/\s\s*/ /g')
sha=$(echo $line | cut -d' ' -f4)
status=$(echo $line | cut -d' ' -f5)
filename=$(echo $line | cut -d' ' -f6)
# file extension
ext=$(echo $filename | sed 's/^.*\.//')
# only check files with php extension
if [ "$ext" != "php" ]
then
continue
fi
# do not check deleted files
if [ "$status" = "D" ]
then
continue
fi
# check the staged file content for syntax errors
# using php -l (lint)
result=$(git cat-file -p $sha | "$PHP_BIN" -n -l -ddisplay_errors\=1 -derror_reporting\=E_ALL -dlog_errrors\=0 2>&1)
if [ $? -ne 0 ]
then
# Swap back in correct filenames
errors=$(echo "$errors"; echo "$result" | grep ':' | sed -e "s@in - on@in $filename on@g")
fi
done
unset IFS
if [ -n "$errors" ]
then
echo "PHP Syntax check failed: "
$echo_e "$errors"
exit 1
fi
# If there are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached $against --
#!/bin/bash
read oldsha newsha refname
# Make sure we handle the situation when the branch does not exist yet
if ! [ "$oldsha" = "0000000000000000000000000000000000000000" ] ; then
excludes=( ^$oldsha )
else
excludes=( $(git for-each-ref --format '^%(refname:short)' refs/heads/) )
fi
# Get the list of incomming commits
commits=`git rev-list $newsha "${excludes[@]}"`
# For every commit in the list
for commit in $commits
do
# check the log message for issue id
message=`git log --format=%s -1 $commit`
issue=`echo "$message" | grep '\[#[0-9]\+\] [a-zA-Z0-9\.\,\(\)\_\-]\?'`
merge=`echo "$message" | grep 'Merge .*'`
# skip merge branch and tag
if [ "" != "$merge" ]; then
continue
fi
if [ "" = "$issue" ]; then
echo "[POLICY] Commit $commit does not start with an issue id"
exit 1
fi
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment