Get the FREE Ultimate OpenClaw Setup Guide →

ln-742-precommit-setup

npx machina-cli add skill levnikolaevich/claude-code-skills/ln-742-precommit-setup --openclaw
Files (1)
SKILL.md
6.1 KB

Paths: File paths (shared/, references/, ../ln-*) are relative to skills repo root. If not found at CWD, locate this SKILL.md directory and go up one level for repo root.

ln-742-precommit-setup

Type: L3 Worker Category: 7XX Project Bootstrap Parent: ln-740-quality-setup

Sets up Git hooks for automated code quality enforcement before commits.


Purpose & Scope

Does:

  • Installs hook management tools (Husky or pre-commit)
  • Configures staged file linting (lint-staged or pre-commit hooks)
  • Sets up commit message validation (commitlint)
  • Verifies hooks trigger correctly

Does NOT:

  • Configure linters themselves (ln-741 does this)
  • Set up test infrastructure (ln-743 does this)
  • Modify source code

Supported Stacks

TechnologyHook ManagerStaged LintingCommit Validation
Node.jsHuskylint-stagedcommitlint
Pythonpre-commitpre-commit hookspre-commit hook
MixedBoth (if needed)Stack-specificcommitlint

Phase 1: Check Existing Hooks

Before installing, check for existing hook configurations.

Files to Check:

ToolIndicators
Husky.husky/ directory, husky in package.json
pre-commit.pre-commit-config.yaml
lint-stagedlint-staged in package.json or .lintstagedrc*
commitlintcommitlint.config.*, .commitlintrc*

Decision Logic:

  1. If hooks exist and working: SKIP (inform user)
  2. If partial setup: ASK user to complete or replace
  3. If no hooks: CREATE from templates

Phase 2: Install Hook Manager

Node.js Projects (Husky)

npm install -D husky
npx husky init

This creates:

  • .husky/ directory
  • .husky/pre-commit hook file
  • Adds prepare script to package.json

Python Projects (pre-commit)

pip install pre-commit
# OR with uv:
uv add --dev pre-commit

pre-commit install

This creates:

  • .git/hooks/pre-commit (managed by pre-commit)
  • Requires .pre-commit-config.yaml for configuration

Phase 3: Configure Staged Linting

Node.js (lint-staged)

npm install -D lint-staged

Create configuration (.lintstagedrc.mjs or in package.json):

Key Rules:

  • TypeScript files: ESLint + Prettier
  • JSON/MD/CSS: Prettier only
  • C# files: dotnet format (if mixed project)

CRITICAL FIX: For .NET files, use correct syntax: "*.cs": "dotnet format --include" is WRONG Use: "*.cs": "dotnet format whitespace --include" or run dotnet format separately

Python (pre-commit hooks)

Configuration in .pre-commit-config.yaml:

repos:
  - repo: https://github.com/astral-sh/ruff-pre-commit
    rev: v0.8.0
    hooks:
      - id: ruff
        args: [--fix]
      - id: ruff-format

Phase 4: Configure Commit Message Validation

Node.js (commitlint)

npm install -D @commitlint/cli @commitlint/config-conventional

Create commitlint.config.mjs with:

  • Conventional Commits format
  • Allowed types: feat, fix, docs, style, refactor, test, chore, ci
  • Max header length: 100 characters

Update Husky hook

Add commit-msg hook:

echo 'npx --no -- commitlint --edit "$1"' > .husky/commit-msg

Python (pre-commit hook for commit message)

Add to .pre-commit-config.yaml:

  - repo: https://github.com/compilerla/conventional-pre-commit
    rev: v3.4.0
    hooks:
      - id: conventional-pre-commit
        stages: [commit-msg]

Phase 5: Test Hooks

Verify hooks work correctly.

Test 1: Lint-staged triggers

# Create a file with lint issues
echo "const x=1" > test-file.ts
git add test-file.ts
git commit -m "test: verify hooks"
# Expected: lint-staged runs, either fixes or blocks

Test 2: Commit message validation

git commit --allow-empty -m "bad message"
# Expected: commitlint rejects

git commit --allow-empty -m "test: valid message format"
# Expected: commit succeeds

Cleanup:

rm test-file.ts
git reset HEAD~1  # If test commit was made

Critical Rules

RULE 1: Husky requires Git repository (git init first).

RULE 2: lint-staged MUST have linters configured first (run ln-741 before ln-742).

RULE 3: Document --no-verify escape hatch for emergency commits.

RULE 4: pre-commit hooks should auto-fix when possible (--fix flag).


Definition of Done

  • Hook manager installed (Husky or pre-commit)
  • Staged linting configured and working
  • Commit message validation configured
  • Test commit triggers hooks correctly
  • User informed of:
    • How hooks work
    • How to skip hooks in emergency (git commit --no-verify)
    • Commit message format required

Reference Files

FilePurpose
husky_precommit_template.shHusky pre-commit hook
husky_commitmsg_template.shHusky commit-msg hook
lintstaged_template.mjslint-staged configuration
commitlint_template.mjscommitlint configuration
precommit_config_template.yamlPython pre-commit config
hooks_guide.mdDetailed hooks guide

Error Handling

ErrorCauseResolution
Husky not runningMissing prepare scriptRun npx husky init again
lint-staged failsMissing linterRun ln-741 first
pre-commit not foundNot in PATHpip install pre-commit
Hooks not triggeringGit hooks disabledCheck .git/hooks/ permissions
Windows path issuesShell script formatUse cross-platform syntax

Emergency Bypass

Document for users:

# Skip all hooks (use sparingly!)
git commit --no-verify -m "emergency: bypass hooks"

# Skip only pre-commit (keeps commit-msg)
HUSKY=0 git commit -m "fix: urgent hotfix"

Version: 2.0.0 Last Updated: 2026-01-10

Source

git clone https://github.com/levnikolaevich/claude-code-skills/blob/master/ln-742-precommit-setup/SKILL.mdView on GitHub

Overview

Sets up pre-commit quality checks by configuring hook managers (Husky or pre-commit), staged linting (lint-staged or pre-commit hooks), and commit message validation (commitlint). Supports Node.js and Python projects and verifies hooks trigger correctly.

How This Skill Works

The skill guides you through five phases: check existing hook configurations, install the appropriate hook manager, enable staged linting, configure commit message validation, and test the hooks. It creates standard configurations such as .husky, .pre-commit-config.yaml, and lint-staged settings and wires up linting and commitlint with conventional commit rules. This approach ensures hooks run automatically before each commit without modifying source code.

When to Use It

  • Starting a new Node.js project and want pre-commit linting and commit message checks
  • Maintaining a Python project that needs pre-commit hooks and ruff
  • Managing a mixed repository with both Node and Python components
  • Migrating an existing repo from legacy hook configs to a modern Husky/pre-commit workflow
  • Enforcing conventional commits and commit message discipline across a team

Quick Start

  1. Step 1: Phase 1 – Check for existing hooks and decide action
  2. Step 2: Phase 2 – Install the hook manager for your stack (Husky or pre-commit) and initialize
  3. Step 3: Phase 3-4-5 – Configure staged linting and commit message validation, then test the hooks

Best Practices

  • Check for existing hooks before installing to avoid overwrites
  • Match the hook manager to your tech stack (Husky for Node, pre-commit for Python)
  • Put lint-staged rules in package.json or .lintstagedrc* and keep them file-type aware
  • For Python use the ruff pre-commit hook with --fix as shown
  • Configure commitlint with conventional types and test hooks after install

Example Use Cases

  • A Node.js library adds Husky, lint-staged, and commitlint to enforce fixes before commits
  • A Python data processing project uses pre-commit with ruff to clean code on commit
  • A mixed repository uses both Husky and pre-commit to cover different components
  • A legacy repo migrates from pre-commit to a Husky based workflow
  • A team runs hook tests after install to verify linting and commit message checks fire on commit

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers