Skip to content

Instantly share code, notes, and snippets.

@dlupu
Forked from kiran/rubocop_pre_commit_hook
Last active February 18, 2021 15:50
Show Gist options
  • Save dlupu/a85b334bde34e98263e4 to your computer and use it in GitHub Desktop.
Save dlupu/a85b334bde34e98263e4 to your computer and use it in GitHub Desktop.
forked pre-commit hook for changed ruby files. (whitespace sensitivity in the regex was borking selection)
#!/usr/bin/env ruby
require 'english'
require 'rubocop'
ADDED_OR_MODIFIED = /^\s*(A|AM|M)/.freeze
changed_files = `git status --porcelain`.split(/\n/).
select { |file_name_with_status|
file_name_with_status =~ ADDED_OR_MODIFIED
}.
map { |file_name_with_status|
file_name_with_status.split(' ')[1]
}.
select { |file_name|
File.extname(file_name) == '.rb'
}.join(' ')
system("rubocop -a #{changed_files}") unless changed_files.empty?
exit $CHILD_STATUS.to_s[-1].to_i
@dlupu
Copy link
Author

dlupu commented Feb 9, 2016

throw this into .git/pre-commit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment