.githooks/pre-push

118 lines · 4256 bytes · executable

  1#!/usr/bin/env bash
  2#
  3# Run the complexity ratchet and the accessibility audit against the oldest
  4# supported simulator runtime before pushing.
  5#
  6# Enable once per clone:
  7#     git config core.hooksPath .githooks
  8#
  9# Why pre-push and not pre-commit: the suite takes ~85s. At pre-commit that
 10# blocks every commit, and a hook you routinely bypass with --no-verify is worse
 11# than no hook, because it trains you to ignore it. Pushes are far less frequent
 12# and map to the unit of work that actually reaches CI.
 13#
 14# Why only the floor tier: CI already audits the newest runtime on a clean
 15# checkout. The GitHub image has no old runtimes, so the floor is the one thing
 16# CI structurally cannot cover — and it is the one this machine can. Running
 17# both here would just double the wait to re-check what CI already does.
 18#
 19# Why the hook runs qlty at all: nothing else does. This repository's origin is
 20# gitbay, which has no build configured, so the smell gates in .qlty/qlty.toml
 21# are only ever evaluated by someone running them. A ratchet nobody runs holds
 22# nothing.
 23#
 24# Skip deliberately with: git push --no-verify
 25
 26set -euo pipefail
 27
 28repo_root=$(git rev-parse --show-toplevel)
 29cd "$repo_root"
 30
 31zero='0000000000000000000000000000000000000000'
 32changed=''
 33
 34# stdin: <local ref> <local sha> <remote ref> <remote sha>, one line per ref.
 35while read -r _local_ref local_sha _remote_ref remote_sha; do
 36  [ "$local_sha" = "$zero" ] && continue  # branch deletion
 37
 38  if [ "$remote_sha" = "$zero" ]; then
 39    # New branch: diff against the default branch rather than the whole history.
 40    base=$(git merge-base origin/main "$local_sha" 2>/dev/null || echo '')
 41    range="${base:+$base..}$local_sha"
 42  else
 43    range="$remote_sha..$local_sha"
 44  fi
 45
 46  changed="$changed$(git diff --name-only "$range" 2>/dev/null || true)"$'\n'
 47done
 48
 49if [ -z "$(printf '%s' "$changed" | tr -d '[:space:]')" ]; then
 50  exit 0
 51fi
 52
 53# Only pay the ~85s when something could actually change the rendered UI.
 54if ! printf '%s' "$changed" | grep -qE '\.(swift|xcassets|xcodeproj)|\.pbxproj|xcscheme'; then
 55  echo "pre-push: no Swift/project changes, skipping accessibility audit"
 56  exit 0
 57fi
 58
 59qlty=''
 60for candidate in "$(command -v qlty || true)" "$HOME/.qlty/bin/qlty"; do
 61  if [ -n "$candidate" ] && [ -x "$candidate" ]; then
 62    qlty="$candidate"
 63    break
 64  fi
 65done
 66
 67if [ -z "$qlty" ]; then
 68  echo "pre-push: qlty not found, skipping the complexity ratchet"
 69else
 70  echo "pre-push: checking the complexity ratchet"
 71
 72  # Counted from SARIF rather than trusted to the exit code, which is 0 even
 73  # with findings — `qlty smells --quiet` reports and does not fail. Only
 74  # file-complexity is counted: duplication findings exist today
 75  # (CloudSyncService has a 15-line similar block) and are reported, not gated.
 76  over=$("$qlty" smells --all --sarif 2>/dev/null | python3 -c "
 77import json, sys
 78try:
 79    document = json.load(sys.stdin)
 80except Exception:
 81    print('unknown')
 82    raise SystemExit(0)
 83print(sum(
 84    1
 85    for run in document.get('runs', [])
 86    for result in run.get('results', [])
 87    if result.get('ruleId') == 'qlty:file-complexity'
 88))
 89")
 90
 91  if [ "$over" = "unknown" ]; then
 92    echo "pre-push: could not read qlty output, skipping the ratchet"
 93  elif [ "$over" != "0" ]; then
 94    echo
 95    echo "pre-push: $over file(s) past the complexity threshold. Push aborted." >&2
 96    echo "          See './Scripts/../.qlty/qlty.toml'. Split the file, or lower" >&2
 97    echo "          the threshold deliberately — do not raise it to pass." >&2
 98    echo "          List them with 'qlty smells --all'." >&2
 99    echo "          Bypass with 'git push --no-verify'." >&2
100    exit 1
101  fi
102fi
103
104echo "pre-push: running accessibility audit on the floor runtime (~85s)"
105echo "          skip with 'git push --no-verify'"
106
107if ! ./Scripts/audit-a11y.sh floor; then
108  echo
109  echo "pre-push: audit could not run. Push aborted." >&2
110  echo "          Re-run with './Scripts/audit-a11y.sh floor' to see why," >&2
111  echo "          or bypass with 'git push --no-verify'." >&2
112  exit 1
113fi
114
115# Note: findings are reported, not enforced, so a clean exit here does not mean
116# zero findings — read the list above. Enforcement is controlled by
117# AccessibilityAuditHarness.enforcedAuditTypes.
118exit 0