Cotton code
56 lines · 3.0 KB · ShellRaw
#!/usr/bin/env bash# Pushes run no tests: checks run on main after the push. Test runners can# wait on another lane's build lock indefinitely (16 Oct 2026).# Pushes do run a secret scan of the commits they bring (gitleaks, through# scripts/secret-scan.sh): seconds, and nothing waits on a lock.# Refuse automatic catch-up merges using local git only. (The landed-changes guard# was retired on 22 Oct 2026: main is pushed to directly and fixed forward.)set -uo pipefailZERO='0000000000000000000000000000000000000000'ROOT="$(git rev-parse --show-toplevel 2>/dev/null || true)"[ -n "$ROOT" ] || exit 0REFS="$(cat 2>/dev/null || true)"if [ "${COTTON_ALLOW_CATCH_UP_MERGE:-}" != "1" ]; then  refused=""  while read -r _local_ref local_sha remote_ref remote_sha; do    [ -n "${local_sha:-}" ] && [ "$local_sha" != "$ZERO" ] || continue    [ -n "${remote_sha:-}" ] && [ "$remote_sha" != "$ZERO" ] || continue   # a new branch has no run to cancel    case "${remote_ref:-}" in refs/heads/main|refs/tags/*) continue ;; esac    git -C "$ROOT" cat-file -e "$remote_sha^{commit}" 2>/dev/null || continue    # Only merges this push brings that NO BRANCH ALREADY HAS: a branch rebased    # onto main carries main's own history past the old head, merges and all    # (the 28 Sep train landed dozens), and those are not this push's doing.    known=("$remote_sha")    main_sha="$(git -C "$ROOT" rev-parse -q --verify refs/heads/main 2>/dev/null || true)"    [ -z "$main_sha" ] || known+=("$main_sha")    for merge in $(git -C "$ROOT" rev-list --merges "$local_sha" --not "${known[@]}" --remotes 2>/dev/null); do      # One line, the merge then its parents (bash 3 on a Mac has no mapfile).      read -r -a line <<< "$(git -C "$ROOT" rev-list --parents -n 1 "$merge" 2>/dev/null)"      [ "${#line[@]}" = 3 ] || continue      # Exit 0 means no conflict; any other status (a conflict, or a git too      # old for --write-tree) lets the push through.      auto="$(git -C "$ROOT" merge-tree --write-tree "${line[1]}" "${line[2]}" 2>/dev/null | head -1)" || continue      if [ -n "$auto" ] && [ "$auto" = "$(git -C "$ROOT" rev-parse "$merge^{tree}" 2>/dev/null)" ]; then        refused="${refused:+$refused; }${remote_ref#refs/heads/}: $(git -C "$ROOT" log -1 --format='%h %s' "$merge")"      fi    done  done <<< "$REFS"  if [ -n "$refused" ]; then    echo "[pre-push] push refused — it only catches a branch up and would cancel its run: $refused; rebase or set COTTON_ALLOW_CATCH_UP_MERGE=1." >&2    exit 1  fifi# Secret scan of the commits this push brings, before anything leaves the# machine (SECURITY-CHECKS-0.md, item 9). Pushes that only delete a ref bring none.shas=()while read -r _local_ref local_sha _remote_ref _remote_sha; do  [ -n "${local_sha:-}" ] && [ "$local_sha" != "$ZERO" ] && shas+=("$local_sha")done <<< "$REFS"if [ "${#shas[@]}" -gt 0 ]; then  bash "$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/secret-scan.sh" "${shas[@]}" || exit 1fiecho "[pre-push] tests skipped — checks run on main after the push."