Git pre-commit钩子即使我之前已经暂存了修改过的文件,仍会修改文件。

13

我正在运行 git pre-commit,并将 black 作为其中一个钩子运行。

现在当我运行 commit 时,black 失败并显示:

All done! ✨  ✨
15 files reformatted, 1 file left unchanged.

我检查了重新格式化的文件并对它们满意。所以我将这些文件放入了暂存区并尝试再次运行commit,但我仍然收到与上面相同的消息。我尝试过以下命令,但都没有成功。

git add .
git add -A
git add -u

这是我的.pre-commit-config.yaml文件:

repos:
    -   repo: https://github.com/psf/black
        rev: 19.10b0
        hooks:
            - id: black
              language_version: python3.6
    -   repo: https://github.com/pre-commit/pre-commit-hooks
        rev: v2.5.0
        hooks:
            -   id: check-merge-conflict
            -   id: check-docstring-first
            -   id: check-json
            -   id: check-yaml
            -   id: debug-statements
            -   id: double-quote-string-fixer
            -   id: end-of-file-fixer
            -   id: name-tests-test
                args: [--django]
            -   id: requirements-txt-fixer
            -   id: trailing-whitespace
    -   repo: https://gitlab.com/pycqa/flake8
        rev: 3.7.9
        hooks:
            -   id: flake8
                additional_dependencies: [flake8-typing-imports==1.6.0]
    - repo: https://github.com/asottile/reorder_python_imports
      rev: v1.4.0
      hooks:
            -   id: reorder-python-imports
                args: [--py3-plus]
    -   repo: https://github.com/Lucas-C/pre-commit-hooks-bandit
        rev: v1.0.4
        hooks:
            -   id: python-bandit-vulnerability-check
                args: [-l, --recursive, -x, tests]
                files: .py$
    -   repo: local
        hooks:
            -   id: tests
                name: run tests
                entry: venv/bin/pytest -v -m fast
                language: python
                additional_dependencies: [pre-commit, pytest]
                always_run: true
                pass_filenames: false
                types: [python]
                stages: [commit]
    -   repo: local
        hooks:
            -   id: tests
                name: run tests
                entry: venv/bin/pytest -x
                language: system
                types: [python]
                stages: [push]

当我执行 git status --short 命令时,我会得到以下结果:
M  .pre-commit-config.yaml
M  pytest.ini
M  setup.cfg
RM tests/tests_report.html -> tests/commit_pytest_report.html
R  report.html -> tests/commit_tests_report.html
AM tests/coverage/index.html
A  tests/coverage/file_1.png

当我运行git commit -m "test"命令时,在运行git add .git add -Agit add -u命令后出现以下情况:
black....................................................................Failed
    - hook id: black
    - files were modified by this hook

reformatted <filename>
...
All done! ✨  ✨
15 files reformatted, 1 file left unchanged.

Check for merge conflicts................................................Passed
Check docstring is first.................................................Passed
Check JSON...............................................................Passed
Check Yaml...............................................................Passed
Debug Statements (Python)................................................Passed
Fix double quoted strings................................................Failed
- hook id: double-quote-string-fixer
- exit code: 1
- files were modified by this hook

Fixing strings in <file_name>
...

Fix End of Files.........................................................Failed
- hook id: end-of-file-fixer
- exit code: 1
- files were modified by this hook

Fixing <file_name>
...

Tests should end in _test.py.............................................Passed
Fix requirements.txt.................................(no files to check)Skipped
Trim Trailing Whitespace.................................................Passed
flake8...................................................................Failed
- hook id: flake8
- exit code: 1

<file_name>: <some flake8 error>
...

Reorder python imports...................................................Passed
bandit...................................................................Passed
run tests................................................................Failed
- hook id: tests
- files were modified by this hook

============================= test session starts ==============================
platform darwin -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1

<test details>

(0.00 durations hidden.  Use -vv to show these durations.)
====================== 2 passed, 113 deselected in 2.51s =======================

我不确定自己做错了什么,git似乎没有使用blacks格式更新我的提交。我在Google上找不到任何有用的信息。谢谢!


嗯,你必须在提交之前添加文件。git status --short 显示了什么? - MCI
话虽如此,不清楚为什么你看到了你所看到的情况——但我自己没有使用过这个预提交包。也许重新格式化根本没有在你的工作树中实际发生。(该软件包会将暂存文件精心提取到临时目录中,这是处理Git从其索引而不是从你的工作树提交的事实的唯一明智方法。) - torek
你能提供更多信息和提交的输出吗?我猜你有两个钩子在某些格式上存在分歧(也许是isortblack?) - anthony sottile
嗨@MCI,我已经在问题中添加了所请求的细节。 - rrlamichhane
嗨@torek,我已经在问题中添加了所请求的细节。 - rrlamichhane
显示剩余2条评论
1个回答

23

看起来你正在同时使用 blackdouble-quote-strings-fixer

  • black 更喜欢在 Python 中使用双引号字符串(您可以通过在 pyproject.toml 中配置 skip-string-normalization 来禁用此功能)
  • double-quote-strings-fixer 更喜欢在 Python 中使用单引号字符串(如果您想要双引号字符串,则可以将其删除)

如果两个格式化程序冲突,最终结果将是失败的,因为 pre-commit 将检查确保一切都解决了。


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


Pre-commit(这里指程序,而不是Git钩子)在这方面非常聪明,可以自动检测到更改的文件。我建议在这种情况下可能需要更详细的信息:例如,让它说一些类似于“格式化程序X和Y都试图更改<file>,我们无法决定该怎么做”,等等。 - torek
太好了!是的,那就是问题所在,我误解了“double-quote-strings-fixer”的作用,以为它会强制使用双引号,因为这是我经常使用的方式。感谢您的回复@AnthonySottile。 - rrlamichhane
1
@torek 是啊,那样会很好,但不幸的是很难知道发生了什么变化,并且这两件事情不一致——我一直在努力改进这个问题,但目前它只是以不透明的方式处理差异。 - anthony sottile

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