Skip to content

Commit

Permalink
chore: improve pre-commit hook (add extra checks)
Browse files Browse the repository at this point in the history
  • Loading branch information
idevat committed Feb 3, 2025
1 parent 3e4b0fe commit c146d37
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 0 deletions.
53 changes: 53 additions & 0 deletions .bin/pre-commit/check-all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/sh

# Check definition. Multiline string, every line is for one check.
# Format of line is:
# `Check title ./path/relative/to/this/script/check-script.sh`.
# The last space separates title from script path.
check_list="
BIOME LINT CHECK ./check-lint.sh
BIOME FORMAT CHECK ./check-format.sh
MAKEFILE FILE LISTING CHECK ./check-makefile-file-listing.sh
"

extract_title() {
echo "$1" | awk '{$NF=""; print $0}'
}

extract_command() {
realpath "$(dirname "$0")"/"$(echo "$1" | awk '{print $NF}')"
}

err_report=""
while IFS= read -r line; do
[ -n "$line" ] || continue

check=$(extract_command "$line")

if ! output=$("$check" 2>&1); then
err_report="${err_report}$(extract_title "$line") ($check)\n$output\n\n"
fi
done << EOF
$check_list
EOF

if [ -z "$err_report" ]; then
exit 0
fi

echo "Warning: some check failed"
printf "%b" "$err_report"

printf "Checks failed. Continue with commit? (c)Continue (a)Abort: " > /dev/tty
IFS= read -r resolution < /dev/tty

case "$resolution" in
c | C) exit 0 ;;
a | A)
echo "Commit aborted."
exit 1
;;
esac

echo "Unknown resolution '$resolution', aborting commit..."
exit 1
5 changes: 5 additions & 0 deletions .bin/pre-commit/check-format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

root_dir="$(realpath "$(dirname "$0")"/../../)"

npx @biomejs/biome format "$root_dir"
5 changes: 5 additions & 0 deletions .bin/pre-commit/check-lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

root_dir="$(realpath "$(dirname "$0")"/../../)"

npx @biomejs/biome lint --error-on-warnings "$root_dir"
44 changes: 44 additions & 0 deletions .bin/pre-commit/check-makefile-file-listing.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/sh

makefile_list="\
Makefile.am
packages/app/Makefile.am
packages/test/Makefile.am
"

get_mentioned_files() {
for makefile in $1; do
[ -n "$makefile" ] || continue
"$(dirname "$0")"/extract-extra-dist.sh "$makefile"
done
}

get_unlisted_files() {
makefiles=$1
added_files=$2

mentioned_files=$(get_mentioned_files "$makefiles")

for file in $added_files; do
if ! echo "$mentioned_files" |
grep --quiet --fixed-strings --line-regexp "$file" 2> /dev/null; then
echo "$file"
fi
done
}

git_added="$(git diff --cached --name-only --diff-filter=A)"

if [ -z "$git_added" ]; then
exit 0
fi

unlisted_files="$(get_unlisted_files "$makefile_list" "$git_added")"

if [ -z "$unlisted_files" ]; then
exit 0
fi

echo "Warning: The following files are not listed in any Makefile.am:"
echo "$unlisted_files"
exit 1
30 changes: 30 additions & 0 deletions .bin/pre-commit/extract-extra-dist.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/sh

makefile="$1"
inside_extra_dist=0
files=""

while IFS= read -r line; do
case "$line" in
*EXTRA_DIST[[:space:]]*=*)
inside_extra_dist=1
files="${line#*=}"
files="${files%\\}"
;;
*\\)
[ $inside_extra_dist -eq 1 ] && files="$files ${line%\\}"
;;
*) # the last line in EXTRA_DIST
[ $inside_extra_dist -eq 1 ] && files="$files $line" && break
;;
esac
done < "$makefile"

# no globing
set -f
# split to arguments
# shellcheck disable=2086
set -- $files
for file in "$@"; do
printf "%s\n" "${makefile%Makefile.am}$file"
done
4 changes: 4 additions & 0 deletions .bin/pre-commit/pre-commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh

./.bin/pre-commit/remove-nexus.sh
./.bin/pre-commit/check-all.sh
File renamed without changes.
6 changes: 6 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ EXTRA_DIST = \
.bin/init_nexus.sh \
.bin/init.sh \
.bin/npm_install.sh \
.bin/pre-commit/extract-extra-dist.sh \
.bin/pre-commit/check-all.sh \
.bin/pre-commit/check-format.sh \
.bin/pre-commit/check-lint.sh \
.bin/pre-commit/check-makefile-file-listing.sh \
.bin/pre-commit/remove-nexus.sh \
.bin/pre-commit.sh \
.bin/render-spec.sh \
.browserslistrc \
Expand Down

0 comments on commit c146d37

Please sign in to comment.