Git hooks¶
pre-commit¶
If you'd like your code to be automatically checked pre-commit you can copy a git hook to your git hooks directory, in the carandclassic
repo:
ln -fs "$PWD"/.githooks/pre-commit "$PWD"/.git/hooks/pre-commit
This will set up a pre-commit hook which checks the style of any PHP files modified in that commit.
This uses both PHP_CodeSniffer and PHP-CS-Fixer.
If you need to skip a pre-commit
git hook for whatever reason, you can run git commit --no-verify
or uncheck “run git hooks” from the PhpStorm GUI (or equivalent in another IDE).
Note that if you are using an IDE or other GUI for git commits, you will need to check the console for details if a git commit fails.
Example on PhpStorm:
Note the pre-commit git hook has some limitations. It tries to detect merge commits for example, but some things are harder to detect. For example, if you cherry-pick or revert a commit that did not pass the style check (or the style checks have changed), the operation might fail. Unfortunately, these commands do not have a --no-verify option so in this rare case, the (best?) solution is to temporarily disable the git hook.
There’s no nice way I know to do this. For example, I resort to this: 1. mv .git/hooks/pre-commit .git/hooks/pre-commit.bak 2. do whatever 3. mv .git/hooks/pre-commit.bak .git/hooks/pre-commit
prepare-commit-msg¶
cp .githooks/prepare-commit-msg .git/hooks/ && chmod +x .git/hooks/prepare-commit-msg
Automatically prefixes commit messages with the issue ID that is used in the branch name. This only happens if the commit message does not already start with a prefix.
e.g. a message add translations
in the sc-1337/search-filters
branch will be prefixed and become [SC-1337] add translations
.
This is useful when browsing the commit history, running git blame
or for associating commits to issues.