Only run relevant tests in pre-commit

This commit is contained in:
Teajey 2025-12-10 17:24:14 +09:00
parent df2e58026a
commit d9dc6caea3
Signed by: Teajey
GPG Key ID: 970E790FE834A713
2 changed files with 64 additions and 3 deletions

View File

@ -2,10 +2,23 @@
APISNAP=api.snap.txt APISNAP=api.snap.txt
./scripts/api_snapshot ./core > core/$APISNAP ./scripts/staged_files_match './scripts/**' 'core/**/*.go' && ./scripts/api_snapshot ./core > core/$APISNAP
git diff --quiet core/$APISNAP
if [[ $? -ne 0 ]]; then if ! git diff --quiet core/$APISNAP; then
echo "There are unstaged changes to $APISNAP" echo "There are unstaged changes to $APISNAP"
exit 1 exit 1
fi fi
if ./scripts/staged_files_match 'assert/**' 'core/**/*'; then
go test ./core/... || {
echo "./core test failure"
exit 1
}
fi
if ./scripts/staged_files_match 'assert/**' 'http/props/htmlx/**/*.go'; then
go test ./http/props/htmlx/... || {
echo "./http test failure"
exit 1
}
fi

48
scripts/staged_files_match Executable file
View File

@ -0,0 +1,48 @@
#!/bin/bash
# staged_files_match - Check if any staged files match the provided glob patterns
#
# Usage: staged_files_match <pattern1> [pattern2] [...]
#
# Exits 0 (success) if at least one staged file matches any pattern
# Exits 1 (failure) if no staged files match any pattern
#
# Example:
# staged_files_match './assert/**' './core/**/*.go' && go test ./core/...
set -e
# Check if at least one argument provided
if [[ $# -eq 0 ]]; then
echo "Error: No glob patterns provided" >&2
echo "Usage: $0 <pattern1> [pattern2] [...]" >&2
exit 2
fi
# Get all staged files (including renamed files)
staged_files=$(git diff --cached --name-only --diff-filter=ACMR)
# If no staged files, exit with failure
if [[ -z "$staged_files" ]]; then
exit 1
fi
# Check each glob pattern against each staged file
for pattern in "$@"; do
# Remove './' prefix if present for consistent matching
pattern="${pattern#./}"
while IFS= read -r file; do
# Remove './' prefix from file if present
file="${file#./}"
# Use case for glob matching
case "$file" in
$pattern)
exit 0
;;
esac
done <<< "$staged_files"
done
exit 1