Go 项目 pre-commit 最佳配置实践
pre-commit 介绍
pre-commit 是一个支持多语言的 pre-commit-hooks 框架,pre-commit 允许你自定义一组 hooks,可以是一个开源的代码格式化工具,也可以是一个自定义脚本,用于帮助开发者在代码提交阶段就可以自动完成一些代码格式化的工作和基本的检查。
pre-commit 安装
Using pip:
pip install pre-commit
Using homebrew:
brew install pre-commit
安装成功之后在终端输入 pre-commit --version
, 如果正常输出,则说明安装成功。
~ pre-commit --version
pre-commit 3.7.0
安装常用的 go 代码检查工具
本次 pre-commit 一共启用了五款 golang hooks 工具,再使用 pre-commit 之前,需要确保电脑已经安装了这些 tools。
golangci-lint
golangci-lint 是一个快速的 Go 代码静态检查工具。它能够并行运行多个代码静态检查工具,利用缓存机制提高效率,支持 YAML 配置文件,与主流的集成开发环境(IDE)相互配合,内置了100多个代码静态检查工具。
安装:
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.58.1
go-cyclo
go-cyclo 是一个用于检测 go 代码圈复杂的工具.
安装:
go install github.com/fzipp/gocyclo/cmd/gocyclo@latest
go-fmt
go-fmt 是一款可以自动格式化 go 代码的工具
无需安装,默认是带的。
go-imports
go-imports 是一个处理 go 引用的工具
安装:
go install golang.org/x/tools/cmd/goimports@latest
go-unit-tests
go-unit-tests 是一个运行 go 单元测试的工具
无需安装
在项目下添加配置文件
在项目根目录下新建一个 .pre-commit-config.yaml
配置文件,并添加以下内容,该配置文件定义了一些额外的代码检查,包括空格检查,git commit message 检查....
default_stages: [commit]
fail_fast: true
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.1.0
hooks:
- id: check-merge-conflict
- id: trailing-whitespace
- repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
rev: v9.16.0
hooks:
- id: commitlint
stages: [ commit-msg ]
additional_dependencies: [ '@commitlint/config-conventional' ]
- repo: https://github.com/dnephin/pre-commit-golang
rev: v0.5.1
hooks:
- id: golangci-lint
- id: go-fmt
- id: go-cyclo
args: [ -over=15 ]
- id: go-imports
- id: go-unit-tests
- repo: local
hooks:
- id: Run Test
name: Run Test
entry: sh scripts/run_test.sh
language: system
run_test.sh 脚本可以根据各个项目灵活定义,也可以添加一些额外的脚本,例如敏感信息检查 等。
除了上述的常用的 hooks 之外, pre-commit 还有非常多其他的 hooks 供大家选择,传送门
使用 pre-commit 来管理 git commit 规范
在项目根目录下创建 commitlint.config.js
文件,该文件用于定义 git commit 的规范。
const Configuration = {
/*
* Resolve and load @commitlint/config-conventional from node_modules. * Referenced packages must be installed */ extends: [
'@commitlint/config-conventional'
],
/*
* Resolve and load conventional-changelog-atom from node_modules. * Referenced packages must be installed */ formatter: '@commitlint/format',
/*
* Any rules defined here will override rules from @commitlint/config-conventional */ rules: {
'type-enum': [
2,
'always',
[
'feat',
'fix',
'docs',
'style',
'refactor',
'test',
'perf',
'chore',
],
]
},
/*
* Functions that return true if commitlint should ignore the given message. */ ignores: [
(commit) => commit === '',
(message) => message.includes('Merge'),
(message) => message.includes('merge')
],
/*
* Whether commitlint uses the default ignore rules. */ defaultIgnores: true,
/*
* Custom URL to show upon failure */ helpUrl:
'https://github.com/conventional-changelog/commitlint/#what-is-commitlint',
/*
* Custom prompt configs */ prompt: {
messages: {},
questions: {
type: {
description: 'please input type:',
},
},
},
};
module.exports = Configuration;
该文件参考 AngularJS 规范,详情链接: docs.google.com/document/d/…
git commit -m '标记: 提交的概要注释'
示例:
git commit -m 'fix: 修复部署状态页面进程创建时间展示异常问题'
标记说明
标记 | 说明 |
---|---|
feat | 新功能/特性开发 |
fix | 修复存在的 bug |
docs | 添加/修改文档类内容 |
style | 修改注释,按代码规范格式化等 |
refactor | 代码重构,架构调整 |
perf | 优化配置、参数、逻辑或功能 |
test | 添加/修改单元测试用例 |
chore | 调整构建脚本、任务等 |
在当前项目启动 pre-commit
使用终端进入项目根目录,安装 pre-commit hooks:
pre-commit install
第一次提交时需要一定的时间安装相关的 hooks,当 goland git console 出现以下提示时,则说明配置完成。
之后在每一次提交代码之前,pro-commit 都会运行对应的hooks,如果发现某个 hooks 异常,例如圈复杂度太高,语法错误等,pre-commit 阻止这次提交。
其他Tips:
如何豁免掉某个函数的 golangci-lint 检查
答: 在函数头添加 nolint 即可
// nolint
func Get() *log.Logger {
if _logger != nil {
return _logger
}
return log.GlobalLogger()
}
转载自:https://juejin.cn/post/7369519814639829007