Skip to content

Instantly share code, notes, and snippets.

@MyklClason
Last active July 19, 2025 11:03
Show Gist options
  • Save MyklClason/8925b74b301f0a3bac0353bb4af3b293 to your computer and use it in GitHub Desktop.
Save MyklClason/8925b74b301f0a3bac0353bb4af3b293 to your computer and use it in GitHub Desktop.
Standardrb with lefthook
# .lefthook.yml
# 1) Define a “common” map of commands
.common: &common_commands
check_clean:
run: |
if [ -n "$(git status --porcelain)" ]; then
echo "❌ Uncommitted changes detected. Please commit or stash before pushing."
exit 1
fi
standard_fix:
run: |
echo "Running standardrb --fix..."
bundle exec standardrb --fix
if [ -n "$(git status --porcelain)" ]; then
echo "🔧 Auto-fixes detected. Committing..."
git add -u
git commit -m 'Auto-format: standardrb --fix'
exit 1
else
echo "✅ No style changes to commit."
fi
# 2) Reuse it in as many hooks as you need:
pre-push:
commands:
<<: *common_commands
post-merge:
commands:
<<: *common_commands
post-commit:
commands:
<<: *common_commands
# Uncomment to enforce rails standards, best only done after ruby standards have been fully enforced.
# plugins:
# - standard-rails
# Uncomment to ignore specific directories or files.
# ignore: # default: []
# - 'vendor/**/*'

Usage

Add gems:

bundle add lefthook
bundle add standard
bundle add standard-rails

Setup standard-rails (optional, often requires a lot of changes):

Add the provided .standard.yml file.

Install Left Hook:

lefthook install

Copy/paste the .lefthook.yml file.

Install again to update the hooks

lefthook install

Commits, pushes and merges should now check if clean, it'll also run standardrb --fix before

Commit to verify things are working:

git add -A && git commit -m "Have some standards! Add lefthook and standard"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment