代码检查工作流以及commit规范化

最近再看Vue技术内幕,就把Vue项目fork了一份,然后边看文章边在代码里加上注释。看了几节内容,做了一些笔记,准备提交一次,然后就直接:

git add .
git commit -m '添加代码注释'
……

然后就弹出这么个内容:

   ERROR  invalid commit message format.

  Proper commit message format is required for automated changelog generation. Examples:

    feat(compiler): add 'comments' option
    fix(v-model): handle events on blur (close #28)

  See .github/COMMIT_CONVENTION.md for more details.
  You can also use npm run commit to interactively generate a commit message.


commit-msg hook failed (add --no-verify to bypass)

怎么提交还会报错?这看上去不像冲突啥的啊。npm run commit 又是啥?
键入npm run commit


[email protected], [email protected]


Line 1 will be cropped at 100 characters. All other lines will be wrapped after 100 characters.

? Select the type of change that you're committing: (Use arrow keys)
❯ feat:     A new feature 
  fix:      A bug fix 
  docs:     Documentation only changes 
  style:    Changes that do not affect the meaning of the code (white-space, format
ting, missing semi-colons, etc) 
  refactor: A code change that neither fixes a bug nor adds a feature 
  perf:     A code change that improves performance 
  test:     Adding missing tests or correcting existing tests 
(Move up and down to reveal more choices)

选择这次提交的类型?类型有很多种,除了这些还可以用方向键查看更多的:

  build:    Changes that affect the build system or external dependencies (example 
scopes: gulp, broccoli, npm) 
  ci:       Changes to our CI configuration files and scripts (example scopes: Trav
is, Circle, BrowserStack, SauceLabs) 
  chore:    Other changes that don't modify src or test files 
  revert:   Reverts a previous commit 

选择对应的类型可以清楚明了的说明本次提交的目的。
接下来还会问一些问题:

? What is the scope of this change (e.g. component or file name)? (press enter to s
kip)
 
? Write a short, imperative tense description of the change:
 添加笔记以及注释
? Provide a longer description of the change: (press enter to skip)
 
? Are there any breaking changes? No
? Does this change affect any open issues? No

按着这个一步一步来,一个规范的的commit就出来了。
嗨呀,真的好啊,还能强制性的规范commit,所有项目上都弄这么个东西,这么一来所有项目的commit都会变得清晰明了。
这个命令到底执行了啥呢?跑去package.json文件中去看,有这么一句"commit": "git-cz"git-cz又是啥呢?接着又去google。

代码检查工作流以及commit规范化_第1张图片
google git-cz

第一条点进去,是一个插件,叫 commitizen
切到自己的项目,npm安装, package.jsonscripts添加指令,再在 package.jsonconfig里添加:

  "config": {
    "commitizen": {
      "path": "./node_modules/cz-conventional-changelog"
    }
  }

然后随便改点东西,先用

git add .
git commit -m 'x'

测试下。哈?怎么直接提交了?怎么没报错?这东西没用?npm run commit,这玩意儿有用呀,为啥直接提交不规范commit还能成功?
再看看vue的package.json怎么写的。

  "gitHooks": {
    "pre-commit": "lint-staged",
    "commit-msg": "node scripts/verify-commit-msg.js"
  },
  "lint-staged": {
    "*.js": [
      "eslint --fix",
      "git add"
    ]
  },

gitHooks:顾名思义,git相关的钩子,可以在git的各个时间做不同的处理。实际上想这么用得装个尤大写的yorkie,实际上就是改了一丢丢的husky。

  • commit之前,执行lint-staged
  • 对commit的说明进行判定是否是符合规范的commit

lint-staged :对代码进行静态分析,试图找出潜在的问题。使用 lint-staged只会对每次提交文件进行检查。

  • 对所有提交的js文件进行eslint修复,修复后重新添加该文件到暂存区

加上这两句之后,直接提交不规范的commit就会报错啦,而且还会对提交的js文件进行eslint验证,以及尝试修复eslint。

你可能感兴趣的:(代码检查工作流以及commit规范化)