Skip to content

Instantly share code, notes, and snippets.

@AnnaDamm
Last active December 21, 2020 13:35
Show Gist options
  • Save AnnaDamm/2b24fc9d6d58a12730abe373914dbeb7 to your computer and use it in GitHub Desktop.
Save AnnaDamm/2b24fc9d6d58a12730abe373914dbeb7 to your computer and use it in GitHub Desktop.
pre commit hook to automatically fix code styles and add the changes to the commit
#!/usr/bin/env bash
# Change the directory in which the code style fixes should be executed by changing the WWW_DIR variable
#
# DO NOT USE THIS WITH PARTIAL COMMITS
# If you want to do partial commits, disable hooks by using the "--no-verify" option when commiting
#
# Attention: Not tested with cygwin and linux
set -e
ROOT_DIR="$(git rev-parse --show-toplevel)"
WWW_DIR="$ROOT_DIR/www"
case "$(uname -s)" in
Linux*) machine=Linux;;
Darwin*) machine=Mac;;
CYGWIN*) machine=Cygwin;;
MINGW*) machine=MinGw;;
*) machine="UNKNOWN:${unameOut}"
esac
if [ -d "$WWW_DIR" ]; then
cd $WWW_DIR
if [ -f "phpcs.xml" ]; then
FILES=$(git diff --cached --name-only --diff-filter=ACMRT | grep 'www' | sed "s/www\///" | grep "\.php" || true)
if [[ "$FILES" != "" ]]; then
# fixing errors
if [ "$machine" == "Mac" ]; then
(echo $FILES | xargs -P 4 vendor/bin/phpcbf -q > /dev/null) || true
echo $FILES | xargs -P 4 vendor/bin/phpcs
else
(echo $FILES | xargs --max-procs=4 vendor/bin/phpcbf -q > /dev/null) || true
echo $FILES | xargs --max-procs=4 vendor/bin/phpcs
fi
echo $FILES | xargs git add
fi
fi
fi;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment