我在C++项目中使用这个钩子(2.20.0版本):
repos:
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v14.0.6
hooks:
- id: clang-format
我只是从几个不同的..cpp/.h/cmakelist文件中各放了几行。当我尝试提交这些更改时,从预提交中得到以下错误:
[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to C:\Users\tyler.shellberg\.cache\pre-commit\patch1665600217-21836.
clang-format.............................................................Failed
- hook id: clang-format
- files were modified by this hook
[WARNING] Stashed changes conflicted with hook auto-fixes... Rolling back fixes...
[INFO] Restored changes from C:\Users\tyler.shellberg\.cache\pre-commit\patch1665600217-21836.
我对此感到困惑。预提交不允许我部分提交文件,或者在提交时有未分阶段的更改吗?
所有的文件,无论是阶段性的还是非阶段性的,都是用clang-格式格式化的.我已经手动检查过了。
为清晰而编辑:
如果我对每个文件(已执行和未执行)运行pre-commit run --files [filename]
,则所有报告都会返回“传递”或“跳过”(对于非cpp文件)。如果所有文件都通过了,为什么会出现问题?
发布于 2022-10-12 19:00:37
输出的第一部分是:
[WARNING] Unstaged files detected.
这意味着您进行了部分阶段的提交--这很好,pre-commit
很好地支持这一点。
在那之后,一个钩子做了改变:
- files were modified by this hook
这些更改肯定与未分阶段的部分相冲突,因为接下来会有如下消息:
[WARNING] Stashed changes conflicted with hook auto-fixes... Rolling back fixes...
这意味着您需要确保未分阶段的部分被正确地格式化(pre-commit run --files whatever.cpp
),也需要对它们进行分级,以便它们也被格式化。
免责声明:我创建了预提交。
发布于 2022-10-12 20:25:23
如果确实存在一些没有进行的局部处理,您可以尝试以下解决方法:
# stash your modifications, but keep your index as is:
git stash --keep
git commit
# get back the content of your repo as it was before you ran `git stash`,
# without trying to fix any conflicts
git restore -s=stash .
# *warning* : using `git restore` to restore the worktree is one of those few
# destructive commands, that is allowed to discard changes and delete files
# from your disk without them being stored in git (a lot like
# 'git reset --hard').
# In the current situation, there shouldn't be any uncommitted changes, though.
# you shouldn't need that stash anymore
git stash drop
https://stackoverflow.com/questions/74046718
复制相似问题