Files
lbmk/include/get.sh
Leah Rowe 0216a3104a get.sh: Always update git remotes
Right now, if cache/clone/PROJECT/ already exists,
the logic for pulling new changes doesn't execute,
and neither does the logic for updating remotes.

This is bad when updating revisions, because then
manual updating is required, defeating the purpose
of xbmk's own automation in this regard.

Fix it by only checking the cached download on files,
not Git repositories; the try_git function itself will
already perform this check, before updating remotes
and pulling in new commits from upstream.

The updating only happens when a given target directory
doesn't exist, e.g. src/flashprog/ or src/grub/default/,
so this won't slow down release builds for example.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2025-05-22 12:45:59 +01:00

169 lines
4.9 KiB
Bash

# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (c) 2020-2021,2023-2025 Leah Rowe <leah@libreboot.org>
# Copyright (c) 2022 Caleb La Grange <thonkpeasant@protonmail.com>
eval "`setvars "" loc url bkup_url subcurl subhash subgit subgit_bkup \
depend subcurl_bkup repofail`"
tmpgit="$xbmklocal/gitclone"
tmpgitcache="$XBMK_CACHE/tmpgit"
fetch_targets()
{
e "src/$project/$tree" d && return 0
printf "Creating %s tree %s\n" "$project" "$tree"
git_prep "$url" "$bkup_url" "$xbmkpwd/$configdir/$tree/patches" \
"src/$project/$tree" with_submodules
}
fetch_project()
{
eval "`setvars "" xtree`"
eval "`setcfg "config/git/$project/pkg.cfg"`"
chkvars url bkup_url
[ -n "$xtree" ] && x_ ./mk -f coreboot "$xtree"
[ -z "$depend" ] || for d in $depend ; do
printf "'%s' needs '%s'; grabbing '%s'\n" "$project" "$d" "$d"
x_ ./mk -f $d
done
clone_project
}
clone_project()
{
loc="$XBMK_CACHE/clone/$project" && singletree "$project" && \
loc="src/$project"
printf "Downloading project '%s' to '%s'\n" "$project" "$loc"
e "$loc" d missing && remkdir "${tmpgit%/*}" && git_prep \
"$url" "$bkup_url" "$xbmkpwd/config/$project/patches" "$loc"; :
}
git_prep()
{
_patchdir="$3"
_loc="$4" # $1 and $2 are gitrepo and gitrepo_backup
chkvars rev
xbmkget git "$1" "$2" "$tmpgit" "$rev" "$_patchdir"
if singletree "$project" || [ $# -gt 4 ]; then
dx_ fetch_submodule "$mdir/module.list"
fi
[ "$_loc" = "${_loc%/*}" ] || x_ mkdir -p "${_loc%/*}"
x_ mv "$tmpgit" "$_loc"
}
fetch_submodule()
{
mcfgdir="$mdir/${1##*/}"; eval \
"`setvars "" subhash subgit subgit_bkup subcurl subcurl_bkup st`"
eval "`setcfg "$mcfgdir/module.cfg" 0`"
for xt in git curl; do
_seval="if [ -n \"\$sub$xt\" ] || [ -n \"\$sub${xt}_bkup\" ]"
eval "$_seval; then st=\"\$st \$xt\"; fi"
done
st="${st# }" && [ "$st" = "git curl" ] && err "$mdir: git+curl defined"
[ -z "$st" ] && return 0 # subgit/subcurl not defined
chkvars "sub${st}" "sub${st}_bkup" "subhash"
[ "$st" = "git" ] && x_ rm -Rf "$tmpgit/$1"
eval xbmkget "$st" "\$sub$st" "\$sub${st}_bkup" "$tmpgit/$1" \
"$subhash" "$mdir/${1##*/}/patches"
}
xbmkget()
{
[ "$1" = "curl" ] || [ "$1" = "copy" ] || [ "$1" = "git" ] || \
err "Bad dlop (arg 1): xbmkget $*"
echk="f" && [ "$1" = "git" ] && echk="d"
for url in "$2" "$3"; do
[ -n "$url" ] || err "empty URL given in: xbmkget $*"
try_file "$url" "$@" || continue
eval "[ -$echk \"$4\" ] || continue"
return 0 # successful download/copy
done
err "$1 $2 $3 $4: not downloaded"; :
}
try_file()
{
cached="file/$6" && [ "$2" = "git" ] && cached="clone/${3##*/}" && \
cached="${cached%.git}" # always the main repo as basis for naming,
# in case the backup has another name
cached="$XBMK_CACHE/$cached"
x_ mkdir -p "${5%/*}" "${cached%/*}"
echk="d" && [ "$2" != "git" ] && echk="f" && \
bad_checksum "$6" "$cached" 2>/dev/null && x_ rm -f "$cached"
evalchk="[ -$echk \"$cached\" ] || " && [ "$2" = "git" ] && evalchk=""
eval "${evalchk}try_$2 \"\$cached\" \"\$@\" || return 1"
[ "$2" != "git" ] && [ -f "$5" ] && \
bad_checksum "$6" "$5" 2>/dev/null && x_ cp "$cached" "$5"
eval "[ -$echk \"$cached\" ] || return 1"
if [ "$2" = "git" ]; then
[ -d "$5" ] || tmpclone "$cached" "$5" "$6" "$7" || return 1
else
bad_checksum "$6" "$cached" && x_ rm -f "$cached" && return 1
[ "$cached" != "$5" ] && x_ cp "$cached" "$5"
bad_checksum "$6" "$5" && x_ rm -f "$5" && return 1; :
fi
eval "[ -$echk \"$5\" ] || return 1"
}
try_curl()
{
_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; :
}
try_copy()
{
[ -L "$2" ] && printf "symlink %s (trycp %s)\n" "$2" "$*" && return 1
[ ! -f "$2" ] && printf "%s missing (trycp %s)\n" "$2" "$*" && return 1
( x_ cp "$2" "$1" ) || return 1; :
}
try_git()
{
gitdest="`findpath "$1"`" || err "Can't get readpath for '$1'"
x_ rm -Rf "$tmpgitcache"
[ -d "$gitdest" ] || ( x_ git clone "$2" "$tmpgitcache" ) || return 1
[ -d "$gitdest" ] || x_ mkdir -p "${gitdest%/*}"
[ -d "$gitdest" ] || x_ mv "$tmpgitcache" "$gitdest"
( x_ git -C "$gitdest" remote remove main ) || :
( x_ git -C "$gitdest" remote remove backup ) || :
x_ git -C "$gitdest" remote add main "$4"
x_ git -C "$gitdest" remote add backup "$5"
( x_ git -C "$gitdest" fetch --all ) || :
( x_ git -C "$gitdest" pull --all ) || :; :
}
bad_checksum()
{
[ ! -f "$2" ] && printf "'%s' missing, %s\n" "$2" "$1" 1>&2 && return 0
csum="$(x_ sha512sum "$2" | awk '{print $1}')" || err "!sha512 '$2' $1"
[ "$csum" != "$1" ] && printf "BAD SHA512, '%s'; need '%s', got %s\n" \
"$2" "$1" "$csum" 1>&2 && x_ rm -f "$2" && return 0; return 1
}
tmpclone()
{
( x_ git clone "$1" "$2" ) || return 1
( x_ git -C "$2" reset --hard "$3" ) || return 1
( fx_ "eval x_ git -C \"$2\" am" find "$4" -type f ) || return 1; :
}