|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright 2026 ResQ Software |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +# |
| 5 | +# Canonical ResQ pre-push shim — source: resq-software/dev. |
| 6 | +# Force-push guard on main/master, branch naming convention, and an optional |
| 7 | +# per-repo local-pre-push hook for language-specific checks |
| 8 | +# (e.g. cargo check, ruff check, anchor build). |
| 9 | + |
| 10 | +set -euo pipefail |
| 11 | + |
| 12 | +[ -n "${GIT_HOOKS_SKIP:-}" ] && exit 0 |
| 13 | + |
| 14 | +echo "🚀 Running pre-push checks..." |
| 15 | + |
| 16 | +BRANCH=$(git symbolic-ref --short HEAD 2>/dev/null || echo "") |
| 17 | + |
| 18 | +# Capture stdin (the ref info git passes to pre-push) so both the force-push |
| 19 | +# guard and any local-pre-push hook can read it. |
| 20 | +PUSH_REFS="" |
| 21 | +if [ ! -t 0 ]; then |
| 22 | + PUSH_REFS=$(cat) |
| 23 | +fi |
| 24 | + |
| 25 | +# ── Force-push guard on main/master ───────────────────────────────────────── |
| 26 | +case "$BRANCH" in |
| 27 | + main|master) |
| 28 | + # Only iterate when git actually gave us refs — an empty $PUSH_REFS |
| 29 | + # would otherwise feed one blank line through the heredoc and trip |
| 30 | + # the guard. |
| 31 | + if [ -n "$PUSH_REFS" ]; then |
| 32 | + while read -r _local_ref local_sha _remote_ref remote_sha; do |
| 33 | + [ -z "$local_sha" ] && continue |
| 34 | + if [ "$remote_sha" != "0000000000000000000000000000000000000000" ] && \ |
| 35 | + [ "$local_sha" != "0000000000000000000000000000000000000000" ]; then |
| 36 | + if ! git merge-base --is-ancestor "$remote_sha" "$local_sha" 2>/dev/null; then |
| 37 | + echo "❌ Force push to $BRANCH is not allowed." |
| 38 | + echo " Override with: git push --no-verify" |
| 39 | + exit 1 |
| 40 | + fi |
| 41 | + fi |
| 42 | + done <<EOF |
| 43 | +$PUSH_REFS |
| 44 | +EOF |
| 45 | + fi |
| 46 | + ;; |
| 47 | +esac |
| 48 | + |
| 49 | +# ── Branch naming convention ──────────────────────────────────────────────── |
| 50 | +ALLOWED_PATTERN="^(feat|fix|docs|chore|refactor|test|ci|perf|release)/.*$" |
| 51 | +SKIP_BRANCHES="^(main|master|dev|develop|staging|production|changeset-release/.*)$" |
| 52 | + |
| 53 | +if [ -n "$BRANCH" ]; then |
| 54 | + if ! grep -qE "$SKIP_BRANCHES" <<<"$BRANCH"; then |
| 55 | + if ! grep -qE "$ALLOWED_PATTERN" <<<"$BRANCH"; then |
| 56 | + echo "❌ Branch '$BRANCH' does not follow naming convention." |
| 57 | + echo " Expected: type/description (e.g. feat/add-login)" |
| 58 | + echo " Allowed prefixes: feat, fix, docs, chore, refactor, test, ci, perf, release" |
| 59 | + echo " Override with: git push --no-verify" |
| 60 | + exit 1 |
| 61 | + fi |
| 62 | + fi |
| 63 | +fi |
| 64 | + |
| 65 | +# ── Local override (language-specific checks) ─────────────────────────────── |
| 66 | +LOCAL_HOOK="$(git rev-parse --show-toplevel)/.git-hooks/local-pre-push" |
| 67 | +if [ -x "$LOCAL_HOOK" ]; then |
| 68 | + # Re-supply captured stdin so local hooks that inspect refs still work. |
| 69 | + "$LOCAL_HOOK" "$@" <<EOF |
| 70 | +$PUSH_REFS |
| 71 | +EOF |
| 72 | +fi |
| 73 | + |
| 74 | +echo "✅ Pre-push checks passed" |
0 commit comments