git规范代码提交格式:commitlint+husky安装

环境: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:

mkdir node.js // 新建目录

cd node.js // 进入目录里面

wget https://nodejs.org/dist/v10.9.0/node-v10.9.0-linux-x64.tar.xz  // 下载

tar xf  node-v10.9.0-linux-x64.tar.xz       // 解压

cd node-v10.9.0-linux-x64/   // 进入解压目录

node -v //查看版本号

解压文件的 bin 目录底下包含了 node、npm 等命令,我们可以使用 ln 命令来设置软连接:
ln -s /usr/software/nodejs/bin/npm   /usr/local/bin/ 
ln -s /usr/software/nodejs/bin/node   /usr/local/bin/

设置npm镜像为国内的
npm config set registry http://registry.npm.taobao.org

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

npm cache clean -f // 清楚node缓存

npm i -g n  //  安装node的版本管理工具n   或者 npm install -g n

升级node:
n stable   // 升级到最新稳定版 (建议)
n latest   // 升级到最新版本
n 版本号   // 升级到指定版本

如果当前版本没有变化的话,可以重新打开服务器再看一下



卸载旧版本的nodejs:
yum remove nodejs npm -y // 卸载npm

进入 /usr/local/lib 删除所有 node 和 node_modules文件夹
进入 /usr/local/include 删除所有 node 和 node_modules 文件夹
检查 ~ 文件夹里面的"local" “lib” “include” 文件夹,然后删除里面的所有 “node” 和 “node_modules” 文件夹可以使用以下命令查找 $ find ~/ -name node $ find ~/ -name node_modules

删除: /usr/local/bin/npm
删除: /usr/local/share/man/man1/node.1
删除: /usr/local/lib/dtrace/node.d
删除: rm -rf /home/[homedir]/.npm
删除: 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

第一步:初始化

npm init  // 初始化一些信息,执行完成后悔创建一个package.json的文件
//建议在执行的过程中,能填的信息都填上;

最后生成的文件如下:
{
  "name": "test001",
  "version": "1.0.1",
  "description": "test0001 description",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "http://xxxx:8001/atong/test001.git"
  },
  "author": "test001",
  "license": "ISC",
  "devDependencies": {
    "@commitlint/cli": "^13.1.0",
    "@commitlint/config-angular": "^13.1.0",
    "@commitlint/config-conventional": "^13.1.0",
    "husky": "^4.2.3"
  },
  "dependencies": {},
  "keywords": [
    "test001"
  ]
}
执行完可以在项目根目录看到package-lock.json,package.json文件

第二步:安装插件 commitlint

npm install --save-dev @commitlint/{cli,config-conventional}

echo "module.exports = { extends: ['@commitlint/config-conventional'] };" > commitlint.config.js

验证是否安装成功:

第一种验证方式:

$ echo "foo: bar"|npx commitlint
⧗ input: foo: bar
✖ type must be one of [jira_id, feature, update, fix, refactor, optimize, style, docs, chore] [type-enum]
 
✖ found 1 problems, 0 warnings
ⓘ Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint


第二种验证方式:

npx commitlint --from HEAD~1 --to HEAD --verbose

第三步:安装husky

建议不要使用默认latest版本安装 可能hooks会失效  这是官方的一个ISSUES


如已经安装并且不生效 请先删除.git/hooks文件夹
然后删除默认版本npm uninstall husky
再次安装即可npm install [email protected] --save-dev

安装:
npm install [email protected] --save-dev

npx husky add .husky/commit-msg 'npx --no-install commitlint --edit $1'

在package.json中增加husky配置:

"husky": {
    "hooks": {
      "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
    }
  }

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

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

/**
 * jira_id:关联jira的issue ID 用于关联动作 必填
 * feat:新功能
 * update:更新某功能
 * fixbug:修补某功能的bug
 * refactor:重构某个功能
 * optimize: 优化构建工具或运行时性能
 * style:仅样式改动
 * docs:仅文档新增/改动
 * chore:构建过程或辅助工具的变动
  
 */

module.exports = {
    extends: [
        '@commitlint/config-conventional'
    ],
    rules: {
        'type-enum': [
            2,           // 表示必须输入的
            'always', [
                'jira_id', 
                'feat', 
                'update', 
                'fix', 
                'refactor', 
                'optimize', 
                'style', 
                'docs', 
                'chore'
             ]
        ],
        'type-case': [0],
        'type-empty': [0],
        'scope-empty': [0],
        'scope-case': [0],
        'subject-full-stop': [0, 'never'],
        'subject-case': [0, 'never'],
        'header-max-length': [0, 'always', 72],
    }
};

最后的文件有:

-rw-r--r--   1 root root   490 Sep  3 17:49 commitlint.config.js
drwxr-xr-x 139 root root  4096 Sep  3 17:47 node_modules
-rw-r--r--   1 root root   656 Sep  3 17:47 package.json
-rw-r--r--   1 root root 58766 Sep  3 17:47 package-lock.json


其实还有两个隐藏文件:(.git文件)(.husky文件)
-rw-r--r--   1 root root   490 Sep  3 17:49 commitlint.config.js
drwxr-xr-x   8 root root  4096 Sep  3 20:25 .git
drwxr-xr-x   3 root root  4096 Sep  3 16:40 .husky
drwxr-xr-x 139 root root  4096 Sep  3 17:47 node_modules
-rw-r--r--   1 root root   656 Sep  3 17:47 package.json
-rw-r--r--   1 root root 58766 Sep  3 17:47 package-lock.json

.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  推荐根据官网的安装脚本执行

猜你喜欢

转载自blog.csdn.net/u010953880/article/details/120088694