美文网首页
代码检查工作流以及commit规范化

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

作者: 赫鲁晓夫的玉米棒子 | 来源:发表于2019-03-05 18:10 被阅读0次

最近再看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


cz-cli@2.10.1, cz-conventional-changelog@2.1.0


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。

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规范化

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

  • 2020-12-07(1) 周一

    代码review先检查后再commit A:guo 1:commit 提交到本地 2: 抓取 拉取 pull ...

  • Git常见问题

    1. git commit --no-verify -m "commit" 就可以跳过代码检查 2. 暂时性解...

  • AndroidStudio插件-Git Commit Templ

    为了规范化团队代码提交,可以使用Git Commit Template插件对git提交规范进行一定的约束,方便后期...

  • git 修改备注

    背景 当本地的修改已经commit之后,有一些公司会检查代码,然后才能入库,有些时候,commit message...

  • 使用 git hook 规范 Android 项目

    引言 本文所说的『规范』包含两个部分 git commit 是注释的规范 git commit 时对代码规范的检查...

  • (转载)Git提交信息规范化

    Git提交信息规范化 目的 统一团队Git Commit标准,便于后续代码review、版本发布、自动化生成cha...

  • git常用操作

    1、git提交代码。提交代码前先检查状态,然后add、commit 、pull、push; 2、git远程覆盖本地...

  • git stash的详细讲解

    前言 git是用来管理代码和工作流的强大工具,它以commit为单位,低成本开辟分支等优势备受青睐.在实际开发中,...

  • Git Commit规范化

    以下是参考链接https://www.jianshu.com/p/d8ed169daf82https://chua...

网友评论

      本文标题:代码检查工作流以及commit规范化

      本文链接:https://www.haomeiwen.com/subject/drdouqtx.html