@@ -6,42 +6,57 @@ source "$SCRIPT_DIR/lib/json-emit.sh"
66
77LANGUAGE=" ${LANGUAGE:- python} "
88DIFF_FILE=" ${DIFF_FILE:-/ dev/ stdin} "
9- BASE_SHA=" ${BASE_SHA:- HEAD~10} "
9+ # Resolve BASE_SHA from the PR base ref rather than HEAD~10.
10+ if [ -z " ${BASE_SHA:- } " ]; then
11+ base_ref=" ${GITHUB_BASE_REF:- main} "
12+ if git rev-parse --verify " origin/${base_ref} " > /dev/null 2>&1 ; then
13+ BASE_SHA=$( git merge-base HEAD " origin/${base_ref} " 2> /dev/null || echo " HEAD~10" )
14+ elif git rev-parse --verify " $base_ref " > /dev/null 2>&1 ; then
15+ BASE_SHA=$( git merge-base HEAD " $base_ref " 2> /dev/null || echo " HEAD~10" )
16+ else
17+ BASE_SHA=" HEAD~10"
18+ fi
19+ fi
1020HEAD_SHA=" ${HEAD_SHA:- HEAD} "
1121
1222STARTED=$( now_iso)
1323findings=$( mktemp) ; trap ' rm -f "$findings"' EXIT
1424
1525diff_content=$( cat " $DIFF_FILE " 2> /dev/null || echo " " )
1626
27+ # Helper: `grep -c` returns "0" AND exit 1 on no match. Under `set -e` /
28+ # pipefail the || echo 0 idiom concatenates both, producing "0\n0" and
29+ # breaking arithmetic. Route through wc -l instead.
30+ count_lines () { echo " $1 " | grep -E " $2 " 2> /dev/null | wc -l | tr -d ' ' ; }
31+
1732# PR-SIZE-01: additions > 800
18- additions=$( echo " $diff_content " | grep -cE ' ^\+[^+]' || echo 0 )
33+ additions=$( count_lines " $diff_content " ' ^\+[^+]' )
1934if [ " $additions " -gt 800 ]; then
2035 emit_finding " PR-SIZE-01" " FLAG" " ." 1 \
2136 " PR has $additions additions (>800) — consider splitting into stacked PRs for easier review" \
2237 " See docs/CONTRIBUTING.md § Incremental Delivery for stacked-PR workflow" >> " $findings "
2338fi
2439
2540# PR-SIZE-02: > 15 files
26- files_touched=$( echo " $diff_content " | grep -cE ' ^diff --git' || echo 0 )
41+ files_touched=$( count_lines " $diff_content " ' ^diff --git' )
2742if [ " $files_touched " -gt 15 ]; then
2843 emit_finding " PR-SIZE-02" " FLAG" " ." 1 \
2944 " PR touches $files_touched files (>15) — consider splitting by concern" " " >> " $findings "
3045fi
3146
3247# PR-SIZE-03: > 3 modules
3348if [ " $LANGUAGE " = " python" ]; then
34- mods_count=$( echo " $diff_content " | grep -oE ' src/sap_cloud_sdk/[a-z_]+/' | sed ' s|src/sap_cloud_sdk/||; s|/$||' | sort -u | wc -l | tr -d ' ' )
49+ mods_count=$( echo " $diff_content " | grep -oE ' src/sap_cloud_sdk/[a-z_]+/' 2> /dev/null | sed ' s|src/sap_cloud_sdk/||; s|/$||' | sort -u | wc -l | tr -d ' ' )
3550else
36- mods_count=$( echo " $diff_content " | grep -oE ' src/main/java/com/sap/cloud/sdk/[a-z_]+/' | sed ' s|src/main/java/com/sap/cloud/sdk/||; s|/$||' | sort -u | wc -l | tr -d ' ' )
51+ mods_count=$( echo " $diff_content " | grep -oE ' src/main/java/com/sap/cloud/sdk/[a-z_]+/' 2> /dev/null | sed ' s|src/main/java/com/sap/cloud/sdk/||; s|/$||' | sort -u | wc -l | tr -d ' ' )
3752fi
3853if [ " $mods_count " -gt 3 ]; then
3954 emit_finding " PR-SIZE-03" " FLAG" " ." 1 \
4055 " PR modifies $mods_count modules (>3) — consider one PR per module" " " >> " $findings "
4156fi
4257
43- # PR-SIZE-05: > 30 commits
44- commit_count=$( git log " ${BASE_SHA} ..${HEAD_SHA} " --oneline 2> /dev/null | grep -v -c ' ^Merge ' || echo 0 )
58+ # PR-SIZE-05: > 30 commits (exclude merge commits by looking at parents)
59+ commit_count=$( git log " ${BASE_SHA} ..${HEAD_SHA} " --no-merges -- oneline 2> /dev/null | wc -l | tr -d ' ' )
4560if [ " $commit_count " -gt 30 ]; then
4661 emit_finding " PR-SIZE-05" " FLAG" " ." 1 \
4762 " PR has $commit_count commits (>30) — consider squashing or splitting" " " >> " $findings "
0 commit comments