install-hooks.sh 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. # Install git hooks for release validation.
  4. # Idempotent — safe to run multiple times.
  5. REPO_ROOT=$(git rev-parse --show-toplevel 2>/dev/null)
  6. if [[ -z "$REPO_ROOT" ]]; then
  7. echo "Error: not in a git repository" >&2
  8. exit 1
  9. fi
  10. HOOKS_DIR="$REPO_ROOT/.git/hooks"
  11. SOURCE="$REPO_ROOT/scripts/pre-push"
  12. if [[ ! -f "$SOURCE" ]]; then
  13. echo "Error: scripts/pre-push not found at $SOURCE" >&2
  14. exit 1
  15. fi
  16. # Install pre-push hook
  17. if [[ -L "$HOOKS_DIR/pre-push" ]] && [[ "$(readlink "$HOOKS_DIR/pre-push")" == "$SOURCE" ]]; then
  18. echo "pre-push hook: already installed (symlink)"
  19. elif [[ -f "$HOOKS_DIR/pre-push" ]]; then
  20. # Existing hook that isn't our symlink — back it up
  21. BACKUP="$HOOKS_DIR/pre-push.backup.$(date +%s)"
  22. echo "pre-push hook: backing up existing hook to $(basename "$BACKUP")"
  23. mv "$HOOKS_DIR/pre-push" "$BACKUP"
  24. ln -sf "$SOURCE" "$HOOKS_DIR/pre-push"
  25. echo "pre-push hook: installed (symlink → scripts/pre-push)"
  26. else
  27. ln -sf "$SOURCE" "$HOOKS_DIR/pre-push"
  28. echo "pre-push hook: installed (symlink → scripts/pre-push)"
  29. fi
  30. # Ensure the source is executable
  31. chmod +x "$SOURCE"
  32. echo "Done."