commit:     8c4be9acf1f9fca03df2a2434d3f8638470a32b1
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Sun Jun 15 21:39:49 2025 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Mon Jun 16 01:16:54 2025 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=8c4be9ac

emerge-webrsync: hoist local declarations to the top of functions

Local variables are not lexically scoped in bash and it has been my
experience that declaring them up-front may serve as an effective canary
for undue complexity. Additionally, it renders the calling convention
clear in the case that one or more positional parameters are captured.

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

 bin/emerge-webrsync | 102 ++++++++++++++++++++--------------------------------
 1 file changed, 39 insertions(+), 63 deletions(-)

diff --git a/bin/emerge-webrsync b/bin/emerge-webrsync
index 9c9d561835..5613a49574 100755
--- a/bin/emerge-webrsync
+++ b/bin/emerge-webrsync
@@ -36,6 +36,7 @@ source "${functions_script}" || {
        eend() {
                local r=${1:-0}
                shift
+
                if [[ $r -eq 0 ]] ; then
                        printf '[ %sok%s ]\n' "${GOOD}" "${NORMAL}"
                else
@@ -147,8 +148,8 @@ handle_pgp_setup() {
 
 do_tar() {
        local file=$1
-       shift
        local decompressor
+       shift
 
        case ${file} in
                *.xz)   decompressor="xzcat" ;;
@@ -166,8 +167,7 @@ get_utc_date_in_seconds() {
 }
 
 get_date_part() {
-       local utc_time_in_secs="$1"
-       local part="$2"
+       local utc_time_in_secs=$1 part=$2
 
        if [[ ${USERLAND} == BSD ]] ; then
                date -r "${utc_time_in_secs}" -u +"${part}"
@@ -177,7 +177,7 @@ get_date_part() {
 }
 
 get_utc_second_from_string() {
-       local s="$1"
+       local s=$1
 
        if [[ ${USERLAND} == BSD ]] ; then
                # Specify zeros for the least significant digits, or else those
@@ -199,8 +199,7 @@ get_repository_timestamp() {
 }
 
 fetch_file() {
-       local URI="$1"
-       local FILE="$2"
+       local URI=$1 FILE=$2
        local opts
 
        if [[ "${FETCHCOMMAND/wget/}" != "${FETCHCOMMAND}" ]]; then
@@ -224,15 +223,14 @@ fetch_file() {
 }
 
 check_file_digest() {
-       local digest="$1"
-       local file="$2"
+       local digest=$1 file=$2
        local r=1
 
        [[ ${PORTAGE_QUIET} -eq 1 ]] || einfo "Checking digest ..."
 
        if type -P md5sum > /dev/null; then
-               local md5sum_output=$(md5sum "${file}")
-               local digest_content=$(< "${digest}")
+               md5sum_output=$(md5sum "${file}")
+               digest_content=$(< "${digest}")
                [[ "${md5sum_output%%[[:space:]]*}" = 
"${digest_content%%[[:space:]]*}" ]] && r=0
        elif type -P md5 > /dev/null; then
                [[ "$(md5 -q "${file}")" == "$(cut -d ' ' -f 1 "${digest}")" ]] 
&& r=0
@@ -244,15 +242,15 @@ check_file_digest() {
 }
 
 check_file_signature_gemato() {
-       local signature="$1"
-       local file="$2"
-       local r=1
+       local signature=$1 file=$2
+       local -a gemato_args
+       local key r=1
 
        if type -P gemato > /dev/null; then
                if [[ -n ${PORTAGE_GPG_KEY} ]] ; then
-                       local key="${PORTAGE_GPG_KEY}"
+                       key="${PORTAGE_GPG_KEY}"
                else
-                       local 
key="${EPREFIX}/usr/share/openpgp-keys/gentoo-release.asc"
+                       
key="${EPREFIX}/usr/share/openpgp-keys/gentoo-release.asc"
                fi
 
                if [[ ! -f "${key}" ]] ; then
@@ -260,7 +258,7 @@ check_file_signature_gemato() {
                        die "Needed keys unavailable! Install its package or 
set PORTAGE_GPG_KEY to the right path."
                fi
 
-               local gemato_args=(
+               gemato_args=(
                        openpgp-verify-detached
                        -K "${key}"
                )
@@ -292,14 +290,14 @@ check_file_signature_gemato() {
 }
 
 check_file_signature_gpg_unwrapped() {
-       local signature="$1"
-       local file="$2"
+       local signature=$1 file=$2
+       local gpgdir key
 
        if type -P gpg > /dev/null; then
                if [[ -n ${PORTAGE_GPG_KEY} ]] ; then
-                       local key="${PORTAGE_GPG_KEY}"
+                       key="${PORTAGE_GPG_KEY}"
                else
-                       local 
key="${EPREFIX}/usr/share/openpgp-keys/gentoo-release.asc"
+                       
key="${EPREFIX}/usr/share/openpgp-keys/gentoo-release.asc"
                fi
 
                if [[ ! -f "${key}" ]] ; then
@@ -307,7 +305,7 @@ check_file_signature_gpg_unwrapped() {
                        die "Needed keys unavailable! Install its package or 
set PORTAGE_GPG_KEY to the right path."
                fi
 
-               local gpgdir="${PORTAGE_GPG_DIR}"
+               gpgdir="${PORTAGE_GPG_DIR}"
                if [[ -z ${gpgdir} ]] ; then
                        gpgdir=$(mktemp -d -- 
"${PORTAGE_TMPDIR}/portage/webrsync.XXXXXX") || exit
 
@@ -341,10 +339,8 @@ check_file_signature_gpg_unwrapped() {
 }
 
 check_file_signature() {
-       local signature="$1"
-       local file="$2"
-       local r=1
-       local gnupg_status line
+       local signature=$1 file=$2
+       local gnupg_status line r=1
 
        if [[ ${WEBRSYNC_VERIFY_SIGNATURE} != 0 ]]; then
                [[ ${PORTAGE_QUIET} -eq 1 ]] || einfo "Checking signature ..."
@@ -379,17 +375,18 @@ check_file_signature() {
 }
 
 get_snapshot_timestamp() {
-       local file="$1"
+       local file=$1
 
        do_tar "${file}" --to-stdout -f - --wildcards -x 
'*/metadata/timestamp.x' | cut -f 1 -d " "
 }
 
 sync_local() {
-       local file="$1"
+       local file=$1
+       local chown_opts rsync_opts ownership post_sync
 
        [[ ${PORTAGE_QUIET} -eq 1 ]] || einfo "Syncing local repository ..."
 
-       local ownership="${PORTAGE_USERNAME}:${PORTAGE_GRPNAME}"
+       ownership="${PORTAGE_USERNAME}:${PORTAGE_GRPNAME}"
        if contains_word usersync "${FEATURES}"; then
                case "${USERLAND}" in
                        BSD)
@@ -402,7 +399,7 @@ sync_local() {
        fi
 
        if type -P tarsync > /dev/null ; then
-               local chown_opts="-o ${ownership%:*} -g ${ownership#*:}"
+               chown_opts="-o ${ownership%:*} -g ${ownership#*:}"
                chown ${ownership} "${repo_location}" > /dev/null 2>&1 || 
chown_opts=""
 
                if ! tarsync $(vvecho -v) -s 1 ${chown_opts} \
@@ -420,7 +417,7 @@ sync_local() {
                # Free disk space
                ${keep} || rm -f "${file}"
 
-               local rsync_opts="${PORTAGE_RSYNC_OPTS} 
${PORTAGE_RSYNC_EXTRA_OPTS} $(nvecho -q)"
+               rsync_opts="${PORTAGE_RSYNC_OPTS} ${PORTAGE_RSYNC_EXTRA_OPTS} 
$(nvecho -q)"
                if chown ${ownership} . > /dev/null 2>&1; then
                        chown -R ${ownership} .
                        rsync_opts+=" --owner --group"
@@ -440,7 +437,7 @@ sync_local() {
                "${emerge}" --metadata
        fi
 
-       local post_sync=${PORTAGE_CONFIGROOT%/}/etc/portage/bin/post_sync
+       post_sync=${PORTAGE_CONFIGROOT%/}/etc/portage/bin/post_sync
        [[ -x "${post_sync}" ]] && "${post_sync}"
 
        # --quiet suppresses output if there are no relevant news items
@@ -451,17 +448,8 @@ sync_local() {
 }
 
 do_snapshot() {
-       local ignore_timestamp="$1"
-       local date="$2"
-
-       local r=1
-
-       local compression
-
-       local have_files=0
-       local mirror
-
-       local compressions=""
+       local ignore_timestamp=$1 date=$2
+       local snapshot_timestamp compression{,s} utc_seconds have_files 
signature digest mirror file name r=1
 
        type -P xzcat > /dev/null && compressions="${compressions} 
${repo_name}:xz portage:xz"
        type -P bzcat > /dev/null && compressions="${compressions} 
${repo_name}:bz2 portage:bz2"
@@ -475,13 +463,13 @@ do_snapshot() {
                [[ ${PORTAGE_QUIET} -eq 1 ]] || einfo "Trying to retrieve 
${date} snapshot from ${mirror} ..."
 
                for compression in ${compressions} ; do
-                       local name=${compression%%:*}
+                       name=${compression%%:*}
 
                        compression=${compression#*:}
 
-                       local file="${name}-${date}.tar.${compression}"
-                       local digest="${file}.md5sum"
-                       local signature="${file}.gpgsig"
+                       file="${name}-${date}.tar.${compression}"
+                       digest="${file}.md5sum"
+                       signature="${file}.gpgsig"
 
                        if [[ -s "${DISTDIR}/${file}" && -s 
"${DISTDIR}/${digest}" && -s "${DISTDIR}/${signature}" ]] ; then
                                check_file_digest "${DISTDIR}/${digest}" 
"${DISTDIR}/${file}" && \
@@ -507,7 +495,6 @@ do_snapshot() {
                        if [[ ${have_files} -eq 1 ]]; then
                                [[ ${PORTAGE_QUIET} -eq 1 ]] || einfo "Getting 
snapshot timestamp ..."
 
-                               local snapshot_timestamp
                                snapshot_timestamp=$(get_snapshot_timestamp 
"${DISTDIR}/${file}")
 
                                if [[ ${ignore_timestamp} == 0 ]]; then
@@ -516,7 +503,6 @@ do_snapshot() {
                                                have_files=0
                                        fi
                                else
-                                       local utc_seconds
                                        
utc_seconds=$(get_utc_second_from_string "${date}")
 
                                        # Check that this snapshot is what the 
age it claims to be
@@ -551,8 +537,8 @@ do_snapshot() {
 }
 
 do_latest_snapshot() {
-       local attempts=0
-       local r=1
+       local timestamp_{difference,problem} snapshot_date{,_seconds} 
approx_snapshot_time existing_timestamp start_{hour,time}
+       local min_time_diff attempts=0 r=1
 
        [[ ${PORTAGE_QUIET} -eq 1 ]] || einfo "Fetching most recent snapshot 
..."
 
@@ -563,16 +549,7 @@ do_latest_snapshot() {
 
        # Timestamps that differ by less than 2 hours
        # are considered to be approximately equal.
-       local min_time_diff=$(( 2 * 60 * 60 ))
-
-       local existing_timestamp
-       local timestamp_difference
-       local timestamp_problem
-       local approx_snapshot_time
-       local start_time
-       local start_hour
-       local snapshot_date
-       local snapshot_date_seconds
+       min_time_diff=$(( 2 * 60 * 60 ))
 
        existing_timestamp=$(get_repository_timestamp)
        start_time=$(get_utc_date_in_seconds)
@@ -658,11 +635,10 @@ usage() {
 }
 
 main() {
-       local arg
-       local revert_date
+       local revert_date arg v
 
        for arg in "$@" ; do
-               local v=${arg#*=}
+               v=${arg#*=}
                case ${arg} in
                        -h|--help)    usage ;;
                        -k|--keep)    keep=true ;;

Reply via email to