From 9558e2fce790b044c33c54dfc58780a66e98b770 Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Mon, 11 Dec 2023 04:59:44 +0000 Subject: improved safety/error handling on multitree git-am update/trees wasn't correctly returning non-zero status, even though it was printing an error message, when git-am failed. this is due to the way subshells work, and it was overlooked in previous auditing. additionally: don't directly copy trees to the destination, instead patch/reset first, then copy only under normal condition, just as with single-tree projects. when running build/roms, the script would continue after a bad git-am, without exit. this patch fixes it in the most paranoid way possible. i'm now fairly confident that lbmk will fail gracefully and efficiently, under error conditions. this should prevent bad image builds. Signed-off-by: Leah Rowe --- include/git.sh | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) (limited to 'include') diff --git a/include/git.sh b/include/git.sh index fa53fed1..49148224 100755 --- a/include/git.sh +++ b/include/git.sh @@ -4,7 +4,7 @@ # This file is only used by update/project/trees -eval "$(setvars "" _target rev _xm loc url bkup_url depend)" +eval "$(setvars "" _target rev _xm loc url bkup_url depend patchfail)" tmp_git_dir="${PWD}/tmp/gitclone" fetch_project_trees() @@ -58,16 +58,27 @@ prepare_new_tree() { printf "Creating %s tree %s (%s)\n" "${project}" "${tree}" "${_target}" - x_ cp -R "src/${project}/${project}" "src/${project}/${tree}" - git_reset_rev "src/${project}/${tree}" "${rev}" + rm -Rf "${tmp_git_dir%/*}" || \ + err "prepare_new_tree ${project}/${tree}: can't rm tmpclone" + mkdir "${tmp_git_dir%/*}" || \ + err "prepare_new_tree ${project}/${tree}: can't mkdir tmp" + cp -R "src/${project}/${project}" "${tmp_git_dir}" || \ + err "prepare_new_tree ${project}/${tree}: can't make tmpclone" + git_reset_rev "${tmp_git_dir}" "${rev}" ( - x_ cd "src/${project}/${tree}" + cd "${tmp_git_dir}" || \ + err "prepare_new_tree ${project}/${tree}: can't cd tmpclone" if [ -f ".gitmodules" ]; then git submodule update --init --checkout || \ err "prepare_new_tree ${project}/${tree}: !submodules" fi ) - git_am_patches "$PWD/src/$project/$tree" "$PWD/$cfgsdir/$tree/patches" + git_am_patches "${tmp_git_dir}" "$PWD/$cfgsdir/$tree/patches" || \ + err "prepare_new_tree ${project}/${tree}: patch fail" + [ "${patchfail}" = "y" ] && err "PATCH FAIL" + + mv "${tmp_git_dir}" "src/${project}/${tree}" || \ + err "prepare_new_tree ${project}/${tree}: can't copy tmpclone" } fetch_project_repo() @@ -107,7 +118,9 @@ clone_project() git clone ${bkup_url} "${tmp_git_dir}" || \ err "clone_project: could not download ${project}" git_reset_rev "${tmp_git_dir}" "${rev}" - git_am_patches "${tmp_git_dir}" "${PWD}/config/${project}/patches" + git_am_patches "${tmp_git_dir}" "${PWD}/config/${project}/patches" \ + || err "clone_project ${project} ${loc}: patch fail" + [ "${patchfail}" = "y" ] && err "PATCH FAIL" x_ rm -Rf "${loc}" [ "${loc}" = "${loc%/*}" ] || x_ mkdir -p ${loc%/*} @@ -137,17 +150,18 @@ git_am_patches() for patch in "${patchdir}/"*; do [ -L "${patch}" ] && continue [ -f "${patch}" ] || continue - patchfail="n" git am "${patch}" || patchfail="y" if [ "${patchfail}" = "y" ]; then git am --abort || err "${sdir}: !git am --abort" err "!git am ${patch} -> ${sdir}" fi done - ) + ) || err "PATCH FAILURE" for patches in "${patchdir}/"*; do [ -L "${patches}" ] && continue [ ! -d "${patches}" ] && continue git_am_patches "${sdir}" "${patches}" done + [ "${patchfail}" = "y" ] && return 1 + return 0 } -- cgit v1.2.1