Skip to content

Commit 5ac449d

Browse files
authored
Fix some local version issues using black and also "make format-pr" (#3783)
* build: make format-pr more robust - Support both 'main' and 'master' as base branch - Only pass existing files to the formatter - Eliminates noisy "ERROR: X jobs failed" when files were deleted - handles filenames with spaces * ci: run formatting check on ubuntu-24.04 (Python 3.12 instead of3.10) * update coding-conventions (prints black version)
1 parent 0c9dab0 commit 5ac449d

3 files changed

Lines changed: 32 additions & 10 deletions

File tree

.github/workflows/formatting.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ on:
1818
jobs:
1919
all:
2020
name: C/C++, CMake and Python
21-
runs-on: ubuntu-22.04
21+
runs-on: ubuntu-24.04
2222
timeout-minutes: 5
2323
steps:
2424
- uses: actions/checkout@v4

CMakeLists.txt

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -949,21 +949,43 @@ add_custom_target(
949949
COMMAND ${PROJECT_SOURCE_DIR}/external/coding-conventions/bin/format
950950
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
951951

952-
# Prepare a shell script to format only files modified with respect to master branch
952+
# Prepare a shell script to format only files modified with respect to master/main branch that still
953+
# exist on disk (filters out deleted files).
953954
file(
954955
WRITE ${CMAKE_CURRENT_BINARY_DIR}/format-pr.sh
955-
"\
956-
#!bash\n\
957-
set -e\n\
958-
cmd='cd ${PROJECT_SOURCE_DIR} && external/coding-conventions/bin/format `git diff --name-only master`'\n\
959-
echo $cmd\n\
960-
cd ${PROJECT_SOURCE_DIR} && external/coding-conventions/bin/format `git diff --name-only master`\n\
956+
"#!/bin/bash
957+
set -euo pipefail
958+
959+
# Try 'main' first, then fall back to 'master'
960+
for base in main master; do
961+
if git rev-parse --verify \"\$base\" >/dev/null 2>&1; then
962+
BASE_BRANCH=\"\$base\"
963+
break
964+
fi
965+
done
966+
967+
if [ -z \"\${BASE_BRANCH:-}\" ]; then
968+
echo \"Error: Neither 'main' nor 'master' branch found.\" >&2
969+
exit 1
970+
fi
971+
972+
echo \"Formatting changes vs '\$BASE_BRANCH' (existing files only)...\" >&2
973+
974+
# Get changed files (null-delimited so names with spaces work), only pass
975+
# files that still exist.
976+
git diff -z --name-only \"\$BASE_BRANCH\" | \\
977+
while IFS= read -r -d '' file; do
978+
if [ -e \"\$file\" ]; then
979+
printf '%s\\0' \"\$file\"
980+
fi
981+
done | \\
982+
xargs -0 --no-run-if-empty external/coding-conventions/bin/format \"\$@\"
961983
")
962984

963985
add_custom_target(
964986
format-pr
965987
COMMAND bash ${CMAKE_CURRENT_BINARY_DIR}/format-pr.sh
966-
COMMENT "Format only files modified with respect to master branch."
988+
COMMENT "Format only files modified with respect to main/master (existing files only)"
967989
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
968990

969991
# =============================================================================

external/coding-conventions

0 commit comments

Comments
 (0)