[repo] Hygiene: .gitignore and pre-commit hook
This commit is contained in:
@@ -0,0 +1,3 @@
|
|||||||
|
* text=auto eol=lf
|
||||||
|
*.png binary
|
||||||
|
*.ico binary
|
||||||
Executable
+65
@@ -0,0 +1,65 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# SPDX-License-Identifier: MPL-2.0
|
||||||
|
#
|
||||||
|
# Ianitor pre-commit hook. Rejects staged files that must never be in git:
|
||||||
|
# - binaries (anything git itself classifies as binary)
|
||||||
|
# - private keys (PEM / OpenSSH / PKCS#8 / PGP headers, and key-store file names)
|
||||||
|
# - files over 5 MB
|
||||||
|
# - build output and dependencies (target/, node_modules/, dist/)
|
||||||
|
#
|
||||||
|
# Enable once per clone: git config core.hooksPath .githooks
|
||||||
|
# Test it: sh .githooks/test-pre-commit.sh
|
||||||
|
# Plain POSIX sh: works in Git Bash on Windows and on Linux.
|
||||||
|
|
||||||
|
LIMIT=5242880 # 5 MiB
|
||||||
|
|
||||||
|
# Every check prints one "FAIL" line on stdout per problem (reasons go to stderr),
|
||||||
|
# so the loop can run in a pipeline subshell and still be counted here.
|
||||||
|
failures=$(
|
||||||
|
# numstat prints "-<TAB>-<TAB>path" for files git treats as binary.
|
||||||
|
git -c core.quotePath=false diff --cached --numstat --diff-filter=ACMR | while IFS= read -r line; do
|
||||||
|
added=${line%%" "*}
|
||||||
|
rest=${line#*" "}
|
||||||
|
deleted=${rest%%" "*}
|
||||||
|
path=${rest#*" "}
|
||||||
|
if [ "$added" = "-" ] && [ "$deleted" = "-" ]; then
|
||||||
|
printf 'pre-commit: rejected %s: binary file\n' "$path" >&2
|
||||||
|
echo FAIL
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
git -c core.quotePath=false diff --cached --name-only --diff-filter=ACMR | while IFS= read -r path; do
|
||||||
|
case "/$path" in
|
||||||
|
*/target/*|*/node_modules/*|*/dist/*)
|
||||||
|
printf 'pre-commit: rejected %s: build output or dependency directory\n' "$path" >&2
|
||||||
|
echo FAIL ;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
base=${path##*/}
|
||||||
|
case "$base" in
|
||||||
|
*.pub) ;;
|
||||||
|
*.key|*.p12|*.pfx|*.jks|*.keystore|id_rsa|id_dsa|id_ecdsa|id_ed25519|id_rsa.*|id_dsa.*|id_ecdsa.*|id_ed25519.*)
|
||||||
|
printf 'pre-commit: rejected %s: private key file name\n' "$path" >&2
|
||||||
|
echo FAIL ;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# Size of the staged blob, not of the working-tree file.
|
||||||
|
size=$(git cat-file -s ":$path" 2>/dev/null || echo 0)
|
||||||
|
if [ "$size" -gt "$LIMIT" ]; then
|
||||||
|
printf 'pre-commit: rejected %s: %s bytes is over the 5 MB limit\n' "$path" "$size" >&2
|
||||||
|
echo FAIL
|
||||||
|
fi
|
||||||
|
|
||||||
|
if git cat-file -p ":$path" 2>/dev/null | grep -E -q -e '-----BEGIN [A-Z0-9 ]*PRIVATE KEY( BLOCK)?-----'; then
|
||||||
|
printf 'pre-commit: rejected %s: contains a private key\n' "$path" >&2
|
||||||
|
echo FAIL
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
)
|
||||||
|
|
||||||
|
case "$failures" in
|
||||||
|
*FAIL*)
|
||||||
|
printf 'pre-commit: commit blocked. Unstage the files above (git rm --cached <file>).\n' >&2
|
||||||
|
exit 1 ;;
|
||||||
|
esac
|
||||||
|
exit 0
|
||||||
Executable
+94
@@ -0,0 +1,94 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# SPDX-License-Identifier: MPL-2.0
|
||||||
|
#
|
||||||
|
# Self-test for .githooks/pre-commit. Creates a throw-away repository, points it at the
|
||||||
|
# hook and checks that every forbidden case is rejected and a normal file is accepted.
|
||||||
|
# Usage: sh .githooks/test-pre-commit.sh (Git Bash on Windows or Linux)
|
||||||
|
|
||||||
|
set -u
|
||||||
|
HOOKS=$(cd "$(dirname "$0")" && pwd)
|
||||||
|
WORK=$(mktemp -d 2>/dev/null || mktemp -d -t ianitor-hook)
|
||||||
|
trap 'rm -rf "$WORK"' EXIT INT TERM
|
||||||
|
|
||||||
|
cd "$WORK" || exit 2
|
||||||
|
git init -q .
|
||||||
|
git config user.name hook-test
|
||||||
|
git config user.email hook-test@invalid
|
||||||
|
git config core.hooksPath "$HOOKS"
|
||||||
|
git config core.autocrlf false
|
||||||
|
git commit -q --allow-empty -m base --no-gpg-sign
|
||||||
|
|
||||||
|
pass=0
|
||||||
|
fail=0
|
||||||
|
|
||||||
|
# expect <accept|reject> <description> <path> (file must already exist)
|
||||||
|
expect() {
|
||||||
|
want=$1; what=$2; path=$3
|
||||||
|
git add -f -- "$path"
|
||||||
|
if git commit -q -m "$what" --no-gpg-sign >/dev/null 2>"$WORK/.err"; then got=accept; else got=reject; fi
|
||||||
|
if [ "$got" = "$want" ]; then
|
||||||
|
pass=$((pass + 1)); printf 'ok %-7s %s\n' "$want" "$what"
|
||||||
|
else
|
||||||
|
fail=$((fail + 1)); printf 'FAIL wanted %s, got %s: %s\n' "$want" "$got" "$what"; cat "$WORK/.err"
|
||||||
|
fi
|
||||||
|
# Leave the index clean for the next case.
|
||||||
|
git reset -q --hard HEAD >/dev/null 2>&1
|
||||||
|
git clean -q -fdx >/dev/null 2>&1
|
||||||
|
}
|
||||||
|
|
||||||
|
B='-----BEGIN'
|
||||||
|
E='-----END'
|
||||||
|
|
||||||
|
printf 'hello\n' > readme.txt
|
||||||
|
expect accept "plain text file" readme.txt
|
||||||
|
|
||||||
|
printf 'fn main() {}\n' > main.rs
|
||||||
|
expect accept "source file" main.rs
|
||||||
|
|
||||||
|
printf '\000\001\002\003binary\000' > blob.bin
|
||||||
|
expect reject "binary file" blob.bin
|
||||||
|
|
||||||
|
printf '%s RSA PRIVATE KEY-----\nMIIBOgIBAAJBAK\n%s RSA PRIVATE KEY-----\n' "$B" "$E" > notes.txt
|
||||||
|
expect reject "PEM RSA private key in a .txt file" notes.txt
|
||||||
|
|
||||||
|
printf '%s PRIVATE KEY-----\nMIGHAgEAMBMG\n%s PRIVATE KEY-----\n' "$B" "$E" > pkcs8.pem
|
||||||
|
expect reject "PKCS#8 private key" pkcs8.pem
|
||||||
|
|
||||||
|
printf '%s ENCRYPTED PRIVATE KEY-----\nMIIFHDBOBgkq\n%s ENCRYPTED PRIVATE KEY-----\n' "$B" "$E" > enc.pem
|
||||||
|
expect reject "encrypted PKCS#8 private key" enc.pem
|
||||||
|
|
||||||
|
printf '%s EC PRIVATE KEY-----\nMHcCAQEEI\n%s EC PRIVATE KEY-----\n' "$B" "$E" > ec.txt
|
||||||
|
expect reject "EC private key" ec.txt
|
||||||
|
|
||||||
|
printf '%s OPENSSH PRIVATE KEY-----\nb3BlbnNzaC1rZXktdjEAAAAA\n%s OPENSSH PRIVATE KEY-----\n' "$B" "$E" > deploy
|
||||||
|
expect reject "OpenSSH private key" deploy
|
||||||
|
|
||||||
|
printf 'not really a key\n' > server.key
|
||||||
|
expect reject "*.key file name" server.key
|
||||||
|
|
||||||
|
printf 'not really a key\n' > id_ed25519
|
||||||
|
expect reject "id_ed25519 file name" id_ed25519
|
||||||
|
|
||||||
|
printf 'ssh-ed25519 AAAAC3Nza test\n' > id_ed25519.pub
|
||||||
|
expect accept "public key id_ed25519.pub" id_ed25519.pub
|
||||||
|
|
||||||
|
printf '%s CERTIFICATE-----\nMIIBszCCAVmg\n%s CERTIFICATE-----\n' "$B" "$E" > cert.pem
|
||||||
|
expect accept "certificate (public) PEM" cert.pem
|
||||||
|
|
||||||
|
head -c 6291456 /dev/zero | tr '\000' 'a' > big.txt
|
||||||
|
expect reject "6 MB text file" big.txt
|
||||||
|
|
||||||
|
head -c 4194304 /dev/zero | tr '\000' 'a' > medium.txt
|
||||||
|
expect accept "4 MB text file" medium.txt
|
||||||
|
|
||||||
|
mkdir -p crates/x/target/debug && printf 'x\n' > crates/x/target/debug/out.txt
|
||||||
|
expect reject "file under target/" crates/x/target/debug/out.txt
|
||||||
|
|
||||||
|
mkdir -p apps/web/node_modules/pkg && printf 'x\n' > apps/web/node_modules/pkg/index.js
|
||||||
|
expect reject "file under node_modules/" apps/web/node_modules/pkg/index.js
|
||||||
|
|
||||||
|
mkdir -p apps/web/dist && printf 'x\n' > apps/web/dist/index.html
|
||||||
|
expect reject "file under dist/" apps/web/dist/index.html
|
||||||
|
|
||||||
|
printf '\nhook: %d passed, %d failed\n' "$pass" "$fail"
|
||||||
|
[ "$fail" -eq 0 ]
|
||||||
+34
@@ -0,0 +1,34 @@
|
|||||||
|
# Build output
|
||||||
|
target/
|
||||||
|
dist/
|
||||||
|
result
|
||||||
|
result-*
|
||||||
|
|
||||||
|
# JavaScript
|
||||||
|
node_modules/
|
||||||
|
.svelte-kit/
|
||||||
|
.pnpm-store/
|
||||||
|
|
||||||
|
# Private keys and key stores (never committed; the pre-commit hook also blocks them)
|
||||||
|
*.key
|
||||||
|
*.p12
|
||||||
|
*.pfx
|
||||||
|
*.jks
|
||||||
|
*.keystore
|
||||||
|
id_rsa*
|
||||||
|
id_ed25519*
|
||||||
|
id_ecdsa*
|
||||||
|
|
||||||
|
# Local secrets and state
|
||||||
|
.env
|
||||||
|
.env.*
|
||||||
|
!.env.example
|
||||||
|
credentials/
|
||||||
|
*.log
|
||||||
|
|
||||||
|
# Editors and OS
|
||||||
|
.idea/
|
||||||
|
.vscode/
|
||||||
|
*.swp
|
||||||
|
.DS_Store
|
||||||
|
Thumbs.db
|
||||||
Reference in New Issue
Block a user