Skip to content

Instantly share code, notes, and snippets.

@simon-engledew
Created February 9, 2022 22:47
Show Gist options
  • Save simon-engledew/4105e92ac5c0c4d88ce08d89d7c417e1 to your computer and use it in GitHub Desktop.
Save simon-engledew/4105e92ac5c0c4d88ce08d89d7c417e1 to your computer and use it in GitHub Desktop.
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