Add pre-commit hook

This commit is contained in:
Ben Goldsworthy 2021-02-20 20:44:33 +00:00
parent 077ac22f27
commit 81a41202bb
4 changed files with 176 additions and 32 deletions

4
_tools/setup.sh Normal file
View file

@ -0,0 +1,4 @@
#!/bin/sh
cp contrib/pre-commit .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit

View file

@ -1,13 +1,11 @@
{ {
"scripts": { "require-dev": {
"post-install-cmd": [ "dealerdirect/phpcodesniffer-composer-installer": "^0.7.1",
"PHPCodeChecker\\Installer::postInstall" "wp-coding-standards/wpcs": "^2.3"
], },
"post-update-cmd": [ "scripts": {
"PHPCodeChecker\\Installer::postInstall" "post-install-cmd": [
] "bash _tools/setup.sh"
}, ]
"require": {
"wickedreports/phpcs-pre-commit-hook": "^0.7.0"
} }
} }

125
composer.lock generated
View file

@ -4,8 +4,79 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "957eb6c729bd4e3aa67e474d0bd31acc", "content-hash": "3a147d50695956de52f7cbf827b47830",
"packages": [ "packages": [],
"packages-dev": [
{
"name": "dealerdirect/phpcodesniffer-composer-installer",
"version": "v0.7.1",
"source": {
"type": "git",
"url": "https://github.com/Dealerdirect/phpcodesniffer-composer-installer.git",
"reference": "fe390591e0241955f22eb9ba327d137e501c771c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Dealerdirect/phpcodesniffer-composer-installer/zipball/fe390591e0241955f22eb9ba327d137e501c771c",
"reference": "fe390591e0241955f22eb9ba327d137e501c771c",
"shasum": ""
},
"require": {
"composer-plugin-api": "^1.0 || ^2.0",
"php": ">=5.3",
"squizlabs/php_codesniffer": "^2.0 || ^3.0 || ^4.0"
},
"require-dev": {
"composer/composer": "*",
"phpcompatibility/php-compatibility": "^9.0",
"sensiolabs/security-checker": "^4.1.0"
},
"type": "composer-plugin",
"extra": {
"class": "Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\Plugin"
},
"autoload": {
"psr-4": {
"Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Franck Nijhof",
"email": "franck.nijhof@dealerdirect.com",
"homepage": "http://www.frenck.nl",
"role": "Developer / IT Manager"
}
],
"description": "PHP_CodeSniffer Standards Composer Installer Plugin",
"homepage": "http://www.dealerdirect.com",
"keywords": [
"PHPCodeSniffer",
"PHP_CodeSniffer",
"code quality",
"codesniffer",
"composer",
"installer",
"phpcs",
"plugin",
"qa",
"quality",
"standard",
"standards",
"style guide",
"stylecheck",
"tests"
],
"support": {
"issues": "https://github.com/dealerdirect/phpcodesniffer-composer-installer/issues",
"source": "https://github.com/dealerdirect/phpcodesniffer-composer-installer"
},
"time": "2020-12-07T18:04:37+00:00"
},
{ {
"name": "squizlabs/php_codesniffer", "name": "squizlabs/php_codesniffer",
"version": "3.5.8", "version": "3.5.8",
@ -63,47 +134,57 @@
"time": "2020-10-23T02:01:07+00:00" "time": "2020-10-23T02:01:07+00:00"
}, },
{ {
"name": "wickedreports/phpcs-pre-commit-hook", "name": "wp-coding-standards/wpcs",
"version": "0.7", "version": "2.3.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/WickedReports/phpcs-pre-commit-hook.git", "url": "https://github.com/WordPress/WordPress-Coding-Standards.git",
"reference": "d376e83d5d8038cf9eb8e4cccc0fce160763e0ad" "reference": "7da1894633f168fe244afc6de00d141f27517b62"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/WickedReports/phpcs-pre-commit-hook/zipball/d376e83d5d8038cf9eb8e4cccc0fce160763e0ad", "url": "https://api.github.com/repos/WordPress/WordPress-Coding-Standards/zipball/7da1894633f168fe244afc6de00d141f27517b62",
"reference": "d376e83d5d8038cf9eb8e4cccc0fce160763e0ad", "reference": "7da1894633f168fe244afc6de00d141f27517b62",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"squizlabs/php_codesniffer": "3.*" "php": ">=5.4",
"squizlabs/php_codesniffer": "^3.3.1"
}, },
"type": "scripts", "require-dev": {
"autoload": { "dealerdirect/phpcodesniffer-composer-installer": "^0.5 || ^0.6",
"psr-4": { "phpcompatibility/php-compatibility": "^9.0",
"PHPCodeChecker\\": "src/" "phpcsstandards/phpcsdevtools": "^1.0",
} "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0"
}, },
"suggest": {
"dealerdirect/phpcodesniffer-composer-installer": "^0.6 || This Composer plugin will sort out the PHPCS 'installed_paths' automatically."
},
"type": "phpcodesniffer-standard",
"notification-url": "https://packagist.org/downloads/", "notification-url": "https://packagist.org/downloads/",
"license": [ "license": [
"GPL-3.0" "MIT"
], ],
"authors": [ "authors": [
{ {
"name": "Stanislav Malkin", "name": "Contributors",
"email": "stan@wickedreports.com" "homepage": "https://github.com/WordPress/WordPress-Coding-Standards/graphs/contributors"
} }
], ],
"description": "PHPCS check before commit", "description": "PHP_CodeSniffer rules (sniffs) to enforce WordPress coding conventions",
"keywords": [
"phpcs",
"standards",
"wordpress"
],
"support": { "support": {
"issues": "https://github.com/WickedReports/phpcs-pre-commit-hook/issues", "issues": "https://github.com/WordPress/WordPress-Coding-Standards/issues",
"source": "https://github.com/WickedReports/phpcs-pre-commit-hook/tree/master" "source": "https://github.com/WordPress/WordPress-Coding-Standards",
"wiki": "https://github.com/WordPress/WordPress-Coding-Standards/wiki"
}, },
"time": "2018-03-13T23:57:17+00:00" "time": "2020-05-13T23:57:56+00:00"
} }
], ],
"packages-dev": [],
"aliases": [], "aliases": [],
"minimum-stability": "stable", "minimum-stability": "stable",
"stability-flags": [], "stability-flags": [],

61
contrib/pre-commit Normal file
View file

@ -0,0 +1,61 @@
#!/bin/sh
# Modified from: https://github.com/bjornjohansen/wp-pre-commit-hook
# Also: https://softdiscover.com/wordpress/perfect-setup-for-a-wordpress-project-development/
PROJECT=`php -r "echo dirname(dirname(dirname(realpath('$0'))));"`
STAGED_FILES_CMD=`git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\\\.php`
# Determine if a file list is passed
if [ "$#" -eq 1 ]
then
oIFS=$IFS
IFS='
'
SFILES="$1"
IFS=$oIFS
fi
SFILES=${SFILES:-$STAGED_FILES_CMD}
echo "Checking PHP Lint..."
for FILE in $SFILES
do
php -l -d display_errors=0 $PROJECT/$FILE
if [ $? != 0 ]
then
echo "Fix the error before commit."
exit 1
fi
FILES="$FILES $PROJECT/$FILE"
done
if [ -f "$PROJECT/phpcs.ruleset.xml" ]
then
RULESET="$PROJECT/phpcs.ruleset.xml"
elif [ -f "$PROJECT/phpcs.xml.dist" ]
then
RULESET="$PROJECT/phpcs.xml.dist"
else
RULESET="WordPress"
fi
if [ "$FILES" != "" ]
then
echo "Checking Code Standard Compliance, using $RULESET as ruleset standard..."
./vendor/bin/phpcs --standard="$RULESET" --colors --encoding=utf-8 -n -p $FILES
if [ $? != 0 ]
then
echo "Coding standards errors have been detected. Running phpcbf..."
./vendor/bin/phpcbf --standard="$RULESET" --encoding=utf-8 -n -p $FILES
git add $FILES
echo "Running Code Sniffer again..."
./vendor/bin/phpcs --standard="$RULESET" --colors --encoding=utf-8 -n -p $FILES
if [ $? != 0 ]
then
echo "Errors found not fixable automatically. You need to manually fix them."
exit 1
fi
fi
fi
exit $?