-
Notifications
You must be signed in to change notification settings - Fork 1
chore: add Lefthook for Git hooks management #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
justin808
wants to merge
11
commits into
main
Choose a base branch
from
justin808/add-precommit-hook
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+132
−1
Open
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f5f00c3
Add pre-commit hook to prevent simple test and lint failures
justin808 1cd20ea
chore: fix prettier formatting
justin808 f007be6
Improve pre-commit hook performance and usability
justin808 68d4dd4
Migrate to Lefthook for Git hooks management
justin808 77ff7fc
fix: correct glob patterns to match Ruby files in subdirectories
justin808 7f5cc9d
fix: handle filenames with spaces safely in hook scripts
justin808 952ed56
fix: address PR review feedback
justin808 ce81ace
docs: address PR review feedback on git hooks
justin808 db4154f
refactor: remove RSpec from pre-commit hook
justin808 d036e07
docs: fix markdown code block style for markdownlint
justin808 30fc454
fix: correct trailing newline detection logic
justin808 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| #!/usr/bin/env bash | ||
| # Check for trailing newlines on staged files | ||
| set -euo pipefail | ||
|
|
||
| if [ $# -eq 0 ]; then | ||
| echo "✅ No files to check for trailing newlines" | ||
| exit 0 | ||
| fi | ||
|
|
||
| echo "🔍 Checking trailing newlines on staged files..." | ||
|
|
||
| failed_files=() | ||
| for file in "$@"; do | ||
| if [ -f "$file" ] && [ -s "$file" ]; then | ||
| if ! tail -c 1 "$file" | grep -q '^$'; then | ||
| echo "❌ Missing trailing newline: $file" | ||
| failed_files+=("$file") | ||
| fi | ||
| fi | ||
| done | ||
|
|
||
| if [ ${#failed_files[@]} -gt 0 ]; then | ||
| echo "" | ||
| echo "❌ Trailing newline check failed!" | ||
| echo "💡 Add trailing newlines to: ${failed_files[*]}" | ||
| echo "🔧 Quick fix: for file in ${failed_files[*]}; do echo >> \"\$file\"; done" | ||
| echo "🚫 Skip hook: git commit --no-verify" | ||
| exit 1 | ||
| fi | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| echo "✅ All files have proper trailing newlines" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| #!/usr/bin/env bash | ||
| # Run RSpec tests for affected files | ||
| set -euo pipefail | ||
|
|
||
| if [ $# -eq 0 ]; then | ||
| echo "✅ No Ruby files changed" | ||
| exit 0 | ||
| fi | ||
|
|
||
| # Find corresponding spec files for changed lib files | ||
| spec_files=() | ||
| for file in "$@"; do | ||
| if [[ $file == lib/* ]]; then | ||
| # Convert lib/package_json/foo.rb to spec/package_json/foo_spec.rb | ||
| spec_file=$(echo "$file" | sed 's|^lib/|spec/|' | sed 's|\.rb$|_spec.rb|') | ||
| if [ -f "$spec_file" ]; then | ||
| spec_files+=("$spec_file") | ||
| fi | ||
| elif [[ $file == spec/* ]]; then | ||
| # If it's already a spec file, include it | ||
| spec_files+=("$file") | ||
| fi | ||
| done | ||
|
|
||
| if [ ${#spec_files[@]} -eq 0 ]; then | ||
| echo "✅ No corresponding spec files found for changed files" | ||
| exit 0 | ||
| fi | ||
|
|
||
| echo "🧪 Running affected RSpec tests:" | ||
| printf " %s\n" "${spec_files[@]}" | ||
|
|
||
| if ! bundle exec rspec "${spec_files[@]}"; then | ||
| echo "" | ||
| echo "❌ Tests failed!" | ||
| echo "💡 Run full suite: bundle exec rspec" | ||
| echo "🚫 Skip hook: git commit --no-verify" | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "✅ Affected tests passed" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #!/usr/bin/env bash | ||
| # Lint Ruby files with RuboCop | ||
| set -euo pipefail | ||
|
|
||
| if [ $# -eq 0 ]; then | ||
| echo "✅ No Ruby files to lint" | ||
| exit 0 | ||
| fi | ||
|
|
||
| echo "🔍 RuboCop on staged Ruby files:" | ||
| printf " %s\n" "$@" | ||
|
|
||
| if ! bundle exec rubocop --force-exclusion --display-cop-names -- "$@"; then | ||
| echo "" | ||
| echo "❌ RuboCop check failed!" | ||
| echo "💡 Auto-fix: bundle exec rubocop -a --force-exclusion -- <files>" | ||
| echo "🚫 Skip hook: git commit --no-verify" | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "✅ RuboCop checks passed" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # Lefthook configuration | ||
| # Fast pre-commit hooks that check staged files only | ||
| # Install with: bundle exec lefthook install | ||
|
|
||
| pre-commit: | ||
| parallel: true | ||
| commands: | ||
| rubocop: | ||
| glob: '**/*.rb' | ||
| run: bin/lefthook/rubocop-lint {staged_files} | ||
|
|
||
| rspec: | ||
| glob: '**/*.rb' | ||
| run: bin/lefthook/rspec-affected {staged_files} | ||
|
|
||
| trailing-newlines: | ||
| run: bin/lefthook/check-trailing-newlines {staged_files} | ||
|
|
||
| pre-push: | ||
| commands: | ||
| full-rubocop: | ||
| run: bundle exec rubocop | ||
|
|
||
| full-rspec: | ||
| run: bundle exec rspec |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you put this section after the existing paragraph ("To install this gem onto your local machine")? since that isn't part of this subheader