Skip to content

Instantly share code, notes, and snippets.

@juliendufresne
Created December 24, 2016 22:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliendufresne/f784d20425e863c36ebb447df3707de7 to your computer and use it in GitHub Desktop.
Save juliendufresne/f784d20425e863c36ebb447df3707de7 to your computer and use it in GitHub Desktop.
pre-commit php-cs-fixer hook using vagrant
#!/usr/bin/env bash
# Note: the file must be named pre-commit, be executable and be located in .git/hooks/
echo "php-cs-fixer pre commit hook start"
# This pre-commit check if the files that are going to be committed suits our coding style
# The commit will fail when this happens.
#
# We could try to automatically fix files include in the commit
# BUT there are some special cases that makes it impossible to be totally safe/
# Imagine the following situation:
# $ echo '<?php echo "foo";' > t.php
# $ git add t.php
# $ echo 'echo "this is some broken code that I do not want to commit";' >> t.php
#
# In this situation, the fixer will fix the file to put the echo on a new line,
# if we add the file to the commit, it will include the line we don't expect to add.
#
# This means, we can not take the decision for the developer.
VAGRANT_PROJECT_DIR="<set your project directory here>"
function code_style_check_one {
local file=$1
# running git inside vagrant is very slow. This is a workaround
tmp=$(mktemp -p $PWD)
tmpfilename=$(basename $tmp)
# Get the ready-to-be-committed file version. User may have modified the file after adding it to the list of file to be committed
git --no-pager show :$file > ${tmp}
# we use vagrant because we may not have php in the host
vagrant ssh -- -n "cd $VAGRANT_PROJECT_DIR; cat $tmpfilename | php ./vendor/bin/php-cs-fixer fix --config=.php_cs.dist -vvv --diff --using-cache=no - &>/dev/null"
ret=$?
rm $tmp
return $ret
}
function code_style_fix_one {
local file=$1
vagrant ssh -- -n "cd $VAGRANT_PROJECT_DIR; php ./vendor/bin/php-cs-fixer fix --config=.php_cs.dist --using-cache=no $file &>/dev/null"
}
function code_style_check_commit_files {
# Search in every file added or modified
for file in $(git status --porcelain | grep -e '^\s*[AM][AM]*\(.*\).php$' | awk '{ print $2; }')
do
code_style_check_one $file
ret=$?
if [ $ret -gt 0 ]
then
return $ret
fi
done
# search for moved/replaced files
for file in $(git status --porcelain | grep -e '^\s*R.*->\s*\(.*\).php$' | awk '{ print $4; }')
do
code_style_check_one $file
ret=$?
if [ $ret -gt 0 ]
then
return $ret
fi
done
return 0
}
code_style_check_commit_files
if [ $? -gt 0 ]
then
echo "At least one file is not compliant with our coding style"
echo "Please run php-cs-fixer, check the modified files, add them and try again"
echo
echo -e "\tphp ./vendor/bin/php-cs-fixer fix --config=.php_cs.dist"
echo
fi
echo "php-cs-fixer pre commit hook end"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment