get.sh: unroll condensed code lines

Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
Leah Rowe
2025-09-24 08:37:18 +01:00
parent 193001bc71
commit 0275c60111
+163 -50
View File
@@ -10,8 +10,11 @@ tmpgitcache="$xbtmp/tmpgit"
fetch_targets() fetch_targets()
{ {
[ -d "src/$project/$tree" ] || git_prep "$url" "$bkup_url" \ if [ ! -d "src/$project/$tree" ]; then
"$xbmkpwd/$configdir/$tree/patches" "src/$project/$tree" submod; : git_prep "$url" "$bkup_url" \
"$xbmkpwd/$configdir/$tree/patches" \
"src/$project/$tree" "submod"
fi
} }
fetch_project() fetch_project()
@@ -19,20 +22,33 @@ fetch_project()
eval "`setvars "" xtree`" eval "`setvars "" xtree`"
eval "`setcfg "config/git/$project/pkg.cfg"`" eval "`setcfg "config/git/$project/pkg.cfg"`"
chkvars url bkup_url && [ -n "$xtree" ] && x_ ./mk -f coreboot "$xtree" chkvars url bkup_url
[ -z "$depend" ] || for d in $depend ; do
x_ ./mk -f $d if [ -n "$xtree" ]; then
done x_ ./mk -f coreboot "$xtree"
fi
if [ -n "$depend" ]; then
for d in $depend ; do
x_ ./mk -f $d
done
fi
clone_project clone_project
} }
clone_project() clone_project()
{ {
loc="$XBMK_CACHE/clone/$project" && singletree "$project" && \ loc="$XBMK_CACHE/clone/$project"
loc="src/$project" if singletree "$project"; then
loc="src/$project"
fi
e "$loc" d missing && remkdir "${tmpgit%/*}" && git_prep \ if e "$loc" d missing; then
"$url" "$bkup_url" "$xbmkpwd/config/$project/patches" "$loc"; : remkdir "${tmpgit%/*}"
git_prep "$url" "$bkup_url" \
"$xbmkpwd/config/$project/patches" "$loc"
fi
} }
git_prep() git_prep()
@@ -43,19 +59,25 @@ git_prep()
_loc="$4" # $1 and $2 are gitrepo and gitrepo_backup _loc="$4" # $1 and $2 are gitrepo and gitrepo_backup
chkvars rev chkvars rev
xbget git "$1" "$2" "$tmpgit" "$rev" "$_patchdir" xbget git "$1" "$2" "$tmpgit" "$rev" "$_patchdir"
if singletree "$project" || [ $# -gt 4 ]; then if singletree "$project" || [ $# -gt 4 ]; then
dx_ fetch_submodule "$mdir/module.list" dx_ fetch_submodule "$mdir/module.list"
fi fi
[ "$_loc" = "${_loc%/*}" ] || x_ mkdir -p "${_loc%/*}" if [ "$_loc" != "${_loc%/*}" ]; then
x_ mkdir -p "${_loc%/*}"
fi
x_ mv "$tmpgit" "$_loc" x_ mv "$tmpgit" "$_loc"
} }
fetch_submodule() fetch_submodule()
{ {
mcfgdir="$mdir/${1##*/}"; eval \ mcfgdir="$mdir/${1##*/}"
eval \
"`setvars "" subhash subgit subgit_bkup subcurl subcurl_bkup st`" "`setvars "" subhash subgit subgit_bkup subcurl subcurl_bkup st`"
eval "`setcfg "$mcfgdir/module.cfg" 0`" eval "`setcfg "$mcfgdir/module.cfg" 0`"
for xt in git curl; do for xt in git curl; do
@@ -63,57 +85,124 @@ fetch_submodule()
eval "$_seval; then st=\"\$st \$xt\"; fi" eval "$_seval; then st=\"\$st \$xt\"; fi"
done done
st="${st# }" && [ "$st" = "git curl" ] && \ st="${st# }"
err "$mdir: git+curl defined" fetch_submodule "$@" if [ "$st" = "git curl" ]; then
[ -z "$st" ] && return 0 # subgit/subcurl not defined err "$mdir: git+curl defined" "fetch_submodule" "$@"
chkvars "sub${st}" "sub${st}_bkup" "subhash" fi
[ "$st" = "git" ] && x_ rm -Rf "$tmpgit/$1" if [ -n "$st" ]
eval xbget "$st" "\$sub$st" "\$sub${st}_bkup" "$tmpgit/$1" \ then
"$subhash" "$mdir/${1##*/}/patches" chkvars "sub${st}" "sub${st}_bkup" "subhash"
if [ "$st" = "git" ]; then
x_ rm -Rf "$tmpgit/$1"
fi
eval xbget "$st" "\$sub$st" "\$sub${st}_bkup" "$tmpgit/$1" \
"$subhash" "$mdir/${1##*/}/patches"
fi
} }
# TODO: in the following functions, argument numbers are used
# which is hard to understand. the code should be modified
# so that variable names are used instead, for easy reading
xbget() xbget()
{ {
[ "$1" = "curl" ] || [ "$1" = "copy" ] || [ "$1" = "git" ] || \ if [ "$1" != "curl" ] && [ "$1" != "copy" ] && [ "$1" != "git" ]; then
err "Bad dlop (arg 1)" xbget "$@" err "Bad dlop (arg 1)" "xbget" "$@"
fi
for url in "$2" "$3"
do
if [ -z "$url" ]; then
err "empty URL given in" "xbget" "$@"
fi
if ! try_file "$url" "$@"; then
continue
fi
for url in "$2" "$3"; do
[ -n "$url" ] || err "empty URL given in: xbget $*"
try_file "$url" "$@" || continue
case "$1" in case "$1" in
git) [ -d "$4" ] || continue ;; git)
*) [ -f "$4" ] || continue ;; if [ ! -d "$4" ]; then
continue
fi
;;
*)
if [ ! -f "$4" ]; then
continue
fi
;;
esac esac
return 0 # successful download/copy return 0 # successful download/copy
done done
err "failed to download file/repository" xbget "$@"; :
err "failed to download file/repository" "xbget" "$@"; :
} }
# TODO: this function "try_file" actually handles git as well.
# the name is misleading. think of a better name instead.
# TODO: try_file is also a bit messy. those eval statements can
# be tidied up, or eval can be dropped entirely.
# (it works much better than the old code, but it's over-engineered)
try_file() try_file()
{ {
cached="file/$6" && [ "$2" = "git" ] && cached="clone/${3##*/}" && \ cached="file/$6"
cached="${cached%.git}" # always the main repo as basis for naming, if [ "$2" = "git" ]
# in case the backup has another name then
# always the main repo as basis for naming,
# in case the backup has another name
cached="clone/${3##*/}"
cached="${cached%.git}"
fi
cached="$XBMK_CACHE/$cached" cached="$XBMK_CACHE/$cached"
x_ mkdir -p "${5%/*}" "${cached%/*}" x_ mkdir -p "${5%/*}" "${cached%/*}"
echk="d" && [ "$2" != "git" ] && echk="f" && \ echk="d"
bad_checksum "$6" "$cached" 2>/dev/null && x_ rm -f "$cached" if [ "$2" != "git" ]; then
echk="f"
if bad_checksum "$6" "$cached" 2>/dev/null; then
x_ rm -f "$cached"
fi
fi
evalchk="[ -$echk \"$cached\" ] || "
if [ "$2" = "git" ]; then
evalchk=""
fi
evalchk="[ -$echk \"$cached\" ] || " && [ "$2" = "git" ] && evalchk=""
eval "${evalchk}try_$2 \"\$cached\" \"\$@\" || return 1" eval "${evalchk}try_$2 \"\$cached\" \"\$@\" || return 1"
[ "$2" != "git" ] && [ -f "$5" ] && \
bad_checksum "$6" "$5" 2>/dev/null && x_ cp "$cached" "$5" if [ "$2" != "git" ] && [ -f "$5" ]; then
if bad_checksum "$6" "$5" 2>/dev/null; then
x_ cp "$cached" "$5"
fi
fi
eval "[ -$echk \"$cached\" ] || return 1" eval "[ -$echk \"$cached\" ] || return 1"
if [ "$2" = "git" ]; then if [ "$2" = "git" ]
[ -d "$5" ] || tmpclone "$cached" "$5" "$6" "$7" || \ then
err "Can't clone final repo" try_file "$@"; : if [ ! -d "$5" ]; then
tmpclone "$cached" "$5" "$6" "$7" || \
err "Can't clone final repo" "try_file" "$@"; :
fi
else else
bad_checksum "$6" "$cached" && x_ rm -f "$cached" && return 1 if bad_checksum "$6" "$cached"; then
[ "$cached" != "$5" ] && x_ cp "$cached" "$5" x_ rm -f "$cached"
bad_checksum "$6" "$5" && x_ rm -f "$5" && return 1; : return 1
fi
if [ "$cached" != "$5" ]; then
x_ cp "$cached" "$5"
fi
if bad_checksum "$6" "$5"; then
x_ rm -f "$5"
return 1
fi
fi fi
eval "[ -$echk \"$5\" ] || return 1" eval "[ -$echk \"$5\" ] || return 1"
@@ -122,13 +211,23 @@ try_file()
try_curl() try_curl()
{ {
_ua="Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0" _ua="Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0"
( x_ curl --location --retry 3 -A "$_ua" "$2" -o "$1" ) || \
( x_ wget --tries 3 -U "$_ua" "$2" -O "$1" ) || return 1; : ( x_ curl --location --retry 3 -A "$_ua" "$2" -o "$1" ) \
|| ( x_ wget --tries 3 -U "$_ua" "$2" -O "$1" ) \
|| return 1; :
} }
try_copy() try_copy()
{ {
e "$2" f missing && return 1; : if e "$2" f missing
then
# TODO: is this if statement necessary? the following
# x_ cp command would provide the same effective
# error handle, and provide equivalent feedback
return 1
fi
( x_ cp "$2" "$1" ) || return 1; : ( x_ cp "$2" "$1" ) || return 1; :
} }
@@ -136,26 +235,40 @@ try_git()
{ {
gitdest="`findpath "$1" || err "Can't get findpath for '$1'"`" || \ gitdest="`findpath "$1" || err "Can't get findpath for '$1'"`" || \
err "failed findpath for '$1'" try_get "$@" err "failed findpath for '$1'" try_get "$@"
x_ rm -Rf "$tmpgitcache" x_ rm -Rf "$tmpgitcache"
[ -d "$gitdest" ] || ( x_ git clone "$2" "$tmpgitcache" ) || return 1 if [ ! -d "$gitdest" ]; then
[ -d "$gitdest" ] || x_ mkdir -p "${gitdest%/*}" ( x_ git clone "$2" "$tmpgitcache" ) || return 1
[ -d "$gitdest" ] || x_ mv "$tmpgitcache" "$gitdest"
x_ mkdir -p "${gitdest%/*}"
x_ mv "$tmpgitcache" "$gitdest"
fi
( x_ git -C "$gitdest" remote remove main ) || : ( x_ git -C "$gitdest" remote remove main ) || :
( x_ git -C "$gitdest" remote remove backup ) || : ( x_ git -C "$gitdest" remote remove backup ) || :
x_ git -C "$gitdest" remote add main "$4" x_ git -C "$gitdest" remote add main "$4"
x_ git -C "$gitdest" remote add backup "$5" x_ git -C "$gitdest" remote add backup "$5"
( x_ git -C "$gitdest" pull --all ) || :; : ( x_ git -C "$gitdest" pull --all ) || :; :
} }
bad_checksum() bad_checksum()
{ {
e "$2" f missing && return 0 if e "$2" f missing; then
return 0
fi
csum="$(x_ sha512sum "$2" | awk '{print $1}')" || \ csum="$(x_ sha512sum "$2" | awk '{print $1}')" || \
err "!sha512 '$2' $1" bad_checksum "$@" err "!sha512 '$2' $1" bad_checksum "$@"
[ "$csum" = "$1" ] && return 1; x_ rm -f "$2"
printf "BAD SHA512 %s, '%s'; need '%s'\n" "$csum" "$2" "$1" 1>&2 if [ "$csum" = "$1" ]; then
return 1
else
x_ rm -f "$2"
printf "BAD SHA512 %s, '%s'; need %s\n" "$csum" "$2" "$1" 1>&2
fi
} }
tmpclone() tmpclone()