Skip to content

Instantly share code, notes, and snippets.

@Cheffheid
Forked from wesbos/commit-msg
Last active March 27, 2017 20:08
Show Gist options
  • Save Cheffheid/5d06d7872a4d8ac6c1354af9d8f6305a to your computer and use it in GitHub Desktop.
Save Cheffheid/5d06d7872a4d8ac6c1354af9d8f6305a to your computer and use it in GitHub Desktop.
ESLint / PHPCS Git Pre Commit Hook
# Ruthlessly stolen from Wes Bos' ESLint pre commit hook, and expanded to also run PHPCS.
#!/bin/bash
jsfiles=$(git diff --cached --name-only | grep '\.jsx\?$')
# Only do something if we have JS files.
if [[ $jsfiles != "" ]] ; then
jsfailed=0
for jsfile in ${jsfiles}; do
git show :$jsfile | eslint $jsfile
if [[ $? != 0 ]] ; then
jsfailed=1
fi
done;
if [[ $jsfailed != 0 ]] ; then
echo "ESLint failed, git commit denied!"
exit $jsfailed
fi
fi
phpfiles=$(git diff --cached --name-only | grep '\.php\?$')
# Only do something if we have PHP files.
if [[ $phpfiles != "" ]] ; then
phpfailed=0
for phpfile in ${phpfiles}; do
git show :$phpfile | phpcs --standard=WordPress-Extra $phpfile
if [[ $? != 0 ]] ; then
phpfailed=1
fi
done;
if [[ $phpfailed != 0 ]] ; then
echo "PHPCS failed, git commit denied!"
exit $phpfailed
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment