Skip to content

Commit

Permalink
Make shellcheck more efficient
Browse files Browse the repository at this point in the history
Only checking changed files
  • Loading branch information
giggio committed Jun 1, 2024
1 parent 61228c0 commit 5146b88
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions .githooks/pre-commit/01-shellcheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

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`
echo "Running shellcheck on the following files:"
echo "$all_files"
# shellcheck disable=SC2086
shellcheck --shell bash $all_files

0 comments on commit 5146b88

Please sign in to comment.