#!/usr/bin/env bash # Copyright 2026 ResQ Software # SPDX-License-Identifier: Apache-2.0 # # Canonical ResQ pre-push shim — source: resq-software/dev. # Force-push guard on main/master, branch naming convention, and an optional # per-repo local-pre-push hook for language-specific checks # (e.g. cargo check, ruff check, anchor build). set -euo pipefail [ -n "${GIT_HOOKS_SKIP:-}" ] && exit 0 echo "🚀 Running pre-push checks..." # Deliberately no `BRANCH=$(git symbolic-ref HEAD)` here. Every rule below reads # the ref being pushed to, and a variable holding the checked-out branch is what # both of them used to key on — which is how a force push to main from another # branch got waved through. Keeping it around only invites that back. # Capture stdin (the ref info git passes to pre-push) so both the force-push # guard and any local-pre-push hook can read it. PUSH_REFS="" if [ ! -t 0 ]; then PUSH_REFS=$(cat) fi # ── Per-ref guards: force-push protection and branch naming ───────────────── # # Both rules read the ref being *written to*, never the checked-out branch. # git names them separately for a reason: `git push origin +feature/x:main` # rewrites main while HEAD is feature/x, so a guard keyed on HEAD waves through # exactly the push it exists to stop. The same mistake inverts the naming rule, # checking `main` (skipped) instead of the branch actually being created. ZERO_SHA="0000000000000000000000000000000000000000" ALLOWED_PATTERN="^(feat|fix|docs|chore|refactor|test|ci|perf|release)/.*$" SKIP_BRANCHES="^(main|master|dev|develop|staging|production|changeset-release/.*)$" # Only iterate when git actually gave us refs — an empty $PUSH_REFS would # otherwise feed one blank line through and be read as a record. if [ -n "$PUSH_REFS" ]; then while read -r _local_ref local_sha remote_ref remote_sha; do [ -z "$remote_ref" ] && continue # Tags and other refs fall outside both rules. case "$remote_ref" in refs/heads/*) target="${remote_ref#refs/heads/}" ;; *) continue ;; esac # Force-push guard. Skipped when either side is the all-zero SHA: that # is a branch being created or deleted, and there is no history to # overwrite. case "$target" in main|master) if [ "$remote_sha" != "$ZERO_SHA" ] && [ "$local_sha" != "$ZERO_SHA" ]; then if ! git merge-base --is-ancestor "$remote_sha" "$local_sha" 2>/dev/null; then echo "❌ Force push to $target is not allowed." echo " Override with: git push --no-verify" exit 1 fi fi ;; esac # Naming convention, on creates and updates only — deleting a # badly-named branch is how you get rid of it. if [ "$local_sha" != "$ZERO_SHA" ]; then if ! grep -qE "$SKIP_BRANCHES" <<<"$target"; then if ! grep -qE "$ALLOWED_PATTERN" <<<"$target"; then echo "❌ Branch '$target' does not follow naming convention." echo " Expected: type/description (e.g. feat/add-login)" echo " Allowed prefixes: feat, fix, docs, chore, refactor, test, ci, perf, release" echo " Override with: git push --no-verify" exit 1 fi fi fi done <