Skip to content

Commit 7c1104b

Browse files
maebealeclaude
andauthored
Add pre-commit and pre-push git hooks (#1437)
* Add pre-commit hook for rubocop, brakeman, and safety checks Runs on every commit: - rubocop on staged .rb files only - brakeman security scan - checks for debug statements (binding.pry, byebug, etc.) - checks for merge conflict markers - blocks committing .env/secrets files Hook is auto-installed via bin/setup. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * Move rubocop and brakeman to pre-push hook for faster commits Pre-commit now only runs lightweight safety checks (debug statements, conflict markers, secrets). Heavier tools run before push instead. Also removes credentials.yml.enc from secrets blocklist since it's meant to be committed (it's encrypted). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * Allow .env.example through pre-commit secrets check Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 5112f08 commit 7c1104b

3 files changed

Lines changed: 44 additions & 0 deletions

File tree

bin/pre-commit

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bash
2+
3+
staged_files=$(git diff --cached --name-only --diff-filter=d)
4+
staged_rb_files=$(echo "$staged_files" | grep '\.rb$' || true)
5+
6+
# Check for debug statements
7+
if [ -n "$staged_rb_files" ]; then
8+
if echo "$staged_rb_files" | xargs grep -n 'binding\.pry\|binding\.irb\|byebug\|debugger' 2>/dev/null; then
9+
echo "ERROR: Debug statements found in staged files. Remove them before committing."
10+
exit 1
11+
fi
12+
fi
13+
14+
# Check for merge conflict markers
15+
if [ -n "$staged_files" ]; then
16+
if echo "$staged_files" | xargs grep -n '<<<<<<<\|>>>>>>>\|=======' 2>/dev/null; then
17+
echo "ERROR: Merge conflict markers found in staged files. Resolve them before committing."
18+
exit 1
19+
fi
20+
fi
21+
22+
# Check for secrets/env files (allow .env.example)
23+
if echo "$staged_files" | grep '\.env$\|\.env\.\|master\.key' | grep -qv '\.env\.example$'; then
24+
echo "ERROR: Potentially sensitive files staged for commit:"
25+
echo "$staged_files" | grep '\.env$\|\.env\.\|master\.key' | grep -v '\.env\.example$'
26+
echo "Remove them from staging before committing."
27+
exit 1
28+
fi

bin/pre-push

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
3+
eval "$(command /opt/homebrew/bin/mise activate zsh)"
4+
5+
# Run rubocop on all Ruby files
6+
echo "Running rubocop..."
7+
bundle exec rubocop || exit 1
8+
9+
# Run brakeman security scan
10+
echo "Running brakeman security scan..."
11+
bundle exec brakeman --no-pager -q || exit 1

bin/setup

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ FileUtils.chdir APP_ROOT do
7070
puts "\n== Building Vite test assets =="
7171
system! "RAILS_ENV=test npx vite build --mode test"
7272

73+
puts "\n== Installing git hooks =="
74+
system! 'cp bin/pre-commit .git/hooks/pre-commit'
75+
system! 'chmod +x .git/hooks/pre-commit'
76+
system! 'cp bin/pre-push .git/hooks/pre-push'
77+
system! 'chmod +x .git/hooks/pre-push'
7378
puts "\n== Cleaning logs and tempfiles =="
7479
system! "bin/rails log:clear tmp:clear"
7580

0 commit comments

Comments
 (0)