agentleFS
Cogni-AI-OUCogni-AI-OU/cogni-ai-agent-skills/pre-commit/SKILL.md

pre-commit

Skill4 starsChanged 7 months agoMITView on GitHub

Guide for using pre-commit hooks to validate code formatting, linting, and security checks before commits. You MUST load this skill when using pre-commit hooks for validation.

13 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use license, coverage and 2 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • PradnyeshpSkill

    license

    Add or update an open-source LICENSE file for the project, filling in the right SPDX license, copyright holder, and year, and optionally inserting license headers into source files. Use when the user says "add a license", "what license should I use", "add MIT/Apache license", "add license headers", or "set up licensing".

    Also in 3 projects that use this

  • PydanticSkill

    coverage

    Fetch coverage diff from Codecov for the current branch or a specific PR. Shows uncovered lines, patch coverage, and overall coverage change.

    Also in 3 projects that use this

  • dragonflydbSkill

    benchmark

    Benchmark Dragonfly (and compare against Valkey/Redis) on local or remote cloud instances, then produce a performance + memory report with charts and a raw-data appendix. Use this whenever the user runs /benchmark, or asks to benchmark / load-test / measure throughput, QPS, latency, or memory efficiency of Dragonfly or another Redis-compatible server, to compare Dragonfly vs Valkey or Redis, or to generate a benchmark report. Trigger it even when the user just describes the goal ("see how fast Dragonfly is on this box", "compare memory per key vs Valkey") without saying the word "benchmark".

    Also in 3 projects that use this

  • SentrySkill

    onboard

    Onboard a service to sentry-options. Use when integrating a new service, creating schemas, or setting up the multi-repo sentry-options workflow.

    Also in 3 projects that use this

The file

We split this skill into the rules it gives an agent

11 rules. 0 are common, 6 are distinctive, and 5 are very specific to this project.

  • 0

    Common

    Most of the 13 edited versions we read keep these.

  • 6

    Distinctive

    This author's own choices. Worth a look.

  • 5

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Skip only when necessary: Use --no-verify or SKIP=<hook-id> sparingly.

    line 30
  2. Distinctive:

    When writing complex, multi-file integration tests that require a full environment boot (pre-commit is for fast, isolated, file-level …

    line 35
  3. Very specific:

    If the repository has explicitly opted out of pre-commit in favor of a different localized linting orchestration tool (like npm run lint).

    line 36
  4. Very specific:

    Custom config: Create .gitleaks.toml in repository root to:

    line 121
  5. Distinctive:

    Add custom secret patterns

    line 123
  6. Very specific:

    Baseline file: Create .secrets.baseline to store known false positives:

    line 149
  7. Very specific:

    Custom config: Create .j2lint.toml or .j2lint.yaml for custom linting rules (rarely needed).

    line 161
  8. Distinctive:

    Create script in repository:

    line 191
  9. Distinctive:

    Create script with uv shebang:

    line 226
  10. Distinctive:

    Document dependencies: If script requires specific tools, document in script comments or repo README.

    line 276
  11. Distinctive:

    YAML formatting conflicts: Run yamllint before yamlfix to catch syntax errors first:

    line 327

“Common” is measured: kept by at least 6 in 10 of the 13 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Pre-commit

<!-- markdownlint-disable MD013 -->

Expert guide for pre-commit framework usage, configuration, and custom hook development. Focus on autonomous validation, diagnostics, and fix workflows.

When to Use

  • Analyzing why pre-commit validation blocked a user's local git commit.
  • Running mass syntax/lint checks on the repository proactively.
  • User needs to configure, update, or bypass a pre-commit hook (markdownlint, yamllint, etc.).

Core Principles

  • Staged files only: Pre-commit runs exclusively on staged (cached) files, not the entire working directory.
  • Fast feedback: Catch issues before commit/push to reduce CI failures and review cycles.
  • Configuration-driven: Hooks reference external config files (.yamllint, .markdownlint.yaml, etc.) for consistency.
  • Idempotent fixes: Many hooks auto-fix issues (e.g., trailing-whitespace, end-of-file-fixer); re-stage and re-run to pass.
  • Skip only when necessary: Use --no-verify or SKIP=<hook-id> sparingly; prefer fixing issues over bypassing validation.

When Not to Use

  • When writing complex, multi-file integration tests that require a full environment boot (pre-commit is for fast, isolated, file-level checks).
  • If the repository has explicitly opted out of pre-commit in favor of a different localized linting orchestration tool (like npm run lint).
  • For enforcing subjective architectural patterns that cannot be statically analyzed.

Common Pitfalls

  • Bypassing the Cache: Running pre-commit run on unstaged files and wondering why the linter didn't catch the errors (it only checks the git index by default).
  • Auto-Fix Loops: Forgetting to git add the files that pre-commit auto-formatted (like trailing whitespace fixes), causing the hook to fail repeatedly.
  • Mismatched Python Versions: Running a local Python hook that requires >=3.11 on a system with an older default Python, causing the hook script to crash.

Testing Commands

Run all hooks on all files (recommended before committing):

pre-commit run --all-files

Run all hooks on staged files only (default behavior):

pre-commit run

Run specific hook on all files:

pre-commit run <hook-id> --all-files

Run specific hook on staged files:

pre-commit run <hook-id>

Examples for individual linters:

pre-commit run yamllint --all-files          # YAML linting
pre-commit run markdownlint --all-files      # Markdown linting
pre-commit run flake8 --all-files            # Python linting
pre-commit run black --all-files             # Python formatting
pre-commit run ansible-lint --all-files      # Ansible linting
pre-commit run actionlint --all-files        # GitHub Actions workflow linting
pre-commit run gitleaks --all-files          # Secret scanning
pre-commit run detect-secrets --all-files    # Alternative secret detection
pre-commit run trailing-whitespace -a        # Fix trailing spaces (auto-fix)
pre-commit run end-of-file-fixer -a          # Fix EOF newlines (auto-fix)

Run hook on specific file:

pre-commit run <hook-id> --files <file-path>

Configuration Dependencies

Pre-commit hooks reference external dotfiles for linting rules. Key dependencies in this repository:

Hook IDConfig FilePurposeNotes
yamllint.yamllintYAML linting rulesMax line length: 120, enforces consistent formatting
markdownlint.markdownlint.yamlMarkdown style rulesMax line length: 120, allows consistent heading styles
yamlfix.yamlfix.tomlYAML auto-formattingLine length: 110, forces block-style sequences
flake8CLI args in .pre-commit-config.yamlPython lintingMax line length: 120 (via --max-line-length=120)
blackDefaultsPython code formattingUses Black's default line length (88)
ansible-lintEmbedded defaultsAnsible best practicesNo custom config file in this repo
actionlintEmbedded defaultsGitHub Actions validationNo custom config file in this repo
gitleaksOptional .gitleaks.tomlSecret scanning patternsCan override default rules (see below)
detect-secretsOptional .secrets.baselineSecret detection baselineCan store known false positives
All hooks.editorconfigCross-editor consistencyIndent size: 2 for YAML/JSON, 4 for others; LF line endings

Why trailing-whitespace excludes YAML: The trailing-whitespace hook in the repository's .pre-commit-config.yaml excludes \.ya?ml$ (YAML files) because yamllint handles trailing spaces for YAML files with warning-level enforcement, avoiding duplicate checks.

Optional Configuration Files

Some hooks support optional config files to customize behavior beyond defaults:

gitleaks

Default: Uses embedded ruleset for secret detection (API keys, passwords, tokens).

Custom config: Create .gitleaks.toml in repository root to:

  • Add custom secret patterns
  • Exclude specific paths or files
  • Allowlist known false positives

Example .gitleaks.toml:

[extend]
useDefault = true

[[rules]]
id = "custom-api-key"
description = "Custom API Key Pattern"
regex = '''MYAPI-[A-Z0-9]{32}'''

[allowlist]
paths = [
  '''.*test.*''',
  '''.*example.*'''
]

detect-secrets

Default: Scans all files for high-entropy strings and common secret patterns.

Baseline file: Create .secrets.baseline to store known false positives:

detect-secrets scan > .secrets.baseline

Future runs compare against baseline; only new secrets trigger failures.

j2lint

Default: Validates Jinja2 template syntax using embedded rules.

Custom config: Create .j2lint.toml or .j2lint.yaml for custom linting rules (rarely needed).

Update Process

Update all hooks to latest versions:

pre-commit autoupdate

This updates .pre-commit-config.yaml with the newest rev for each repo. Always review diff and test after updating:

pre-commit run --all-files

Update specific hook:

pre-commit autoupdate --repo https://github.com/<owner>/<repo>

Freeze at specific version: Manually edit .pre-commit-config.yaml and pin rev to a specific tag/commit.

Creating Custom Local Hooks

Pre-commit supports custom hooks via local repository scripts. Add to .pre-commit-config.yaml:

Example: Local Shell Script Hook

  1. Create script in repository:
#!/usr/bin/env bash
# File: .github/scripts/check-copyright.sh
set -euo pipefail

for file in "$@"; do
  if ! grep -q "Copyright $(date +%Y)" "$file"; then
    echo "Missing copyright header in $file" >&2
    exit 1
  fi
done
  1. Make executable:
chmod +x .github/scripts/check-copyright.sh
  1. Add to .pre-commit-config.yaml:
- repo: local
  hooks:
    - id: check-copyright
      name: Verify copyright headers
      entry: .github/scripts/check-copyright.sh
      language: system
      types: [python]

Example: Local Python Script Hook

  1. Create script with uv shebang:
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.11"
# dependencies = [
#     "pyyaml",
# ]
# ///
import sys
import yaml

def validate_yaml_keys(filepath):
    with open(filepath) as f:
        data = yaml.safe_load(f)
        if 'required_key' not in data:
            print(f"Missing required_key in {filepath}", file=sys.stderr)
            return False
    return True

if __name__ == '__main__':
    all_valid = all(validate_yaml_keys(f) for f in sys.argv[1:])
    sys.exit(0 if all_valid else 1)
  1. Make executable:
chmod +x .github/scripts/validate-yaml-keys.py
  1. Add to .pre-commit-config.yaml:
- repo: local
  hooks:
    - id: validate-yaml-keys
      name: Check YAML required keys
      entry: .github/scripts/validate-yaml-keys.py
      language: system
      types: [yaml]

Local Hook Best Practices

  • Use language: system for scripts that require executables in PATH (bash, python, etc.).
  • Pass filenames as args: Pre-commit passes matching files as arguments to entry command.
  • Exit codes matter: Exit 0 for success, non-zero for failure.
  • Keep scripts idempotent: Hook may run multiple times; avoid side effects.
  • Document dependencies: If script requires specific tools, document in script comments or repo README.

Diagnostics and Troubleshooting

Hook fails on specific file:

pre-commit run <hook-id> --files <file-path> --verbose

Skip hooks temporarily (commit without validation):

git commit --no-verify -m "message"

Skip specific hook (run all except one):

SKIP=<hook-id> git commit -m "message"

Check pre-commit installation and config:

pre-commit --version
pre-commit validate-config

Clean pre-commit cache (fixes stale hook errors):

pre-commit clean
pre-commit install --install-hooks

Debug hook execution:

pre-commit run <hook-id> --verbose --all-files

Common Failure Patterns

Auto-fixable hooks modified files: Re-stage and re-run:

git add -u
pre-commit run

YAML formatting conflicts: Run yamllint before yamlfix to catch syntax errors first:

pre-commit run yamllint --all-files
pre-commit run yamlfix --all-files

Markdown line length violations: Reflow paragraphs manually or use editor formatter (VS Code: "Format Document").

Secret detection false positives:

  • For gitleaks: Add to .gitleaks.toml allowlist.
  • For detect-secrets: Update .secrets.baseline with detect-secrets scan --baseline .secrets.baseline.

What to Avoid

  • Committing with --no-verify without documenting reason.
  • Modifying .pre-commit-config.yaml without testing changes locally.
  • Bypassing hooks to "save time" — fixing issues post-merge costs more.
  • Running hooks on untracked files (pre-commit ignores them; stage first).

Limitations

  • Pre-commit only validates staged files; unstaged changes are ignored.
  • Hooks run sequentially; large repos may experience slower commit times.
  • Some hooks (e.g., yamlfix) may conflict with manual formatting preferences.
  • Custom local hooks require executable scripts in repository; not portable across forks without dependencies.

Related Skills

  • tdd: You MUST load this skill when executing test tasks, designing tests, doing TDD, or verifying system behavior.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.