1# frozen_string_literal: true 2 3class Tree 4 include Gitlab::MarkupHelper 5 include Gitlab::Utils::StrongMemoize 6 7 attr_accessor :repository, :sha, :path, :entries, :cursor 8 9 def initialize(repository, sha, path = '/', recursive: false, pagination_params: nil) 10 path = '/' if path.blank? 11 12 @repository = repository 13 @sha = sha 14 @path = path 15 16 git_repo = @repository.raw_repository 17 @entries, @cursor = Gitlab::Git::Tree.where(git_repo, @sha, @path, recursive, pagination_params) 18 end 19 20 def readme_path 21 strong_memoize(:readme_path) do 22 available_readmes = blobs.select do |blob| 23 Gitlab::FileDetector.type_of(blob.name) == :readme 24 end 25 26 previewable_readmes = available_readmes.select do |blob| 27 previewable?(blob.name) 28 end 29 30 plain_readmes = available_readmes.select do |blob| 31 plain?(blob.name) 32 end 33 34 # Prioritize previewable over plain readmes 35 entry = previewable_readmes.first || plain_readmes.first 36 next nil unless entry 37 38 if path == '/' 39 entry.name 40 else 41 File.join(path, entry.name) 42 end 43 end 44 end 45 46 def readme 47 strong_memoize(:readme) do 48 repository.blob_at(sha, readme_path) if readme_path 49 end 50 end 51 52 def trees 53 @entries.select(&:dir?) 54 end 55 56 def blobs 57 @entries.select(&:file?) 58 end 59 60 def submodules 61 @entries.select(&:submodule?) 62 end 63 64 def sorted_entries 65 trees + blobs + submodules 66 end 67end 68