|
| 1 | +%YAML 1.2 |
| 2 | +--- |
| 3 | +# Pre-commit Configuration |
| 4 | +# This file configures pre-commit hooks that run automatically before git commits |
| 5 | +# to ensure code quality, formatting, and prevent security issues. |
| 6 | +# https://pre-commit.com/ |
| 7 | +default_install_hook_types: |
| 8 | + - pre-commit |
| 9 | + - commit-msg |
| 10 | + |
| 11 | +repos: |
| 12 | + |
| 13 | + ######################################### |
| 14 | + # General Code Quality Checks |
| 15 | + ######################################### |
| 16 | + - repo: https://github.com/pre-commit/pre-commit-hooks |
| 17 | + # Standard collection of pre-commit hooks for various checks |
| 18 | + rev: v5.0.0 |
| 19 | + hooks: |
| 20 | + - id: trailing-whitespace |
| 21 | + name: "✂️ whitespace · Remove trailing whitespace" |
| 22 | + # Removes trailing whitespace at end of lines |
| 23 | + - id: check-ast |
| 24 | + name: "🔍 python · Check Python AST" |
| 25 | + # Validates Python syntax without executing code |
| 26 | + - id: end-of-file-fixer |
| 27 | + name: "📄 files · Fix end of files" |
| 28 | + # Ensures files end with a newline |
| 29 | + - id: check-yaml |
| 30 | + name: "📋 yaml · Validate YAML files" |
| 31 | + exclude: "mkdocs.yml" |
| 32 | + # Validates YAML syntax |
| 33 | + - id: check-toml |
| 34 | + name: "📋 toml · Validate TOML files" |
| 35 | + # Validates TOML syntax |
| 36 | + - id: check-json |
| 37 | + name: "📋 json · Validate JSON files" |
| 38 | + # Validates JSON syntax |
| 39 | + - id: check-added-large-files |
| 40 | + name: "📏 files · Check for added large files" |
| 41 | + # Prevents large files from being committed |
| 42 | + - id: detect-private-key |
| 43 | + name: "🔑 security · Detect private keys" |
| 44 | + # Prevents accidental commit of private keys |
| 45 | + - id: pretty-format-json |
| 46 | + name: "📋 json · Format JSON files" |
| 47 | + # Formats JSON files for readability |
| 48 | + - id: check-merge-conflict |
| 49 | + name: "🔀 git · Check for merge conflicts" |
| 50 | + - id: forbid-new-submodules |
| 51 | + name: "🌳 git · Prevent submodule creation" |
| 52 | + |
| 53 | + - id: no-commit-to-branch |
| 54 | + name: "🌳 git · Protect main branches" |
| 55 | + args: ["--branch", "main", "--branch", "master"] |
| 56 | + # Prevents direct commits to protected branches |
| 57 | + # Detects unresolved merge conflicts |
| 58 | + - id: check-executables-have-shebangs |
| 59 | + name: "📜 files · Check executables have shebangs" |
| 60 | + # Ensures executable files have proper shebangs |
| 61 | + - id: check-shebang-scripts-are-executable |
| 62 | + name: "📜 files · Check shebang scripts are executable" |
| 63 | + # Makes sure files with shebangs are executable |
| 64 | + - id: pretty-format-json |
| 65 | + name: "📋 json · Format JSON files" |
| 66 | + args: [--autofix, --no-sort-keys] # Fix formatting but don't sort keys |
| 67 | + - id: name-tests-test |
| 68 | + name: "🧪 tests · Check test file naming" |
| 69 | + args: [--pytest-test-first] # Enforces pytest naming conventions |
| 70 | + - id: mixed-line-ending |
| 71 | + name: "📄 files · Fix mixed line endings" |
| 72 | + args: ['--fix=lf'] # Standardize on LF line endings |
| 73 | + - id: check-case-conflict |
| 74 | + name: "📁 filesystem/📝 names · Check case sensitivity" |
| 75 | + - id: check-illegal-windows-names |
| 76 | + name: "📁 filesystem/📝 names · Validate Windows filenames" |
| 77 | + - id: check-symlinks |
| 78 | + name: "📁 filesystem/🔗 symlink · Check symlink validity" |
| 79 | + - id: destroyed-symlinks |
| 80 | + name: "📁 filesystem/🔗 symlink · Detect broken symlinks" |
| 81 | + |
| 82 | + ######################################### |
| 83 | + # Python Formatting and Linting |
| 84 | + ######################################### |
| 85 | + - repo: https://github.com/astral-sh/ruff-pre-commit |
| 86 | + # Ruff is a fast Python linter and formatter written in Rust |
| 87 | + rev: v0.11.2 |
| 88 | + hooks: |
| 89 | + - id: ruff |
| 90 | + name: "🐍 python · Format with Ruff" |
| 91 | + args: [--fix, --exit-non-zero-on-fix] # Automatically fix issues when possible |
| 92 | + - id: ruff-format |
| 93 | + name: "🐍 python · Format with Ruff" |
| 94 | + # Automatically formats Python code |
| 95 | + |
| 96 | + |
| 97 | + ######################################### |
| 98 | + # Python Code Modernization |
| 99 | + ######################################### |
| 100 | + - repo: https://github.com/asottile/pyupgrade |
| 101 | + # Automatically upgrades Python syntax to newer versions |
| 102 | + rev: v3.19.1 |
| 103 | + hooks: |
| 104 | + - id: pyupgrade |
| 105 | + name: "🔄 python · Upgrade Python syntax" |
| 106 | + args: ["--py311-plus"] # Use Python 3.11+ specific syntax improvements |
| 107 | + # Upgrades syntax to use newer Python features |
| 108 | + |
| 109 | + - repo: https://github.com/asottile/add-trailing-comma |
| 110 | + # Adds trailing commas to function calls and definitions |
| 111 | + rev: v3.1.0 |
| 112 | + hooks: |
| 113 | + - id: add-trailing-comma |
| 114 | + name: "🔤 python · Add trailing commas" |
| 115 | + # Makes future diffs cleaner and multiline constructs more consistent |
| 116 | + |
| 117 | + ######################################### |
| 118 | + # API Documentation Validation |
| 119 | + ######################################### |
| 120 | + - repo: https://github.com/python-openapi/openapi-spec-validator |
| 121 | + rev: 0.7.1 |
| 122 | + hooks: |
| 123 | + - id: openapi-spec-validator |
| 124 | + name: "📝 api · Validate OpenAPI Spec" |
| 125 | + # Ensures OpenAPI specifications are valid |
| 126 | + |
| 127 | + ######################################### |
| 128 | + # Python Project Configuration Validation |
| 129 | + ######################################### |
| 130 | + - repo: https://github.com/abravalheri/validate-pyproject |
| 131 | + # Validates pyproject.toml file format |
| 132 | + rev: v0.24.1 |
| 133 | + hooks: |
| 134 | + - id: validate-pyproject |
| 135 | + name: "🐍 python · Validate pyproject.toml" |
| 136 | + additional_dependencies: ["validate-pyproject-schema-store[all]"] |
| 137 | + # Ensures pyproject.toml follows PEP specifications |
| 138 | + |
| 139 | + ######################################### |
| 140 | + # Documentation Formatting |
| 141 | + ######################################### |
| 142 | + - repo: https://github.com/hukkin/mdformat |
| 143 | + # Markdown formatter for consistent documents |
| 144 | + rev: 0.7.22 |
| 145 | + hooks: |
| 146 | + - id: mdformat |
| 147 | + name: "📝 markdown · Format markdown" |
| 148 | + # Formats markdown files for consistency |
| 149 | + exclude: | |
| 150 | + (?x)^( |
| 151 | + .*\{\{.*\}\}.* |
| 152 | + )$ |
| 153 | + entry: mdformat |
| 154 | + language: python |
| 155 | + types: [markdown] |
| 156 | + verbose: true |
| 157 | + log_file: .logs/mdformat.log |
| 158 | + fail_fast: false |
| 159 | + files: \.(md|markdown)$ |
| 160 | + args: |
| 161 | + - "--wrap=no" # Wrap lines at 80 characters (customize as needed) |
| 162 | + - "--end-of-line=lf" # Use LF line endings (Unix-style, recommended for consistency) |
| 163 | + # - "--check" # Exit with error if formatting changes are needed |
| 164 | + additional_dependencies: |
| 165 | + - mdformat-gfm==0.4.1 # GitHub Flavored Markdown support |
| 166 | + - mdformat-mkdocs[recommended]==4.1.2 # MkDocs-specific formatting |
| 167 | + - mdformat-frontmatter==2.0.8 # YAML frontmatter support |
| 168 | + - mdformat-footnote==0.1.1 # Footnote support |
| 169 | + - mdformat-tables==1.0.0 # Table formatting |
| 170 | + - mdformat-toc==0.3.0 # Table of contents formatting |
| 171 | + - mdformat-beautysh==0.1.1 # Shell code block formatting |
| 172 | + - mdformat-black==0.1.1 # Python code block formatting |
| 173 | + - mdformat-gfm-alerts==1.0.1 # GitHub-style alerts |
| 174 | + - mdformat-myst==0.2.1 # MyST Markdown support |
| 175 | + - mdformat-config==0.2.1 # Configuration file parsing |
| 176 | + - mdformat-ruff==0.1.3 # Ruff linting for Python code blocks |
| 177 | + - mdformat-web==0.2.0 # Web-related Markdown formatting |
| 178 | + - mdformat-simple-breaks==0.0.1 # Consistent line break handling |
| 179 | + - ruff |
| 180 | + |
| 181 | + ######################################### |
| 182 | + - repo: https://github.com/DavidAnson/markdownlint-cli2 |
| 183 | + rev: v0.17.2 |
| 184 | + hooks: |
| 185 | + - id: markdownlint-cli2 |
| 186 | + name: "📝 markdown · Lint markdown with markdownlint" |
| 187 | + args: [--fix] |
| 188 | + exclude: ^CHANGELOG |
| 189 | + # Lints markdown files for style and consistency issues |
| 190 | + |
| 191 | + ######################################### |
| 192 | + # YAML File Validation |
| 193 | + ######################################### |
| 194 | + - repo: https://github.com/adrienverge/yamllint.git |
| 195 | + rev: v1.37.0 |
| 196 | + hooks: |
| 197 | + - id: yamllint |
| 198 | + name: "📋 yaml · Lint YAML files with yamllint" |
| 199 | + args: [--strict, -c=./.yamllint.yaml] |
| 200 | + # Validates YAML files for style and syntax issues |
| 201 | + |
| 202 | + ######################################### |
| 203 | + # A pre-commit hook to format and lint TOML files |
| 204 | + ######################################### |
| 205 | + - repo: https://github.com/ComPWA/taplo-pre-commit |
| 206 | + rev: v0.9.3 |
| 207 | + hooks: |
| 208 | + - id: taplo-format |
| 209 | + name: "📋 toml · Format TOML files with taplo" |
| 210 | + # Formats TOML files for consistency |
| 211 | + - id: taplo-lint |
| 212 | + name: "📋 toml · Lint TOML files with taplo" |
| 213 | + # Validates TOML files for errors and style issues |
| 214 | + |
| 215 | + ######################################### |
| 216 | + # Makefile Validation |
| 217 | + ######################################### |
| 218 | + - repo: https://github.com/mrtazz/checkmake.git |
| 219 | + # Makefile linter |
| 220 | + rev: 0.2.2 |
| 221 | + hooks: |
| 222 | + - id: checkmake |
| 223 | + name: "🐮 Makefile · Lint Makefile" |
| 224 | + # Validates Makefile structure and conventions |
| 225 | + # No clear documentation on how to skip certain checks. |
| 226 | + # Fixed as many as I could, but best option is to ignore the file. |
| 227 | + exclude: | |
| 228 | + (?x)^( |
| 229 | + .*\{\{.*\}\}.*| |
| 230 | + )$ |
| 231 | +
|
| 232 | + ######################################### |
| 233 | + # Scanning Hardcoded Secrets |
| 234 | + ######################################### |
| 235 | + - repo: https://github.com/gitleaks/gitleaks |
| 236 | + rev: v8.24.2 |
| 237 | + hooks: |
| 238 | + - id: gitleaks |
| 239 | + name: "🔒 security · Scan for hardcoded secrets" |
| 240 | + # Detects and prevents secrets from being committed |
| 241 | + |
| 242 | +# Set default Python version for all hooks |
| 243 | +default_language_version: |
| 244 | + python: python3.11 |
| 245 | + |
| 246 | +# Define which git hooks these run on (defaults to pre-commit) |
| 247 | +default_stages: [pre-commit] |
0 commit comments