|
2 | 2 | set -e |
3 | 3 | set -o pipefail |
4 | 4 |
|
| 5 | +# --- Argument Parsing --- |
| 6 | +# Initialize flags |
| 7 | +FORMAT_ALL=false |
| 8 | +RUFF_UNSAFE_FIXES_FLAG="" |
| 9 | + |
| 10 | +# Process command-line arguments |
| 11 | +# We use a while loop with shift to process each argument |
| 12 | +while [[ "$#" -gt 0 ]]; do |
| 13 | + case "$1" in |
| 14 | + --all) |
| 15 | + FORMAT_ALL=true |
| 16 | + echo "Detected --all flag: Formatting all Python files." |
| 17 | + shift # Consume the argument |
| 18 | + ;; |
| 19 | + --unsafe-fixes) |
| 20 | + RUFF_UNSAFE_FIXES_FLAG="--unsafe-fixes" |
| 21 | + echo "Detected --unsafe-fixes flag: Ruff will run with unsafe fixes." |
| 22 | + shift # Consume the argument |
| 23 | + ;; |
| 24 | + *) |
| 25 | + # Handle unknown arguments or just ignore them if we only care about specific ones |
| 26 | + echo "Warning: Unknown argument '$1'. Ignoring." |
| 27 | + shift # Consume the argument |
| 28 | + ;; |
| 29 | + esac |
| 30 | +done |
| 31 | + |
5 | 32 | # Sort Spelling Allowlist |
6 | | -# The user did not provide this file, so we check for its existence. |
7 | 33 | SPELLING_ALLOW_FILE=".github/actions/spelling/allow.txt" |
8 | 34 | if [ -f "$SPELLING_ALLOW_FILE" ]; then |
| 35 | + echo "Sorting and de-duplicating $SPELLING_ALLOW_FILE" |
9 | 36 | sort -u "$SPELLING_ALLOW_FILE" -o "$SPELLING_ALLOW_FILE" |
10 | 37 | fi |
11 | 38 |
|
12 | | -TARGET_BRANCH="origin/${GITHUB_BASE_REF:-main}" |
13 | | -git fetch origin "${GITHUB_BASE_REF:-main}" --depth=1 |
| 39 | +CHANGED_FILES="" |
| 40 | + |
| 41 | +if $FORMAT_ALL; then |
| 42 | + echo "Formatting all Python files in the repository." |
| 43 | + # Find all Python files, excluding grpc generated files as per original logic. |
| 44 | + # `sort -u` ensures unique files and consistent ordering for display/xargs. |
| 45 | + CHANGED_FILES=$(find . -name '*.py' -not -path './src/a2a/grpc/*' | sort -u) |
14 | 46 |
|
15 | | -# Find merge base between HEAD and target branch |
16 | | -MERGE_BASE=$(git merge-base HEAD "$TARGET_BRANCH") |
| 47 | + if [ -z "$CHANGED_FILES" ]; then |
| 48 | + echo "No Python files found to format." |
| 49 | + exit 0 |
| 50 | + fi |
| 51 | +else |
| 52 | + echo "No '--all' flag found. Formatting changed Python files based on git diff." |
| 53 | + TARGET_BRANCH="origin/${GITHUB_BASE_REF:-main}" |
| 54 | + git fetch origin "${GITHUB_BASE_REF:-main}" --depth=1 |
17 | 55 |
|
18 | | -# Get python files changed in this PR, excluding grpc generated files |
19 | | -CHANGED_FILES=$(git diff --name-only --diff-filter=ACMRTUXB "$MERGE_BASE" HEAD -- '*.py' ':!src/a2a/grpc/*') |
| 56 | + MERGE_BASE=$(git merge-base HEAD "$TARGET_BRANCH") |
20 | 57 |
|
21 | | -if [ -z "$CHANGED_FILES" ]; then |
22 | | - echo "No changed Python files to format." |
23 | | - exit 0 |
| 58 | + # Get python files changed in this PR, excluding grpc generated files |
| 59 | + CHANGED_FILES=$(git diff --name-only --diff-filter=ACMRTUXB "$MERGE_BASE" HEAD -- '*.py' ':!src/a2a/grpc/*') |
| 60 | + |
| 61 | + if [ -z "$CHANGED_FILES" ]; then |
| 62 | + echo "No changed Python files to format." |
| 63 | + exit 0 |
| 64 | + fi |
24 | 65 | fi |
25 | 66 |
|
26 | | -echo "Formatting changed files:" |
| 67 | +echo "Files to be formatted:" |
27 | 68 | echo "$CHANGED_FILES" |
28 | 69 |
|
29 | | -# Formatters are already installed in the activated venv from the GHA step. |
30 | | -# Use xargs to pass the file list to the formatters. |
| 70 | +# Helper function to run formatters with the list of files. |
| 71 | +# The list of files is passed to xargs via stdin. |
31 | 72 | run_formatter() { |
32 | 73 | echo "$CHANGED_FILES" | xargs -r "$@" |
33 | 74 | } |
34 | 75 |
|
| 76 | +echo "Running pyupgrade..." |
35 | 77 | run_formatter pyupgrade --exit-zero-even-if-changed --py310-plus |
| 78 | +echo "Running autoflake..." |
36 | 79 | run_formatter autoflake -i -r --remove-all-unused-imports |
37 | | -run_formatter ruff check --fix-only |
| 80 | +echo "Running ruff check (fix-only)..." |
| 81 | +run_formatter ruff check --fix-only $RUFF_UNSAFE_FIXES_FLAG |
| 82 | +echo "Running ruff format..." |
38 | 83 | run_formatter ruff format |
| 84 | + |
| 85 | +echo "Formatting complete." |
0 commit comments