gitlab-org--gitlab-foss/lib/gitlab/git/tree.rb

53 lines
964 B
Ruby
Raw Normal View History

module Gitlab
module Git
class Tree
2013-04-02 15:37:20 -04:00
attr_accessor :repository, :sha, :path, :ref, :raw_tree, :id
def initialize(repository, sha, ref = nil, path = nil)
2013-04-02 15:37:20 -04:00
@repository, @sha, @ref, @path = repository, sha, ref, path
@path = nil if @path.blank?
# Load tree from repository
2013-04-02 15:37:20 -04:00
@commit = @repository.commit(@sha)
@raw_tree = @repository.tree(@commit, @path)
end
def exists?
raw_tree
end
def empty?
data.blank?
end
2013-04-02 15:37:20 -04:00
def trees
entries.select { |t| t.is_a?(Grit::Tree) }
end
def blobs
entries.select { |t| t.is_a?(Grit::Blob) }
end
def is_blob?
2013-04-02 15:37:20 -04:00
raw_tree.is_a?(Grit::Blob)
end
def up_dir?
path.present?
end
def readme
@readme ||= blobs.find { |c| c.name =~ /^readme/i }
2013-04-02 15:37:20 -04:00
end
protected
def entries
raw_tree.contents
end
end
end
end