Skip to content

Instantly share code, notes, and snippets.

@keang
Last active November 1, 2019 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keang/1066e8c0d2ca7face910491ac1794017 to your computer and use it in GitHub Desktop.
Save keang/1066e8c0d2ca7face910491ac1794017 to your computer and use it in GitHub Desktop.
pre-commit hook calling Rubocop to check staged ruby style
#!/bin/sh
# Commit this file to your project somewhere, then instruct teammates who wants to opt in to symlink to run the hook:
# ln -sf ../../pre-commit .git/hooks/pre-commit
# To capture user input
exec < /dev/tty
./style_check # or wherever you keep the style_check script
if [ $? != 0 ]; then
while true; do
echo ""
read -p "Do you want to fix the above offences before committing, and be an awesome dev? (Y/n)" yn
case $yn in
[Yy]* ) exit 1;;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
esac
done
fi
#!/usr/bin/env ruby
#
# Checks style on staged ruby files
# Commit this to your project root
#
require 'rubocop'
ADDED_OR_MODIFIED = /A|AM|^M/.freeze
changed_files = `git status --porcelain`.split(/\n/).
select { |file_name_with_status|
file_name_with_status =~ ADDED_OR_MODIFIED
}.
map { |file_name_with_status|
file_name_with_status.split(' ')[1]
}.
select { |file_name|
File.extname(file_name) == '.rb'
}.join(' ')
system("rubocop #{changed_files}") unless changed_files.empty?
exit $CHILD_STATUS.to_s[-1].to_i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment