【问题标题】:pre-commit: How to disable the check for unstaged files?预提交:如何禁用对未暂存文件的检查?
【发布时间】:2021-04-24 18:09:33
【问题描述】:

我的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")

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

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

【问题讨论】:

    标签: git pre-commit.com


    【解决方案1】:

    从您的输出中,您没有暂存任何文件以进行提交。所以git 本身阻止了你提交(不是pre-commit)——如果你真的不想提交任何东西,你可以使用git commit --allow-empty。如果要暂存一些文件以进行提交,请使用git add,或使用git commit -a

    请注意,这里的pre-commit 没有错误——只有一个警告,告诉您未暂存的内容已被暂时删除so that partial commits can be linted successfully


    免责声明:我是 pre-commit 的作者

    【讨论】:

    • 谢谢!我是瞎子。也许预提交在这里可能更明确(对于像我这样的愚蠢和盲目的用户)。
    • 我认为 pre-commit 不仅仅是一个简单的助手。为什么不使用它来设置在 pyhon 世界之外有要求的 python 开发环境?
    • 我不确定您具体要问什么:S -- 预提交适用于各种不同的编程语言(尽管是用 python 编写的并且在 python 中最流行)
    【解决方案2】:

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

    如果您要重复使用其他人的挂钩,那么您需要调查这些挂钩以确定他们是否可以选择性地禁用更改。

    【讨论】:

    • 您的回答是关于一般的预提交,但问题是关于 pre-commit.com 钩子。
    • 当然,这就是为什么我说您将不得不查看其他人提供的挂钩。它不是存在或默认的配置选项,也没有标准化的标志。如果您查看挂钩的代码,您现在就知道要查找什么了。
    • 你是对的“看代码”几乎总是一个答案(除了许多其他答案)
    猜你喜欢
    • 2019-05-06
    • 2015-01-09
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    • 2020-07-18
    • 2018-02-05
    • 2018-10-04
    • 2018-01-11
    相关资源
    最近更新 更多