From 7b26ea518fa9cdba29755184753ba181b1e8dc08 Mon Sep 17 00:00:00 2001 From: Giovanni Bassi Date: Sat, 1 Jun 2024 04:58:35 -0300 Subject: [PATCH] Make shellcheck more efficient Only checking changed files --- .githooks/pre-commit/01-shellcheck.sh | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/.githooks/pre-commit/01-shellcheck.sh b/.githooks/pre-commit/01-shellcheck.sh index e25a103..bd4c853 100755 --- a/.githooks/pre-commit/01-shellcheck.sh +++ b/.githooks/pre-commit/01-shellcheck.sh @@ -2,15 +2,16 @@ set -euo pipefail -DIR="$(pwd)" # hooks are always ran from the root of the repo +# hooks are always ran from the root of the repo -submodule_dirs=$(git submodule | awk '{ printf "-not -path '"$DIR"'/" $2 "/* " }') -set -f # disable globbing +# get all files that have been changed +all_diff_files=`git diff --name-only --cached --ignore-submodules=all --diff-filter=ACM` +if [ -z "$all_diff_files" ]; then exit 0; fi +diff_files=`echo "$all_diff_files" | grep -v /.testsupport/` # shellcheck disable=SC2086 -sh_files=$(find "$DIR" \( -name '*.sh' -or -name '*.bash' \) -not -path "$DIR"/.testsupport/'*' $submodule_dirs) +exec_files=$(find $diff_files -exec sh -c 'head -n1 $1 | grep -qE '"'"'^#!(.*/|\/usr\/bin\/env +)bash'"'" sh {} \; -exec echo {} \;) # shellcheck disable=SC2086 -exec_files=$(find "$DIR" -not -path "$DIR"/.testsupport/'*' $submodule_dirs -type f -exec sh -c 'head -n1 $1 | grep -qE '"'"'^#!(.*/|\/usr\/bin\/env +)bash'"'" sh {} \; -exec echo {} \;) -set +f +sh_files=$(find $diff_files \( -name '*.sh' -or -name '*.bash' \)) all_files=`echo -e "$sh_files\n$exec_files" | sort | uniq` # shellcheck disable=SC2086 shellcheck --shell bash $all_files