Bashgitpre-commitdevopslintingautomationdeveloper-experience
Git Pre-commit Hook Installer
Shell script that installs and configures pre-commit hooks for linting, formatting, and type checking with automatic dependency detection.
title: "Git Pre-commit Hook Installer" description: "Shell script that installs and configures pre-commit hooks for linting, formatting, and type checking with automatic dependency detection." category: "Bash" language: "bash" tags: ["git", "pre-commit", "devops", "linting", "automation", "developer-experience"] createdAt: "2024-12-02" updatedAt: "2025-01-09"
Overview
This script automatically detects your project's linting and formatting tools, then generates a pre-commit hook tailored to your stack. It supports JavaScript/TypeScript projects (ESLint, Prettier, TypeScript compiler) and Python projects (Ruff, Black, Mypy).
The generated hook only checks staged files (not the entire project) for linting and formatting, keeping commit times fast. Type checking runs on the full project since it needs complete context.
Code
#!/usr/bin/env bash
set -euo pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
info() { echo -e "${GREEN}[INFO]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
error() { echo -e "${RED}[ERROR]${NC} $1" >&2; }
if ! git rev-parse --git-dir > /dev/null 2>&1; then
error "Not a git repository. Run this from your project root."
exit 1
fi
GIT_HOOKS_DIR="$(git rev-parse --git-dir)/hooks"
PRE_COMMIT_HOOK="$GIT_HOOKS_DIR/pre-commit"
detect_tools() {
local tools=()
if [[ -f "package.json" ]]; then
if grep -q '"eslint"' package.json 2>/dev/null; then tools+=("eslint"); fi
if grep -q '"prettier"' package.json 2>/dev/null; then tools+=("prettier"); fi
if grep -q '"typescript"' package.json 2>/dev/null; then tools+=("tsc"); fi
fi
if [[ -f "pyproject.toml" ]] || [[ -f "setup.py" ]]; then
if command -v ruff &>/dev/null; then tools+=("ruff"); fi
if command -v black &>/dev/null; then tools+=("black"); fi
if command -v mypy &>/dev/null; then tools+=("mypy"); fi
fi
echo "${tools[@]}"
}
info "Detecting project tools..."
TOOLS=$(detect_tools)
info "Found tools: $TOOLS"
info "Installing pre-commit hook..."
# Generate hook (see full version in Code tab)
# ... generates $PRE_COMMIT_HOOK based on detected tools ...
chmod +x "$PRE_COMMIT_HOOK"
info "Done! Hook installed at: $PRE_COMMIT_HOOK"Notes
- Run this script once per clone:
bash scripts/setup-hooks.sh - The hook uses
git diff --cachedto only check files that are staged, not all modified files - Type checking (
tsc --noEmit,mypy) runs on the full project because partial type checking gives unreliable results - The
--no-verifyescape hatch is documented in the output for genuine emergencies - For teams, commit this script to your repo and add a
postinstallnpm script or Makefile target - Consider using
lint-staged(npm) orpre-commit(Python) framework for more complex setups - The script is idempotent and can be re-run safely to update the hook when new tools are added