[P.1] chore(hooks): add 800-line file size pre-commit gate - #209
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
.githooks/pre-committhat blocks commits introducing source files over 800 lines. Keeps module boundaries honest: split files before they grow unreviewable.apps/**andpackages/**source only*.test.*,*.spec.*,tests/**),*.d.ts, generated IPC map, build output,node_modules,.worktrees,.claudeMAX_LINES=800(lives in the hook itself)core.hooksPath=.githooksis set by the existing rootpreparescript, sopnpm installpicks it up automaticallyHook source
` ``sh
#!/usr/bin/env bash
Reject commits that introduce or modify any source file exceeding MAX_LINES.
Keeps module boundaries honest — split files before they become unreviewable.
set -eu
MAX_LINES=800
excluded() {
case "$1" in
node_modules/|/node_modules/) return 0 ;;
.worktrees/|/.worktrees/) return 0 ;;
.claude/) return 0 ;;
dist/|/dist/) return 0 ;;
out/|/out/) return 0 ;;
coverage/|/coverage/) return 0 ;;
tests/|/tests/*) return 0 ;;
.test.ts|.test.tsx) return 0 ;;
.spec.ts|.spec.tsx) return 0 ;;
.d.ts) return 0 ;;
generated-ipc-invoke-map.ts) return 0 ;;
esac
case "$1" in
apps/|packages/) return 1 ;;
/) return 0 ;;
*) return 1 ;;
esac
}
offender_files=()
offender_lines=()
while IFS= read -r file; do
[ -z "$file" ] && continue
[ -f "$file" ] || continue
if excluded "$file"; then
continue
fi
lines=$(wc -l < "$file" | tr -d ' ')
if [ "$lines" -gt "$MAX_LINES" ]; then
offender_files+=("$file")
offender_lines+=("$lines")
fi
done < <(git diff --cached --name-only --diff-filter=ACMR)
count=${#offender_files[@]}
if [ "$count" -gt 0 ]; then
printf '\n\033[31mpre-commit: %d file(s) exceed %d-line limit:\033[0m\n\n' "$count" "$MAX_LINES" >&2
printf ' %-70s %s\n' "FILE" "LINES" >&2
printf ' %-70s %s\n' "----" "-----" >&2
for i in "${!offender_files[@]}"; do
printf ' %-70s %s\n' "${offender_files[$i]}" "${offender_lines[$i]}" >&2
done
printf '\nSplit these files into smaller modules before committing.\n' >&2
printf 'Limit lives in .githooks/pre-commit (MAX_LINES).\n\n' >&2
exit 1
fi
exit 0
` ``
Fail-case transcript (801-line file rejected)
` ``
$ awk 'BEGIN{for(i=0;i<801;i++)print "// line " i}' > apps/desktop/src/_throwaway.ts
$ wc -l apps/desktop/src/_throwaway.ts
801 apps/desktop/src/_throwaway.ts
$ git add apps/desktop/src/_throwaway.ts
$ git commit -m "test-oversized"
pre-commit: 1 file(s) exceed 800-line limit:
FILE LINES
apps/desktop/src/_throwaway.ts 801
Split these files into smaller modules before committing.
Limit lives in .githooks/pre-commit (MAX_LINES).
` ``
Hook exits non-zero, commit aborted.
Pass-case transcript (799-line file accepted)
`` \$ awk 'BEGIN{for(i=0;i<799;i++)print "// line " i}' > apps/desktop/src/_okay.ts \$ wc -l apps/desktop/src/_okay.ts 799 apps/desktop/src/_okay.ts \$ git add apps/desktop/src/_okay.ts \$ git commit -m "test-ok" [debt/P.1-file-size-precommit 7d4f5726] test-ok 1 file changed, 799 insertions(+) create mode 100644 apps/desktop/src/_okay.ts \$ git reset --hard HEAD~1 HEAD is now at 77ec9ea3 chore(hooks): add 800-line file size pre-commit gate``Test plan
-rwxr-xr-x)core.hooksPathresolves to.githooks(set by rootpreparescript)pnpm lintpassespnpm --filter @memry/desktop typecheck:nodepassespnpm --filter @memry/desktop typecheck:webpassespnpm typecheck:packagespassespnpm testpasses (ignored knowncalendar-page.test.tsx:258"Due draft" flake)