git - 如何将 clang-formatting 添加到预提交 Hook ?

标签 git clang githooks pre-commit-hook

我是提交钩子(Hook)和 Clang 格式的新手,我正在尝试将两者结合起来。我已经设置了预提交 Hook ,并且我知道如何在命令行上运行 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

我还将 clang 格式添加到 package.json :
    "pre-commit": "check-clang-format",
    "format": "git-clang-format",

请帮我整合 clang-format 。

最佳答案

我将以下内容添加到我的 REPO_ROOT/.git/hooks/pre-commit 的顶部文件:

for FILE in $(git diff --cached --name-only)
do
        clang-format -i $FILE
done
.clang-format文件放在REPO_ROOT .

原始问题的另一个答案和第一条评论并没有说明为什么最好避免这种解决方案,所以我很高兴听到更多关于这个的信息。

关于git - 如何将 clang-formatting 添加到预提交 Hook ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55965712/

相关文章:

GIT:更改了具有特定扩展名的文件名

Git squash 更改被远程存储库拒绝

c++ - 在 mac 上的 gnu/clang 编译器中定义相对于可执行文件的框架路径

git - 在预 merge 提交 Hook 中获取 MERGE_HEAD

git - 在 bash 脚本中执行 git 命令

Git GPG 签名失败,没有明确消息

Git 分支没有像我期望的那样工作

c++ - 使用多个框架给出 clang : error: linker command failed with exit code 1

c++ - 默认情况下,clang 中是否有一组已知的 `c++11` 功能不需要 `-std=c++11` ?