pre-commit

Introduction

pre-commit is better githooks that are generic across projects, used to check yaml, markdown, run linters, and make sure the code is in good shape before making a commit and pushing it, or just running it in CI.

Configurations

File goes in root: .pre-commit.config.yaml

Base file

# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.3.0
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer
      - id: check-yaml
      - id: check-toml
      - id: check-added-large-files

Rust file

# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v3.2.0
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer
      - id: check-yaml
      - id: check-toml
      - id: forbid-new-submodules
      - id: check-added-large-files
        args: ["--maxkb=2000"]
  - repo: https://github.com/doublify/pre-commit-rust
    rev: v1.0
    hooks:
      - id: fmt
      - id: clippy
        args: ["--", "-D", "clippy::pedantic"]
  - repo: https://github.com/hadolint/hadolint
    rev: v2.10.0
    hooks:
      - id: hadolint

One liners

# Initialize a repo
pre-commit sample-config | tee .pre-commit-config.yaml && pre-commit autoupdate

References

Documentation Hooks