.githooks/pre-push

123 lines · 4293 bytes · executable

  1#!/usr/bin/env bash
  2#
  3# Run the complexity ratchet, and warn when the pushed tree has no recorded
  4# accessibility audit pass. The audit is optional; the warning does not block.
  5#
  6# Enable once per clone:
  7#     git config core.hooksPath .githooks
  8#
  9# The audit runs before the push, not here: `./Scripts/audit-a11y.sh` on a
 10# clean tree records the tree it passed on, and this hook looks it up. git opens
 11# the connection before running pre-push, and gitbay kills a push that moves no
 12# data for a minute or has not reached pre-receive fifteen minutes after taking
 13# its slot. The suite takes around twenty minutes, so run inside the hook it
 14# failed every push it gated.
 15#
 16# Why both tiers: audit coverage is not nested across OS versions — each runtime
 17# reports findings the others miss, in both directions, which
 18# Docs/ACCESSIBILITY.txt records per screen. There is no CI, so a tier that is
 19# not run is not run at all, and an enforced failure on it reaches main. Only a
 20# run of both tiers is recorded.
 21#
 22# Why the hook runs qlty at all: nothing else does. This repository's origin is
 23# gitbay, which has no build configured, so the smell gates in .qlty/qlty.toml
 24# are only ever evaluated by someone running them. A ratchet nobody runs holds
 25# nothing.
 26#
 27# Skip deliberately with: git push --no-verify
 28
 29set -euo pipefail
 30
 31repo_root=$(git rev-parse --show-toplevel)
 32cd "$repo_root"
 33
 34# shellcheck source=../Scripts/lib/audit-pass.sh
 35source Scripts/lib/audit-pass.sh
 36
 37zero='0000000000000000000000000000000000000000'
 38needs_audit=''
 39
 40# stdin: <local ref> <local sha> <remote ref> <remote sha>, one line per ref.
 41while read -r _local_ref local_sha _remote_ref remote_sha; do
 42  [ "$local_sha" = "$zero" ] && continue  # branch deletion
 43
 44  if [ "$remote_sha" = "$zero" ]; then
 45    # New branch: diff against the default branch rather than the whole history.
 46    base=$(git merge-base origin/main "$local_sha" 2>/dev/null || echo '')
 47    range="${base:+$base..}$local_sha"
 48  else
 49    range="$remote_sha..$local_sha"
 50  fi
 51
 52  if git diff --name-only "$range" 2>/dev/null | grep -qE "$AUDIT_INPUTS"; then
 53    needs_audit="$needs_audit $local_sha"
 54  fi
 55done
 56
 57if [ -z "$needs_audit" ]; then
 58  echo "pre-push: no Swift/project changes, skipping accessibility audit"
 59  exit 0
 60fi
 61
 62qlty=''
 63for candidate in "$(command -v qlty || true)" "$HOME/.qlty/bin/qlty"; do
 64  if [ -n "$candidate" ] && [ -x "$candidate" ]; then
 65    qlty="$candidate"
 66    break
 67  fi
 68done
 69
 70if [ -z "$qlty" ]; then
 71  echo "pre-push: qlty not found, skipping the complexity ratchet"
 72else
 73  echo "pre-push: checking the complexity ratchet"
 74
 75  # Counted from SARIF rather than trusted to the exit code, which is 0 even
 76  # with findings — `qlty smells --quiet` reports and does not fail. Only
 77  # file-complexity is counted: duplication findings exist today
 78  # (CloudSyncService has a 15-line similar block) and are reported, not gated.
 79  over=$("$qlty" smells --all --sarif 2>/dev/null | python3 -c "
 80import json, sys
 81try:
 82    document = json.load(sys.stdin)
 83except Exception:
 84    print('unknown')
 85    raise SystemExit(0)
 86print(sum(
 87    1
 88    for run in document.get('runs', [])
 89    for result in run.get('results', [])
 90    if result.get('ruleId') == 'qlty:file-complexity'
 91))
 92")
 93
 94  if [ "$over" = "unknown" ]; then
 95    echo "pre-push: could not read qlty output, skipping the ratchet"
 96  elif [ "$over" != "0" ]; then
 97    echo
 98    echo "pre-push: $over file(s) past the complexity threshold. Push aborted." >&2
 99    echo "          See './Scripts/../.qlty/qlty.toml'. Split the file, or lower" >&2
100    echo "          the threshold deliberately — do not raise it to pass." >&2
101    echo "          List them with 'qlty smells --all'." >&2
102    echo "          Bypass with 'git push --no-verify'." >&2
103    exit 1
104  fi
105fi
106
107passes=$(audit_pass_file)
108missing=''
109for sha in $needs_audit; do
110  grep -qxF "$(git rev-parse "$sha^{tree}")" "$passes" 2>/dev/null || missing="$missing $sha"
111done
112
113if [ -n "$missing" ]; then
114  echo "pre-push: no recorded accessibility audit pass (optional) for:"
115  for sha in $missing; do
116    echo "            $(git log -1 --format='%h %s' "$sha")"
117  done
118  echo "          Run './Scripts/audit-a11y.sh' on a clean tree to check it."
119  exit 0
120fi
121
122echo "pre-push: accessibility audit passed on this tree"
123exit 0