Files
lbmk/script/update/project/repo
Leah Rowe 8c03b886c4 Greatly simplify error handling in shell scripts
Instead of having detailed error messages, run most
commands through a function that calls err() under
fault conditions.

Where detail is still required, err() is still called
manually. Where it isn't, the error message is simply
whatever command was executed to cause the error.

This results in a massive sloccount reduction for lbmk;
specifically, 178 sloc reduction, or a 8.1% reduction.
The total sloccount is now 2022, for shell scripts.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2023-10-01 22:47:02 +01:00

76 lines
1.7 KiB
Bash
Executable File

#!/usr/bin/env sh
# SPDX-License-Identifier: GPL-3.0-only
# SPDX-FileCopyrightText: 2022 Caleb La Grange <thonkpeasant@protonmail.com>
# SPDX-FileCopyrightText: 2022 Ferass El Hafidi <vitali64pmemail@protonmail.com>
# SPDX-FileCopyrightText: 2023 Leah Rowe <leah@libreboot.org>
. "include/err.sh"
. "include/git.sh"
. "include/option.sh"
eval "$(setvars "" name rev loc url bkup_url depend)"
tmp_dir="${PWD}/tmp/gitclone"
main()
{
[ $# -gt 0 ] || fail "no argument given"
[ -z "${1+x}" ] && fail 'main(): name not set'
name=${1}
scan_config "${name}" "config/git" "fail"
verify_config
clone_project
[ "${depend}" = "" ] || for d in ${depend} ; do
xx_ ./update project repo ${d}
done
xx_ rm -Rf "${tmp_dir}"
}
verify_config()
{
[ -z "${rev+x}" ] && fail 'verify_config: rev not set'
[ -z "${loc+x}" ] && fail 'verify_config: loc not set'
[ -z "${url+x}" ] && fail 'verify_config: url not set'
}
clone_project()
{
xx_ rm -Rf "${tmp_dir}"
xx_ mkdir -p "${tmp_dir%/*}"
git clone ${url} "${tmp_dir}" || git clone ${bkup_url} "${tmp_dir}" || \
fail "clone_project: could not download ${name}"
git_reset_rev "${tmp_dir}" "${rev}" "fail" || \
fail "clone_project ${loc}/: cannot reset <- ${rev}"
git_am_patches "${tmp_dir}" "${PWD}/config/${name}/patches" "fail" || \
fail "clone_project ${loc}/: cannot apply patches"
[ ! -d "${loc}" ] || \
xx_ rm -Rf "${loc}"
[ "${loc}" = "${loc%/*}" ] || xx_ mkdir -p ${loc%/*}
xx_ mv "${tmp_dir}" "${loc}"
}
fail()
{
for x in "${loc}" "${tmp_dir}"; do
[ -z "${x}" ] || [ ! -d "${x}" ] || rm -Rf "${loc}" || :
done
usage
err "${1}"
}
usage()
{
cat <<- EOF
Usage: ./update project repo [name]
Options:
name: Module name as specified in files under config/git/
EOF
}
main $@