Skip to content

Commit 5146b88

Browse files
committed
Make shellcheck more efficient
Only checking changed files
1 parent 61228c0 commit 5146b88

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

.githooks/pre-commit/01-shellcheck.sh

+9-6
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22

33
set -euo pipefail
44

5-
DIR="$(pwd)" # hooks are always ran from the root of the repo
5+
# hooks are always ran from the root of the repo
66

7-
submodule_dirs=$(git submodule | awk '{ printf "-not -path '"$DIR"'/" $2 "/* " }')
8-
set -f # disable globbing
7+
# get all files that have been changed
8+
all_diff_files=`git diff --name-only --cached --ignore-submodules=all --diff-filter=ACM`
9+
if [ -z "$all_diff_files" ]; then exit 0; fi
10+
diff_files=`echo "$all_diff_files" | grep -v /.testsupport/`
911
# shellcheck disable=SC2086
10-
sh_files=$(find "$DIR" \( -name '*.sh' -or -name '*.bash' \) -not -path "$DIR"/.testsupport/'*' $submodule_dirs)
12+
exec_files=$(find $diff_files -exec sh -c 'head -n1 $1 | grep -qE '"'"'^#!(.*/|\/usr\/bin\/env +)bash'"'" sh {} \; -exec echo {} \;)
1113
# shellcheck disable=SC2086
12-
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 {} \;)
13-
set +f
14+
sh_files=$(find $diff_files \( -name '*.sh' -or -name '*.bash' \))
1415
all_files=`echo -e "$sh_files\n$exec_files" | sort | uniq`
16+
echo "Running shellcheck on the following files:"
17+
echo "$all_files"
1518
# shellcheck disable=SC2086
1619
shellcheck --shell bash $all_files

0 commit comments

Comments
 (0)