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 amPerform before
applypatch-msg git amAfter performing
post-applypatch git amAfter performing Does not affect thegit amThe results of
pre-commit git commitPerform before You can usegit commit --no-verifybypass
commit-msg git commitPerform before You can usegit commit --no-verifybypass
post-commit git commitAfter performing Does not affect thegit commitThe results of the
pre-merge-commit git mergePerform before You can usegit merge --no-verifyBypass.
prepare-commit-msg git commitAfter execution, before the editor opens
pre-rebase git rebasePerform before
post-checkout git checkoutorgit switchAfter performing If not used--no-checkoutParameter, then ingit cloneIt will be executed later.
post-merge git commitAfter performing In the implementationgit pull“Will also be called
pre-push git pushPerform before
pre-receive git-receive-packPerform before
update
post-receive git-receive-packAfter performing Does not affect thegit-receive-packThe results of the
post-update whengit-receive-packrightgit pushReact and update references in the repository
push-to-checkout whengit-receive-pack rightgit pushReact and update references in the repository, and when a push attempts to update a branch that is currently checked outreceive.denyCurrentBranchThe configuration is set toupdateInsteadwhen
pre-auto-gc git gc --autoPerform before
post-rewrite performgit commit --amendorgit rebasewhen
sendemail-validate git send-emailPerform before
fsmonitor-watchman configurationcore.fsmonitorIs set to.git/hooks/fsmonitor-watchmanor.git/hooks/fsmonitor-watchmanv2when
p4-pre-submit git-p4 submitPerform before You can usegit-p4 submit --no-verifybypass
p4-prepare-changelist git-p4 submitAfter execution, before the editor starts You can usegit-p4 submit --no-verifybypass
p4-changelist git-p4 submitExecute and editchangelist messageafter You can usegit-p4 submit --no-verifybypass
p4-post-changelist git-p4 submitAfter performing
post-index-change The index is written toread-cache.c do_write_locked_indexafter

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

  1. level(Level), the value is0, 1, 2,, 0 indicates to disable the rule, 1 indicates to disable warning, and 2 indicates error
  2. Applicable(Whether to apply), the value isalways | never.
  3. 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