Skip to content

Instantly share code, notes, and snippets.

@unRob
Created August 22, 2012 02:57
Show Gist options
  • Save unRob/3421843 to your computer and use it in GitHub Desktop.
Save unRob/3421843 to your computer and use it in GitHub Desktop.
Pre-commit hook para hacer lint de php/js
#!/bin/bash
# esto es un WIP!
PHP_BIN='/usr/local/bin/php'
JSL_BIN='/usr/local/bin/jsl'
echo -e "Revisando sintáxis de PHP..."
if [ "$(echo -e test)" = test ]
then
echo_e="echo -e"
else
echo_e="echo"
fi
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
error=0
errors=""
IFS='
'
for line in $(git diff-index --cached --full-index --diff-filter=ACM $against | grep \.php$)
do
sha=$(echo $line | cut -d' ' -f4)
temp=$(echo $line | cut -d' ' -f5)
filename=$(echo $temp | cut -d' ' -f2)
fn=$(basename "$filename")
ext="${fn##*.}"
echo -n "$filename... "
case "$ext" in
"js") bin=$JSL_BIN;;
"php") bin="$PHP_BIN -l";;
esac
result=$(git cat-file -p $sha | "$bin" 2>&1)
#echo "$result $filename";
if [ $? -ne 0 ]
then
error=1
stuff=$(echo -e "$result" | grep "^Parse error:" | awk '{split($0,a," in - on line "); print a[2], "-", a[1]}')
echo "FAIL @$stuff"
# Swap back in correct filenames
#errors=$(echo "$errors"; echo "$result" |sed -e "s@in - on@in $filename on@g")
else
echo "OK!"
fi
done
if [ $error -eq 1 ]
then
echo "Hay errores, no puedo commitear si no se arreglan!"
exit 1
else
echo "OK!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment