install-hooks.sh
850 Bytes
#!/usr/bin/env bash
# Symlink (or copy on Windows) tracked git hooks from scripts/git-hooks/ into .git/hooks/.
# Auto-run on `npm install` via the "prepare" script in package.json.
# Silently skips if .git/ doesn't exist (e.g., installing from a tarball, not a clone).
set -e
repo_root="$(git rev-parse --show-toplevel 2>/dev/null || true)"
[ -z "$repo_root" ] && exit 0 # not inside a git repo
[ -d "$repo_root/.git/hooks" ] || exit 0
src_dir="$repo_root/scripts/git-hooks"
dst_dir="$repo_root/.git/hooks"
[ -d "$src_dir" ] || exit 0
for hook in "$src_dir"/*; do
name="$(basename "$hook")"
dst="$dst_dir/$name"
# Replace if missing or content differs from tracked hook
if [ ! -f "$dst" ] || ! cmp -s "$hook" "$dst"; then
cp "$hook" "$dst"
chmod +x "$dst"
echo "[install-hooks] installed .git/hooks/$name"
fi
done