Sha256: 5455d92b0593de04b6a763463b4d1ad8f13d426548a13b9134fde44f419b42e2

Contents?: true

Size: 945 Bytes

Versions: 5

Compression:

Stored size: 945 Bytes

Contents

module Dandelion
  class Tree
    attr_reader :commit

    def initialize(repo, commit)
      @repo = repo
      @commit = commit
    end

    def data(path)
      object = @commit.tree
      info = {}

      path.split('/').each do |name|
        info = object[name]
        return nil unless info
        return nil unless info[:type]

        object = @repo.lookup(info[:oid])
        return nil unless object
      end

      content(info, object)
    end

  private

    def content(info, object)
      # https://github.com/libgit2/libgit2/blob/development/include/git2/types.h
      if info[:filemode] == 0120000
        symlink_content(object)
      else
        blob_content(object)
      end
    end

    def blob_content(object)
      object.read_raw.data
    end

    def symlink_content(object)
      path = object.read_raw.data

      result = data(path)
      result ||= IO.read(path) # external link
      result
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dandelion-0.4.6 lib/dandelion/tree.rb
dandelion-0.4.5 lib/dandelion/tree.rb
dandelion-0.4.4 lib/dandelion/tree.rb
dandelion-0.4.3 lib/dandelion/tree.rb
dandelion-0.4.2 lib/dandelion/tree.rb