Created
February 9, 2022 22:47
-
-
Save simon-engledew/4105e92ac5c0c4d88ce08d89d7c417e1 to your computer and use it in GitHub Desktop.
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
require "pathspec/gitignorespec" | |
class Codeowners | |
def initialize(codeowners) | |
@captures, @index = codeowners.each_line(chomp: true).map(&:strip).reject { |line| line.empty? || line.start_with?("#") }.map { |line| line.split(/\s+/, 2) }.map do |glob, owners| | |
pattern = ::GitIgnoreSpec.new(glob).instance_variable_get(:@regex) | |
["(#{pattern})", owners.split(/\s+/)] | |
end.reverse.transpose | |
@pattern = Regexp.compile(@captures.join("|")) | |
end | |
def [](path) | |
@pattern.match(path) do |matchdata| | |
idx = matchdata.captures.find_index { |capture| !capture.nil? } | |
@index[idx] | |
end | |
end | |
def for(paths) | |
paths.filter_map { |path| self[path] }.flatten.to_set | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment