Skip to content

Instantly share code, notes, and snippets.

@easterncoder
Last active December 28, 2021 17:11
Show Gist options
  • Save easterncoder/2a0161e2a1146c94aff12661770b9c80 to your computer and use it in GitHub Desktop.
Save easterncoder/2a0161e2a1146c94aff12661770b9c80 to your computer and use it in GitHub Desktop.
PHPCS SVN Pre-commit
#!/bin/bash
# executables
SVNLOOK="/usr/bin/svnlook"
PHPCS="/path/to/composer/vendor/bin/phpcs"
# parameters
REPOS="$1"
TXN="$2"
# end. no phpcs.xml
$SVNLOOK tree -t "$TXN" "$REPOS" | grep 'phpcs.xml' || exit 0
# generate tmp file for phpcs.xml
phpcstmpfile=$(mktemp)".xml"
# default fail values
PHPCSFAIL=0
for f in $($SVNLOOK changed -t "$TXN" "$REPOS" | tr -s ' ' | cut -d' ' -f2-); do
# skip if file is not php or html
[[ "$f" =~ \.(php|html)$ ]] || continue
# skip if file does not exist
$SVNLOOK cat -t "$TXN" "$REPOS" "$f" 2>&1 || continue
# find phpcs.xml
phpcsxml="/$f"
while [[ $phpcsxml != "" ]]; do
phpcsxml=${phpcsxml%/*}
$SVNLOOK tree -t "$TXN" "$REPOS" "$phpcsxml/phpcs.xml" 2>&1 && break
done
[[ "$phpcsxml" == "" ]] && phpcsxml="phpcs.xml" || phpcsxml="$phpcsxml/phpcs.xml"
# skip if phpcs.xml is not found
$SVNLOOK cat -t "$TXN" "$REPOS" $phpcsxml > $phpcstmpfile 2>&1 || continue
$SVNLOOK cat -t "$TXN" "$REPOS" "$f" | \
$PHPCS -n --standard="$phpcstmpfile" -q --stdin-path="$f" >&2
[[ $? -gt 0 ]] && PHPCSFAIL=1
done
# delete phpcs.xml tmp file
[[ -e $phpcstmpfile ]] && rm $phpcstmpfile
# phpcs failed
[[ $PHPCSFAIL -gt 0 ]] && exit 1
# all checks passed
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment