lib/code_ownership/private.rb in code_ownership-1.32.8 vs lib/code_ownership/private.rb in code_ownership-1.32.9
- old
+ new
@@ -79,9 +79,27 @@
def self.tracked_files
@tracked_files ||= T.let(@tracked_files, T.nilable(T::Array[String]))
@tracked_files ||= Dir.glob(configuration.owned_globs) - Dir.glob(configuration.unowned_globs)
end
+ sig { params(file: String).returns(T::Boolean) }
+ def self.file_tracked?(file)
+ # Another way to accomplish this is
+ # (Dir.glob(configuration.owned_globs) - Dir.glob(configuration.unowned_globs)).include?(file)
+ # However, globbing out can take 5 or more seconds on a large repository, dramatically slowing down
+ # invocations to `bin/codeownership validate --diff`.
+ # Using `File.fnmatch?` is a lot faster!
+ in_owned_globs = configuration.owned_globs.all? do |owned_glob|
+ File.fnmatch?(owned_glob, file, File::FNM_PATHNAME | File::FNM_EXTGLOB)
+ end
+
+ in_unowned_globs = configuration.unowned_globs.all? do |unowned_glob|
+ File.fnmatch?(unowned_glob, file, File::FNM_PATHNAME | File::FNM_EXTGLOB)
+ end
+
+ in_owned_globs && !in_unowned_globs
+ end
+
sig { params(team_name: String, location_of_reference: String).returns(CodeTeams::Team) }
def self.find_team!(team_name, location_of_reference)
found_team = CodeTeams.find(team_name)
if found_team.nil?
raise StandardError, "Could not find team with name: `#{team_name}` in #{location_of_reference}. Make sure the team is one of `#{CodeTeams.all.map(&:name).sort}`"