Last active
July 26, 2019 01:58
-
-
Save brand-it/bb2fe3899282024321dad5feaacd6d06 to your computer and use it in GitHub Desktop.
just a script to check code climate stuff on git commit or what ever
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 | |
files = [] | |
waiting = true | |
threads = [] | |
lines = [] | |
ignore = [ | |
'db/schema.rb' | |
] | |
def compile_lines_changed(files) | |
lines_changed = {} | |
files.each do |file| | |
changes = `git diff --unified=0 --cached #{file}`.scan(/@@.*@@/) | |
lines_changed[file] = [] | |
changes.each do |c| | |
c.delete('@').strip.split(' ').each do |change| | |
values = change.split(',') | |
if values.size == 2 | |
values = values.map(&:to_i) | |
values.last.times.each do |time| | |
lines_changed[file].push(values.first.abs + time) | |
end | |
elsif values.size == 1 | |
lines_changed[file].push(values.first.to_i.abs) | |
end | |
end | |
end | |
end | |
lines_changed | |
end | |
files += `git diff --name-only --cached`.split("\n") | |
lines_changed = compile_lines_changed(files) | |
if files.size.zero? | |
puts 'no files staged' | |
exit 0 | |
end | |
files.reject! { |x| ignore.include?(x) } | |
puts 'Checking changes' | |
files.each do |file| | |
puts file | |
end | |
exit 0 if files.size.zero? | |
threads << Thread.new do | |
index = 0 | |
chars = %w[| / - \\] | |
while waiting | |
print "codeclimate analyzing ... #{chars[(index += 1) % chars.length]}\r" | |
sleep(1) | |
end | |
puts 'codeclimate analyzing ... done' | |
end | |
threads << Thread.new do | |
lines = %x(codeclimate analyze #{files.join(' ')}) | |
waiting = false | |
end | |
threads.map(&:join) | |
exit true if lines.include?('Found 0 issues') | |
line_numbers = [] | |
lines.split("\n").each do |line| | |
if line.match?(/==.*==/) | |
line_numbers = lines_changed[line.delete('=').strip.split.first] || [] | |
puts line | |
elsif line.match(/^(\d*-\d*|\d*):/) | |
numbers = line.match(/^(\d*-\d*|\d*):/)[0].split('-').map(&:to_i) | |
range = if numbers.size == 2 | |
Range.new(*numbers) | |
else | |
Range.new(numbers.first, numbers.first) | |
end | |
puts(line) if (line_numbers & range.to_a).any? | |
else | |
puts line | |
end | |
end | |
# 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 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