File size: 2,364 Bytes
48ba1a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env bash
set -euo pipefail

usage() {
  cat <<'EOF'
Usage: scripts/check_changed_python.sh [--base REV] [--head REV] [--format] [--lint] [--typecheck]

Runs Ruff format check, Ruff lint, and/or mypy on Python files changed between two revisions.

Defaults:
  base: merge-base(HEAD, origin/dev) when origin/dev exists, else HEAD~1
  head: HEAD
  checks: format + lint + typecheck

Examples:
  bash scripts/check_changed_python.sh
  bash scripts/check_changed_python.sh --base origin/dev --lint --typecheck
  bash scripts/check_changed_python.sh --base "$BASE" --format
EOF
}

repo_root="$(git rev-parse --show-toplevel)"
cd "$repo_root"

base=""
head="HEAD"
declare -a checks=()

while [[ $# -gt 0 ]]; do
  case "$1" in
    --base)
      base="${2:?missing value for --base}"
      shift 2
      ;;
    --head)
      head="${2:?missing value for --head}"
      shift 2
      ;;
    --format)
      checks+=("format")
      shift
      ;;
    --lint)
      checks+=("lint")
      shift
      ;;
    --typecheck)
      checks+=("typecheck")
      shift
      ;;
    -h|--help)
      usage
      exit 0
      ;;
    *)
      echo "Unknown argument: $1" >&2
      usage >&2
      exit 2
      ;;
  esac
done

if [[ ${#checks[@]} -eq 0 ]]; then
  checks=("format" "lint" "typecheck")
fi

if [[ -z "$base" ]]; then
  if git show-ref --verify --quiet refs/remotes/origin/dev; then
    base="$(git merge-base "$head" origin/dev)"
  elif git rev-parse --verify -q "${head}~1" >/dev/null; then
    base="${head}~1"
  else
    echo "Unable to determine a base revision automatically" >&2
    exit 2
  fi
fi

mapfile -d '' files < <(git diff -z --name-only --diff-filter=ACMR "$base" "$head" -- '*.py')

if [[ ${#files[@]} -eq 0 ]]; then
  echo "No changed Python files between $base and $head"
  exit 0
fi

echo "Changed Python files between $base and $head:"
printf '  %s\n' "${files[@]}"

for check in "${checks[@]}"; do
  case "$check" in
    format)
      echo
      echo "==> Ruff format check"
      python -m ruff format --check --diff "${files[@]}"
      ;;
    lint)
      echo
      echo "==> Ruff lint"
      python -m ruff check "${files[@]}"
      ;;
    typecheck)
      echo
      echo "==> Mypy"
      python -m mypy --config-file pyproject.toml "${files[@]}"
      ;;
    *)
      echo "Unknown check: $check" >&2
      exit 2
      ;;
  esac
done