我是提交钩子(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/