我使用pylint 1.6.4和git-pylint-commit-hook 2.1.1 在预提交时整理我的文件。我还使用alembic来生成我的迁移,它们存储在<project-root>/migrations/versions
.
问题是生成的迁移无效,pylint 会为它们生成警告。我可以使用 --ignore=migrations 忽略迁移。(无论如何,Pylint 都会默认忽略它们,因为迁移不是 Python 模块,它只是一个目录)。但是 git-pylint-commit-hook 调用带有更改文件列表的 pylint 进行验证。如果你给它一个文件名列表,而不是模块,pylint 不会检查文件是否应该被忽略。
当有新的迁移要提交时,这会导致 pre-commit 钩子失败。
Running pylint on migrations/versions/d1f0e08ea6d2_skill_table.py (file 2/13).. 8.6/10.00 FAILED
************* Module d1f0e08ea6d2_skill_table.py
C: 5, 0: Trailing whitespace (trailing-whitespace)
C: 1, 0: Invalid module name "d1f0e08ea6d2_skill_table" (invalid-name)
C: 16, 0: Import "from alembic import op" should be placed at the top of the module (wrong-import-position)
C: 17, 0: Import "import sqlalchemy as sa" should …
Run Code Online (Sandbox Code Playgroud) 是否有办法准确指定git-hook
使用时要跳过哪些内容--no-verify
?--no-verify
或者除了完成此任务之外还有其他标志吗?也许只是一个只能跳过的标志pre-commit
?
我有两个经常使用的钩子,pre-commit
和commit-msg
。pre-commit
运行我的 linting、流程检查和一些单元测试。commit-msg
将我的分支名称附加到提交消息的末尾。
有时我很想跑,--no-verify
但pre-commit
还是想跑commit-msg
。
似乎有很多与此类似的 SO 帖子,但没有什么比选择性跳过--no-verify
.
我刚刚开始使用钩子,并且在使用 git 提交时简单地运行脚本遇到了一些麻烦。当从 bash 终端运行时,该脚本完全按照预期工作
bash pre-commit
Run Code Online (Sandbox Code Playgroud)
命令。但用 git 提交时它根本不运行。
该文件位于C:/path/to/my/project/.git/hooks/pre-commit,文件内容为:
#!/bin/bash
cd ../
cd ../
echo "pre-commit start"
if py -m unittest discover 2>&1 | grep -q "FAILED"; then
echo "Test(s) FAILED"
exit 1
fi
echo "pre-commit end"
Run Code Online (Sandbox Code Playgroud)
我已经尝试在终端中运行 chmod 命令,正如其他帖子中所建议的那样,没有任何更改。
chmod +x pre-commit
Run Code Online (Sandbox Code Playgroud) 我在安装预提交挂钩时遇到了障碍。根据下面的错误,挂钩安装在目录中找不到 python exe C:\\\\Users\\\\dangler\\\\.cache\\\\pre-commit\\\\repoith5dg7x\\\\py_env-default\\\\Scripts\\\\python.EXE
(该文件夹中不存在 python.EXE...应该吗?)。我\xe2\x80\x99在过去的几个小时里一直在尝试解决它,但没有成功。希望有一个解决方案,也许我已经搜索了错误的地方。
(fiddle-env) PS C:\\Users\\dangler\\Documents\\projects\\fiddle> pre-commit clean\n(fiddle-env) PS C:\\Users\\dangler\\Documents\\projects\\fiddle> pre-commit install\n(fiddle-env) PS C:\\Users\\dangler\\Documents\\projects\\fiddle> git commit -am "README file with links"\n[INFO] Installing environment for https://github.com/pre-commit/pre-commit-hooks.\n[INFO] Once installed this environment will be reused.\n[INFO] This may take a few minutes...\nAn unexpected error has occurred: CalledProcessError: command: ('C:\\\\Users\\\\dangler\\\\.cache\\\\pre-commit\\\\repookmqanwy\\\\py_env-default\\\\Scripts\\\\python.EXE', '-mpip', 'install', '.')\nreturn code: 3221225781\nexpected return code: 0\nstdout: (none)\nstderr: (none)\nCheck the log at C:\\Users\\dangler\\.cache\\pre-commit\\pre-commit.log\n
Run Code Online (Sandbox Code Playgroud)\n该.pre-commit-config.yml
文件是这样的:
repos:\n- repo: https://github.com/pre-commit/pre-commit-hooks\n rev: v2.4.0\n hooks:\n - id: double-quote-string-fixer\n - id: check-added-large-files\n …
Run Code Online (Sandbox Code Playgroud) 我正在尝试通过预提交来自动化我的测试和代码静态分析。我.pre-commit-config.yaml
的如下所示:
# .
# .
# .
- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v0.910'
hooks:
- id: mypy
args: [--no-strict-optional, --ignore-missing-imports]
exclude: "[a-zA-Z]*/[a-zA-Z]*/(migrations)/(.)*"
additional_dependencies: [
'tokenize-rt,
'djangorestframework-stubs',
'django-stubs',
]
Run Code Online (Sandbox Code Playgroud)
是pyproject.toml
:
[tool.mypy]
python_version = "3.8"
plugins = ["mypy_django_plugin.main", "mypy_drf_plugin.main"]
[mypy.plugins.django-stubs]
django_settings_module = "api.shop.shop.settings"
Run Code Online (Sandbox Code Playgroud)
当我运行时pre-commit run --all-files
出现以下错误:
Error constructing plugin instance of NewSemanalDjangoPlugin
Traceback (most recent call last):
File "/home/alipqb/.cache/pre-commit/repo94ds7xs0/py_env-python3.8/bin/mypy", line 8, in <module>
sys.exit(console_entry())
File "/home/alipqb/.cache/pre-commit/repo94ds7xs0/py_env-python3.8/lib/python3.8/site-packages/mypy/__main__.py", line 11, in console_entry
main(None, sys.stdout, sys.stderr)
File "mypy/main.py", line 87, …
Run Code Online (Sandbox Code Playgroud) python django pre-commit django-rest-framework pre-commit.com
我有一些 python 代码格式化程序作为 git 预提交挂钩,并且我已将我的 python 版本更改为
brew list | grep python
python@3.7
python@3.9
brew unlink python@3.7
brew unlink python@3.9
brew link python@3.7
python -V
Python 3.7.9
Run Code Online (Sandbox Code Playgroud)
并且知道似乎有些东西被破坏了,并且在 git commit 上我得到了env: python3.9: No such file or directory
,那么是什么env
?以及我如何编辑它以使用python@3.7
?
毒物:https: //tox.wiki/en/latest/
我想了解这两种选择的界限。
我知道预提交会创建一个 py 环境 - 与 tox 相同。对我来说,他们的架构看起来有点相似。
有些人将它们结合使用...... pre-commit 不能做什么,tox 可以?我看到了一些示例,其中通过 CI 管道期间的预提交,您可以运行单元测试等。
哪一个最适合集成到 CI 构建中?
我在运行“pre-commit run --all-files”时收到以下错误,并且在尝试提交时出现以下错误,这是一些 python 错误,使用的 python 版本是“Python 2.7.16”。
\n[INFO] Installing environment for https://github.com/pre-commit/mirrors-mypy.\n[INFO] Once installed this environment will be reused.\n[INFO] This may take a few minutes...\nAn unexpected error has occurred: CalledProcessError: command: ('/Users/user/.cache/pre-commit/repoq_mv31g8/py_env-default/bin/python', '-mpip', 'install', '.', 'tokenize-rt==3.2.0', 'pydantic')\nreturn code: 1\nexpected return code: 0\nstdout:\n Processing /Users/user/.cache/pre-commit/repoq_mv31g8\n Preparing metadata (setup.py): started\n Preparing metadata (setup.py): finished with status 'done'\n Collecting tokenize-rt==3.2.0\n Using cached tokenize_rt-3.2.0-py2.py3-none-any.whl (6.1 kB)\n Collecting pydantic\n Using cached pydantic-1.9.0-cp310-cp310-macosx_11_0_arm64.whl (2.4 MB)\n Collecting mypy==0.782\n Using cached mypy-0.782-py3-none-any.whl (2.4 MB)\n Collecting mypy-extensions<0.5.0,>=0.4.3\n …
Run Code Online (Sandbox Code Playgroud) 我最近从我的环境中卸载了预提交。我在 pipelinev 中执行了以下操作:
pipenv --rm
<deleted Pipfile and Pipfile.lock>
pipenv install -r requirements.txt
Run Code Online (Sandbox Code Playgroud)
我确保预提交模块不再位于requirements.txt 中。当我进行 git 提交时,我得到:
~/my_project/.venv/bin/python: No module named pre_commit
Run Code Online (Sandbox Code Playgroud)
这阻止了我提交,而且我不知道这是从哪里来的,因为未安装预提交。此外,指定的回溯路径指向 python 而不是 python3。我缺少什么?
我在这里使用 Python 3.10.4,并在使用 pyproject.toml 的 Poetry 1.1.13 项目中使用 .pre-commit-config.yaml 文件
这是我的 .pre-commit-config.yaml 的样子
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
# /sf/ask/4545491891/
default_language_version:
python: python3.10
default_stages: [commit, push]
repos:
- repo: local
hooks:
# https://github.com/pre-commit/pre-commit-hooks#check-ast
- id: check-ast
description: Simply checks whether the files parse as valid python.
entry: check-ast
name: Check python syntax
language: system
types: [python]
# https://github.com/pre-commit/pre-commit-hooks#check-added-large-files
- id: check-added-large-files
description: prevents giant files from being committed.
entry: check-added-large-files
name: Check added …
Run Code Online (Sandbox Code Playgroud)