diff --git a/.gitignore b/.gitignore index 2467e2f..8be9f7a 100644 --- a/.gitignore +++ b/.gitignore @@ -27,8 +27,5 @@ devenv.local.nix # direnv .direnv -# pre-commit -.pre-commit-config.yaml - # Local runtime artifacts /.state/ diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..5564ea6 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,23 @@ +# Shared by Nix/devenv and Guix shells. Keep hook entries PATH-based. +default_stages: + - pre-commit + +repos: + - repo: local + hooks: + - id: check-added-large-files + name: check added large files + entry: scripts/hooks/check-added-large-files + args: ["--maxkb=16384"] + language: system + types: [file] + stages: [pre-commit, pre-push, manual] + + - id: npm-format-check + name: npm format check + entry: npm run format:check + language: system + pass_filenames: false + files: '\.(cjs|js|json|md|mjs|ts|tsx|yaml|yml)$' + types: [text] + stages: [pre-commit] diff --git a/scripts/hooks/check-added-large-files b/scripts/hooks/check-added-large-files new file mode 100755 index 0000000..b46fa23 --- /dev/null +++ b/scripts/hooks/check-added-large-files @@ -0,0 +1,38 @@ +#!/usr/bin/env sh +set -eu + +maxkb=500 +while [ "$#" -gt 0 ]; do + case "$1" in + --maxkb=*) + maxkb=${1#--maxkb=} + shift + ;; + --) + shift + break + ;; + -*) + echo "check-added-large-files: unknown option: $1" >&2 + exit 2 + ;; + *) + break + ;; + esac +done + +limit=$((maxkb * 1024)) +failed=0 + +for file in "$@"; do + [ -f "$file" ] || continue + size=$(wc -c < "$file" | tr -d '[:space:]') + if [ "$size" -gt "$limit" ]; then + kb=$(((size + 1023) / 1024)) + echo "$file (${kb} KB) exceeds ${maxkb} KB" >&2 + failed=1 + fi +done + +exit "$failed"