ln-742-precommit-setup

Configures Husky, lint-staged, commitlint, and Python pre-commit hooks. Use when adding Git hook automation to a project.

310 stars

Best use case

ln-742-precommit-setup is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

Configures Husky, lint-staged, commitlint, and Python pre-commit hooks. Use when adding Git hook automation to a project.

Teams using ln-742-precommit-setup should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/ln-742-precommit-setup/SKILL.md --create-dirs "https://raw.githubusercontent.com/levnikolaevich/claude-code-skills/main/skills-catalog/ln-742-precommit-setup/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/ln-742-precommit-setup/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How ln-742-precommit-setup Compares

Feature / Agentln-742-precommit-setupStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

Configures Husky, lint-staged, commitlint, and Python pre-commit hooks. Use when adding Git hook automation to a project.

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

Related Guides

SKILL.md Source

> **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. If `shared/` is missing, fetch files via WebFetch from `https://raw.githubusercontent.com/levnikolaevich/claude-code-skills/master/skills/{path}`.

# ln-742-precommit-setup

**Type:** L3 Worker
**Category:** 7XX Project Bootstrap

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

| Technology | Hook Manager | Staged Linting | Commit Validation |
|------------|--------------|----------------|-------------------|
| Node.js | Husky | lint-staged | commitlint |
| Python | pre-commit | pre-commit hooks | pre-commit hook |
| Mixed | Both (if needed) | Stack-specific | commitlint |

---

## Phase 1: Check Existing Hooks

Before installing, check for existing hook configurations.

**Files to Check:**

| Tool | Indicators |
|------|------------|
| Husky | `.husky/` directory, `husky` in package.json |
| pre-commit | `.pre-commit-config.yaml` |
| lint-staged | `lint-staged` in package.json or `.lintstagedrc*` |
| commitlint | `commitlint.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)

```bash
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)

```bash
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)

```bash
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`:

```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)

```bash
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:
```bash
echo 'npx --no -- commitlint --edit "$1"' > .husky/commit-msg
```

### Python (pre-commit hook for commit message)

Add to `.pre-commit-config.yaml`:
```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**
```bash
# 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**
```bash
git commit --allow-empty -m "bad message"
# Expected: commitlint rejects

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

**Cleanup:**
```bash
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

| File | Purpose |
|------|---------|
| [husky_precommit_template.sh](references/husky_precommit_template.sh) | Husky pre-commit hook |
| [husky_commitmsg_template.sh](references/husky_commitmsg_template.sh) | Husky commit-msg hook |
| [lintstaged_template.mjs](references/lintstaged_template.mjs) | lint-staged configuration |
| [commitlint_template.mjs](references/commitlint_template.mjs) | commitlint configuration |
| [precommit_config_template.yaml](references/precommit_config_template.yaml) | Python pre-commit config |
| [hooks_guide.md](references/hooks_guide.md) | Detailed hooks guide |

---

## Error Handling

| Error | Cause | Resolution |
|-------|-------|------------|
| Husky not running | Missing prepare script | Run `npx husky init` again |
| lint-staged fails | Missing linter | Run ln-741 first |
| pre-commit not found | Not in PATH | `pip install pre-commit` |
| Hooks not triggering | Git hooks disabled | Check `.git/hooks/` permissions |
| Windows path issues | Shell script format | Use cross-platform syntax |

---

## Emergency Bypass

Document for users:

```bash
# 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

Related Skills

ln-774-healthcheck-setup

310
from levnikolaevich/claude-code-skills

Configures health check endpoints for Kubernetes readiness/liveness/startup probes. Use when deploying to Kubernetes.

ln-772-error-handler-setup

310
from levnikolaevich/claude-code-skills

Configures global exception handling middleware. Use when adding centralized error handling to .NET or Python backends.

ln-770-crosscutting-setup

310
from levnikolaevich/claude-code-skills

Sets up logging, error handling, CORS, health checks, and API docs. Use when adding cross-cutting concerns to backend projects.

ln-760-security-setup

310
from levnikolaevich/claude-code-skills

Sets up security scanning for secrets and dependency vulnerabilities. Use when adding security infrastructure to a project.

ln-740-quality-setup

310
from levnikolaevich/claude-code-skills

Sets up linters, pre-commit hooks, and test infrastructure. Use when adding code quality tooling to a project.

ln-730-devops-setup

310
from levnikolaevich/claude-code-skills

Sets up Docker, CI/CD, and environment configuration with auto-detection. Use when adding DevOps infrastructure to a project.

ln-010-dev-environment-setup

310
from levnikolaevich/claude-code-skills

Installs agents, configures MCP servers, syncs configs, creates and audits instructions. Use after setup or when agents/MCP need alignment.

ln-914-community-responder

310
from levnikolaevich/claude-code-skills

Responds to unanswered GitHub discussions and issues with codebase-informed replies. Use when clearing community question backlog.

ln-913-community-debater

310
from levnikolaevich/claude-code-skills

Launches RFC and debate discussions on GitHub. Use when proposing changes that need community input or voting.

ln-912-community-announcer

310
from levnikolaevich/claude-code-skills

Composes and publishes announcements to GitHub Discussions. Use when sharing releases, updates, or news with the community.

ln-911-github-triager

310
from levnikolaevich/claude-code-skills

Produces prioritized triage report from open GitHub issues, PRs, and discussions. Use when reviewing community backlog.

ln-910-community-engagement

310
from levnikolaevich/claude-code-skills

Analyzes community health and delegates engagement tasks. Use when managing GitHub issues, discussions, and announcements.