Last active
August 29, 2015 14:05
-
-
Save wedy/96aa5bfd3959cc0a0dfa to your computer and use it in GitHub Desktop.
git pre-commit hook using rubocop in ruby
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 | |
require 'rubocop' | |
git_changed_rows = `git status --porcelain` | |
changed_rows_array = git_changed_rows.split(/\n/) | |
added_or_changed_rows_array = changed_rows_array.select { |row| row =~ /A|AM|^M/ } | |
added_or_changed_files_array = added_or_changed_rows_array.collect { |row| row.split(' ')[1] } | |
#sanitize - rb only | |
added_or_changed_files_array.select! { |file| File.extname(file) == '.rb' } | |
changed_files_to_s = added_or_changed_files_array.join(' ') | |
system("rubocop #{changed_files_to_s}") unless changed_files_to_s.empty? | |
error = $CHILD_STATUS.to_s[-1].to_i | |
STDIN.reopen('/dev/tty') | |
if error === 1 | |
puts "Would you like to continue press 'any key' or 'n/N' to halt? " | |
if ['N','n'].include? gets.chomp | |
exit 1 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment