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

61 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
2017-08-17 22:00:37 +05:30
def initialize(repository, sha, path = '/', recursive: false)
2014-09-02 18:07:02 +05:30
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
2018-03-27 19:54:05 +05:30
@entries = Gitlab::Git::Tree.where(git_repo, @sha, @path, recursive)
2015-04-26 12:48:37 +05:30
end
def readme
return @readme if defined?(@readme)
2014-09-02 18:07:02 +05:30
2017-08-17 22:00:37 +05:30
available_readmes = blobs.select do |blob|
Gitlab::FileDetector.type_of(blob.name) == :readme
end
2016-04-02 18:10:28 +05:30
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)
2017-09-10 17:25:29 +05:30
@readme = repository.blob_at(sha, readme_path)
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