diff --git a/Scripts/Common/Functions.sh b/Scripts/Common/Functions.sh index d01b12c8..ada302ff 100644 --- a/Scripts/Common/Functions.sh +++ b/Scripts/Common/Functions.sh @@ -21,11 +21,22 @@ _fetchError(){ local error_line_number="$2"; local last_func="$3"; local file=$(echo "$4" | sed "s#$DOS_WORKSPACE_ROOT#\$DOS_WORKSPACE_ROOT#g"); + + # ignore when pressing TAB or sim. + if [[ "$file" =~ .*bash_completion ]];then return; fi + case $last_func in + command_not_found_handle|_filedir) return;; + esac + if [ ! -z "$last_func" ] && [ ! -z "$file" ];then echo -e "\e[0;31mERROR: $file -> ${last_func}() ended with status >${last_status}< at line >$((error_line_number -1))<\e[0m"; + elif [ ! -z "$last_func" ];then + echo -e "\e[0;31mERROR: ${last_func}() ended with status >${last_status}< at line >$((error_line_number -1))<\e[0m"; else echo -e "\e[0;31mERROR: last command ended with status >${last_status}< at line >$((error_line_number -1))<\e[0m"; fi + export TR_ERR=$last_status + _exit_report } export -f _fetchError; @@ -72,7 +83,7 @@ enterAndClear() { export -f enterAndClear; gitReset() { - git add -A && git reset --hard; + (git add -A && git reset --hard) || true; } export -f gitReset; @@ -86,16 +97,22 @@ applyPatchReal() { git format-patch -1 HEAD --zero-commit --no-signature --output="$currentWorkingPatch"; fi; fi; + else + echo "Applying (git am): $currentWorkingPatch - FAILED" + git am --abort || true + echo "Applying (patch fallback): $currentWorkingPatch" + patch -r - --no-backup-if-mismatch --forward --ignore-whitespace --verbose -p1 < $currentWorkingPatch fi; else - git apply "$@"; echo "Applying (as diff): $currentWorkingPatch"; + git apply "$@"; fi; } export -f applyPatchReal; applyPatch() { currentWorkingPatch=$1; + set -E if [ -f "$currentWorkingPatch" ]; then if git apply --check "$@" &> /dev/null; then applyPatchReal "$@"; @@ -108,11 +125,13 @@ applyPatch() { echo "Applied (as 3way): $currentWorkingPatch"; else echo -e "\e[0;31mERROR: Cannot apply: $currentWorkingPatch\e[0m"; + false fi; fi; fi; else echo -e "\e[0;31mERROR: Patch doesn't exist: $currentWorkingPatch\e[0m"; + false fi; } export -f applyPatch; diff --git a/Scripts/init.sh b/Scripts/init.sh index dc82caf0..aebcd689 100644 --- a/Scripts/init.sh +++ b/Scripts/init.sh @@ -111,14 +111,15 @@ umask 0022; export TR_ERR=0 export TR_PID=$$ +unset nokill +if [ -z "$UNATTENDED_PATCHING" ];then export UNATTENDED_PATCHING=1;fi set -E; #required for resetEnv() resetEnv(){ trap - ERR EXIT USR2 SIGINT SIGHUP TERM echo -e "\n\e[0;32mThe environment has been reset.\e[0m\nRemember to always '\e[0;31msource ../../Scripts/init.sh\e[0m' before building.\n" set +E +f -} -export -f resetEnv +}; export -f resetEnv # print result # will also ensure the corresponding status code gets returned properly @@ -129,8 +130,47 @@ _errorReport(){ echo -e "\n\e[0;32m[FINAL RESULT] No error detected (please check the above output nevertheless!)\e[0m" fi return $TR_ERR -} -export -f _errorReport +}; export -f _errorReport + +# exit +_exit(){ + if [ "$1" == "noreset" ] || [ $TR_ERR -eq 0 ] ;then + echo -e "Ended with $TR_ERR.\nThe shell env has NOT been reset, type: resetEnv if needed.\n" + else + if [ -z "$nokill" ];then nokill=0;fi + resetEnv + echo -e "\nExecution has been STOPPED (TR_ERR=$TR_ERR)." + if [ "$UNATTENDED_PATCHING" -eq 1 ];then + echo -e "\n\e[0;31mPressing any key or waiting 10s will close this shell (set UNATTENDED_PATCHING=0 to disable auto-close)!\e[0m" + read -t 10 -p "- press any key to exit the shell NOW (auto closes after 10s) -" DUMMY || true + else + read -p "- press any key to exit the shell NOW -" DUMMY || true + fi + _SPIDS=$(ps -s $TR_PID -o pid= | tr '\n' ' ') + if [ -z "$_SPIDS" ];then + echo -e "... ok, no childs running (I am: $TR_PID)" + else + echo -e "... killing childs: $_SPIDS" + kill -9 $_SPIDS + fi + if [ $nokill -eq 0 ];then + echo "... killing shell: $TR_PID" + kill -9 $TR_PID + fi + fi +}; export -f _exit + +# exit & reset & report +_exit_report(){ + _errorReport + _exit +}; export -f _exit_report + +# exit without reset/kill +_exit_sigint(){ + echo -e "\n\nCTRL+C pressed or process has been terminated.." + _exit noreset +}; export _exit_sigint # trap and print errors # ERR: needed to fetch aborts when set -e is set @@ -138,8 +178,8 @@ trap 'E=$?; \ [ $E -ne 0 ] && _fetchError $E $LINENO $FUNCNAME $BASH_SOURCE \ && export TR_ERR=$((TR_ERR + $E))' EXIT ERR -trap _errorReport USR2 -trap resetEnv SIGINT SIGHUP TERM +trap _exit_report SIGUSR2 USR2 +trap _exit_sigint SIGINT SIGHUP TERM gpgVerifyGitHead() { if [ -r "$DOS_TMP_GNUPG/pubring.kbx" ]; then