d8githooks
d8githooks copied to clipboard
Drupal core committer git hooks
Drupal core committer git hooks
About this project
This project performs automated checks (e.g. file permissions, PHP/CSS/JS coding standards) before/after performing a commit, to ensure regressions are not accidentally committed.
Project setup
How do I, as a developer, start working on the project?
This project depends on:
- [git] (https://git-scm.com/downloads)
- composer
- Drupal 8
- Node.js
- eslint
- yarn
- Running composer install in the root directory of the repository you have checked out
Optional dependencies
- Homebrew (You can use homebrew to install many of the dependencies above.)
- pbcopy & pbpaste
- cowsay (
brew install cowsay
)
How to install
-
Clone the respository into any directory; for example your home directory:
cd ~; git clone https://github.com/alexpott/d8githooks.git
-
Navigate to the
.git/hooks
directory inside the Drupal 8 clone from which you commit patchescd ~/Sites/8.x/.git/hooks
-
Create symlinks to pre-commit and post-commit files:
ln -sfn ~/d8githooks/pre-commit pre-commit;
ln -sfn ~/d8githooks/post-commit post-commit;
-
Mark the hooks as executable.
chmod u+x pre-commit; chmod u+x post-commit
To test that it's working, introduce a file permissions error and then attempt to commit a text change:
cd ~/Sites/8.x
composer install
chmod 777 core/core.services.yml
echo 'test' >> core/core.services.yml
git add core/core.services.yml
git commit -m "Test."
You should see the error:
git pre-commit check failed: file core/core.services.yml should be 644 not 777
Troubleshooting
Skipping spellchecking
Spelling checking can be skipped using the SKIP_SPELL environment variable.
SKIP_SPELL=1 git commit -m "SOME MESSAGE"
Using core's Coder dev dependency
If you previously installed PHPCS or Coder globally according to instructions on https://www.drupal.org/node/1419988 and your pre-commit hook starts failing silently, you may need to do:
composer global remove drupal/coder
composer global remove phpcs
composer install
This change is required because a canonical version of Coder is now added as a core dev dependency.
Manually checking or fixing a PHP standard
To manually test the standard on a particular file prior to commit:
-
Apply any relevant patch (including fixes, newly configured rules, etc.)
-
From within the repository, run:
composer install
-
From the root of the repository, run:
vendor/bin/phpcs --standard="core/phpcs.xml.dist" core/path/to/file.php
You can confirm that the check is working properly by (e.g.) adding an unused use statement to the file and ensuring it raises an error.
Similarly, to use phpcbf to fix a file, run:
vendor/bin/phpcs --standard="core/phpcs.xml.dist" core/path/to/file.php
Note that in some cases the enabled core rules are not enough to fix errors correctly! You can also check:
vendor/bin/phpcs --standard=Drupal core/path/to/file.php
Be aware, however, that this may introduce out-of-scope changes as it will run rules that core does not yet comply with.
Troubleshooting eslint
sudo npm update -g
How to use
@todo something about what standard message(s) to copy/paste.