Skip to content

pre-commit

约 1285 字大约 4 分钟

2025-01-21

pre-commit

git hooks pre-commit接入校验及修复。

  • 安装pre-commit
pip3 install pre-commit
  • 配置pre-commit,项目根目录下创建.pre-commit-config.yaml文件
repos:
  - repo: https://github.com/pycqa/isort
    rev: 5.13.2
    hooks:
      - id: isort
  - repo: https://github.com/pre-commit/mirrors-autopep8
    rev: 2.1.0
    hooks:
      - id: autopep8
        args: [--max-line-length=120]
  - repo: https://gitlab.com/pycqa/flake8
    rev: 7.0.0
    hooks:
      - id: flake8
        args:
            - --max-line-length=120

要使用 pre-commit 工具配置 autopep8,以在提交代码前自动格式化 Python 代码以符合 PEP 8 规范,您需要按照以下步骤操作:

最后,如下的.yaml配置才通过pre-commit autoupdate下载成功。开始有两个问题: 第一个就是autopep8的版本,通过运行pre-commit autoupdate的最新版本是2.0.4,只能写成'v2.0.4'才能成功。 第二个问题就是flake8的地址,需要使用https://github.com/PyCQA/flake8,如果使用https://gitlab.com/pycqa/flake8在下载时需要输入密码。

repos:
  - repo: https://github.com/pycqa/isort
    rev: 5.13.2
    hooks:
      - id: isort
  - repo: https://github.com/pre-commit/mirrors-autopep8
    rev: 'v2.0.4'
    hooks:
      - id: autopep8
        args: [--max-line-length=120]
  - repo: https://github.com/PyCQA/flake8
    rev: 7.0.0
    hooks:
      - id: flake8
        args:
            - --max-line-length=120
pre-commit autoupdate
[https://github.com/pycqa/isort] already up to date!
[https://github.com/pre-commit/mirrors-autopep8] already up to date!
[https://github.com/PyCQA/flake8] already up to date!

配置好了之后,提交通过了测试,期间有配置报错,最后发现是注释不能和内容放在同一行。

.isort.cfg配置:

[settings]
line_length = 120

flake8配置:

[flake8]
# 忽略 E711 错误
ignore = E711
# 设置最大行长度为 120 字符
max-line-length = 120
# 检查所有类型的错误和警告
select = E, W, F, C
# 在忽略列表中添加更多特定错误
# F841 未使用的变量
extend-ignore = F841
# 排除以下目录或文件
exclude = 
    .venv/
    build/
    dist/
    docs/_build/
    *.log
    migrations/*.py

只简单设置了几个。

注意如果编辑器也有相关配置,那么配置需要一样才行,不然编辑后保存的和提交时检查的格式不一样,那就不会通过。

$ git commit -m 'feat: - 测试pre-commit'
isort....................................................................Passed
autopep8.................................................................Passed
flake8...................................................................Passed
[main 1a8c2e3] feat: - 测试pre-commit
 Committer: 唐道勇 <matias@tangdaoyongdeMacBook-Pro.local>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 4 files changed, 16 insertions(+), 13 deletions(-)

-------LLM回答------

步骤 1:安装 pre-commit

确保您已经在本地机器上安装了 pre-commit。如果没有,可以使用 pip 安装:

pip install pre-commit

步骤 2:在项目根目录下创建 .pre-commit-config.yaml 文件

在您的项目根目录下创建一个名为 .pre-commit-config.yaml 的文件。这个文件用于定义 pre-commit 需要执行的各种钩子(hooks)。添加以下内容以配置 autopep8:

repos:
  - repo: https://github.com/pre-commit/mirrors-autopep8
    rev: 'v<latest_version>'  # 替换为所需的 autopep8 版本
    hooks:
      - id: autopep8
        args: [--in-place, --aggressive, --aggressive]  # 可选:自定义 autopep8 参数

这里写了版本号后,提示有问题:

[INFO] Initializing environment for https://github.com/pre-commit/mirrors-autopep8.
An unexpected error has occurred: CalledProcessError: command: ('/Applications/Xcode.app/Contents/Developer/usr/libexec/git-core/git', 'checkout', 'v2.1.0')
return code: 1
stdout: (none)
stderr:
    error: pathspec 'v2.1.0' did not match any file(s) known to git
Check the log at /Users/matias/.cache/pre-commit/pre-commit.log

后面直接写了个空字符。 github上的提示:

repos:
-   repo: https://github.com/hhatto/autopep8
    rev: ...  # select the tag or revision you want, or run `pre-commit autoupdate`
    hooks:
    -   id: autopep8

pre-commit autoupdate 命令用于自动更新 .pre-commit-config.yaml 文件中指定的所有钩子到最新版本。这个命令并不直接提供一个进度条或百分比来显示更新的进度,因为它主要是一个串行过程,逐个更新钩子。

这里的 rev 字段指定了要使用的 autopep8 版本,您可以替换 <latest_version> 为实际的版本号(例如 v1.6.0)。访问 autopep8 GitHub releases 页面 查看并选择合适的版本。

args 字段是可选的,用于指定传递给 autopep8 命令的额外参数。示例中的参数会使 autopep8 以最激进的模式自动修复代码格式问题。您可以根据团队的偏好调整这些参数。

步骤 3:安装钩子

在项目根目录下运行以下命令,让 pre-commit 安装并启用刚刚配置的 autopep8 钩子:

$ pre-commit install
pre-commit installed at .git/hooks/pre-commit

步骤 4:测试 pre-commit 钩子

现在,每当您尝试提交代码时,pre-commit 将自动运行配置好的 autopep8,对即将提交的 Python 文件进行格式化。要验证配置是否生效,您可以尝试提交一些含有 PEP 8 违规的代码:

git add .
git commit -m "Test pre-commit with autopep8"

如果一切正常,autopep8 应该会自动格式化违规代码,并显示一个消息告知您哪些文件被修改。如果您之前配置了 --in-place 参数,这些改动会直接应用到您的工作目录中。确认无误后,再次执行 git commit 命令完成提交。

至此,您已经在项目中成功配置了 pre-commit 以使用 autopep8 自动格式化 Python 代码。每次提交前,autopep8 都会确保代码符合 PEP 8 规范,有助于保持代码库的一致性和可读性。

贡献者: matiastang