Skip to content

Commit bb2c0ba

Browse files
committed
style: Add format all option
1 parent bc97cba commit bb2c0ba

File tree

1 file changed

+50
-13
lines changed

1 file changed

+50
-13
lines changed

scripts/format.sh

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,74 @@
22
set -e
33
set -o pipefail
44

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+
513
# 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.
715
SPELLING_ALLOW_FILE=".github/actions/spelling/allow.txt"
816
if [ -f "$SPELLING_ALLOW_FILE" ]; then
17+
echo "Sorting and de-duplicating $SPELLING_ALLOW_FILE"
918
sort -u "$SPELLING_ALLOW_FILE" -o "$SPELLING_ALLOW_FILE"
1019
fi
1120

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)
1429

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
1739

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")
2042

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
2451
fi
2552

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.
2755
echo "$CHANGED_FILES"
2856

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.
3159
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.
3263
echo "$CHANGED_FILES" | xargs -r "$@"
3364
}
3465

66+
echo "Running pyupgrade..."
3567
run_formatter pyupgrade --exit-zero-even-if-changed --py310-plus
68+
echo "Running autoflake..."
3669
run_formatter autoflake -i -r --remove-all-unused-imports
70+
echo "Running ruff check (fix-only)..."
3771
run_formatter ruff check --fix-only
72+
echo "Running ruff format..."
3873
run_formatter ruff format
74+
75+
echo "Formatting complete."

0 commit comments

Comments
 (0)