@@ -8,24 +8,23 @@ FORMAT_ALL=false
88RUFF_UNSAFE_FIXES_FLAG=" "
99
1010# Process command-line arguments
11- # We use a while loop with shift to process each argument
1211while [[ " $# " -gt 0 ]]; do
1312 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- ;;
13+ --all)
14+ FORMAT_ALL=true
15+ echo " Detected --all flag: Formatting all tracked Python files."
16+ shift # Consume the argument
17+ ;;
18+ --unsafe-fixes)
19+ RUFF_UNSAFE_FIXES_FLAG=" --unsafe-fixes"
20+ echo " Detected --unsafe-fixes flag: Ruff will run with unsafe fixes."
21+ shift # Consume the argument
22+ ;;
23+ * )
24+ # Handle unknown arguments or just ignore them
25+ echo " Warning: Unknown argument '$1 '. Ignoring."
26+ shift # Consume the argument
27+ ;;
2928 esac
3029done
3130
3938CHANGED_FILES=" "
4039
4140if $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)
46-
47- if [ -z " $CHANGED_FILES " ]; then
48- echo " No Python files found to format."
49- exit 0
50- fi
41+ echo " Finding all tracked Python files in the repository..."
42+ CHANGED_FILES=$( git ls-files -- ' *.py' ' :!src/a2a/grpc/*' )
5143else
52- echo " No '--all' flag found. Formatting changed Python files based on git diff."
44+ echo " Finding changed Python files based on git diff.. ."
5345 TARGET_BRANCH=" origin/${GITHUB_BASE_REF:- main} "
5446 git fetch origin " ${GITHUB_BASE_REF:- main} " --depth=1
5547
5648 MERGE_BASE=$( git merge-base HEAD " $TARGET_BRANCH " )
5749
58- # Get python files changed in this PR, excluding grpc generated files
50+ # Get python files changed in this PR, excluding grpc generated files.
5951 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
6552fi
6653
67- echo " Files to be formatted:"
68- echo " $CHANGED_FILES "
54+ # Exit if no files were found
55+ if [ -z " $CHANGED_FILES " ]; then
56+ echo " No changed or tracked Python files to format."
57+ exit 0
58+ fi
6959
70- # Helper function to run formatters with the list of files.
71- # The list of files is passed to xargs via stdin.
60+ # --- Helper Function ---
61+ # Runs a command on a list of files passed via stdin.
62+ # $1: A string containing the list of files (space-separated).
63+ # $2...: The command and its arguments to run.
7264run_formatter () {
73- echo " $CHANGED_FILES " | xargs -r " $@ "
65+ local files_to_format=" $1 "
66+ shift # Remove the file list from the arguments
67+ if [ -n " $files_to_format " ]; then
68+ echo " $files_to_format " | xargs -r " $@ "
69+ fi
7470}
7571
76- echo " Running pyupgrade..."
77- run_formatter pyupgrade --exit-zero-even-if-changed --py310-plus
78- echo " Running autoflake..."
79- run_formatter autoflake -i -r --remove-all-unused-imports
80- echo " Running ruff check (fix-only)..."
81- run_formatter ruff check --fix $RUFF_UNSAFE_FIXES_FLAG
82- echo " Running ruff format..."
83- run_formatter ruff format
72+ # --- Python File Formatting ---
73+ if [ -n " $CHANGED_FILES " ]; then
74+ echo " --- Formatting Python Files ---"
75+ echo " Files to be formatted:"
76+ echo " $CHANGED_FILES "
77+
78+ echo " Running autoflake..."
79+ run_formatter " $CHANGED_FILES " autoflake -i -r --remove-all-unused-imports
80+ echo " Running ruff check (fix-only)..."
81+ run_formatter " $CHANGED_FILES " ruff check --fix-only $RUFF_UNSAFE_FIXES_FLAG
82+ echo " Running ruff format..."
83+ run_formatter " $CHANGED_FILES " ruff format
84+ echo " Python formatting complete."
85+ else
86+ echo " No Python files to format."
87+ fi
8488
85- echo " Formatting complete."
89+ echo " All formatting tasks are complete."
0 commit comments