What are Git Hook and Husky
Husky is a nodeJS package that listens for Git hooks, making it easier for NodeJS developers to handle Git hook tasks.
Git hooks are custom scripts that implement functions related to the appropriate Git actions. What hooks does Git support?
git hooks | Call time | instructions |
---|---|---|
pre-applypatch | git am Perform before |
|
applypatch-msg | git am After performing |
|
post-applypatch | git am After performing |
Does not affect thegit am The results of |
pre-commit | git commit Perform before |
You can usegit commit --no-verify bypass |
commit-msg | git commit Perform before |
You can usegit commit --no-verify bypass |
post-commit | git commit After performing |
Does not affect thegit commit The results of the |
pre-merge-commit | git merge Perform before |
You can usegit merge --no-verify Bypass. |
prepare-commit-msg | git commit After execution, before the editor opens |
|
pre-rebase | git rebase Perform before |
|
post-checkout | git checkout orgit switch After performing |
If not used--no-checkout Parameter, then ingit clone It will be executed later. |
post-merge | git commit After performing |
In the implementationgit pull “Will also be called |
pre-push | git push Perform before |
|
pre-receive | git-receive-pack Perform before |
|
update | ||
post-receive | git-receive-pack After performing |
Does not affect thegit-receive-pack The results of the |
post-update | whengit-receive-pack rightgit push React and update references in the repository |
|
push-to-checkout | whengit-receive-pack rightgit push React and update references in the repository, and when a push attempts to update a branch that is currently checked outreceive.denyCurrentBranch The configuration is set toupdateInstead when |
|
pre-auto-gc | git gc --auto Perform before |
|
post-rewrite | performgit commit --amend orgit rebase when |
|
sendemail-validate | git send-email Perform before |
|
fsmonitor-watchman | configurationcore.fsmonitor Is set to.git/hooks/fsmonitor-watchman or.git/hooks/fsmonitor-watchmanv2 when |
|
p4-pre-submit | git-p4 submit Perform before |
You can usegit-p4 submit --no-verify bypass |
p4-prepare-changelist | git-p4 submit After execution, before the editor starts |
You can usegit-p4 submit --no-verify bypass |
p4-changelist | git-p4 submit Execute and editchangelist message after |
You can usegit-p4 submit --no-verify bypass |
p4-post-changelist | git-p4 submit After performing |
|
post-index-change | The index is written toread-cache.c do_write_locked_index after |
For detailed hook descriptions, see the Git website
Install dependencies
/ / commitlint installation
npm install --save-dev @commitlint/config-conventional @commitlint/cli
/ / install husky
npm install --save-dev husky
Copy the code
Commitlint works
The new file
Create a commitlint.config.js file, which can be as follows
/** * feat: update * fix * refactor * optimize: Optimize build tool or runtime performance * style: Only style changes * docs: Only document additions/changes * test: Added tests * chore: Changes to the build process or helper tools */
module.exports = {
extends: [
'@commitlint/config-conventional'].rules: {
'type-enum': [2.'always'['feat'.'update'.'fix'.'refactor'.'optimize'.'style'.'docs'.'test'.'chore'
]],
'type-case': [0].'type-empty': [0].'scope-empty': [0].'scope-case': [0].'subject-full-stop': [0.'never'].'subject-case': [0.'never'].'header-max-length': [0.'always'.72]}};Copy the code
Rules consist of names and configuration arrays. Configuration array contains
- level(Level), the value is
0, 1, 2,
, 0 indicates to disable the rule, 1 indicates to disable warning, and 2 indicates error - Applicable(Whether to apply), the value is
always | never
. - Value,
Detailed configuration rules can be found on the CommitLint website
Add the following configuration to the package.json file
"husky": {
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"}}Copy the code
Test whether the application is successful
// Run the following command
git commit -m "first commit"
// The following error is displayed
husky > commit-msg (node v1417.3.⧗ input: First commit * Subject may not be empty [subject-Empty] * found1 problems, 0 warnings
/ / or
⧗ input: feat1: xxx
✖ type must be one of [feat, update, fix, refactor, optimize, style, docs, test, chore] [type-enum]
✖ found 1 problems, 0Warnings ⓘ Get help: HTTPS://github.com/conventional-changelog/commitlint/#what-is-commitlint
Copy the code
Commitlint checks commit information for any type we define before git commit execution and intercepts the commit if it does not comply with the commit rules.
Some important pits
Husky version
Using the latest husky in a Windows environment results in no overwrite./git/hooks
The following is the default file in./git/hooks after git init is initialized
I used version 3.1.0 of Husky to successfully generate hooks files
CNPM and NPM issues
Dependencies using CNPM and NPM downloads may cause an error when husky executes the commit-msg hook function.
You are advised to use YARN Install to download dependencies
Reference:
Learn Git Hooks configuration from top to bottom
Front-end codeLint– Integrates ESLint, StyleLint, commitLint practices and principles for the project