debian-mirror-gitlab/app/models/tree.rb

62 lines
1.3 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
class Tree
2015-09-11 14:41:01 +05:30
include Gitlab::MarkupHelper
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
attr_accessor :repository, :sha, :path, :entries
2014-09-02 18:07:02 +05:30
def initialize(repository, sha, path = '/')
path = '/' if path.blank?
2015-09-11 14:41:01 +05:30
2015-04-26 12:48:37 +05:30
@repository = repository
@sha = sha
@path = path
git_repo = @repository.raw_repository
@entries = Gitlab::Git::Tree.where(git_repo, @sha, @path)
end
def readme
return @readme if defined?(@readme)
2014-09-02 18:07:02 +05:30
2016-04-02 18:10:28 +05:30
available_readmes = blobs.select(&:readme?)
previewable_readmes = available_readmes.select do |blob|
previewable?(blob.name)
end
plain_readmes = available_readmes.select do |blob|
plain?(blob.name)
end
2015-04-26 12:48:37 +05:30
2016-04-02 18:10:28 +05:30
# Prioritize previewable over plain readmes
readme_tree = previewable_readmes.first || plain_readmes.first
# Return if we can't preview any of them
if readme_tree.nil?
2015-09-11 14:41:01 +05:30
return @readme = nil
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
readme_path = path == '/' ? readme_tree.name : File.join(path, readme_tree.name)
git_repo = repository.raw_repository
@readme = Gitlab::Git::Blob.find(git_repo, sha, readme_path)
2016-04-02 18:10:28 +05:30
@readme.load_all_data!(git_repo)
@readme
2014-09-02 18:07:02 +05:30
end
def trees
@entries.select(&:dir?)
end
def blobs
@entries.select(&:file?)
end
def submodules
@entries.select(&:submodule?)
end
def sorted_entries
trees + blobs + submodules
end
end