commit:     d594f7a29999779aa644c30a85a25c9811f02970
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Fri May 30 03:40:16 2025 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Sun Jun  1 21:41:58 2025 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=d594f7a2

phase-helpers.sh: reimplement the ugly eapply() argument parser

The present parser is needlessly complicated; so much so, that it comes
across as being perverse. Replace it with a tasteful implementation that
processes the positional parameters in a single pass. Also, refine the
diagnostic messages slightly.

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

 bin/phase-helpers.sh | 51 +++++++++++++++++++++------------------------------
 1 file changed, 21 insertions(+), 30 deletions(-)

diff --git a/bin/phase-helpers.sh b/bin/phase-helpers.sh
index cd5b4d173d..365d072317 100644
--- a/bin/phase-helpers.sh
+++ b/bin/phase-helpers.sh
@@ -977,9 +977,8 @@ if ___eapi_has_einstalldocs; then
 fi
 
 if ___eapi_has_eapply; then
-       # shellcheck disable=2199
        eapply() {
-               local f failed found_doublehyphen i patch_cmd path
+               local f failed patch_cmd path
                local -a files operands options
 
                # for bsd userland support, use gpatch if available
@@ -1024,36 +1023,28 @@ if ___eapi_has_eapply; then
                        fi
                }
 
-               # First, try to split on --
-               for (( i = 1; i <= ${#@}; ++i )); do
-                       if [[ ${@:i:1} == -- ]]; then
-                               options=( "${@:1:i-1}" )
-                               operands=( "${@:i+1}" )
-                               found_doublehyphen=1
-                               break
-                       fi
-               done
-
-               # Then, try to split on first non-option
-               if [[ -z ${found_doublehyphen} ]]; then
-                       for (( i = 1; i <= ${#@}; ++i )); do
-                               if [[ ${@:i:1} != -* ]]; then
-                                       options=( "${@:1:i-1}" )
-                                       operands=( "${@:i}" )
+               while (( $# )); do
+                       case $1 in
+                               --)
+                                       shift
+                                       operands+=("$@")
                                        break
-                               fi
-                       done
-
-                       # Ensure that no options were interspersed with operands
-                       for path in "${operands[@]}"; do
-                               if [[ ${path} == -* ]]; then
-                                       die "eapply: all options must be passed 
before non-options"
-                               fi
-                       done
-               fi
+                                       ;;
+                               -*)
+                                       if (( ! ${#operands[@]} )); then
+                                               options+=("$1")
+                                       else
+                                               die "eapply: options must 
precede non-option arguments"
+                                       fi
+                                       ;;
+                               *)
+                                       operands+=("$1")
+                       esac
+                       shift
+               done
 
-               if [[ ${#operands[@]} -eq 0 ]]; then
-                       die "eapply: no operands specified"
+               if (( ! ${#operands[@]} )); then
+                       die "eapply: no operands were specified"
                fi
 
                for path in "${operands[@]}"; do

Reply via email to