2018-07-25 09:30:33 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2011-11-20 20:32:12 +00:00
|
|
|
class Tree
|
2018-10-12 13:42:35 +00:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2014-08-11 09:59:36 +00:00
|
|
|
|
2021-07-29 09:08:46 +00:00
|
|
|
attr_accessor :repository, :sha, :path, :entries, :cursor
|
2011-11-20 20:32:12 +00:00
|
|
|
|
2022-08-30 18:09:50 +00:00
|
|
|
def initialize(repository, sha, path = '/', recursive: false, skip_flat_paths: true, pagination_params: nil)
|
2013-10-01 14:00:28 +00:00
|
|
|
path = '/' if path.blank?
|
2015-05-12 23:40:11 +00:00
|
|
|
|
2015-03-17 09:33:25 +00:00
|
|
|
@repository = repository
|
|
|
|
@sha = sha
|
|
|
|
@path = path
|
|
|
|
|
|
|
|
git_repo = @repository.raw_repository
|
2022-08-30 18:09:50 +00:00
|
|
|
@entries, @cursor = Gitlab::Git::Tree.where(git_repo, @sha, @path, recursive, skip_flat_paths, pagination_params)
|
2015-03-17 09:29:06 +00:00
|
|
|
end
|
|
|
|
|
2018-10-12 13:42:35 +00:00
|
|
|
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|
|
2022-10-07 00:08:16 +00:00
|
|
|
Gitlab::MarkupHelper.previewable?(blob.name)
|
2018-10-12 13:42:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
plain_readmes = available_readmes.select do |blob|
|
2022-10-07 00:08:16 +00:00
|
|
|
Gitlab::MarkupHelper.plain?(blob.name)
|
2018-10-12 13:42:35 +00:00
|
|
|
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-05 13:00:10 +00:00
|
|
|
end
|
2018-10-12 13:42:35 +00:00
|
|
|
end
|
2013-10-01 14:00:28 +00:00
|
|
|
|
2018-10-12 13:42:35 +00:00
|
|
|
def readme
|
|
|
|
strong_memoize(:readme) do
|
|
|
|
repository.blob_at(sha, readme_path) if readme_path
|
2013-10-01 14:00:28 +00:00
|
|
|
end
|
2011-11-20 20:32:12 +00:00
|
|
|
end
|
2011-11-22 12:50:47 +00:00
|
|
|
|
2013-10-01 14:00:28 +00:00
|
|
|
def trees
|
|
|
|
@entries.select(&:dir?)
|
2011-11-22 12:50:47 +00:00
|
|
|
end
|
2013-03-31 20:48:12 +00:00
|
|
|
|
2013-10-01 14:00:28 +00:00
|
|
|
def blobs
|
|
|
|
@entries.select(&:file?)
|
|
|
|
end
|
2013-03-31 20:48:12 +00:00
|
|
|
|
2013-10-01 14:00:28 +00:00
|
|
|
def submodules
|
|
|
|
@entries.select(&:submodule?)
|
2013-03-31 20:48:12 +00:00
|
|
|
end
|
2014-02-04 12:46:15 +00:00
|
|
|
|
|
|
|
def sorted_entries
|
|
|
|
trees + blobs + submodules
|
|
|
|
end
|
2011-11-20 20:32:12 +00:00
|
|
|
end
|