Sha256: 090e910c5a5a633ba66d3aa5ed0f3a6dbadb4017ae45b7bb0c3cc530c9bd0b48

Contents?: true

Size: 1.2 KB

Versions: 3

Compression:

Stored size: 1.2 KB

Contents

# Git-based project
#
# analyze a given git repository for license information
module Licensee
  class GitProject < Licensee::Project
    attr_reader :repository, :revision

    class InvalidRepository < ArgumentError; end

    def initialize(repo, revision: nil, **args)
      @repository = if repo.is_a? Rugged::Repository
        repo
      else
        Rugged::Repository.new(repo)
      end

      @revision = revision
      super(**args)
    rescue Rugged::RepositoryError
      raise InvalidRepository
    end

    private

    def commit
      @commit ||= if revision
        repository.lookup(revision)
      else
        repository.last_commit
      end
    end

    MAX_LICENSE_SIZE = 64 * 1024

    def load_blob_data(oid)
      data, = Rugged::Blob.to_buffer(repository, oid, MAX_LICENSE_SIZE)
      data
    end

    def find_file
      files = commit.tree.map do |entry|
        next unless entry[:type] == :blob
        if (score = yield entry[:name]) > 0
          { name: entry[:name], oid: entry[:oid], score: score }
        end
      end.compact

      return if files.empty?
      files.sort! { |a, b| b[:score] <=> a[:score] }

      f = files.first
      [load_blob_data(f[:oid]), f[:name]]
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
licensee-8.0.0 lib/licensee/projects/git_project.rb
licensee-7.0.1 lib/licensee/projects/git_project.rb
licensee-7.0.0 lib/licensee/projects/git_project.rb