Last active
September 7, 2022 16:25
-
-
Save brand-it/93d04c839238283e3e756811b9c9cee0 to your computer and use it in GitHub Desktop.
A simple tool to run rubocop on files you have changed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
echo "Creating $(git rev-parse --git-dir)/hooks/pre-commit.d" | |
mkdir $(git rev-parse --git-dir)/hooks/pre-commit.d | |
echo "Downloading rubocop hook into $(git rev-parse --git-dir)/hooks/pre-commit.d/rubocop" | |
wget -O $(git rev-parse --git-dir)/hooks/pre-commit.d/rubocop https://gist.githubusercontent.com/brand-it/93d04c839238283e3e756811b9c9cee0/raw/616bf812b58bcefc52d564ed8abacbf30966193c/pre-commit.d_rubocop | |
echo "Changing rubocop into a executable $(git rev-parse --git-dir)/hooks/pre-commit.d/rubocop" | |
chmod 755 $(git rev-parse --git-dir)/hooks/pre-commit.d/rubocop | |
echo "adding hooks_directory variable to $(git rev-parse --git-dir)/hooks/pre-commit if it does not already exist" | |
grep -qxF 'hooks_directory=$(git rev-parse --git-dir)/hooks/pre-commit.d' $(git rev-parse --git-dir)/hooks/pre-commit || echo 'hooks_directory=$(git rev-parse --git-dir)/hooks/pre-commit.d' >> $(git rev-parse --git-dir)/hooks/pre-commit | |
echo "adding $hooks_directory/rubocop variable to $(git rev-parse --git-dir)/hooks/pre-commit if it does not already exist" | |
grep -qxF '$hooks_directory/rubocop' $(git rev-parse --git-dir)/hooks/pre-commit || echo '$hooks_directory/rubocop' >> $(git rev-parse --git-dir)/hooks/pre-commit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# frozen_string_literal: true | |
require 'json' | |
def files | |
@files ||= begin | |
`git diff --staged --name-only --cached`.split("\n").reject do |file| | |
!File.exists?(file) | |
end | |
end | |
end | |
def rubocop | |
@rubocop ||= JSON.parse( | |
`bundle exec rubocop --parallel --force-exclusion --format json #{files.join(' ')}`, | |
symbolize_names: true | |
) | |
end | |
def violations | |
rubocop[:files].select { |x| x[:offenses].any? } | |
end | |
def format_violations | |
violations.each do |violation| | |
violation[:offenses].each do |offense| | |
puts "#{violation[:path]}:#{offense.dig(:location, :line)}:#{offense.dig(:location, :column)} #{offense[:cop_name]} #{offense[:message]}\n" | |
end | |
end | |
puts "\n\n#{rubocop.dig(:summary, :inspected_file_count)} file inspected, #{rubocop.dig(:summary, :offense_count)} offense detected" | |
end | |
def success(message) | |
puts message | |
exit 0 | |
end | |
def rubocop_autocorrect | |
system("bundle exec rubocop -A #{violations.map { |v| v[:path] }.join(' ')}") | |
end | |
def waiting | |
index = 0 | |
chars = %w[| / - \\] | |
while @rubocop.nil? | |
print "rubocop analyzing ... #{chars[(index += 1) % chars.length]}\r" | |
sleep(1) | |
end | |
puts 'rubocop analyzing ... done' | |
end | |
success('no file to check') if files.size.zero? | |
[].tap do |threads| | |
threads << Thread.new { rubocop } | |
threads << Thread.new { waiting } | |
threads.map(&:join) | |
end | |
exit true if violations.empty? | |
format_violations | |
# Open input from keyboard usin IO.new(0) normal standard in is closed | |
fd = IO.sysopen '/dev/tty', 'r' | |
ios = IO.new(fd, 'r') | |
print 'Do you want to fix and exit?(Y) ' | |
if ios.gets.chomp == 'Y' | |
rubocop_autocorrect | |
exit 1 | |
end | |
print 'Do you want to commit anyway?(Y) ' | |
exit ios.gets.chomp == 'Y' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
curl https://gist.githubusercontent.com/brand-it/93d04c839238283e3e756811b9c9cee0/raw/0b5124535880c21536c0c8d936b3ee723d1d196a/install | bash
To add this rubocop to your current repo git directory, run the above command in the terminal.
Every time you do a git commit -m 'something', it will run rubocop against the files you have changed
To ignore hooks altogether just add
--no-verify