#!/usr/bin/env bash # mr-size-gate — deterministic reviewable-lines check for a branch/MR vs a base ref. # # Counts "reviewable lines" (added + removed) that a human must actually read: # excludes lockfiles, generated/asset files, binaries, and pure renames, so a # 40,000-line lockfile bump or a directory rename never hides real changes — # and never inflates them. # # Usage: # mr-size-gate.sh [options] [base-ref] # Options: # --base REF base ref (default: origin/main, origin/master, main, master) # --head REF head ref (default: HEAD) # --warn N warn threshold in reviewable lines (default 200, env MRGATE_WARN) # --fail N fail threshold in reviewable lines (default 400, env MRGATE_FAIL) # --exclude PAT extra glob pattern to exclude, matches full path (repeatable; # env MRGATE_EXCLUDE, comma/space separated) # --strict exit non-zero also in the warn zone # --json machine-readable output # # Exit codes: 0 ok (or warn without --strict), 1 fail (or warn with --strict), 2 error set -euo pipefail warn="${MRGATE_WARN:-200}" fail="${MRGATE_FAIL:-400}" strict=0 json=0 declare -a pats=() base="" head="HEAD" usage() { sed -n '2,22p' "$0" | sed 's/^# \{0,1\}//'; } while [ $# -gt 0 ]; do case "$1" in --base) base="$2"; shift 2;; --head) head="$2"; shift 2;; --warn) warn="$2"; shift 2;; --fail) fail="$2"; shift 2;; --exclude) pats+=("$2"); shift 2;; --strict) strict=1; shift;; --json) json=1; shift;; -h|--help) usage; exit 0;; --*) echo "error: unknown option $1" >&2; usage >&2; exit 2;; *) base="$1"; shift;; esac done if [ -n "${MRGATE_EXCLUDE:-}" ]; then # shellcheck disable=SC2206 pats+=(${MRGATE_EXCLUDE//,/ }) fi git rev-parse --git-dir >/dev/null 2>&1 || { echo "error: not inside a git repository" >&2; exit 2; } if [ -z "$base" ]; then for cand in origin/main origin/master main master; do if git rev-parse --verify --quiet "$cand" >/dev/null 2>&1; then base="$cand"; break; fi done fi [ -n "$base" ] || { echo "error: no base ref found (tried origin/main, origin/master, main, master) — pass --base" >&2; exit 2; } git rev-parse --verify --quiet "$base" >/dev/null 2>&1 || { echo "error: base ref '$base' not found" >&2; exit 2; } git rev-parse --verify --quiet "$head" >/dev/null 2>&1 || { echo "error: head ref '$head' not found" >&2; exit 2; } declare -A nfiles=([reviewable]=0 [lockfile]=0 [generated]=0 [asset]=0 [binary]=0 [rename]=0 [user]=0) declare -A nlines=([reviewable]=0 [lockfile]=0 [generated]=0 [asset]=0 [binary]=0 [rename]=0 [user]=0) classify() { local added="$1" removed="$2" path="$3" base_name base_name="${path##*/}" if [ "$added" = "-" ]; then nfiles[binary]=$((nfiles[binary]+1)); return; fi if [ "$added" = "0" ] && [ "$removed" = "0" ] && [[ "$path" == *"=>"* ]]; then nfiles[rename]=$((nfiles[rename]+1)); return fi case "$base_name" in package-lock.json|yarn.lock|pnpm-lock.yaml|bun.lockb|bun.lock|deno.lock|go.sum|go.work.sum|Cargo.lock|poetry.lock|uv.lock|Gemfile.lock|composer.lock|Pipfile.lock|packages.locked|gradle.lockfile|flake.lock) nfiles[lockfile]=$((nfiles[lockfile]+1)); return;; esac case "$path" in *.min.js|*.min.mjs|*.min.css|*.js.map|*.css.map|*.snap|*.pb.go|*_pb2.py|*_pb2_grpc.py|*.generated.*|*.asset.php) nfiles[generated]=$((nfiles[generated]+1)); return;; esac case "$base_name" in *.svg|*.svgz) nfiles[asset]=$((nfiles[asset]+1)); return;; esac local pat for pat in ${pats[@]+"${pats[@]}"}; do # shellcheck disable=SC2053 # unquoted glob match is intended [[ "$path" == $pat ]] && { nfiles[user]=$((nfiles[user]+1)); return; } done nfiles[reviewable]=$((nfiles[reviewable]+1)) nlines[reviewable]=$((nlines[reviewable] + added + removed)) } diff="$(git diff --numstat --find-renames "$base...$head")" || { echo "error: git diff failed (are $base and $head related?)" >&2; exit 2; } while IFS=$'\t' read -r added removed path; do [ -n "${path:-}" ] || continue classify "$added" "$removed" "$path" done <<<"$diff" total=${nlines[reviewable]} if [ "$total" -gt "$fail" ]; then verdict="FAIL"; rc=1 elif [ "$total" -gt "$warn" ]; then verdict="WARN"; rc=$(( strict ? 1 : 0 )) else verdict="OK"; rc=0 fi if [ "$json" = "1" ]; then jq -n \ --arg base "$base" --arg head "$head" \ --argjson reviewable_lines "$total" \ --argjson reviewable_files "${nfiles[reviewable]}" \ --argjson warn "$warn" --argjson fail "$fail" \ --arg verdict "$verdict" \ --argjson excluded "$(jq -n \ --argjson lockfile "${nfiles[lockfile]}" --argjson generated "${nfiles[generated]}" \ --argjson asset "${nfiles[asset]}" --argjson binary "${nfiles[binary]}" \ --argjson rename "${nfiles[rename]}" --argjson user "${nfiles[user]}" \ '{lockfile: $lockfile, generated: $generated, asset: $asset, binary: $binary, pure_renames: $rename, user_patterns: $user}')" \ '{base: $base, head: $head, reviewable_lines: $reviewable_lines, reviewable_files: $reviewable_files, excluded_files: $excluded, warn_threshold: $warn, fail_threshold: $fail, verdict: $verdict}' else echo "MR size gate: $base ... $head" echo " reviewable: $total lines across ${nfiles[reviewable]} files" echo " excluded files: lockfile=${nfiles[lockfile]} generated=${nfiles[generated]} asset=${nfiles[asset]} binary=${nfiles[binary]} pure-renames=${nfiles[rename]} user-patterns=${nfiles[user]}" echo " limits: warn $warn / fail $fail reviewable lines (≈30-minute senior review budget: 200 comfort, 400 ceiling)" case "$verdict" in OK) echo " verdict: OK — comfortably reviewable in one sitting";; WARN) echo " verdict: WARN — at the edge of a 30-minute review; consider trimming or splitting";; FAIL) echo " verdict: FAIL — over the review budget; split the MR before requesting review";; esac fi exit "$rc"