Skip to content

Commit

Permalink
chore: devenv: enhance perltidy/shellcheck pre-commit logic
Browse files Browse the repository at this point in the history
  • Loading branch information
speed47 committed Feb 21, 2024
1 parent 3c6dd69 commit c2a6faf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 50 deletions.
19 changes: 6 additions & 13 deletions bin/dev/perl-tidy.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
38 changes: 14 additions & 24 deletions bin/dev/shell-check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
22 changes: 9 additions & 13 deletions contrib/git/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit c2a6faf

Please sign in to comment.