Home | Ubuntu/Mint | Arch/Manjaro | ssh/gpg | texlive | python | git | steam

git

pre-commit hooks

To add pre-commit hooks following pre-commit.com↗, install pre-commit via pip3 install --user --no-cache-dir --upgrade --break-system-packages pre-commit, add a .pre-commit-config.yaml file, containing, e.g.,

# See https://pre-commit.com/ for documentation
# For installation:
# - install via
#   pip install pre-commit
# - activate via
#   pre-commit install
# Now the below hooks will run at any invocation of
#  git commit
# To check all files in the repositoy run
#  pre-commit run --all-files
exclude: (config_caddy|data_caddy|data_rustdesk|data_uptime_kuma)
repos:
- repo: meta # this repo checks pre-commit itself
  hooks:
  - id: check-hooks-apply # configured hooks apply to at least one file
  - id: check-useless-excludes # ensures exclude directives apply to at least one file
- repo: https://gitlab.com/vojko.pribudic.foss/pre-commit-update
  rev: v0.9.0
  hooks:
  - id: pre-commit-update
    args: [--dry-run]
- repo: https://github.com/pre-commit/pre-commit-hooks
  rev: v6.0.0
  hooks:
  - id: check-added-large-files
  - id: check-ast
  - id: check-builtin-literals
  - id: check-case-conflict
  - id: check-executables-have-shebangs
  # - id: check-json
  - id: check-merge-conflict
  - id: check-shebang-scripts-are-executable
  - id: check-toml
  # - id: check-xml
  - id: check-yaml
  - id: debug-statements
  - id: end-of-file-fixer
  - id: mixed-line-ending
  # - id: no-commit-to-branch
  # - id: pretty-format-json
  #   args: ["--autofix"]
  # - id: requirements-txt-fixer
  # - id: sort-simple-yaml
  - id: trailing-whitespace
    types: [text]
- repo: https://github.com/koalaman/shellcheck-precommit
  rev: v0.11.0
  hooks:
  - id: shellcheck
    # args: ["--severity=warning"] # Optionally only show errors and warnings
- repo: https://github.com/codespell-project/codespell
  rev: v2.4.3
  hooks:
  - id: codespell
    args: ['--skip', '*.csv,*.txt']
# - repo: https://github.com/rstcheck/rstcheck
#   rev: v6.2.4
#   hooks:
#   - id: rstcheck
#     args: ["--ignore-directives", "uml"]
#     additional_dependencies: ["rstcheck[sphinx]", "sphinxcontrib-plantuml"]
#     exclude: docs/_templates/
- repo: https://github.com/pre-commit/pygrep-hooks
  rev: v1.10.0
  hooks:
  - id: python-check-blanket-noqa
  - id: python-check-blanket-type-ignore
  - id: python-check-mock-methods
  - id: python-no-eval
  - id: python-no-log-warn
  - id: python-use-type-annotations
  # - id: rst-backticks
  # - id: rst-directive-colons
  # - id: rst-inline-touching-normal
  - id: text-unicode-replacement-char
# - repo: https://github.com/thclark/pre-commit-sphinx
#   rev: 0.0.3
#   hooks:
#   - id: build-docs
#     args: ['--html-dir', 'docs/_build/html', '--source-dir', 'docs/']
#     language_version: python3
#     additional_dependencies: [sphinx, sphinx-autoapi, furo, sphinxcontrib-plantuml]
- repo: https://github.com/hhatto/autopep8
  rev: v2.3.2
  hooks:
  - id: autopep8
- repo: https://github.com/PyCQA/bandit
  rev: 1.9.4
  hooks:
  - id: bandit
    args: ["-c", "pyproject.toml"]
    additional_dependencies: ["bandit[toml]"]
- repo: https://github.com/asottile/pyupgrade
  rev: v3.21.2
  hooks:
  - id: pyupgrade
    args: ["--py313-plus"]
- repo: https://github.com/psf/black-pre-commit-mirror
  rev: 26.5.1
  hooks:
  - id: black
    # It is recommended to specify the latest version of Python
    # supported by your project here, or alternatively use
    # pre-commit's default_language_version, see
    # https://pre-commit.com/#top_level-default_language_version
    language_version: python3.14
- repo: local
  hooks:
  - id: isort
    name: isort
    entry: isort
    language: system
    types: [python]
    args: ["--settings=pyproject.toml", "--line-length=120", "--profile=black", "--force-grid-wrap=2", "--multi-line=3"]
  - id: flake8
    name: flake8
    entry: flake8
    language: system
    types: [python]
    # E265 block comment should start with '# '
    # E266 too many leading '#' for block comment
    # E704 multiple statements on one line (def)
    # W503 line break before binary operator
    args: [
      "--config=setup.cfg", "--ignore=E265,E266,E704,W503,", "--max-line-length=120", "--exclude=.git,__pycache__",
      "--select=C,E,F,W,B,B9"
    ]
  - id: pycodestyle
    name: pycodestyle
    entry: pycodestyle
    language: system
    types: [python]
    # E203 whitespace before ‘,’, ‘;’, or ‘:’ - falsely triggers on list slices
    # E265 block comment should start with '# '
    # E266 too many leading '#' for block comment
    # E704 multiple statements on one line (def)
    # W503 line break before binary operator
    args: [
      "--config=setup.cfg", "--ignore=E203,E265,E266,E704,W503,", "--max-line-length=120", "--exclude=.git,__pycache__"
    ]
  - id: pydocstyle
    name: pydocstyle
    entry: pydocstyle
    language: system
    types: [python]
    args: ["--config=pyproject.toml", "--convention=numpy"]
  - id: sphinx-lint
    name: sphinx-lint
    entry: sphinx-lint
    language: system
  - id: pylint
    name: pylint
    entry: pylint
    language: system
    types: [python]
    # C0302 too many lines
    # C0325 superfluous-parens
    # R0801 duplicate-code
    # R0912 too-many-branches
    # R0913 too-many-arguments
    # R0914 too-many-locals
    # R0915 too-many-statements
    # W0511 fixme
    args: [
      "-rn", "-sn", "--rcfile=pyproject.toml", "--disable=C0302,C0325,R0801,R0912,R0913,R0914,R0915,W0511,",
      "--max-line-length=120", "--ignore=.git,__pycache__"
    ]
  - id: mypy
    name: mypy
    pass_filenames: false # suppress the normal filename passing
    entry: mypy .
    verbose: true
    language: system
    types: [python]
    args: ["--config-file=pyproject.toml", "--exclude=(.git|__pycache__|config_caddy|data_caddy|data_rustdesk|data_uptime_kuma)"]
  - id: pyright
    name: pyright
    entry: pyright .
    language: system
    types: [python]
- repo: https://github.com/astral-sh/ruff-pre-commit
  rev: v0.16.3
  hooks:
  - id: ruff-check
  - id: ruff-format
  

and, finally, activate via pre-commit install.

To run the hook(s) on all files of the repository, run pre-commit run --all-files or for individual file(s), run pre-commit run --file ....
Ensure to have the proper linters, etc. installed (see python).