当前位置:   article > 正文

git规范代码提交格式:commitlint+husky安装_npx husky add .husky/commit-msg "npx --no-install

npx husky add .husky/commit-msg "npx --no-install commitlint -e $husky_git_p

环境:centos

版本:

node -v : v14.17.6

npm -v : 6.14.15

npx -v : 6.14.15

git --version : git version 2.16.1

其中版本要求至少:nodejs >= 12   git >= 2.13.2

gitlab安装:https://blog.csdn.net/hnmpf/article/details/80518460

安装完成gitlab后,新建一个目录作为代码仓库目录;

安装必备软件:

nodejs:

  1. mkdir node.js // 新建目录
  2. cd node.js // 进入目录里面
  3. wget https://nodejs.org/dist/v10.9.0/node-v10.9.0-linux-x64.tar.xz // 下载
  4. tar xf node-v10.9.0-linux-x64.tar.xz // 解压
  5. cd node-v10.9.0-linux-x64/ // 进入解压目录
  6. node -v //查看版本号
  7. 解压文件的 bin 目录底下包含了 node、npm 等命令,我们可以使用 ln 命令来设置软连接:
  8. ln -s /usr/software/nodejs/bin/npm /usr/local/bin/
  9. ln -s /usr/software/nodejs/bin/node /usr/local/bin/
  10. 设置npm镜像为国内的
  11. npm config set registry http://registry.npm.taobao.org

因为我安装的版本较低,因此需要升级版本:

  1. npm cache clean -f // 清楚node缓存
  2. npm i -g n // 安装node的版本管理工具n 或者 npm install -g n
  3. 升级node:
  4. n stable // 升级到最新稳定版 (建议)
  5. n latest // 升级到最新版本
  6. n 版本号 // 升级到指定版本
  7. 如果当前版本没有变化的话,可以重新打开服务器再看一下
  8. 卸载旧版本的nodejs:
  9. yum remove nodejs npm -y // 卸载npm
  10. 进入 /usr/local/lib 删除所有 node 和 node_modules文件夹
  11. 进入 /usr/local/include 删除所有 node 和 node_modules 文件夹
  12. 检查 ~ 文件夹里面的"local" “lib” “include” 文件夹,然后删除里面的所有 “node” 和 “node_modules” 文件夹可以使用以下命令查找 $ find ~/ -name node $ find ~/ -name node_modules
  13. 删除: /usr/local/bin/npm
  14. 删除: /usr/local/share/man/man1/node.1
  15. 删除: /usr/local/lib/dtrace/node.d
  16. 删除: rm -rf /home/[homedir]/.npm
  17. 删除: rm -rf /home/root/.npm

如果npm安装成功,但是执行的时候找不到命令?

解决:需要设置环境变量就可以了:

# 用一个通用的命令配置环境变量

 echo -e "export PATH=$(npm prefix -g)/bin:$PATH" >> ~/.bashrc && source ~/.bashrc

# 上面的命令中使用 npm prefix -g 获取node安装目录

如果报找不到npx命令:

npm i -g npx

或者

npm install -g npx

安装 commitlint

第一步:初始化

  1. npm init // 初始化一些信息,执行完成后悔创建一个package.json的文件
  2. //建议在执行的过程中,能填的信息都填上;
  3. 最后生成的文件如下:
  4. {
  5. "name": "test001",
  6. "version": "1.0.1",
  7. "description": "test0001 description",
  8. "main": "index.js",
  9. "scripts": {
  10. "test": "echo \"Error: no test specified\" && exit 1"
  11. },
  12. "repository": {
  13. "type": "git",
  14. "url": "http://xxxx:8001/atong/test001.git"
  15. },
  16. "author": "test001",
  17. "license": "ISC",
  18. "devDependencies": {
  19. "@commitlint/cli": "^13.1.0",
  20. "@commitlint/config-angular": "^13.1.0",
  21. "@commitlint/config-conventional": "^13.1.0",
  22. "husky": "^4.2.3"
  23. },
  24. "dependencies": {},
  25. "keywords": [
  26. "test001"
  27. ]
  28. }
  29. 执行完可以在项目根目录看到package-lock.json,package.json文件

第二步:安装插件 commitlint

  1. npm install --save-dev @commitlint/{cli,config-conventional}
  2. echo "module.exports = { extends: ['@commitlint/config-conventional'] };" > commitlint.config.js

验证是否安装成功:

  1. 第一种验证方式:
  2. $ echo "foo: bar"|npx commitlint
  3. ⧗ input: foo: bar
  4. type must be one of [jira_id, feature, update, fix, refactor, optimize, style, docs, chore] [type-enum]
  5. ✖ found 1 problems, 0 warnings
  6. ⓘ Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint
  7. 第二种验证方式:
  8. npx commitlint --from HEAD~1 --to HEAD --verbose

第三步:安装husky

  1. 建议不要使用默认latest版本安装 可能hooks会失效 这是官方的一个ISSUES
  2. 如已经安装并且不生效 请先删除.git/hooks文件夹
  3. 然后删除默认版本npm uninstall husky
  4. 再次安装即可npm install husky@4.2.3 --save-dev
  5. 安装:
  6. npm install husky@4.2.3 --save-dev
  7. npx husky add .husky/commit-msg 'npx --no-install commitlint --edit $1'

在package.json中增加husky配置:

  1. "husky": {
  2. "hooks": {
  3. "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
  4. }
  5. }

第四步:添加配置规范到commitlint.config.js

这个文件代表以后commit -m 后面的提交应遵循的规范

  1. /**
  2. * jira_id:关联jira的issue ID 用于关联动作 必填
  3. * feat:新功能
  4. * update:更新某功能
  5. * fixbug:修补某功能的bug
  6. * refactor:重构某个功能
  7. * optimize: 优化构建工具或运行时性能
  8. * style:仅样式改动
  9. * docs:仅文档新增/改动
  10. * chore:构建过程或辅助工具的变动
  11. */
  12. module.exports = {
  13. extends: [
  14. '@commitlint/config-conventional'
  15. ],
  16. rules: {
  17. 'type-enum': [
  18. 2, // 表示必须输入的
  19. 'always', [
  20. 'jira_id',
  21. 'feat',
  22. 'update',
  23. 'fix',
  24. 'refactor',
  25. 'optimize',
  26. 'style',
  27. 'docs',
  28. 'chore'
  29. ]
  30. ],
  31. 'type-case': [0],
  32. 'type-empty': [0],
  33. 'scope-empty': [0],
  34. 'scope-case': [0],
  35. 'subject-full-stop': [0, 'never'],
  36. 'subject-case': [0, 'never'],
  37. 'header-max-length': [0, 'always', 72],
  38. }
  39. };

最后的文件有:

  1. -rw-r--r-- 1 root root 490 Sep 3 17:49 commitlint.config.js
  2. drwxr-xr-x 139 root root 4096 Sep 3 17:47 node_modules
  3. -rw-r--r-- 1 root root 656 Sep 3 17:47 package.json
  4. -rw-r--r-- 1 root root 58766 Sep 3 17:47 package-lock.json
  5. 其实还有两个隐藏文件:(.git文件)(.husky文件)
  6. -rw-r--r-- 1 root root 490 Sep 3 17:49 commitlint.config.js
  7. drwxr-xr-x 8 root root 4096 Sep 3 20:25 .git
  8. drwxr-xr-x 3 root root 4096 Sep 3 16:40 .husky
  9. drwxr-xr-x 139 root root 4096 Sep 3 17:47 node_modules
  10. -rw-r--r-- 1 root root 656 Sep 3 17:47 package.json
  11. -rw-r--r-- 1 root root 58766 Sep 3 17:47 package-lock.json
  12. .git目录中有 hooks文件夹

提交规范:

格式

Commit Message 格式
每次提交,Commit message 都包括三个部分:HeaderBodyFooter。其中,Header 是必需的,BodyFooter 可以省略。
​
<type>(<scope>): <subject>
<空行>
<body>
<空行>
<footer>

不管是哪一个部分,任何一行都不得超过72个字符(或100个字符)。

Header部分只有一行,包括三个字段:type(必需)、scope(可选)和 subject(必需)。

demo必须使用下面的方式 ** 每个类型必须以冒号":" 加空格" "格式,jira_id必须写在第一位

  • jira_id: DEV-111 修复了BUG (备注 必填项 否则代码将无法推送到gitlab仓库)

  • feat : 新增了功能

  • fix: 修复了BUG 

  • docs: 仅仅修改了文档

  • style: 修改了空格、格式缩进、逗号等,不改变代码逻辑

  • perf: 优化相关,比如提升性能、体验

  • test: 测试用例,比如单元测试、集成测试等

  • chore: 改变构建流程、或者增加依赖库、工具等

  • revert: 回滚到上一个版本

提交错误提示

未按照规范提交,将无法提交代码,并提示如下

# git commit -m "test 02"

⧗   input: test 02
✖   subject may not be empty [subject-empty]

✖   found 1 problems, 0 warnings
ⓘ   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint

husky - commit-msg hook exited with code 1 (error)

正确的提交:

# git commit -m "jira_id: test 02"


[master a7a2488] jira_id: test 02
 1 file changed, 1 insertion(+), 1 deletion(-)
 

参考资料:

https://blog.csdn.net/wei371522/article/details/84070803

https://commitlint.js.org/#/guides-local-setup?id=install-commitlint  推荐根据官网的安装脚本执行

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/466747
推荐阅读
相关标签
  

闽ICP备14008679号