commit:     495d920b6c2dc735cde163ca58df7fafa6f76fae
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Tue Jul  1 18:16:32 2025 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Fri Jul  4 02:17:00 2025 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=495d920b

phase-helpers.sh: avoid repeated PATH searches for gpatch

Presently, the eapply() function checks whether gpatch is available by
using the hash builtin. If found, it is presumed to be GNU patch(1) and
is selected as the preferred implementation. However, in the unlikely
event that it is missing, the hash builtin will incur a PATH search upon
each occasion that the function is called. That is because the cache of
utility locations can only contain positive entries (found paths by
utility name), not negative entries (unfound paths by utility name).

Address this minor issue by having the "phase-helpers.sh" unit check for
the existence of gpatch at the time that it is sourced, and by having it
declare a patch() function that serves as a wrapper.

Signed-off-by: Kerin Millar <kfm <AT> plushkava.net>
Signed-off-by: Sam James <sam <AT> gentoo.org>

 bin/phase-helpers.sh | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/bin/phase-helpers.sh b/bin/phase-helpers.sh
index 94b922d45b..fef77e52d7 100644
--- a/bin/phase-helpers.sh
+++ b/bin/phase-helpers.sh
@@ -983,8 +983,13 @@ if ___eapi_has_einstalldocs; then
 fi
 
 if ___eapi_has_eapply; then
+       # For BSD userland support, use gpatch if available.
+       if hash gpatch 2>/dev/null; then
+               patch() { gpatch "$@"; }
+       fi
+
        eapply() {
-               local LC_ALL LC_COLLATE=C f i patch_cmd path
+               local LC_ALL LC_COLLATE=C f i path
                local -a operands options
 
                _eapply_patch() {
@@ -997,7 +1002,7 @@ if ___eapi_has_eapply; then
                        # -g0 to guarantee no VCS interaction
                        # --no-backup-if-mismatch not to pollute the sources
                        set -- -p1 -f -g0 --no-backup-if-mismatch "$@"
-                       if output=$(LC_ALL= LC_MESSAGES=C "${patch_cmd}" "$@" < 
"${patch}" 2>&1); then
+                       if output=$(LC_ALL= LC_MESSAGES=C patch "$@" < 
"${patch}" 2>&1); then
                                # The patch was successfully applied. Maintain
                                # silence unless applied with fuzz.
                                if [[ ${output} == *[0-9]' with fuzz '[0-9]* 
]]; then
@@ -1033,11 +1038,6 @@ if ___eapi_has_eapply; then
 
                if (( ! ${#operands[@]} )); then
                        die "eapply: no operands were specified"
-               elif hash gpatch 2>/dev/null; then
-                       # for bsd userland support, use gpatch if available
-                       patch_cmd=gpatch
-               else
-                       patch_cmd=patch
                fi
 
                for path in "${operands[@]}"; do

Reply via email to