代码检查 pre-commit如何使用

pre-commit顾名思义是在git提交代码前用于代码检查的,话不多说,直接入正题:

1.首先安装工具

pip install pre-commit

2.安装完成后我们需要新建一个配置文件命名为.pre-commit-config.yaml并且放到项目代码的根目录


 

repos:
  - repo: https://github.com/asottile/pyupgrade
    rev: v2.19.4
    hooks:
      - id: pyupgrade
        args: [ "--py38-plus" ]
  - repo: https://github.com/pre-commit/mirrors-isort
    rev: 1ba6bfc # Use the revision sha / tag you want to point at
    hooks:
      - id: isort
        args: ["--profile", "black"]
  - repo: https://github.com/psf/black
    rev: 21.12b0
    hooks:
      - id: black
  - repo: https://gitlab.com/pycqa/flake8
    rev: 4.0.1
    hooks:
      - id: flake8
        language_version: python3
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.0.1
    hooks:
      - id: check-docstring-first
      - id: check-json
      - id: check-merge-conflict
      - id: check-yaml
      - id: debug-statements
      - id: end-of-file-fixer
      - id: trailing-whitespace
      - id: requirements-txt-fixer
  - repo: https://github.com/pre-commit/mirrors-pylint
    rev: 56b3cb4
    hooks:
      - id: pylint
        args:
          - --max-line-length=120
          - --ignore-imports=yes
          - -d duplicate-code
  - repo: https://github.com/pre-commit/pygrep-hooks
    rev: v1.9.0
    hooks:
      - id: python-check-mock-methods
      - id: python-use-type-annotations
      - id: python-check-blanket-noqa
      - id: python-use-type-annotations
      - id: text-unicode-replacement-char
  - repo: https://github.com/pre-commit/mirrors-mypy
    rev: 9feadeb
    hooks:
      - id: mypy
        exclude: ^tests/
        args:
          [
              --disallow-untyped-defs,
              --check-untyped-defs,
              --warn-redundant-casts,
              --no-implicit-optional,
              --strict-optional
          ]

或者参考这里

GitHub - pre-commit/pre-commit-hooks: Some out-of-the-box hooks for pre-commitSome out-of-the-box hooks for pre-commit. Contribute to pre-commit/pre-commit-hooks development by creating an account on GitHub.https://github.com/pre-commit/pre-commit-hooks

pre-commithttps://pre-commit.com/hooks.html

等下在运行的时候项目会按照以上定义的配置把我们的代码修改规范。

4. 执行git commit 则会自动检查代码,自动修改,不用手动逐个定位+修改,非常方便。

你可能感兴趣的:(github,git)