Git хуков предварительной фиксации продолжает изменять файлы даже после того, как я поставил ранее измененные файлы - PullRequest
1 голос
/ 08 мая 2020

Я использую git предварительную фиксацию и использую черный как один из хуков.

Теперь, когда я запускаю 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, похоже, не обновил мои коммиты с черным форматированием. Я не смог ничего найти с помощью своего исследования в Google. Спасибо!

1 Ответ

3 голосов
/ 08 мая 2020

Похоже, вы используете black и double-quote-strings-fixer вместе

  • первый любит строки с двойными кавычками в python (вы можете отключить это, настроив черный на skip-string-normalization в pyproject.toml)
  • последнему нравятся строки с одинарными кавычками в python (вы можете удалить его, если хотите, чтобы строки с двойными кавычками)

Если два средства форматирования сражаются, конечный результат будет ошибкой, поскольку проверки перед фиксацией, чтобы убедиться, что все разрешено


отказ от ответственности: я автор pre-commit-hooks

...