|
2 | 2 | set -e |
3 | 3 | set -o pipefail |
4 | 4 |
|
| 5 | +# --- Argument Parsing --- |
| 6 | +# Check if the first argument is '--all' |
| 7 | +FORMAT_ALL=false |
| 8 | +if [[ "$1" == "--all" ]]; then |
| 9 | + FORMAT_ALL=true |
| 10 | + shift # Consume the '--all' argument so it doesn't interfere with later logic if any |
| 11 | +fi |
| 12 | + |
5 | 13 | # Sort Spelling Allowlist |
6 | | -# The user did not provide this file, so we check for its existence. |
| 14 | +# This operation is independent of file formatting logic, keeping it at the top. |
7 | 15 | SPELLING_ALLOW_FILE=".github/actions/spelling/allow.txt" |
8 | 16 | if [ -f "$SPELLING_ALLOW_FILE" ]; then |
| 17 | + echo "Sorting and de-duplicating $SPELLING_ALLOW_FILE" |
9 | 18 | sort -u "$SPELLING_ALLOW_FILE" -o "$SPELLING_ALLOW_FILE" |
10 | 19 | fi |
11 | 20 |
|
12 | | -TARGET_BRANCH="origin/${GITHUB_BASE_REF:-main}" |
13 | | -git fetch origin "${GITHUB_BASE_REF:-main}" --depth=1 |
| 21 | +CHANGED_FILES="" |
| 22 | + |
| 23 | +if $FORMAT_ALL; then |
| 24 | + echo "The '--all' flag was passed. Formatting all Python files in the repository." |
| 25 | + # The prompt requested "all files (.)" but the script's formatters (pyupgrade, autoflake, ruff) |
| 26 | + # are Python-specific. Therefore, this command will find and format all Python files. |
| 27 | + # It excludes files under './src/a2a/grpc/' as per the original script's logic. |
| 28 | + CHANGED_FILES=$(find . -name '*.py' -not -path './src/a2a/grpc/*' | sort -u) |
14 | 29 |
|
15 | | -# Find merge base between HEAD and target branch |
16 | | -MERGE_BASE=$(git merge-base HEAD "$TARGET_BRANCH") |
| 30 | + if [ -z "$CHANGED_FILES" ]; then |
| 31 | + echo "No Python files found to format." |
| 32 | + exit 0 |
| 33 | + fi |
| 34 | +else |
| 35 | + echo "No '--all' flag found. Formatting changed Python files based on git diff." |
| 36 | + TARGET_BRANCH="origin/${GITHUB_BASE_REF:-main}" |
| 37 | + # Fetch the target branch to ensure it's available for git merge-base |
| 38 | + git fetch origin "${GITHUB_BASE_REF:-main}" --depth=1 |
17 | 39 |
|
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/*') |
| 40 | + # Find the merge base commit between HEAD (current branch) and the target branch |
| 41 | + MERGE_BASE=$(git merge-base HEAD "$TARGET_BRANCH") |
20 | 42 |
|
21 | | -if [ -z "$CHANGED_FILES" ]; then |
22 | | - echo "No changed Python files to format." |
23 | | - exit 0 |
| 43 | + # Get Python files that have been Added, Copied, Modified, Renamed, had Type change, are Unmerged, Unknown, or Broken pairing. |
| 44 | + # Exclude files under 'src/a2a/grpc/' from the list. |
| 45 | + CHANGED_FILES=$(git diff --name-only --diff-filter=ACMRTUXB "$MERGE_BASE" HEAD -- '*.py' ':!src/a2a/grpc/*') |
| 46 | + |
| 47 | + if [ -z "$CHANGED_FILES" ]; then |
| 48 | + echo "No changed Python files to format." |
| 49 | + exit 0 |
| 50 | + fi |
24 | 51 | fi |
25 | 52 |
|
26 | | -echo "Formatting changed files:" |
| 53 | +echo "Files to be formatted:" |
| 54 | +# Using echo "$CHANGED_FILES" directly will print files separated by newlines, which is clear. |
27 | 55 | echo "$CHANGED_FILES" |
28 | 56 |
|
29 | | -# Formatters are already installed in the activated venv from the GHA step. |
30 | | -# Use xargs to pass the file list to the formatters. |
| 57 | +# Define a helper function to run formatters with the list of files. |
| 58 | +# The list of files is passed to xargs via stdin. |
31 | 59 | run_formatter() { |
| 60 | + # `xargs -r` ensures the command is not run if there are no inputs. |
| 61 | + # This is important to prevent formatters from running without files, |
| 62 | + # which might result in errors or unintended behavior. |
32 | 63 | echo "$CHANGED_FILES" | xargs -r "$@" |
33 | 64 | } |
34 | 65 |
|
| 66 | +echo "Running pyupgrade..." |
35 | 67 | run_formatter pyupgrade --exit-zero-even-if-changed --py310-plus |
| 68 | +echo "Running autoflake..." |
36 | 69 | run_formatter autoflake -i -r --remove-all-unused-imports |
| 70 | +echo "Running ruff check (fix-only)..." |
37 | 71 | run_formatter ruff check --fix-only |
| 72 | +echo "Running ruff format..." |
38 | 73 | run_formatter ruff format |
| 74 | + |
| 75 | +echo "Formatting complete." |
0 commit comments