Last active
December 26, 2015 02:29
-
-
Save ka2n/7079018 to your computer and use it in GitHub Desktop.
pre-commit hook to check source code syntax PHP, Coffee, Sass
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
checkPHP=1 | |
checkCoffee=1 | |
checkSass=1 | |
PHP=`which php` | |
COFFEE=`which coffee` | |
SASS=`which sass` | |
# Check conflicts | |
isConflicts=`git diff --cached --name-only -S'<<<<<< HEAD'` | |
if [ -n "$isConflicts" ]; then | |
echo "Unresolved merge conflicts in this commit:" | |
echo $isConflicts | |
fi | |
# Check PHP syntax | |
if [ $checkPHP -eq 1 ]; then | |
for i in `git diff --cached --name-only | grep '\.php$'`; do | |
if [ -e $i ]; then | |
phperror=`$PHP -l "$i" | grep -v 'No syntax errors detected in'` | |
if [ -n "$phperror" ]; then | |
echo "PHP errors added in file: " $i | |
isPHPError='true' | |
fi | |
fi | |
done | |
fi | |
# Check CoffeeScript syntax | |
if [ $checkCoffee -eq 1 ]; then | |
for i in `git diff --cached --name-only | grep '\.coffee$'`; do | |
if [ -e $i ]; then | |
coffeeerror=`$COFFEE "$i"` | |
if [ $? -ne 0 ]; then | |
isCoffeeError='true' | |
fi | |
fi | |
done | |
fi | |
# Check SASS syntax | |
if [ $checkSass -eq 1 ]; then | |
for i in `git diff --cached --name-only | grep '\.sass$'`; do | |
if [ -e $i ]; then | |
sasserror=`$SASS -c "$i"` | |
if [ $? -ne 0 ]; then | |
isSassError='true' | |
fi | |
fi | |
done | |
fi | |
isError="$isConflicts$isPHPError$isCoffeeError$isSassError" | |
########### | |
if [[ -n $isError ]]; then | |
echo | |
echo "Commit with '-n' to bypass checks" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment