Commit 31e48408 authored by Douwe Maan's avatar Douwe Maan

Find tree readme lazily.

parent 88f9ed3d
class Tree class Tree
include Gitlab::MarkdownHelper include Gitlab::MarkdownHelper
attr_accessor :entries, :readme attr_accessor :entries
def initialize(repository, sha, path = '/') def initialize(repository, sha, path = '/')
path = '/' if path.blank? path = '/' if path.blank?
git_repo = repository.raw_repository git_repo = repository.raw_repository
@entries = Gitlab::Git::Tree.where(git_repo, sha, path) @entries = Gitlab::Git::Tree.where(git_repo, sha, path)
end
def readme
return @readme if defined?(@readme)
available_readmes = @blobs.select(&:readme?)
available_readmes = @entries.select(&:readme?) if available_readmes.count == 0
return @readme = nil
if available_readmes.count > 0
# If there is more than 1 readme in tree, find readme which is supported
# by markup renderer.
if available_readmes.length > 1
supported_readmes = available_readmes.select do |readme|
previewable?(readme.name)
end
# Take the first supported readme, or the first available readme, if we
# don't support any of them
readme_tree = supported_readmes.first || available_readmes.first
else
readme_tree = available_readmes.first
end
readme_path = path == '/' ? readme_tree.name : File.join(path, readme_tree.name)
@readme = Gitlab::Git::Blob.find(git_repo, sha, readme_path)
end end
# Take the first previewable readme, or the first available readme, if we
# can't preview any of them
readme_tree = available_readmes.find do |readme|
previewable?(readme.name)
end || available_readmes.first
readme_path = path == '/' ? readme_tree.name : File.join(path, readme_tree.name)
@readme = Gitlab::Git::Blob.find(git_repo, sha, readme_path)
end end
def trees def trees
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment