Skip to content

Commit

Permalink
Refactor pre-commit hook to run rubocop only on changed files
Browse files Browse the repository at this point in the history
  • Loading branch information
faqndo97 committed Jan 2, 2024
1 parent 630fd02 commit a439f2f
Showing 1 changed file with 39 additions and 0 deletions.
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

0 comments on commit a439f2f

Please sign in to comment.