Skip to content

Commit

Permalink
pre-commit.sh: refactor to make it easier to read
Browse files Browse the repository at this point in the history
The mdl_version_test function was inserted in a place where it would be
run before checking if mdl was installed or not.

Now we have one function for each of the tools where a check is made if
it exists and if it is of the right version and then the tool is run.

Signed-off-by: Raghavendra Talur <raghavendra.talur@gmail.com>
  • Loading branch information
raghavendra-talur committed Dec 6, 2023
1 parent 96b2cbe commit 123e0cb
Showing 1 changed file with 76 additions and 39 deletions.
115 changes: 76 additions & 39 deletions hack/pre-commit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,92 @@
scriptdir="$(dirname "$(realpath "$0")")"
cd "$scriptdir/.." || exit 1


OUTPUTS_FILE="$(mktemp --tmpdir tool-errors-XXXXXX)"

echo "${OUTPUTS_FILE}"

# run_check <file_regex> <checker_exe> [optional args to checker...]
function run_check() {
regex="$1"
shift
exe="$1"
shift

if [ -x "$(command -v "$exe")" ]; then
echo "===== $exe ====="
find . \
-path ./testbin -prune -o \
-path ./bin -prune -o \
-regextype egrep -iregex "$regex" -print0 | \
xargs -0r "$exe" "$@" 2>&1 | tee -a "${OUTPUTS_FILE}"
echo
echo
else
echo "FAILED: All checks required, but $exe not found!"
check_version() {
if ! [[ "$1" == "$(echo -e "$1\n$2" | sort -V | tail -n1)" ]] ; then
echo "ERROR: $3 version is too old. Expected $2, found $1"
exit 1
fi
}

get_files() {
git ls-files -z | grep --binary-files=without-match --null-data --null -E "$1"
}

# markdownlint: https://github.com/markdownlint/markdownlint
# https://github.com/markdownlint/markdownlint/blob/master/docs/RULES.md
# Install via: gem install mdl
mdl_version_test() {
IFS=. read -r x y _ <<-a
$(mdl --version)
a
test "$x" -gt 0 || test "$x" -eq 0 && test "$y" -gt 11
set -- $?
unset -v x y
return "$1"
run_mdl() {
echo "===== mdl ====="

local detected_version
local required_version="0.11.0"

if ! command -v mdl >/dev/null 2>&1; then
echo "ERROR: mdl is not installed"
echo "You can install it by running:"
echo " gem install mdl"
exit 1
fi

detected_version=$(mdl --version)
check_version "${detected_version}" "${required_version}" "mdl"

get_files ".*\.md" | xargs -0 -r mdl --style "${scriptdir}/mdl-style.rb" | tee -a "${OUTPUTS_FILE}"
echo
echo
}
if ! mdl_version_test; then
echo error: mdl version precedes minimum
exit 1
fi
unset -f mdl_version_test
run_check '.*\.md' mdl --style "${scriptdir}/mdl-style.rb"

# Install via: dnf install ShellCheck
run_check '.*\.(ba)?sh' shellcheck

# Install via: dnf install yamllint
run_check '.*\.ya?ml' yamllint -s -c "${scriptdir}/yamlconfig.yaml"

run_shellcheck() {
local tool="shellcheck"
echo "===== $tool ====="

local detected_version
local required_version="0.7.0"

if ! command -v "${tool}" >/dev/null 2>&1; then
echo "ERROR: ${tool} is not installed"
echo "You can install it by running:"
echo " dnf install ShellCheck"
exit 1
fi

detected_version=$("${tool}" --version | grep "version:" | cut -d' ' -f2)
check_version "${detected_version}" "${required_version}" "${tool}"

get_files '.*\.(ba)?sh' | xargs -0 -r "${tool}" | tee -a "${OUTPUTS_FILE}"
echo
echo
}

run_yamllint() {
local tool="yamllint"
echo "===== $tool ====="

local detected_version
local required_version="1.33.0"

if ! command -v "${tool}" >/dev/null 2>&1; then
echo "ERROR: ${tool} is not installed"
echo "You can install it by running:"
echo " dnf install yamllint"
exit 1
fi

detected_version=$("${tool}" -v | cut -d' ' -f2)
check_version "${detected_version}" "${required_version}" "${tool}"

get_files '.*\.ya?ml' | xargs -0 -r "${tool}" -s -c "${scriptdir}/yamlconfig.yaml" | tee -a "${OUTPUTS_FILE}"
echo
echo
}


run_mdl
run_shellcheck
run_yamllint

(! < "${OUTPUTS_FILE}" read -r)

0 comments on commit 123e0cb

Please sign in to comment.