@@ -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).
953954file (
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
963985add_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# =============================================================================
0 commit comments