Add pre-commit hook to run standard

This commit is contained in:
Sami Samhuri 2026-04-11 13:11:52 -07:00
parent a64c2cc929
commit 30b7485709
No known key found for this signature in database
4 changed files with 55 additions and 2 deletions

View file

@ -12,5 +12,5 @@ gem "bake", "~> 0.20"
group :development, :test do
gem "guard", "~> 2.18"
gem "minitest", "~> 6.0"
gem "standard", "~> 1.43"
gem "standard", "~> 1.52"
end

View file

@ -174,7 +174,7 @@ DEPENDENCIES
minitest (~> 6.0)
phlex (~> 2.3)
rouge (~> 4.6)
standard (~> 1.43)
standard (~> 1.52)
RUBY VERSION
ruby 4.0.2

View file

@ -34,4 +34,8 @@ else
bundle install
fi
echo "*** installing git hooks"
mkdir -p .git/hooks
ln -sf "../../bin/hooks/pre-commit" .git/hooks/pre-commit
echo "*** done"

49
bin/hooks/pre-commit Executable file
View file

@ -0,0 +1,49 @@
#!/bin/zsh
set -euo pipefail
STAGED_FILES=(${(f)"$(git diff --cached --name-only --diff-filter=ACM)"})
if [[ ${#STAGED_FILES[@]} -eq 0 ]]; then
exit 0
fi
RUBY_FILES=()
for file in "${STAGED_FILES[@]}"; do
if [[ -f "$file" ]]; then
if [[ "$file" =~ \.rb$ ]] || head -1 "$file" 2>/dev/null | grep -q ruby; then
RUBY_FILES+=("$file")
fi
fi
done
if [[ ${#RUBY_FILES[@]} -eq 0 ]]; then
exit 0
fi
echo "Running StandardRB on staged Ruby files..."
if command -v rbenv &>/dev/null; then
rbenv exec bundle exec standardrb --fix "${RUBY_FILES[@]}"
else
bundle exec standardrb --fix "${RUBY_FILES[@]}"
fi
MODIFIED=false
for file in "${RUBY_FILES[@]}"; do
if ! git diff --quiet "$file"; then
MODIFIED=true
echo "✗ $file was modified by StandardRB"
fi
done
if [[ "$MODIFIED" == "true" ]]; then
echo ""
echo "❌ Formatting changes were made. Please review and stage the changes, then commit again."
echo ""
echo "To see the changes: git diff"
echo "To add the changes: git add -u"
exit 1
fi
echo "✅ All files are properly formatted"
exit 0