Skip to content

Commit bbbbb3d

Browse files
David Rodríguezsds
David Rodríguez
authored andcommitted
Add an included_files method to base hook
In the minitest pre-push hook, we don't want to use `applicable_files` but run the whole suite instead. However, we do want to allow expliciting an `include` pattern in the configuration. Extracting an `included_files` method so it can be reused in the hook seemed the easiest solution. This commit includes some minor refactorings.
1 parent 3206ea4 commit bbbbb3d

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

lib/overcommit/hook/base.rb

+12-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def to_s
1717
class Base # rubocop:disable Metrics/ClassLength
1818
extend Forwardable
1919

20-
def_delegators :@context, :modified_files
20+
def_delegators :@context, :all_files, :modified_files
2121
attr_reader :config
2222

2323
# @param config [Overcommit::Configuration]
@@ -155,11 +155,21 @@ def flags
155155
# Gets a list of staged files that apply to this hook based on its
156156
# configured `include` and `exclude` lists.
157157
def applicable_files
158-
@applicable_files ||= modified_files.select { |file| applicable_file?(file) }.sort
158+
@applicable_files ||= select_applicable(modified_files)
159+
end
160+
161+
# Gets a list of all files that apply to this hook based on its
162+
# configured `include` and `exclude` lists.
163+
def included_files
164+
@included_files ||= select_applicable(all_files)
159165
end
160166

161167
private
162168

169+
def select_applicable(list)
170+
list.select { |file| applicable_file?(file) }.sort
171+
end
172+
163173
def applicable_file?(file)
164174
includes = Array(@config['include']).flatten.map do |glob|
165175
Overcommit::Utils.convert_glob_to_absolute(glob)

lib/overcommit/hook_context/base.rb

+7
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ def modified_files
7979
[]
8080
end
8181

82+
# Returns the full list of files tracked by git
83+
#
84+
# @return [Array<String>]
85+
def all_files
86+
Overcommit::GitRepo.all_files
87+
end
88+
8289
# Returns the contents of the entire standard input stream that were passed
8390
# to the hook.
8491
#

lib/overcommit/hook_context/run_all.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module Overcommit::HookContext
77
# which is useful for automated CI scripts.
88
class RunAll < Base
99
def modified_files
10-
@modified_files ||= Overcommit::GitRepo.all_files
10+
@modified_files ||= all_files
1111
end
1212

1313
# Returns all lines in the file since in this context the entire repo is

0 commit comments

Comments
 (0)