33set -o pipefail
44
55# --- Argument Parsing ---
6- # Check if the first argument is '--all'
6+ # Initialize flags
77FORMAT_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
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
1231
1332# Sort Spelling Allowlist
14- # This operation is independent of file formatting logic, keeping it at the top.
1533SPELLING_ALLOW_FILE=" .github/actions/spelling/allow.txt"
1634if [ -f " $SPELLING_ALLOW_FILE " ]; then
1735 echo " Sorting and de-duplicating $SPELLING_ALLOW_FILE "
2139CHANGED_FILES=" "
2240
2341if $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.
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.
2845 CHANGED_FILES=$( find . -name ' *.py' -not -path ' ./src/a2a/grpc/*' | sort -u)
2946
3047 if [ -z " $CHANGED_FILES " ]; then
@@ -34,14 +51,11 @@ if $FORMAT_ALL; then
3451else
3552 echo " No '--all' flag found. Formatting changed Python files based on git diff."
3653 TARGET_BRANCH=" origin/${GITHUB_BASE_REF:- main} "
37- # Fetch the target branch to ensure it's available for git merge-base
3854 git fetch origin " ${GITHUB_BASE_REF:- main} " --depth=1
3955
40- # Find the merge base commit between HEAD (current branch) and the target branch
4156 MERGE_BASE=$( git merge-base HEAD " $TARGET_BRANCH " )
4257
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.
58+ # Get python files changed in this PR, excluding grpc generated files
4559 CHANGED_FILES=$( git diff --name-only --diff-filter=ACMRTUXB " $MERGE_BASE " HEAD -- ' *.py' ' :!src/a2a/grpc/*' )
4660
4761 if [ -z " $CHANGED_FILES " ]; then
5165fi
5266
5367echo " Files to be formatted:"
54- # Using echo "$CHANGED_FILES" directly will print files separated by newlines, which is clear.
5568echo " $CHANGED_FILES "
5669
57- # Define a helper function to run formatters with the list of files.
70+ # Helper function to run formatters with the list of files.
5871# The list of files is passed to xargs via stdin.
5972run_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.
6373 echo " $CHANGED_FILES " | xargs -r " $@ "
6474}
6575
@@ -68,7 +78,7 @@ run_formatter pyupgrade --exit-zero-even-if-changed --py310-plus
6878echo " Running autoflake..."
6979run_formatter autoflake -i -r --remove-all-unused-imports
7080echo " Running ruff check (fix-only)..."
71- run_formatter ruff check --fix-only
81+ run_formatter ruff check --fix-only $RUFF_UNSAFE_FIXES_FLAG
7282echo " Running ruff format..."
7383run_formatter ruff format
7484
0 commit comments