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
available_readmes = @entries.select(&:readme?) def readme
return @readme if defined?(@readme)
if available_readmes.count > 0 available_readmes = @blobs.select(&:readme?)
# 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 if available_readmes.count == 0
# don't support any of them return @readme = nil
readme_tree = supported_readmes.first || available_readmes.first
else
readme_tree = available_readmes.first
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_path = path == '/' ? readme_tree.name : File.join(path, readme_tree.name)
@readme = Gitlab::Git::Blob.find(git_repo, sha, readme_path) @readme = Gitlab::Git::Blob.find(git_repo, sha, readme_path)
end end
end
def trees def trees
@entries.select(&:dir?) @entries.select(&:dir?)
......
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