Created
May 1, 2014 20:14
-
-
Save kevinjalbert/6c1c2af40d8365cc48c5 to your computer and use it in GitHub Desktop.
Executable to run rubocop only on added/modified files in a git repository
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 | |
# Improved from https://gist.github.com/mpeteuil/6147292 | |
require 'english' | |
require 'rubocop' | |
VALID_FILE_EXT = %w(.rb .rabl .rake) | |
ADDED_OR_MODIFIED = /^\s*(A|AM|M)/.freeze | |
class NotInRepository < StandardError; end | |
def get_repository(path = Dir.pwd) | |
ascender = Pathname.new(path).to_enum(:ascend) | |
repo_path = ascender.detect { |dir| (dir + '.git').exist? } | |
raise NotInRepository if repo_path.nil? | |
repo_path.to_s | |
rescue NotInRepository | |
puts "You must be within a Git repository to run this command." | |
exit 0 | |
end | |
Dir.chdir(get_repository) do | |
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| | |
VALID_FILE_EXT.include? File.extname(file_name) | |
}.join(' ') | |
system("rubocop #{changed_files}") unless changed_files.empty? | |
end | |
exit $CHILD_STATUS.to_s[-1].to_i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Skipants You seen this? I need a git hook that just works. And I need you to do the work for me 😟