Skip to content

Instantly share code, notes, and snippets.

@wedy
Last active August 29, 2015 14:05
Show Gist options
  • Save wedy/96aa5bfd3959cc0a0dfa to your computer and use it in GitHub Desktop.
Save wedy/96aa5bfd3959cc0a0dfa to your computer and use it in GitHub Desktop.
git pre-commit hook using rubocop in ruby
#!/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