From c2a6fafbac680373a12b43021d6ae8e17d07950e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Lesimple?= Date: Tue, 20 Feb 2024 17:02:01 +0000 Subject: [PATCH] chore: devenv: enhance perltidy/shellcheck pre-commit logic --- bin/dev/perl-tidy.sh | 19 ++++++------------- bin/dev/shell-check.sh | 38 ++++++++++++++------------------------ contrib/git/pre-commit | 22 +++++++++------------- 3 files changed, 29 insertions(+), 50 deletions(-) diff --git a/bin/dev/perl-tidy.sh b/bin/dev/perl-tidy.sh index 6cc6afc00..dcfdfe225 100755 --- a/bin/dev/perl-tidy.sh +++ b/bin/dev/perl-tidy.sh @@ -1,6 +1,6 @@ #! /usr/bin/env bash # vim: set filetype=sh ts=4 sw=4 sts=4 et: -set -ueo pipefail +set -ue basedir=$(readlink -f "$(dirname "$0")"/../..) # shellcheck source=lib/shell/functions.inc @@ -39,18 +39,11 @@ params="$params \ --maximum-line-length=120 \ " -if [ -z "${2:-}" ]; then - # run on all perl files - # shellcheck disable=SC2086 - find . -type f ! -name "*.tdy" ! -name "*.ERR" ! -name ".tidybak" ! -name "$(basename "$0")" -print0 | \ - xargs -r0 grep -l 'set filetype=perl' -- | \ - xargs -r perltidy $params -else - # run on only one file - action_detail "${BLUE}$2${NOC}" - # shellcheck disable=SC2086 - perltidy $params "$2" -fi +# run on all perl files (".") or only the $2 file if specified +# shellcheck disable=SC2086 +find "${2:-.}" -type f ! -name "*.tdy" ! -name "*.ERR" ! -name "*.tidybak" ! -name "*.html" ! -name "$(basename "$0")" -print0 | \ + xargs -r0 grep -l 'set filetype=perl' -- | \ + xargs -r perltidy $params bad="" nbbad=0 diff --git a/bin/dev/shell-check.sh b/bin/dev/shell-check.sh index be3111f27..5cdcb89a2 100755 --- a/bin/dev/shell-check.sh +++ b/bin/dev/shell-check.sh @@ -46,30 +46,20 @@ else action_doing "Checking shell files syntax using system shellcheck" fi -if [ -z "${2:-}" ]; then - for i in $(find . -type f ! -name "*.swp" ! -name "*.orig" ! -name "*.rej" -print0 | xargs -r0 grep -l 'set filetype=sh' | sort) - do - run_shellcheck "$i"; ret=$? - if [ $ret != 0 ]; then - (( fails++ )) - fi - if [ $ret = 3 ] || [ $ret = 4 ]; then - echo "${RED}WARNING: your shellcheck seems too old (code $ret), please upgrade it or use a more recent docker tag!${NOC}" >&2 - fi - done - - if [ "$fails" -ne 0 ] ; then - action_error "Got $fails errors" - else - action_done "success" +for i in $(find "${2:-.}" -type f ! -name "*.swp" ! -name "*.orig" ! -name "*.rej" -print0 | xargs -r0 grep -l 'set filetype=sh' | sort) +do + run_shellcheck "$i"; ret=$? + if [ $ret != 0 ]; then + (( fails++ )) fi - exit "$fails" -else - run_shellcheck "$2"; ret=$? - if [ "$ret" -ne 0 ] ; then - action_error - else - action_done + if [ $ret = 3 ] || [ $ret = 4 ]; then + echo "${RED}WARNING: your shellcheck seems too old (code $ret), please upgrade it or use a more recent docker tag!${NOC}" >&2 fi - exit $ret +done + +if [ "$fails" -ne 0 ] ; then + action_error "Got $fails errors" +else + action_done "success" fi +exit "$fails" diff --git a/contrib/git/pre-commit b/contrib/git/pre-commit index 57aa03dc6..bf2928dcb 100755 --- a/contrib/git/pre-commit +++ b/contrib/git/pre-commit @@ -91,25 +91,21 @@ fi # Now execute shellcheck in every changed .sh file for f in $(git diff --cached --name-only --diff-filter=ACM $against); do - if echo "$f" | grep -q -e "lib/shell" -e "bin/" -e "docker/devenv" && head -1 "$f" | grep -Eq '/env (ba)?sh|/bin/(ba)?sh'; then - if ! ./docker/devenv/run-tool.sh shellcheck "$f"; then - echo - echo "ERROR: shell-check failed on $f" - isbad=1 - fi + if ! ./docker/devenv/run-tool.sh shellcheck "$f"; then + echo + echo "ERROR: shell-check failed on $f" + isbad=1 fi - if echo "$f" | grep -q -e "lib/perl" -e "bin/" && head -20 "$f" | grep -Eq '/env perl|/bin/perl|package OVH::|use common::|use strict|use warnings'; then - if ! ./docker/devenv/run-tool.sh checktidy "$f"; then - echo - echo "ERROR: perl tidy failed on $f" - isbad=1 - fi + if ! ./docker/devenv/run-tool.sh checktidy "$f"; then + echo + echo "ERROR: perl tidy failed on $f" + isbad=1 fi done # check versions and docs -ver=$(grep -F 'our $VERSION =' lib/perl/OVH/Bastion.pm | cut -d"'" -f2) +ver=$(grep -F 'our $VERSION =' lib/perl/OVH/Bastion.pm | cut -d"'" -f2 | cut -d- -f1) if ! grep -Eq "^## v$ver - 202./[01][0-9]/.." doc/CHANGELOG.md; then echo echo "ERROR: detected version '$ver', but no matching entry in doc/CHANGELOG.md"