Agency/.githooks/pre-commit
Ryan Schultz dfd6b5e18c docs: guard new top-level doc files; fix README after STYLE.md
- pre-commit hook: third check — new file directly under docs/ requires
  README.md to be staged (closes the gap that let STYLE.md slip through)
- README: add docs/STYLE.md to project structure diagram and documentation
  system section; add AI agent note on checking the diagram proactively
- .gitmessage: tighten diagram checklist item to name the docs/ depth-1 case

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-11 20:42:37 -05:00

67 lines
2.7 KiB
Bash

#!/usr/bin/env bash
# Three checks:
# 1. Require CHANGELOG.md to be staged alongside any substantive changes.
# 2. Require README.md to be staged when a new directory is introduced.
# 3. Require README.md to be staged when a new file is added directly under docs/.
staged=$(git diff --cached --name-only)
# ── Check 1: CHANGELOG.md ────────────────────────────────────────────────────
substantive=$(echo "$staged" | grep -v '^CHANGELOG\.md$' | grep -E '^(docs/|src/|data/|config\.yaml|main\.py|requirements\.txt|README\.md|\.gitmessage)' || true)
if [ -n "$substantive" ] && ! echo "$staged" | grep -q '^CHANGELOG\.md$'; then
echo ""
echo " pre-commit: substantive files staged but CHANGELOG.md is not."
echo ""
echo " Files requiring a changelog entry:"
echo "$substantive" | sed 's/^/ /'
echo ""
echo " Update CHANGELOG.md and stage it, or bypass with: git commit --no-verify"
echo ""
exit 1
fi
# ── Check 2: README.md project structure ─────────────────────────────────────
# If a staged new file lives in a directory that doesn't exist in HEAD,
# the project structure may have changed — require README.md to be staged.
new_files=$(git diff --cached --name-only --diff-filter=A)
new_dir=""
while IFS= read -r f; do
[ -z "$f" ] && continue
dir=$(dirname "$f")
[ "$dir" = "." ] && continue
# Check if this directory existed in HEAD
if ! git ls-tree HEAD "$dir/" > /dev/null 2>&1; then
new_dir="$dir"
break
fi
done <<< "$new_files"
if [ -n "$new_dir" ] && ! echo "$staged" | grep -q '^README\.md$'; then
echo ""
echo " pre-commit: new directory '$new_dir' introduced but README.md is not staged."
echo ""
echo " Check whether the project structure diagram in README.md needs updating."
echo " Stage README.md (with or without changes), or bypass with: git commit --no-verify"
echo ""
exit 1
fi
# ── Check 3: README.md — new file directly under docs/ ───────────────────────
# Files like docs/STYLE.md appear in the README structure diagram.
# A new file at that depth may need a diagram entry.
new_top_doc=$(echo "$new_files" | grep -E '^docs/[^/]+$' | head -1 || true)
if [ -n "$new_top_doc" ] && ! echo "$staged" | grep -q '^README\.md$'; then
echo ""
echo " pre-commit: new file '$new_top_doc' added directly under docs/ but README.md is not staged."
echo ""
echo " Check whether the project structure diagram in README.md lists this file."
echo " Stage README.md (with or without changes), or bypass with: git commit --no-verify"
echo ""
exit 1
fi