Da Anonimo (non verificato) , 29 Aprile 2026

Il caso per i test pre-push

Un push rotto spreca il tempo di tutti.

L'hook pre-push piu semplice

# .git/hooks/pre-push
#!/usr/bin/env bash
set -e
npm test --silent
chmod +x .git/hooks/pre-push

Leggere cosa viene pushato

#!/usr/bin/env bash
while read local_ref local_sha remote_ref remote_sha; do
  if [ "$local_sha" = "0000000000000000000000000000000000000000" ]; then
    continue
  fi
  git checkout "$local_sha" -- .
  npm test
done

Esecuzione test selettiva

changed=$(git diff --name-only origin/main...HEAD)
affected=$(./scripts/find-affected-tests.sh "$changed")
npx jest $affected

Enforcement consapevole del branch

protected_branches=("main" "release")
branch=$(git symbolic-ref --short HEAD)
if [[ " ${protected_branches[*]} " =~ " $branch " ]]; then
  echo "Refusing to push directly to $branch" >&2
  exit 1
fi

Usare Husky per hook condivisi

# .husky/pre-push
npm test --silent
npm run typecheck --silent

Tip di performance

  • Eseguire test in parallelo.
  • Saltare test invariati.
  • Cachare le trasformazioni.
  • Riservare i test di integrazione pesanti per CI.

Bypassare

git push --no-verify

Hook lato server

# /srv/git/repo.git/hooks/pre-receive
#!/usr/bin/env bash
while read old new ref; do
  if ! gitleaks detect --redact --no-banner --staged 2>/dev/null; then
    echo "Push rejected: secrets detected" >&2
    exit 1
  fi
done

Il confine giusto

  • Pre-commit: check istantanei.
  • Pre-push: test unitari, lint completo.
  • CI: test di integrazione, smoke test.