Skip to content

Instantly share code, notes, and snippets.

@stevenharman
Created March 19, 2020 15:05
Show Gist options
  • Save stevenharman/07c7c1adae2cb4b562cd6ee24c31ca4a to your computer and use it in GitHub Desktop.
Save stevenharman/07c7c1adae2cb4b562cd6ee24c31ca4a to your computer and use it in GitHub Desktop.
Run `standardrb --fix` as a git pre-commit hook. NOTE: this assumes `gem "standard"` is in your Gemfile
#!/bin/sh
# This file is `.git/hooks/format-ruby` and it has been `chmod +x`'d
# Assumption: https://github.com/testdouble/standard is in your Gemfile
set -e
rubyfiles=$(git diff --cached --name-only --diff-filter=ACM "*.rb" "*.rake" "Gemfile" "Rakefile" | tr '\n' ' ')
[ -z "$rubyfiles" ] && exit 0
# Standardize all ruby files
echo "💅 Formatting staged Ruby files using standardrb ($(echo $rubyfiles | wc -w | awk '{print $1}') total)"
echo "$rubyfiles" | xargs bundle exec standardrb --fix
# Add back the modified/prettified files to staging
# echo "$rubyfiles" | xargs git add
exit 0
#!/bin/sh
# This file is `.git/hooks/pre-commit` and it has been `chmod +x`'d
.git/hooks/format-ruby
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment