From 3c0b053f4899de7dba0aae208b815277f5703109 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= <karel.koci@nic.cz>
Date: Fri, 1 Apr 2022 13:22:15 +0200
Subject: [PATCH] helpers/common.sh: the minimal version is Bash 5.0

The syntax used in the Bash scripts here requires at minimum Bash 5.0.
This checks for it and reports reasonable error message instead of
failure.

There are two discovered issues with older versions of bash.

The first issue is the change of empty array expansion in Bash 4.4
resulted in empty arrays not being considered as undefined anymore. We
depended on this heavily and thus we can't easily go bellow that
version.

The second issue is that syntax `[[ -v "foo[key]" ]` works only in Bash
5.0 and newer. This checks if specific key is in the associative array.
The replacement could be `[ "${foo[key]:+_}" ]` in some cases but this
only works if value is also nonempty. Thus it makes sense to just the
more clear and reliable syntax and such depend on Bash 5.0.
---
 compile_pkgs      | 2 +-
 generate_lists    | 2 +-
 helpers/common.sh | 5 +++++
 3 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/compile_pkgs b/compile_pkgs
index b2bcd71c2..d790f5ff4 100755
--- a/compile_pkgs
+++ b/compile_pkgs
@@ -14,7 +14,7 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
-set -e
+set -eu
 
 # Step list that is run when no commands are specified
 DEFAULT_STEPS=( "prepare" "compile" "sign" "store_hash" "stats" "pkgsrepo" "gen_junit" )
diff --git a/generate_lists b/generate_lists
index 97acc8fa9..1e25b4e46 100755
--- a/generate_lists
+++ b/generate_lists
@@ -14,7 +14,7 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
-set -e
+set -eu
 
 FEED_NAME="updater-lists"
 
diff --git a/helpers/common.sh b/helpers/common.sh
index 52a9f3f93..aa8d5274f 100644
--- a/helpers/common.sh
+++ b/helpers/common.sh
@@ -14,6 +14,11 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+[[ "${BASH_VERSINFO[0]}" -ge 5 ]] || {
+	echo "Please use Bash version 5.0 or newer!" >&2
+	exit 1
+}
+
 ## Common printing functions ############
 
 # Detect tty if IS_TTY not already set
-- 
GitLab