Created
August 3, 2013 17:44
-
-
Save mpeteuil/6147292 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 hidden or 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' | |
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(' ') | |
system("rubocop #{changed_files}") unless changed_files.empty? | |
exit $CHILD_STATUS.to_s[-1].to_i |
You can use
git diff --diff-filter AM --name-only
to list the modified files.
But I would like to show only issues of changed lines, not files.
@mklink to only show changed lines, you'll need to write something up yourself, I haven't found anything out there I'm happy with.
E.g., something that:
- Collects all the changed ruby file names in diff
- Collects all the changed lines in the diff. E.g., for
@@ -98,20 +98,20 @@
it would be lines 98..107. - Maps the changed lines to their file name
- Runs
rubocop
on those changed file names - Filters the rubocop output to only show offenses that match a file/line combo from step 3
fwiw $CHILD_STATUS.exitstatus
seems to work just as well, see https://ruby-doc.org/core-2.2.3/Process/Status.html
Needed to change require "english"
to require "English"
I'm using this to run against staged files only: https://gist.github.com/drmartell/a45a0d277921943aab91c6b593956de4
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Above code goes into .git/hooks/pre-commit as per new structure. You may also have to run this in command line to make this file executable: