【发布时间】:2020-08-23 11:11:37
【问题描述】:
我正在运行 git pre-commit 并运行黑色作为钩子之一。
现在当我运行commit 时,黑色失败并说:
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 -A或git 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 似乎没有用黑人格式更新我的提交。我无法通过我的谷歌研究找到任何东西。谢谢!
【问题讨论】:
-
嗯,你必须在提交文件之前添加文件。
git status --short说什么? -
让任何预提交挂钩尝试修改要提交的内容通常是不明智的。您正在使用的预提交包程序中包含特殊代码来尝试处理此问题,但在某些情况下它太难了 (
git commit --only) 并且它会出错。最好只使用一个预提交钩子来验证提交是否正常,如果不是,请告诉你你应该做什么来修复它,以避免这些极端情况。 -
也就是说,目前尚不清楚为什么您会看到您所看到的——但我自己没有使用过这个预提交包。也许重新格式化实际上并没有发生在您的工作树中。 (该软件包会小心地将 staged 文件提取到一个临时目录中,这是处理 Git 从其索引而不是您的工作树提交这一事实的唯一明智的方法。)
-
您能否提供更多信息和提交的输出?我猜你有两个钩子,它们不同意某些东西的格式(
isort和black也许?) -
嗨@MCI,我已在问题中添加了请求的详细信息。
标签: python git pre-commit pre-commit.com python-black