2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
class Tree
|
2015-09-11 14:41:01 +05:30
|
|
|
include Gitlab::MarkupHelper
|
2018-12-13 13:39:08 +05:30
|
|
|
include Gitlab::Utils::StrongMemoize
|
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
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
def readme_path
|
|
|
|
strong_memoize(:readme_path) do
|
|
|
|
available_readmes = blobs.select do |blob|
|
|
|
|
Gitlab::FileDetector.type_of(blob.name) == :readme
|
|
|
|
end
|
|
|
|
|
|
|
|
previewable_readmes = available_readmes.select do |blob|
|
|
|
|
previewable?(blob.name)
|
|
|
|
end
|
|
|
|
|
|
|
|
plain_readmes = available_readmes.select do |blob|
|
|
|
|
plain?(blob.name)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Prioritize previewable over plain readmes
|
|
|
|
entry = previewable_readmes.first || plain_readmes.first
|
|
|
|
next nil unless entry
|
|
|
|
|
|
|
|
if path == '/'
|
|
|
|
entry.name
|
|
|
|
else
|
|
|
|
File.join(path, entry.name)
|
|
|
|
end
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|
2018-12-13 13:39:08 +05:30
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
def readme
|
|
|
|
strong_memoize(:readme) do
|
|
|
|
repository.blob_at(sha, readme_path) if readme_path
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
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
|