Git Hooks, husky, Lint-staged

Git Hooks

Git Hooks are Hooks that trigger custom scripts during Git execution. Each.git file contains an hooks file $project/.git/hooks. You can create hooks in this folder according to the name of the hooks. The corresponding hook file is executed

Git Hooks classification

Client-side hook

As the name implies, the following hooks are commonly used when the client performs certain operations

  1. Pre-commit: this parameter is invoked by the Git commit. It is executed before the commit to check the snapshot to be committed. If the snapshot exits with a non-zero value, the commit is interrupted. Examples include format checking that can be used for code, running test cases…
  2. Pre-merge-commit: this command is called by Git merge. It is executed before merge. If the merge exits in a non-zero state, the merge is interrupted
  3. Prepare-commit-msg: this command is invoked by git commit. It is executed before preparing default logs and starting editing. It can be used with a template to dynamically insert logs
  4. Commit-msg: when git commit or git merge is invoked, the operation is interrupted if the status code is not 0
  5. Pre-push: invoked by Git push, executed before Git push to prevent push from being performed, and interrupted by exit with non-0 status code…

Server-side hook

As the name implies, the following hooks are commonly used to execute certain operations on the server

  1. Pre-receive: it is called when updating the storage in response to git push. If the status code is not 0, the push fails. For example, it can be used to check whether the message pushed this time is compliant
  2. Update: after git push, it will be called when updating the storage. If it exits with a non-0 status code, the push fails. The difference between this and pre-receive is that while pre-receive only runs once when multiple branches are updated in a single push, Update runs once for each issue
  3. Post-receive: All references are updated after git push is complete. For example, it can be used for automatic build and deployment
  4. Post-update: All references are updated after git push is complete. Unlike post-receive, post-update can only receive updates. It cannot know the old and new values, but post-receive can know not only which files have been updated, but also the old and new values

Refer to the Githooks docs for details

Code sample

  1. Creating a hook file
Create a pre-commit file in the $peoject/.git/hooks folder and write the following code

# !/bin/sh
echo "<<<<<< I am in pre-commit >>>>>>"
exit 1 Exit 0 if you want to succeed
Copy the code
  1. Change the file to an executable
After the #### file is created, run the following command
chmod a+x .git/hooks/pre-commit
Copy the code
  1. test
git add .
git commit -m "Test code" # You can see the printed message and failed
Copy the code

Husky (6)

Husky manages Git Hooks in javascript code, mainly because it is inconvenient to directly modify.git/ Hooks files, and the shell is not a language familiar to the front-end

Husky is divided into three operations

  1. Install: initialization
  • Create one in the project root directory.huskyFolder and copyhusky.shEnter the.husky_.
  • To set up git configuration.huskyAdd it as the hooks path,git config core.hooksPath .husky
  1. Set, add: Add Git Hooks. The Set and Add functions both add Git Hooks, but the case is different for Git Hooks that have already been added
  • Set overwrites existing Git Hooks directly
  • Add Existing Git Hooks for chess will be added later
npx husky set .husky/pre-commit "npm test" # This is the first Git Hooks

npx husky set .husky/pre-commit "npm test1" # overwrite the pre-commit file

npx husky add .husky/pre-commit "npm test2" NPM test2 will be added directly to the end of the pre-commit
Copy the code
  1. Uninstall: removes the.husky* hook file, removes the.husky* configuration from the hooks path.git config --unset core.hooksPath

Can use the git config – local – list | grep “core. Hookspath” to view your configuration git Hooks path

Configure the instance

Ps husky(V6) has two ways to generate Git Hooks. The original package.json configuration {” Hooks “: {… }} Husky has dropped conventional JS config

Manually add

npm install husky --save-dev
npx husky install # initialization

npx husky add .husky/pre-commit "npm test" # indicates that the NPM test is executed at pre-commit
Copy the code

The script to add

{
  "scripts": {
    "prepare": "husky install"}}Copy the code
npm install husky --save-dev # Due to NPM install's S lifecycle, You can perform less initialization preinstall --> install --> postinstall --> prepublish --> preprepare --> prepare --> postprepare
npx husky add .husky/pre-commit "npm test"
Copy the code

lint-staged

Lint-staged simply reads files from staging areas and runs configured scripts, avoiding impacting code that has not been committed to staging. This can be used with husky, which triggers Lint-staged scripts and then executes them

Git diff –staged –diff-filter=ACMR –name-only -z Git diff –staged –diff-filter=ACMR –name-only -z git diff –staged –diff-filter=ACMR –name-only -z git diff –staged –diff-filter=ACMR –name-only -z

  1. Get the file name of the staging area by using the command
  2. After splitting the file, serialize it to obtain the full path of the file
  3. After obtaining the complete path, create a task based on the configured execution rules and execute the task