-
-
Save gaganawhad/b292a0d07601fa8ea341 to your computer and use it in GitHub Desktop.
Ruby style guide git pre-commit hook using Rubocop as the style guide checker. Only runs on staged ruby files that have been added and/or modified.
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 'english' | |
require 'rubocop' | |
require 'byebug' | |
ADDED_OR_MODIFIED = /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(' ') | |
any ? system("git ls-files --modified --others --exclude-standard") #get a list of all modified and new files that aren't staged | |
if any? system("git stash git stash -k -u") # stash all unstaged files | |
status = system("rubocop -a #{changed_files}") unless changed_files.empty? | |
if any? system("git stash pop") # pop the latest stashed files | |
exit status.to_s[-1].to_i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment