Skip to content

Instantly share code, notes, and snippets.

@StuMason
Last active May 29, 2019 11:09
Show Gist options
  • Save StuMason/9fdad4d2ce3af238b43105a36f5168c2 to your computer and use it in GitHub Desktop.
Save StuMason/9fdad4d2ce3af238b43105a36f5168c2 to your computer and use it in GitHub Desktop.
dd(), console.log and php-cs-fixer .git pre-commit
#!/bin/bash
# This file is stored here: ~/.githooks/pre-commit
# Makesure the file is executable: chmod +x ~/.githooks/pre-commit
# Needs: git config --global core.hooksPath ~/.githooks
# Needs phpcbf installed (this command installs globally): composer global require squizlabs/php_codesniffer
# Redirect output to stderr and allow user input
exec 1>&2
exec < /dev/tty
# Run a style fix
phpfiles=$(git diff --cached --name-only --diff-filter=ACM | grep '\.php$')
consoleregexp='^\+.*console\.log('
# CHECK
if test $(git diff --cached | grep $consoleregexp | wc -l) != 0
then
exec git diff --cached | grep -ne $consoleregexp
read -p "There are some occurrences of console.log at your modification. Are you sure want to continue? (y/n)" yn
echo $yn | grep ^[Yy]$
if [ $? -eq 0 ]
then
#exit 0; #THE USER WANTS TO CONTINUE
echo "done console.log check"
else
exit 1; # THE USER DONT WANT TO CONTINUE SO ROLLBACK
fi
fi
diedumpregexp='^\+.*dd('
# CHECK
if test $(git diff --cached | grep $diedumpregexp | wc -l) != 0
then
exec git diff --cached | grep -ne $diedumpregexp
read -p "There are some occurrences of laravels die & dump (dd) at your modification. Are you sure want to continue? (y/n)" yn
echo $yn | grep ^[Yy]$
if [ $? -eq 0 ]
then
#exit 0; #THE USER WANTS TO CONTINUE
echo "done dd() check"
else
exit 1; # THE USER DONT WANT TO CONTINUE SO ROLLBACK
fi
fi
[ -z "$phpfiles" ] && exit 0
for file in $phpfiles; do
phpcbf -p --standard=PSR2 "$file" --extensions=php
git add "$file"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment