Skip to content

Instantly share code, notes, and snippets.

@vesh95
Forked from palkan/rubocop_pre_commit_hook
Created November 28, 2019 09:23
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 vesh95/ea30c1e7468a28fc87334c829c6714ea to your computer and use it in GitHub Desktop.
Save vesh95/ea30c1e7468a28fc87334c829c6714ea to your computer and use it in GitHub Desktop.
Rubocop pre-commit hook
#!/usr/bin/env ruby
ADDED_OR_MODIFIED = /^\s*(A|AM|M)/.freeze
changed_files = `git status --porcelain`.split(/\n/)
unstaged_files = `git ls-files -m`.split(/\n/)
changed_files = changed_files.select { |f| f =~ ADDED_OR_MODIFIED }
changed_files = changed_files.map { |f| f.split(" ")[1] }
changed_files -= (unstaged_files - changed_files)
changed_files = changed_files.select { |file_name| File.extname(file_name) == ".rb" }
changed_files = changed_files.join(" ")
exit(0) if changed_files.empty?
success = system(%(
rubocop #{changed_files}
))
STDIN.reopen('/dev/tty')
if success == false
puts "Would you like to continue press 'any key' or 'n/N' to halt? "
exit(1) if %w(N n).include?(gets.chomp)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment