Mar*_*ina 8 git clang pre-commit-hook githooks
我是提交钩子和 Clang 格式的新手,我正在尝试将两者结合起来。我设置了预提交挂钩,并且知道如何在命令行上运行 Clang 格式,但不确定如何将其添加到文件中。
这是我在命令行中运行的用于格式化的代码:
clang-format -i -style=llvm fileName
我也试图在所有提交提交的文件上运行它。 git diff --cached --name-only
这是我的pre-commit
文件:
hook_enabled=true
# Redirect output to stderr.
exec 1>&2
# If the hook is enabled and there are one or more files added to the commit run
# code formatting.
if [ "$hook_enabled" != "false" ] &&
test $(git diff --cached --name-only $against | wc -c) != 0
then
cat <<\EOF
Code formatting changed some files, please review and re-add files with git add
EOF
exit 1
Run Code Online (Sandbox Code Playgroud)
我还将 clang-formatting 添加到package.json
:
"pre-commit": "check-clang-format",
"format": "git-clang-format",
Run Code Online (Sandbox Code Playgroud)
请帮我整合 clang 格式。
Hen*_*ner 20
现在(最终)使用开源https://pre-commit.com(框架)变得非常简单:
repos:
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v14.0.6
hooks:
- id: clang-format
Run Code Online (Sandbox Code Playgroud)
它从 PyPI 获取适用于所有常见平台(Windows 64 和 32、macOS 通用、manylinux 64 和 32、arm、ppc 和 s390x)的 1-2 MB 二进制文件。您可以固定 clang-format 10、11 或 12(现在是 13、14 和各种补丁版本,通常是同一天发布!)。请参阅https://github.com/ssciwr/clang-format-wheel。如果您使用https://pre-commit.ci,您将获得自动更新 PR,并且您的 PR 会自动修复。
我将以下内容添加到我的REPO_ROOT/.git/hooks/pre-commit
文件顶部:
for FILE in $(git diff --cached --name-only)
do
clang-format -i $FILE
done
Run Code Online (Sandbox Code Playgroud)
该.clang-format
文件位于REPO_ROOT
.
另一个答案和对原始问题的第一条评论并没有说明为什么最好避免这种解决方案,所以我很高兴听到更多关于这一点的信息。
实际上,您不会在预提交挂钩处调用 clang 格式的二进制文件。
以下是如何在预提交挂钩上设置 clang 格式的说明: https: //github.com/andrewseidl/githook-clang-format
安装 首先,验证是否
clang-format
已安装。在 Linux 上,这应该包含在常规clang
软件包中。为了带有 Homebrew 的 MacOSX
clang-format
可通过brew install clang-format
.现在从此存储库安装
clang-format.hook
到您存储库的.git/hooks
. 如果您还没有预提交挂钩,则只需复制clang-format.hook
到.git/hooks/pre-commit
. 例如:
cp githook-clang-format/clang-format.hook myrepo/.git/hooks/pre-commit
用法 安装预提交挂钩后,
clang-format
将在运行时对提交中包含的每个文件运行git commit
。默认情况下,
clang-format
使用 LLVM 风格。.clang-format
要更改此设置,请在存储库的顶层创建一个具有所需格式的文件,或者hooks.clangformat.style
在存储库中设置配置选项。.clang-format
如果您将与团队合作或对样式进行任何重大自定义,则首选文件方法。
.clang-format
您可以使用以下命令从所需的样式(此处为 llvm)生成文件:
clang-format -style=llvm -dump-config > .clang-format
要使用该
git config
方法,请在您的存储库中执行以下操作:
git config hooks.clangformat.style llvm
归档时间: |
|
查看次数: |
10424 次 |
最近记录: |