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

67 lines
1.5 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
class Tree
2018-12-13 13:39:08 +05:30
include Gitlab::Utils::StrongMemoize
2014-09-02 18:07:02 +05:30
2021-10-27 15:23:28 +05:30
attr_accessor :repository, :sha, :path, :entries, :cursor
2014-09-02 18:07:02 +05:30
2022-09-01 20:07:04 +05:30
def initialize(repository, sha, path = '/', recursive: false, skip_flat_paths: true, pagination_params: nil)
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
2022-09-01 20:07:04 +05:30
@entries, @cursor = Gitlab::Git::Tree.where(git_repo, @sha, @path, recursive, skip_flat_paths, pagination_params)
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|
2022-11-25 23:54:43 +05:30
Gitlab::MarkupHelper.previewable?(blob.name)
2018-12-13 13:39:08 +05:30
end
plain_readmes = available_readmes.select do |blob|
2022-11-25 23:54:43 +05:30
Gitlab::MarkupHelper.plain?(blob.name)
2018-12-13 13:39:08 +05:30
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
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