commit: 9b4dd55ec74ea19556cff467d98f6b86601f3e57
Author: Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Fri May 30 03:07:20 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=9b4dd55e
phase-helpers.sh: name and employ variables sensibly in eapply()
In the course of parsing the arguments conveyed to the eapply()
function, add options to an array named 'options', and operands to an
array named 'operands'.
Employ a variable named 'path' to iterate over the members of
'operands', rather than repurpose the 'i' variable. Doing so makes it
clear that each operand is expected to be a pathname.
Within the _eapply_patch() function, don't rely on the initial set of
patch(1) options being the subject of a variable defined by an upper
scope. Instead, convey the options to the function as positional
parameters.
Within the _eapply_patch() function, don't manipulate a variable defined
by an upper scope. Instead, define a local 'patch_opts' array in which
to incorporate - and augment - the provided patch(1) options.
Signed-off-by: Kerin Millar <kfm <AT> plushkava.net>
Signed-off-by: Sam James <sam <AT> gentoo.org>
bin/phase-helpers.sh | 77 ++++++++++++++++++++++++++--------------------------
1 file changed, 39 insertions(+), 38 deletions(-)
diff --git a/bin/phase-helpers.sh b/bin/phase-helpers.sh
index 9d9a7a4e3a..cd5b4d173d 100644
--- a/bin/phase-helpers.sh
+++ b/bin/phase-helpers.sh
@@ -977,15 +977,21 @@ if ___eapi_has_einstalldocs; then
fi
if ___eapi_has_eapply; then
+ # shellcheck disable=2199
eapply() {
- local failed patch_cmd=patch
+ local f failed found_doublehyphen i patch_cmd path
+ local -a files operands options
# for bsd userland support, use gpatch if available
- type -P gpatch > /dev/null && patch_cmd=gpatch
+ if type -P gpatch >/dev/null; then
+ patch_cmd=gpatch
+ else
+ patch_cmd=patch
+ fi
_eapply_get_files() {
- local LC_ALL=POSIX
- local f
+ local LC_ALL=POSIX f
+
for f in "${1}"/*; do
if [[ ${f} == *.@(diff|patch) ]]; then
files+=( "${f}" )
@@ -994,40 +1000,35 @@ if ___eapi_has_eapply; then
}
_eapply_patch() {
- local f=${1}
- local prefix=${2}
+ local patch=$1 prefix=$2
+ local -a patch_opts
+ shift 2
- ebegin "${prefix:-Applying }${f##*/}"
+ ebegin "${prefix:-Applying }${patch##*/}"
# -p1 as a sane default
# -f to avoid interactivity
# -g0 to guarantee no VCS interaction
# --no-backup-if-mismatch not to pollute the sources
- local all_opts=(
- -p1 -f -g0 --no-backup-if-mismatch
- "${patch_options[@]}"
- )
+ patch_opts=( -p1 -f -g0 --no-backup-if-mismatch "$@" )
# Try applying with -F0 first, output a verbose warning
# if fuzz factor is necessary
- if ${patch_cmd} "${all_opts[@]}" --dry-run -s -F0 \
- < "${f}" &>/dev/null; then
- all_opts+=( -s -F0 )
+ if "${patch_cmd}" "${patch_opts[@]}" --dry-run -s -F0 <
"${patch}" &>/dev/null; then
+ patch_opts+=( -s -F0 )
fi
- ${patch_cmd} "${all_opts[@]}" < "${f}"
+ "${patch_cmd}" "${patch_opts[@]}" < "${patch}"
failed=${?}
if ! eend "${failed}"; then
- __helpers_die "patch -p1 ${patch_options[*]}
failed with ${f}"
+ __helpers_die "patch -p1 $* failed with
${patch}"
fi
}
- local patch_options=() files=()
- local i found_doublehyphen
# First, try to split on --
for (( i = 1; i <= ${#@}; ++i )); do
if [[ ${@:i:1} == -- ]]; then
- patch_options=( "${@:1:i-1}" )
- files=( "${@:i+1}" )
+ options=( "${@:1:i-1}" )
+ operands=( "${@:i+1}" )
found_doublehyphen=1
break
fi
@@ -1037,41 +1038,41 @@ if ___eapi_has_eapply; then
if [[ -z ${found_doublehyphen} ]]; then
for (( i = 1; i <= ${#@}; ++i )); do
if [[ ${@:i:1} != -* ]]; then
- patch_options=( "${@:1:i-1}" )
- files=( "${@:i}" )
+ options=( "${@:1:i-1}" )
+ operands=( "${@:i}" )
break
fi
done
- # Ensure that no options were interspersed with files
- for i in "${files[@]}"; do
- if [[ ${i} == -* ]]; then
+ # 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 [[ ${#files[@]} -eq 0 ]]; then
- die "eapply: no files specified"
+ if [[ ${#operands[@]} -eq 0 ]]; then
+ die "eapply: no operands specified"
fi
- local f
- for f in "${files[@]}"; do
- if [[ -d ${f} ]]; then
- local files=()
- _eapply_get_files "${f}"
- [[ ${#files[@]} -eq 0 ]] && die "No
*.{patch,diff} files in directory ${f}"
+ for path in "${operands[@]}"; do
+ if [[ -d ${path} ]]; then
+ files=()
+ _eapply_get_files "${path}"
+ if [[ ${#files[@]} -eq 0 ]]; then
+ die "No *.{patch,diff} files in
directory ${path}"
+ fi
- einfo "Applying patches from ${f} ..."
- local f2
- for f2 in "${files[@]}"; do
- _eapply_patch "${f2}" ' '
+ einfo "Applying patches from ${path} ..."
+ for f in "${files[@]}"; do
+ _eapply_patch "${f}" ' '
"${options[@]}"
# in case of nonfatal
[[ ${failed} -ne 0 ]] && return
"${failed}"
done
else
- _eapply_patch "${f}"
+ _eapply_patch "${path}" '' "${options[@]}"
# In case of nonfatal
[[ ${failed} -ne 0 ]] && return "${failed}"