commit:     91ebb13ace13b9a0fc3d324fd8834e768bbc40d0
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Thu Aug 11 04:12:05 2022 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Thu Jun  5 11:22:04 2025 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=91ebb13a

isolated-functions.sh: use a conditional expression to parse MAKEOPTS

Not only is -r a non-standard sed(1) option (unlike -E), but the
conditional expression of bash supports extended regular expression
matching. Use it and avoid three subshells into the bargain.

Where the external utilities are executed, neither create a subshell nor
redirect STDERR more than once.

Use the printf builtin instead of echo. Quote the expansion of 'jobs' so
as to eliminate an SC2068 warning.

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

 bin/isolated-functions.sh | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/bin/isolated-functions.sh b/bin/isolated-functions.sh
index 644795f811..aab0f667ba 100644
--- a/bin/isolated-functions.sh
+++ b/bin/isolated-functions.sh
@@ -494,19 +494,20 @@ if [[ -z ${XARGS} ]] ; then
 fi
 
 ___makeopts_jobs() {
+       local jobs
+
        # Copied from multiprocessing.eclass:makeopts_jobs
        # This assumes the first .* will be more greedy than the second .*
        # since POSIX doesn't specify a non-greedy match (i.e. ".*?").
-       local jobs=$(echo " ${MAKEOPTS} " | sed -r -n \
-               -e 
's:.*[[:space:]](-[a-z]*j|--jobs[=[:space:]])[[:space:]]*([0-9]+).*:\2:p' || 
die)
-
-       # Fallbacks for if MAKEOPTS parsing failed
-       [[ -n ${jobs} ]] || \
-               jobs=$(getconf _NPROCESSORS_ONLN 2>/dev/null) || \
-               jobs=$(sysctl -n hw.ncpu 2>/dev/null) || \
+       if [[ " ${MAKEOPTS} " =~ 
.*[[:space:]](-[a-z]*j|--jobs[=[:space:]])[[:space:]]*([0-9]+).* ]]; then
+               jobs=${BASH_REMATCH[2]}
+       elif jobs=$({ getconf _NPROCESSORS_ONLN || sysctl -n hw.ncpu; } 
2>/dev/null); then
+               :
+       else
                jobs=1
+       fi
 
-       echo ${jobs}
+       printf '%s\n' "${jobs}"
 }
 
 # Run ${XARGS} in parallel for detected number of CPUs, if supported.

Reply via email to