Sha256: 7debbbabf5734f9d7292dece3be13d68754013578c50eaeec9159d81d372863c

Contents?: true

Size: 1.36 KB

Versions: 5

Compression:

Stored size: 1.36 KB

Contents

require "pathname"
require "set"
require "thor"
require_relative "owners/cli"
require_relative "owners/config"
require_relative "owners/search"
require_relative "owners/tree"
require_relative "owners/version"

module Owners
  class << self
    # @api public
    attr_writer :file

    # The name of the file used to store ownership
    # subscriptions. Defaults to OWNERS.
    #
    # @api public
    def file
      @file ||= "OWNERS"
    end

    # Accepts a list of file paths and returns an array of
    # owners that have subscribed to the specified files.
    #
    # @api public
    def for(*files)
      Search.new(files).owners
    end

    # Accepts a git ref and an optional base ref and returns
    # an array of owners that have subscribed to the changes.
    #
    # The base ref defaults to "master".
    #
    # @api public
    def for_diff(ref, base = "master")
      files = `git diff --name-only #{base}...#{ref}`.split("\n")

      # TODO: why doesn't this work? It works in the command line...
      # blobs = `git ls-tree -r #{ref} | **/#{file}`.split("\n")
      blobs = `git ls-tree -r #{ref} | egrep "(^|/)#{file}$"`.split("\n")

      configs = blobs.reduce({}) do |hash, line|
        _, _, sha, file = line.split(/\s+/, 4)
        contents = `git show #{sha}`
        hash.update(file => contents)
      end

      Search.new(files, configs).owners
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
owners-0.0.8 lib/owners.rb
owners-0.0.7 lib/owners.rb
owners-0.0.6 lib/owners.rb
owners-0.0.5 lib/owners.rb
owners-0.0.4 lib/owners.rb