Fix test on precommit hook

This commit is contained in:
Laurent Destailleur 2021-03-12 14:30:32 +01:00
parent ab9d10edd3
commit dc5557e4f7
2 changed files with 38 additions and 21 deletions

View File

@ -1,13 +1,17 @@
#!/bin/sh #!/bin/sh
# To install this precommit file: put this file in your local repo in .git/hooks directory and make it executable # To install this precommit file: put this file in your local repo in .git/hooks directory and make it executable.
# you need to adapt the path to your phpcs install # You may need to set th DIRPHPCS to the path to your phpcs install.
# if phpcs check fail, then it run phpcbf to fix automaticaly the syntax, and git commit is canceled # If phpcs check fail and AUTOFIX is set to 1, then it run phpcbf to fix automaticaly the syntax, and git commit is canceled.
# if you have a multiprocessor computer, you can add to the option --parallel=xx # If you have a multiprocessor computer, you can add to the option --parallel=xx
# when running git commit, it first execute this file checking only modified files, so it is faster than running on all files # When running git commit, it first execute this file checking only modified files, so it is faster than running on all files
# To run the fix manually: cd ~/git/dolibarr; phpcbf -s -p -d memory_limit=-1 --extensions=php --colors --tab-width=4 --standard=dev/setup/codesniffer/ruleset.xml --encoding=utf-8 --runtime-set ignore_warnings_on_exit true fileordir # To run the fix manually: cd ~/git/dolibarr; phpcbf -s -p -d memory_limit=-1 --extensions=php --colors --tab-width=4 --standard=dev/setup/codesniffer/ruleset.xml --encoding=utf-8 --runtime-set ignore_warnings_on_exit true "fileordir"
PROJECT=`php -r "echo dirname(dirname(dirname(realpath('$0'))));"` PROJECT=`php -r "echo dirname(dirname(dirname(realpath('$0'))));"`
STAGED_FILES_CMD=`git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\\\.php` STAGED_FILES_CMD=`git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\\\.php`
DIRPHPCS=""
AUTOFIX=1
echo "Running precommit hook in .git/hooks/pre-commit" 1>&2;
# Determine if a file list is passed # Determine if a file list is passed
if [ "$#" -eq 1 ] if [ "$#" -eq 1 ]
@ -20,36 +24,50 @@ then
fi fi
SFILES=${SFILES:-$STAGED_FILES_CMD} SFILES=${SFILES:-$STAGED_FILES_CMD}
echo "Checking PHP Lint..." echo "Checking PHP Lint with php -l ..."
for FILE in $SFILES for FILE in $SFILES
do do
php -l -d display_errors=0 $PROJECT/$FILE php -l -d display_errors=0 $PROJECT/$FILE
if [ $? != 0 ]
result1=$?
if [ "x$result1" != "x0" ]
then then
echo "Fix the error before commit." echo "Fix the error before commit." 1>&2;
exit 1 exit 1
fi fi
FILES="$FILES $PROJECT/$FILE" FILES="$FILES $PROJECT/$FILE"
done done
if [ "$FILES" != "" ] if [ "$FILES" != "" ]
then then
echo "Running PHPCS Code Sniffer..." echo "Running PHPCS Code Sniffer..."
#~/vendor/bin/phpcs --version #~/vendor/bin/phpcs --version
#phpcs --standard=PSR2 --encoding=utf-8 -n -p $FILES #phpcs --standard=PSR2 --encoding=utf-8 -n -p $FILES
# Check Dolibarr standard # Check Dolibarr standard
~/vendor/bin/phpcs -s -p -d memory_limit=-1 --parallel=2 --extensions=php --colors --tab-width=4 --standard=dev/setup/codesniffer/ruleset.xml --encoding=utf-8 --runtime-set ignore_warnings_on_exit true $FILES phpcs -s -p -d memory_limit=-1 --parallel=2 --extensions=php --colors --tab-width=4 --standard=dev/setup/codesniffer/ruleset.xml --encoding=utf-8 --runtime-set ignore_warnings_on_exit true $FILES
# Check your own standard # Check your own standard
#~/vendor/bin/phpcs -s -p -d memory_limit=-1 --parallel=2 --extensions=php --colors --tab-width=4 --standard=htdocs/custom/codesniffer/ruleset.xml --encoding=utf-8 --runtime-set ignore_warnings_on_exit true $FILES #${DIRPHPCS}phpcs -s -p -d memory_limit=-1 --parallel=2 --extensions=php --colors --tab-width=4 --standard=htdocs/custom/codesniffer/ruleset.xml --encoding=utf-8 --runtime-set ignore_warnings_on_exit true $FILES
result2=$?
if [ $? != 0 ] if [ "x$result2" != "x0" ]
then then
# fix standard errors # Fix standard errors
~/vendor/bin/phpcbf -s -p -d memory_limit=-1 --extensions=php --colors --tab-width=4 --standard=dev/setup/codesniffer/ruleset.xml --encoding=utf-8 --runtime-set ignore_warnings_on_exit true $FILES if [ "x$AUTOFIX" != "x0" ]
#~/vendor/bin/phpcbf -s -p -d memory_limit=-1 --extensions=php --colors --tab-width=4 --standard=htdocs/custom/codesniffer/ruleset.xml --encoding=utf-8 --runtime-set ignore_warnings_on_exit true $FILES then
echo "Fix the error before commit." phpcbf -s -p -d memory_limit=-1 --extensions=php --colors --tab-width=4 --standard=dev/setup/codesniffer/ruleset.xml --encoding=utf-8 --runtime-set ignore_warnings_on_exit true $FILES
exit 1 #${DIRPHPCS}phpcbf -s -p -d memory_limit=-1 --extensions=php --colors --tab-width=4 --standard=htdocs/custom/codesniffer/ruleset.xml --encoding=utf-8 --runtime-set ignore_warnings_on_exit true $FILES
echo "Found some errors in syntax rules. An automatice fix has been applied. Check it before commit." 1>&2;
exit 1
else
echo "Found some errors in syntax rules. Fix the error(s) before commit." 1>&2;
exit 1
fi
fi fi
fi fi
exit $? exit 0

View File

@ -35,13 +35,12 @@ $langs->load("companies");
$id = GETPOST('id', 'int'); $id = GETPOST('id', 'int');
$ref = GETPOST('ref', 'alpha'); $ref = GETPOST('ref', 'alpha');
$action = GET PO ST('action','aZ09'); $action = GETPOST('action', 'aZ09');
// Security check // Security check
$fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref : '')); $fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref : ''));
$fieldtype = (!empty($ref) ? 'ref' : 'rowid'); $fieldtype = (!empty($ref) ? 'ref' : 'rowid');
if ($user->socid) if ($user->socid) {
{
$socid = $user->socid; $socid = $user->socid;
} }
$result = restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype); $result = restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype);