吐槽
本来是为了规范git commit的编写,研究了一下Commit message。大多数博文推荐使用Commitizen,而其安装需要借助npm。
在Ubuntu16.04下,依照各贴文的讲解,先装npm再装Commitizen踩了一堆的坑,包括但不限于:
- SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode (版本问题)
- Error: EACCES: permission denied (sudo权限问题)
- “npm install” error: Missing required argument #1(版本问题)
- /usr/bin/env: node: No such file or directory (软链接问题)
折腾了一上午,简直搞到头大。最后找到源码地址,按照官方教程一步一步操作,才算解决了上述问题。
安装npm
- 下载源码(我下载Linux二进制文件(x64)时的版本:node-v12.18.2-linux-x64.tar.xz)
- 解压压缩包,并复制到新建的文件夹
/usr/local/lib/nodejs
(VERSION和DISTRO为你下载时的对应版本号和分发号)
# VERSION=v12.18.2
# DISTRO=linux-x64
$sudo mkdir -p /usr/local/lib/nodejs
$sudo tar -xJvf node-$VERSION-$DISTRO.tar.xz -C /usr/local/lib/nodejs
- 设置环境变量
sudo gedit ~/.profile
,在文档末尾处添加
# Nodejs VERSION=v12.18.2 DISTRO=linux-x64
export PATH=/usr/local/lib/nodejs/node-$VERSION-$DISTRO/bin:$PATH
- 刷新一下
$. ~/.profile
- 检验是否安装正确
$node -v
v12.18.2
$npm version
{
npm: '6.14.5',
ares: '1.16.0',
brotli: '1.0.7',
cldr: '37.0',
http_parser: '2.9.3',
icu: '67.1',
llhttp: '2.0.4',
modules: '72',
napi: '6',
nghttp2: '1.41.0',
node: '12.18.2',
openssl: '1.1.1g',
tz: '2019c',
unicode: '13.0',
uv: '1.38.0',
v8: '7.8.279.23-node.39',
zlib: '1.2.11'
}
$npx -v
6.14.5
- 输出正常后,链接
npm
、node
和npx
到/usr/bin
(注意替换$VERSION和$DISTRO)
$sudo ln -s /usr/local/lib/nodejs/node-$VERSION-$DISTRO/bin/node /usr/bin/node
$sudo ln -s /usr/local/lib/nodejs/node-$VERSION-$DISTRO/bin/npm /usr/bin/npm
$sudo ln -s /usr/local/lib/nodejs/node-$VERSION-$DISTRO/bin/npx /usr/bin/npx
安装Commitizen
- 若上面一切顺利,则可以安装Commitizen了
$sudo npm install -g commitizen
- cd到代码工程的目录里,初始化支持 Angular 的 Commit message 格式
$commitizen init cz-conventional-changelog --save --save-exact
- 如果提示
Error: ENOENT: no such file or directory, open '/home/ProjectXXX/package.json'
,则需要npm init
产生package.json
文件,文件内容按需修改。再进行第8步的操作。 - 以后可以用
git cz
命令来取代git commit
了。
Git commit message规范
这里采用的是Angular风格的Commit message,包括由空行隔开的三个独立部分:Header,Body 和 Footer,即
Header
(blank line)
Body
(blank line)
Footer
这三部分可以进一步细化:
- <type>[optional scope]: <description>
- [optional Body]
- [optional Footer(s)]
Header为必需项,包括type,scope和description三部分:
type-表明所提交更改的类型
scope-可选项,表明更改的影响范围
description-祈使句式简述本次的更改,不超过50字符,句首字母大写,无句号结尾
type仅限从下列7种类型中选择:
- feat: The new feature you’re adding to a particular application(添加新的功能)
- fix: A bug fix(消除bug)
- style: Feature and updates related to styling(更正格式、缺失的标点等等。不涉及代码修改)
- refactor: Refactoring a specific section of the codebase(产品代码的重构。不涉及添新功能、去除bug)
- test: Everything related to testing(测试代码的添加与重构。不涉及产品代码)
- docs: Everything related to documentation(文档修改)
- chore: Regular code maintenance.(更新编译文件、参数配置文件等等。不涉及产品代码)
Body为可选项,在需要给出修改的动机,或更详细些的介绍时添加。解释 what和why,而非how。不超过72字符。
Footer为可选项,指出更改引起的后续影响,如声明大改,对应一个关闭的issue,感谢贡献者等
参考:
- https://www.cnblogs.com/daysme/p/7722474.html
- https://nitayneeman.com/posts/understanding-semantic-commit-messages-using-git-and-angular/#the-header
- https://udacity.github.io/git-styleguide/