forked from rails/rails
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor pre-commit hook to run rubocop only on changed files
- Loading branch information
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
railties/lib/rails/generators/rails/app/templates/bin/hooks/pre-commit.tt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,40 @@ | ||
system("bin/rubocop", exception: true) | ||
|
||
module Git | ||
class Repository | ||
class << self | ||
def modified_ruby_files | ||
select_applicable(modified_files) | ||
end | ||
|
||
def modified_files | ||
`git diff --name-only -z --diff-filter=ACMR --ignore-submodules=all --cached` | ||
.split("\0") | ||
.map(&:strip) | ||
.reject(&:empty?) | ||
.map { |relative_file| File.expand_path(relative_file) } | ||
end | ||
|
||
def select_applicable(list) | ||
list.select { |file| applicable_file?(file) } | ||
end | ||
|
||
def applicable_file?(file) | ||
includes = [ "**/*.rb", "**/Gemfile" ].map { |glob| File.join(root, glob) } | ||
|
||
includes.any? { |glob| File.fnmatch?(glob, file) } | ||
end | ||
|
||
def root | ||
@root ||= File.expand_path("../..", __dir__) | ||
end | ||
end | ||
end | ||
end | ||
|
||
begin | ||
puts "== Running Rubocop ==\n" | ||
system("bin/rubocop #{Git::Repository.modified_ruby_files.join(" ")}", exception: true) | ||
rescue | ||
exit 1 | ||
end |