Skip to content

Commit 5b55040

Browse files
committed
Update formatting script to only check git tracked files
1 parent 16f54ee commit 5b55040

File tree

1 file changed

+58
-54
lines changed

1 file changed

+58
-54
lines changed

scripts/format.sh

Lines changed: 58 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,78 +8,82 @@ FORMAT_ALL=false
88
RUFF_UNSAFE_FIXES_FLAG=""
99

1010
# Process command-line arguments
11-
# We use a while loop with shift to process each argument
1211
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
12+
case "$1" in
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+
;;
28+
esac
3029
done
3130

3231
# Sort Spelling Allowlist
3332
SPELLING_ALLOW_FILE=".github/actions/spelling/allow.txt"
3433
if [ -f "$SPELLING_ALLOW_FILE" ]; then
35-
echo "Sorting and de-duplicating $SPELLING_ALLOW_FILE"
36-
sort -u "$SPELLING_ALLOW_FILE" -o "$SPELLING_ALLOW_FILE"
34+
echo "Sorting and de-duplicating $SPELLING_ALLOW_FILE"
35+
sort -u "$SPELLING_ALLOW_FILE" -o "$SPELLING_ALLOW_FILE"
3736
fi
3837

3938
CHANGED_FILES=""
4039

4140
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)
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/*')
5143
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
55-
56-
MERGE_BASE=$(git merge-base HEAD "$TARGET_BRANCH")
44+
echo "Finding changed Python files based on git diff..."
45+
TARGET_BRANCH="origin/${GITHUB_BASE_REF:-main}"
46+
git fetch origin "${GITHUB_BASE_REF:-main}" --depth=1
5747

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/*')
48+
MERGE_BASE=$(git merge-base HEAD "$TARGET_BRANCH")
6049

61-
if [ -z "$CHANGED_FILES" ]; then
62-
echo "No changed Python files to format."
63-
exit 0
64-
fi
50+
# Get python files changed in this PR, excluding grpc generated files.
51+
CHANGED_FILES=$(git diff --name-only --diff-filter=ACMRTUXB "$MERGE_BASE" HEAD -- '*.py' ':!src/a2a/grpc/*')
6552
fi
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.
7264
run_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

Comments
 (0)