Skip to content

Instantly share code, notes, and snippets.

@svx
Forked from mindbat/pre-commit.lint
Created April 5, 2019 10:14
Show Gist options
  • Save svx/139068201a6e51bb9a01b16fdc3773e6 to your computer and use it in GitHub Desktop.
Save svx/139068201a6e51bb9a01b16fdc3773e6 to your computer and use it in GitHub Desktop.
Git pre-commit script to run all the modified files through php-lint. Won't let you commit php file with a syntax error.
#!/bin/sh
#
# Hook script to check the changed files with php lint
# Called by "git commit" with no arguments.
#
# 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 !(git diff --cached --name-only --diff-filter=AM $against | grep 'php' | xargs -P 10 -n1 php -l)
then
echo
echo "Error: You attempted to commit one or more php files with syntax errors."
echo
echo "Please fix them and retry the commit."
echo
exit 1
fi
exec git diff-index --check --cached $against --
@svx
Copy link
Author

svx commented Apr 5, 2019

maybe use against=$(git rev-list --max-parents=0 HEAD) for initial commit instead of 4b825dc642cb6eb9a060e54bf8d69288fbee4904

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment