The problem background

Gitlab pre-merge-hooks error: commit message does not comply with code merge.

commit fc442934c39fe700aa24d3f2f426d266e07a459e
Author: xxxxx
Date:   Fri Nov 5 10:08:50 2021 +0800Modify the documentCopy the code

The company’s commit specification is this:

  • The first line is the requirement numberSAt the beginning, and then some number
  • The second line isMessage:At the beginning, then follow the content
commit fc442934c39fe700aa24d3f2f426d266e07a459e
Author: xxxxx
Date:   Fri Nov 5 10:08:50 2021 +0800

S200
Message: Revised copyCopy the code

Colleagues know this specification and forget it when they submit it. How do you avoid that? Now the next common solution

The solution

We can move this interception up to the point where committing code locally is prohibited if it does not comply with the commit information specification. You don’t find out when you’re consolidating your positions. The popular idea now is husky + Lint-staged.

  • huskyCan be executedgit-hooksHandle some additional configuration tasks, such as incommit-msgThe hook checks that the submitted information is valid
  • lint-stagedOnly files in the staging area will be run with linter configured or other tasks, such asprettierFormatting code

husky

yarn add husky
Copy the code

After the installation is complete, you can check whether the installation is successful in the. Git /hooks/ folder of the current project

[~/Work_Space/demo/.git/hooks] []
-> % tree 
.
|____pre-rebase
|____pre-applypatch
|____husky.sh // If the husky script is added, the installation is successful
|____husky.local.sh
|____commit-msg
|____pre-commit
|____pre-merge-commit
Copy the code

lint-staged:

yarn add lint-staged
Copy the code

Configure package.json after installation:

{
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"."commit-msg": "echo 'from husky'" // Output a sentence}},"lint-staged": {
    "src/**/*.{js,vue}": [
      "echo 'from lint-stage'"]}}Copy the code

Js. vue (SRC); commit- MSG (SRC);

Let’s test it out:

The result shows that the configuration is OK.

Project configuration

Specific configuration in the project:

  • Lint on staging files before pre-commit
  • Commit – MSG verifies commit information
{
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"."commit-msg": "node scripts/verify-commit.js"}},"lint-staged": {
    "src/**/*.{js,vue}": [
      "eslint"."git add" // eslint readds files to the staging area when finished]}}Copy the code

verify-commit.js:

const chalk = require('chalk');
const path = require('path');
const msg = require('fs')
    .readFileSync(path.resolve('.git/COMMIT_EDITMSG'), 'utf-8')
    .trim();

const commitRE = /^(S|R\d+\r? \n(? : M | M essage: 50 {1}) /;

if(! commitRE.test(msg)) {console.log()
    console.error(
        `    ${chalk.bgRed.white(' ERROR ')} ${chalk.red(
            `Invalid commit message format.`
        )}\n\n` +
        chalk.red(
            ` Proper commit message format as below. Example:\n\n`
        ) +
        `    ${chalk.green(`S2222`)}\n` +
        `    ${chalk.green(
            'Message: Feat (Router): Modify text size'
        )}\n\n`
    );
    process.exit(1);
}
Copy the code

I modified SRC /main.js to pass the pre-commit because the code does not have ESLint errors, and then intercepted the wrong commit correctly.

Other problems

In vscode, both the command line and the plugin gitLen work fine, but many colleagues are used to source-tree code submission, after testing, MAC using source-tree code submission, Even if Lint-staged ESLint errors or incorrectly formatted submission information were present, source-Tree swallowed them and simply submitted the code. Git log description:

Can't find npx in PATH: /Applications/Sourcetree.app/Contents/Resources/git_local/libexec/git-core:/Applications/Sourcetree.app/Contents/Resourc es/bin:/Applications/Sourcetree.app/Contents/Resources/git_local/bin:/Applications/Sourcetree.app/Contents/Resources/git _local/gitflow:/Applications/Sourcetree.app/Contents/Resources/git_local/git-lfs:/usr/bin:/bin:/usr/sbin:/sbin Skipping pre-commit hook!!!Copy the code

The reason is that Soucre-tree could not find the NPX command. This type of client GUI has to be configured in advance to access environment variables. Git /hooks/pre-commit and.git/hooks/commit-message:

#! /bin/sh
# husky
PATH=/Users/root/n/bin:$PATH # Join the line
# Created by Husky v4.3.8 (https://github.com/typicode/husky#readme)
# At: 2021/11/5 2:33:07 PM
# (https://github.com/typicode/husky#readme)

. "$(dirname "$0")/husky.sh"
Copy the code

PATH=/Users/root/n/bin:$PATH /Users/root/n/bin is the directory where Node resides. Configure the node PATH based on your PC

which node
/Users/root/n/bin
Copy the code

After setting the environment variables, re-test:

Successful intercept! If Husky also configures other hook tasks, commit using source-tree also adds the appeal environment variable configuration.

conclusion

Github has a standard commit-msg specification that many open-source projects use, such as vue-Next’s commit specification, as a standard reference.