Skip to content

Instantly share code, notes, and snippets.

@spagu

spagu/pre-commit

Last active Nov 19, 2020
Embed
What would you like to do?
subversion pre-commit hook for PHP lint, PSR-2 and CSS validation + comments
#!/bin/sh
# SVN PRE-COMMIT HOOK
#
# Requirements:
# - PHP installed ( https://secure.php.net/manual/en/install.php )
# - PHPCS installed ( https://github.com/squizlabs/PHP_CodeSniffer + https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards )
# - csslint installed ( https://github.com/CSSLint/csslint/wiki/command-line-interface )
# - i18n0lint installed ( https://github.com/jwarby/i18n-lint )
REPOS="$1"
TXN="$2"
# Make sure that the log message contains some text.
SVNLOOK=/usr/local/bin/svnlook
AWK=/usr/bin/awk
PHP=/usr/local/bin/php
GREP=/usr/bin/grep
PHPCS=/usr/local/sbin/phpcs
CSSLINT=/usr/local/bin/csslint
I18LINT=/usr/local/bin/i18n-lint
MD5=/sbin/md5
SED=/usr/bin/sed
SVNLOOKOK=1
set -e
$SVNLOOK log -t "$TXN" "$REPOS" | \
grep "[a-zA-Z0-9]" > /dev/null || SVNLOOKOK=0
if [ $SVNLOOKOK = 0 ]; then
echo "Empty log messages are not allowed. Please provide a proper log message." >&2
exit 1
fi
LOGMSG=$($SVNLOOK log -t "$TXN" "$REPOS" | grep [a-zA-Z0-9] | wc -c)
if [ "$LOGMSG" -lt 13 ]; then
echo -e "Please provide a meaningful comment when committing changes." 1>&2
exit 1
fi
for i in `$SVNLOOK changed -t "$TXN" "$REPOS" | $GREP -v "D " | $AWK '{print $2}'`
do
if [ ${i##*.} == php ]; then
CHECK=`$SVNLOOK cat -t "$TXN" "$REPOS" $i | $PHP -d html_errors=off -l || echo $i`
RETURN=`echo $CHECK | $GREP "^No syntax" > /dev/null && echo TRUE || echo FALSE`
if [ $RETURN = 'FALSE' ]; then
echo "Found Error(s) in $i:" 1>&2;
echo "$CHECK" 1>&2;
echo "Please fix PHP file(s) and try commit again" 1>&2;
exit 1
fi
CHECKPSR0=`$SVNLOOK cat -t "$TXN" "$REPOS" $i | $PHPCS --encoding=utf-8 -n -q -p || echo $i`
CHECKPSR1=`$SVNLOOK cat -t "$TXN" "$REPOS" $i | $PHPCS --standard=PSR2 --encoding=utf-8 -n -q -p || echo $i`
CHECKPSR2=`$SVNLOOK cat -t "$TXN" "$REPOS" $i | $PHPCS --standard=WordPress --encoding=utf-8 -n -q -p || echo $i`
if [ ! -z "$CHECKPSR1" ] && [ ! -z "$CHECKPSR2" ] && [ ! -z "$CHECKPSR0" ]; then
echo "Found Styles errors PSR-2(Wordpress) in $i:" 1>&2;
echo "$CHECKPSR2" 1>&2;
echo "Please fix PHP file(s) and try commit again" 1>&2;
exit 1
fi
PREFILE=`$SVNLOOK cat -t "$TXN" "$REPOS" $i | sed '/<\?php/,/\?>/d' || $i`
CHECKPSR3=`$I18LINT -i "?php,style,script" -t "<\?php,\?>" | echo $PREFILE`
if [ ! -z "$CHECKPSR3" ]; then
echo "Found HARDCODED texts in $i:" 1>&2;
echo "$CHECKPSR3" 1>&2;
echo "Please use esc_attr_e, esc_html_e to fix it." 1>&2;
exit 1
fi
fi
if [ ${i##*.} == css ]; then
TMPHASH=`echo -n "$i $TXN $REPOS $RANDOM" | $MD5`
TMP="/tmp/$TMPHASH.css";
$SVNLOOK cat -t "$TXN" "$REPOS" $i > $TMP
CHECKCSS=`$CSSLINT $TMP | cut -c 1-80`
RETURNY=`echo $CHECKCSS | $GREP "No errors in" > /dev/null && echo TRUE || echo FALSE`
rm -f $TMP
if [ $RETURNY = 'FALSE' ]; then
MSG1=`echo "$CHECKCSS"| sed -e 's#'$TMP'#'$i'#g'`
MSG2=`echo "$MSG1"| sed -e 's#'$TMPHASH'.css##g'`
MSG=`echo "$MSG2"| sed -e 's#csslint: ##g'`
echo "${MSG}" 1>&2;
echo "" 1>&2;
echo "Please fix CSS in $i and try commit again" 1>&2;
exit 1
fi
fi
done
# All checks passed, so allow the commit.
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment