Guardy
🪝 Hooks

✨ Features

Advanced hook features and configuration patterns

Basic Commands

Run any shell command:

hooks:
  pre-commit:
    custom:
      - command: "npm run lint"
        description: "Run ESLint"

Template Variables

Use variables in commands:

VariableDescriptionExample
{staged_files}Space-separated list of staged filessrc/main.rs src/lib.rs
{all_files}All modified filesREADME.md src/main.rs
{commit_msg_file}Path to commit message file.git/COMMIT_EDITMSG
{changed_files}Files changed in commitsrc/app.js test/app.test.js
hooks:
  pre-commit:
    custom:
      - command: "eslint {staged_files} --fix"
        description: "Fix ESLint issues"
        glob: ["*.{js,jsx,ts,tsx}"]
        stage_fixed: true

Glob Patterns

Only run commands on matching files:

hooks:
  pre-commit:
    custom:
      - command: "black {staged_files}"
        description: "Format Python files"
        glob: ["*.py", "**/*.py"]

      - command: "rustfmt {staged_files}"
        description: "Format Rust files"
        glob: ["*.rs"]

Stage Fixed Files

Automatically stage files modified by the hook:

hooks:
  pre-commit:
    custom:
      - command: "prettier --write {staged_files}"
        description: "Format with Prettier"
        glob: ["*.{js,json,md}"]
        stage_fixed: true  # Auto-stage formatted files

Parallel Execution

Run multiple commands simultaneously:

hooks:
  pre-commit:
    parallel: true  # Enable parallel execution
    custom:
      - command: "cargo fmt -- --check"
        description: "Check Rust formatting"
      - command: "cargo clippy"
        description: "Run Clippy"
      - command: "cargo test"
        description: "Run tests"

Conditional Execution

Only run when files change:

hooks:
  post-checkout:
    custom:
      - command: "pnpm install"
        description: "Install dependencies"
        when: "{changed_files} contains package.json"

Custom Messages

hooks:
  pre-commit:
    custom:
      - command: "cargo test"
        description: "Running tests"
        success_message: "✅ All tests passed!"
        failure_message: "❌ Tests failed. Fix before committing."

Environment Variables

Hooks have access to Git environment variables:

  • GIT_AUTHOR_NAME - Commit author name
  • GIT_AUTHOR_EMAIL - Commit author email
  • GIT_COMMITTER_NAME - Committer name
  • GIT_COMMITTER_EMAIL - Committer email

Performance Tips

Use Parallel Execution

Enable parallel: true for independent checks:

hooks:
  pre-commit:
    parallel: true  # Runs all commands simultaneously

Target Specific Files

Use glob patterns to avoid checking irrelevant files:

custom:
  - command: "eslint {staged_files}"
    glob: ["src/**/*.{js,jsx}"]  # Only lint source files

Cache Results

Some tools support caching:

custom:
  - command: "eslint {staged_files} --cache"
    description: "Lint with cache"