From 761061a0439f9d40e59960663d66dc6744da886e Mon Sep 17 00:00:00 2001 From: Nick Dimiduk Date: Mon, 11 May 2026 12:27:39 +0200 Subject: [PATCH] YETUS-1276. Add --patch-mode flag to control diff-vs-patch preference YETUS-983 hardcoded dryrun_both_files to prefer cumulative .diff over per-commit .patch. This may not work for all projects (binary handling is a known concern), so make the preference configurable. --patch-mode=diff (default) preserves YETUS-983 behavior. --patch-mode=patch restores pre-YETUS-983 behavior. The non-preferred format is always tried as fallback. --- precommit/src/main/shell/core.d/01-common.sh | 4 ++ precommit/src/main/shell/core.d/patchfiles.sh | 39 +++++++++++++------ precommit/src/main/shell/test-patch.sh | 1 + 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/precommit/src/main/shell/core.d/01-common.sh b/precommit/src/main/shell/core.d/01-common.sh index a81668fa7..f49876fcf 100755 --- a/precommit/src/main/shell/core.d/01-common.sh +++ b/precommit/src/main/shell/core.d/01-common.sh @@ -201,6 +201,10 @@ function common_args delete_parameter "${i}" PATCH=${i#*=} ;; + --patch-mode=*) + delete_parameter "${i}" + PATCH_MODE=${i#*=} + ;; --patch-dir=*) delete_parameter "${i}" PATCH_DIR=${i#*=} diff --git a/precommit/src/main/shell/core.d/patchfiles.sh b/precommit/src/main/shell/core.d/patchfiles.sh index d8dd4d5db..6684a14b1 100755 --- a/precommit/src/main/shell/core.d/patchfiles.sh +++ b/precommit/src/main/shell/core.d/patchfiles.sh @@ -26,6 +26,7 @@ PATCH_METHOD="" PATCH_METHODS=("gitapply" "patchcmd") PATCH_LEVEL=0 PATCH_HINT="" +PATCH_MODE="diff" ## @description Use curl to download the patch as a last resort ## @audience private @@ -340,23 +341,39 @@ function patchfile_dryrun_driver return 1 } -## @description dryrun both PATCH and DIFF and determine which one to use +## @description dryrun both PATCH and DIFF and determine which one to use. +## @description PATCH_MODE controls try-order: "diff" (default) avoids stale-file +## @description bugs from per-commit .patch stanzas (YETUS-983); "patch" restores +## @description the pre-YETUS-983 behavior for repos that need it. ## @replaceable no ## @audience private ## @stability evolving function dryrun_both_files { - # prefer the cumulative diff: it represents the PR's net change and avoids - # stale-file bugs when per-commit .patch stanzas add then rename/delete files - # (YETUS-983). Binary files are preserved when the diff is generated locally - # with --binary. - if [[ -f "${INPUT_DIFF_FILE}" ]] && patchfile_dryrun_driver "${INPUT_DIFF_FILE}"; then - INPUT_APPLY_TYPE="diff" - INPUT_APPLIED_FILE="${INPUT_DIFF_FILE}" + declare first_file + declare first_type + declare second_file + declare second_type + + if [[ "${PATCH_MODE}" == "patch" ]]; then + first_file="${INPUT_PATCH_FILE}" + first_type="patch" + second_file="${INPUT_DIFF_FILE}" + second_type="diff" + else + first_file="${INPUT_DIFF_FILE}" + first_type="diff" + second_file="${INPUT_PATCH_FILE}" + second_type="patch" + fi + + if [[ -f "${first_file}" ]] && patchfile_dryrun_driver "${first_file}"; then + INPUT_APPLY_TYPE="${first_type}" + INPUT_APPLIED_FILE="${first_file}" return 0 - elif [[ -f "${INPUT_PATCH_FILE}" ]] && patchfile_dryrun_driver "${INPUT_PATCH_FILE}"; then - INPUT_APPLY_TYPE="patch" - INPUT_APPLIED_FILE="${INPUT_PATCH_FILE}" + elif [[ -f "${second_file}" ]] && patchfile_dryrun_driver "${second_file}"; then + INPUT_APPLY_TYPE="${second_type}" + INPUT_APPLIED_FILE="${second_file}" return 0 else return 1 diff --git a/precommit/src/main/shell/test-patch.sh b/precommit/src/main/shell/test-patch.sh index b471ccf91..2e26f1ff7 100755 --- a/precommit/src/main/shell/test-patch.sh +++ b/precommit/src/main/shell/test-patch.sh @@ -675,6 +675,7 @@ function yetus_usage yetus_add_option "--multijdktests=" "Comma delimited tests to use when multijdkdirs is used. (default: '${jdktlist}')" yetus_add_option "--offline" "Avoid connecting to the network" yetus_add_option "--patch-dir=" "The directory for working and output files (default '/tmp/test-patch-${PROJECT_NAME}/pid')" + yetus_add_option "--patch-mode=" "Try cumulative diff or per-commit patch first (default: '${PATCH_MODE}')" yetus_add_option "--personality=" "The personality file to load" yetus_add_option "--plugins=" "Specify which plug-ins to add/delete (comma delimited; use 'all' for all found) e.g. --plugins=all,-ant,-scalac (all plugins except ant and scalac)" yetus_add_option "--proclimit=" "Limit on the number of processes (default: ${PROC_LIMIT})"