|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Check that a PR does not exceed a configurable line-change threshold. |
| 3 | +# |
| 4 | +# Excludes generated files (lock files, etc.) from the count. |
| 5 | +# Exits 0 on success, 1 on failure, and prints a warning when the |
| 6 | +# soft threshold is exceeded but the hard threshold is not. |
| 7 | +# |
| 8 | +# Environment variables (optional): |
| 9 | +# PR_SIZE_WARN - soft threshold (default: 300) |
| 10 | +# PR_SIZE_FAIL - hard threshold (default: 500) |
| 11 | +# |
| 12 | +# Usage: check-pr-size.sh <base-sha> |
| 13 | +set -euo pipefail |
| 14 | + |
| 15 | +base="${1:?Usage: check-pr-size.sh <base-sha>}" |
| 16 | +warn_threshold="${PR_SIZE_WARN:-300}" |
| 17 | +fail_threshold="${PR_SIZE_FAIL:-500}" |
| 18 | + |
| 19 | +# Patterns for generated/vendored files to exclude from the count. |
| 20 | +exclude_patterns=( |
| 21 | + "*.lock" |
| 22 | + "uv.lock" |
| 23 | + "poetry.lock" |
| 24 | + "package-lock.json" |
| 25 | + "yarn.lock" |
| 26 | + "pnpm-lock.yaml" |
| 27 | + "Cargo.lock" |
| 28 | +) |
| 29 | + |
| 30 | +# Build the pathspec exclusion arguments. |
| 31 | +pathspec_args=() |
| 32 | +for pattern in "${exclude_patterns[@]}"; do |
| 33 | + pathspec_args+=(":(exclude)${pattern}") |
| 34 | +done |
| 35 | + |
| 36 | +# Count added + removed lines, excluding generated files. |
| 37 | +diff_stat=$(git diff --numstat "${base}...HEAD" -- . "${pathspec_args[@]}") |
| 38 | + |
| 39 | +total=0 |
| 40 | +while IFS=$'\t' read -r added removed _path; do |
| 41 | + # Binary files show "-" for added/removed; skip them. |
| 42 | + if [ "$added" = "-" ] || [ "$removed" = "-" ]; then |
| 43 | + continue |
| 44 | + fi |
| 45 | + total=$((total + added + removed)) |
| 46 | +done <<< "$diff_stat" |
| 47 | + |
| 48 | +echo "Total lines changed (excluding generated files): $total" |
| 49 | + |
| 50 | +if [ "$total" -gt "$fail_threshold" ]; then |
| 51 | + echo "::error::PR is too large ($total lines changed, threshold: $fail_threshold)." |
| 52 | + echo "Consider splitting into smaller, focused PRs." |
| 53 | + exit 1 |
| 54 | +fi |
| 55 | + |
| 56 | +if [ "$total" -gt "$warn_threshold" ]; then |
| 57 | + echo "::warning::PR is getting large ($total lines changed, warn threshold: $warn_threshold)." |
| 58 | + echo "Consider whether this can be split into smaller PRs." |
| 59 | +fi |
| 60 | + |
| 61 | +echo "PR size check passed." |
0 commit comments