gitclone: cleaner coding style

main() on top

top-down logic

Signed-off-by: Leah Rowe <leah@libreboot.org>
fsdg20230625
Leah Rowe 2023-05-18 12:34:47 +01:00
parent 4ac0bc8d3e
commit 8d9570b6f7
1 changed files with 88 additions and 63 deletions

137
gitclone
View File

@ -5,63 +5,21 @@
# SPDX-License-Identifier: GPL-3.0-only # SPDX-License-Identifier: GPL-3.0-only
name="" name=""
revision=""
location=""
url=""
bkup_url=""
tmp_dir=""
Print_help(){ main()
cat <<- EOF {
Usage: ./gitclone [name]
Options:
name: The name of the module as specified in resources/git/revisions file
EOF
}
Fail(){
printf "${@}\n"
Print_help
exit 1
}
Check_vars(){
if [ -z "${revision+x}" ]; then
Fail 'Error: revision not set'
fi
if [ -z "${location+x}" ]; then
Fail 'Error: location not set'
fi
if [ -z "${url+x}" ]; then
Fail 'Error: url not set'
fi
}
Patch(){
for patchfile in ${PWD}/${patchdir}/*.patch ; do
if [ ! -f "${patchfile}" ]; then
continue
fi
( cd ${tmp_dir}
git am ${patchfile} || return 1
)
done
}
Run(){
git clone ${url} ${tmp_dir} || git clone ${bkup_url} ${tmp_dir} || Fail "ERROR: couldn't download ${name}\n Check Network connection"
( cd ${tmp_dir} && git reset --hard ${revision} )
patchdir="resources/${name}/patches"
if [ -d "${patchdir}" ]; then
Patch || Fail "ERROR: Faild to patch ${name}"
fi
mv ${tmp_dir} ${location} || Fail "ERROR: couldn't copy temp to destination\n ${tmp_dir} > ${location} check permissions"
}
if [ -z "${1+x}" ]; then if [ -z "${1+x}" ]; then
Fail 'Error: name not set' err 'Error: name not set'
else
name=${1}
fi fi
name=${1}
awkstr=" /\{.*${name}.*}{/ {flag=1;next} /\}/{flag=0} flag { print }"
while read -r line ; do while read -r line ; do
set ${line} >/dev/null 2>&1 set ${line} >/dev/null 2>&1
case ${line} in case ${line} in
@ -79,9 +37,10 @@ while read -r line ; do
;; ;;
esac esac
done << EOF done << EOF
$(eval "awk ' /\{.*${name}.*}{/ {flag=1;next} /\}/{flag=0} flag { print }' resources/git/revisions") $(eval "awk '${awkstr}' resources/git/revisions")
EOF EOF
Check_vars
check_project
tmp_dir=$(mktemp -dt "${name}_XXXXX") tmp_dir=$(mktemp -dt "${name}_XXXXX")
# clean out old version just in case # clean out old version just in case
@ -89,7 +48,73 @@ if [ -d "${location}" ]; then
rm -rf ${location} rm -rf ${location}
fi fi
Run clone_project
# clean in case of failure # clean in case of failure
rm -rf ${tmp_dir} >/dev/null 2>&1 rm -rf ${tmp_dir} >/dev/null 2>&1
}
check_project()
{
if [ -z "${revision+x}" ]; then
err 'Error: revision not set'
fi
if [ -z "${location+x}" ]; then
err 'Error: location not set'
fi
if [ -z "${url+x}" ]; then
err 'Error: url not set'
fi
}
clone_project()
{
git clone ${url} ${tmp_dir} || git clone ${bkup_url} ${tmp_dir} \
|| err "ERROR: could not download ${name}"
(
cd ${tmp_dir} || err "Could not access tmp directory."
git reset --hard ${revision}
)
patchdir="resources/${name}/patches"
if [ -d "${patchdir}" ]; then
patch_project || err "ERROR: errd to patch ${name}"
fi
mv ${tmp_dir} ${location} && return 0
printf "ERROR: Could not copy temp file to destination.\n"
err " ${tmp_dir} > ${location} check permissions"
}
patch_project()
{
for patchfile in ${PWD}/${patchdir}/*.patch ; do
if [ ! -f "${patchfile}" ]; then
continue
fi
( cd ${tmp_dir}
git am ${patchfile} || return 1
)
done
}
usage()
{
cat <<- EOF
Usage: ./gitclone [name]
Options:
name: Module name as specified in resources/git/revisions
EOF
}
err()
{
printf "${@}\n"
usage
exit 1
}
main $@