Skip to content

Instantly share code, notes, and snippets.

@Synkevych
Last active November 10, 2021 14:47
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 Synkevych/1b7c386bc4c4dedf09571beee7ca54b7 to your computer and use it in GitHub Desktop.
Save Synkevych/1b7c386bc4c4dedf09571beee7ca54b7 to your computer and use it in GitHub Desktop.
Run Rubocop on pre-commit - git hook for Rails projects.
#!/usr/bin/env ruby
require 'English'
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' unless file_name == 'db/schema.rb'
}.join(' ')
system("rubocop #{changed_files}") unless changed_files.empty?
exit $CHILD_STATUS.to_s[-1].to_i
@Synkevych
Copy link
Author

Synkevych commented Jan 23, 2021

To setup this Hook in your project use this command:

cd your-project-name;
cd .git/hooks;
curl https://gist.githubusercontent.com/Synkevych/1b7c386bc4c4dedf09571beee7ca54b7/raw/7f46da7006a1eb957a727b1894693f9a34f150e1/pre-commit > pre-commit;
chmod +x pre-commit;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment