预提交:如何禁用未暂存文件的检查?

7

我的pre-commit.com配置如下:

repos:
-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v3.2.0
    hooks:
    -   id: trailing-whitespace
    -   id: end-of-file-fixer
    -   id: check-yaml
    -   id: check-added-large-files
-   repo: https://gitlab.com/pycqa/flake8
    rev: ''  # pick a git hash / tag to point to
    hooks:
    -   id: flake8
-   repo: https://github.com/pre-commit/mirrors-isort
    rev: v5.7.0
    hooks:
    - id: isort

如果我尝试提交,就会出现这个错误:

guettli@yoga15:~/projects/foo$ LANG=C git commit

[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to /home/guettli/.cache/pre-commit/patch1611141286.
Trim Trailing Whitespace.............................(no files to check)Skipped
Fix End of Files.....................................(no files to check)Skipped
Check Yaml...........................................(no files to check)Skipped
Check for added large files..........................(no files to check)Skipped
flake8...............................................(no files to check)Skipped
isort................................................(no files to check)Skipped
[INFO] Restored changes from /home/guettli/.cache/pre-commit/patch1611141286.

On branch feature/super-foo
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   .gitignore

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    .pre-commit-config.yaml

no changes added to commit (use "git add" and/or "git commit -a")

我希望能够避免检查未暂存的文件。我知道自己在做什么,如果我只想提交一些更改(不是全部),那么我想这样做。

如何禁用对未暂存文件的检查?

2个回答

9
从你的输出来看,你没有将任何文件暂存以供提交。因此,是git本身阻止了你提交(而不是pre-commit)——如果你确实想要提交空内容,可以使用git commit --allow-empty。如果你想要为提交暂存一些文件,请使用git add或者使用git commit -a
请注意,这里没有任何pre-commit的错误提示,只有一个警告提示你未暂存的内容被临时删除以便部分提交可以成功地进行代码检查
免责声明:我是pre-commit的作者。

2
谢谢!我真是太糊涂了。也许 pre-commit 可以在这里更明确一些(对于像我这样的愚蠢和糊涂的用户)。 - guettli
我认为pre-commit不仅仅是一个简单的助手。为什么不将其用于设置具有超出Python世界要求的Python开发环境呢? - guettli
我不确定你具体在问什么 :S -- pre-commit 适用于各种不同的编程语言(尽管它是用 Python 编写的,并且在 Python 中最受欢迎)。 - anthony sottile
1
@AnthonySottile 有没有办法禁用 pre-commit 自动暂存任何未暂存的更改?我希望我的 Git 消息能正确反映发生了什么变化,这真的很烦人。 - demberto
@demberto pre-commit永远不会触及暂存区--所以你可能有一些流氓程序在运行git add - anthony sottile
@AnthonySottile 我不知道,也许是 VS Code 本身的问题,但是在命令行中使用 git --no-verify 运行不会导致这种行为(即不会调用 pre-commit 钩子)。 - demberto

-1

预提交钩子将使用一种迭代变更集中文件的方式。可能有类似于git ls-files的东西在其中。如果您使用git ls-files -c,那么它只会显示缓存的更改(默认值)--如果您使用git ls-files -o,则还会显示未提交的(其他)文件。

如果您正在重用其他人的钩子,则需要调查这些钩子以确定它们是否可以选择性地禁用更改。


你的回答是关于一般的pre-commit,但问题是关于pre-commit.com钩子的。 - guettli
1
当然,这就是为什么我说你需要查看别人提供的钩子。它不是一个已经存在或默认配置选项,并且没有标准化的标志。如果您查看钩子的代码,现在您知道要寻找什么。 - AlBlue
你是对的,“看代码”几乎总是一个答案(除了其他很多答案)。 - guettli

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接