git hooks 结合 clang-format 提交前自动格式化代码
ClangFormat
ClangFormat 描述了一组构建在 LibFormat 之上的工具。它可以以多种方式支持您的工作流,包括独立工具和编辑器集成。
其中独立的工具就是clang-format, 可用于格式化 c/c + +/Java/JavaScript/JSON/Objective-C/Protobuf/c # 代码。
clang-format的默认配置文件是.clang-format
或_clang-format
, 也可以通过clang-format -style=file
指定配置文件。
.clang-format
使用yaml格式,指定了如何对文件格式化的规则。
可以基于预定义的样式,快速创建.clang-format
文件。
LLVM
A style complying with the LLVM coding standardsGoogle
A style complying with Google’s C++ style guideChromium
A style complying with Chromium’s style guideMozilla
A style complying with Mozilla’s style guideWebKit
A style complying with WebKit’s style guideMicrosoft
A style complying with Microsoft’s style guideGNU
A style complying with the GNU coding standards
例如生成llvm风格的:
1 |
|
然后可以基于生成的.clang-format
文件修改参数,定制自己的样式。
更多参考: Clang-Format Style Options
mac 下安装 clang-format
1 |
|
将生成的.clang-format
放入工程目录,执行命令格式化文件
1 |
|
-i
格式化当前文件,并将格式化后的内容输出到当前文件。不加-i
会将格式化的内容输出到标准输出。
然后我们希望在提交的时候根据我们的配置,自动格式化代码,如何做呢?
可以通过git hooks 结合clang-format 在提交的时候自动格式化我们的代码。
pre-commit
A framework for managing and maintaining multi-language pre-commit hooks.
安装:
1 |
|
添加配置文件.pre-commit-config.yaml
,通过pre-commit sample-config
命令快速生成一个基本的配置文件。 更多关于文件中选项的配置,参考pre-commit
1 |
|
上面配置指定了如何去格式化python代码,pre commit 支持格式化其他的编程语言,参考
supported hooks查看所有支持的语言。
想结合clang-format对工程中的c++代码自动格式化。在supported hooks找到了GitHub - doublify/pre-commit-clang-format: ClangFormat hook for pre-commit
如何使用呢?
在工程中创建
.pre-commit-config.yaml
文件配置
.pre-commit-config.yaml
文件1
2
3
4
5repos:
- repo: https://github.com/doublify/pre-commit-clang-format.git
rev: 62302476d0da01515660132d76902359bed0f782
hooks:
- id: clang-format安装git hooks 脚本
1
2$ pre-commit install
pre-commit installed at .git/hooks/pre-commit(可选)手动调用pre commit,看看格式化后的效果
1 |
|
- 调用
git commit
, 会触发git hook,调用clang-format
自动化格式代码。