2011-11-20 15:32:12 -05:00
|
|
|
class Tree
|
2014-08-11 05:59:36 -04:00
|
|
|
include Gitlab::MarkdownHelper
|
|
|
|
|
2014-02-22 10:37:10 -05:00
|
|
|
attr_accessor :entries, :readme, :contribution_guide
|
2011-11-20 15:32:12 -05:00
|
|
|
|
2013-10-01 10:00:28 -04:00
|
|
|
def initialize(repository, sha, path = '/')
|
|
|
|
path = '/' if path.blank?
|
|
|
|
git_repo = repository.raw_repository
|
|
|
|
@entries = Gitlab::Git::Tree.where(git_repo, sha, path)
|
|
|
|
|
2014-08-11 02:50:56 -04:00
|
|
|
available_readmes = @entries.select(&:readme?)
|
|
|
|
|
|
|
|
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|
|
2014-08-11 05:59:36 -04:00
|
|
|
gitlab_markdown?(readme.name) || markup?(readme.name)
|
2014-08-11 02:50:56 -04:00
|
|
|
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
|
|
|
|
|
2013-10-13 09:03:16 -04:00
|
|
|
readme_path = path == '/' ? readme_tree.name : File.join(path, readme_tree.name)
|
|
|
|
@readme = Gitlab::Git::Blob.find(git_repo, sha, readme_path)
|
2013-10-01 10:00:28 -04:00
|
|
|
end
|
2014-02-22 10:37:10 -05:00
|
|
|
|
2014-02-27 08:52:14 -05:00
|
|
|
if contribution_tree = @entries.find(&:contributing?)
|
2014-02-22 10:37:10 -05:00
|
|
|
contribution_path = path == '/' ? contribution_tree.name : File.join(path, contribution_tree.name)
|
|
|
|
@contribution_guide = Gitlab::Git::Blob.find(git_repo, sha, contribution_path)
|
|
|
|
end
|
2011-11-20 15:32:12 -05:00
|
|
|
end
|
2011-11-22 07:50:47 -05:00
|
|
|
|
2013-10-01 10:00:28 -04:00
|
|
|
def trees
|
|
|
|
@entries.select(&:dir?)
|
2011-11-22 07:50:47 -05:00
|
|
|
end
|
2013-03-31 16:48:12 -04:00
|
|
|
|
2013-10-01 10:00:28 -04:00
|
|
|
def blobs
|
|
|
|
@entries.select(&:file?)
|
|
|
|
end
|
2013-03-31 16:48:12 -04:00
|
|
|
|
2013-10-01 10:00:28 -04:00
|
|
|
def submodules
|
|
|
|
@entries.select(&:submodule?)
|
2013-03-31 16:48:12 -04:00
|
|
|
end
|
2014-02-04 07:46:15 -05:00
|
|
|
|
|
|
|
def sorted_entries
|
|
|
|
trees + blobs + submodules
|
|
|
|
end
|
2011-11-20 15:32:12 -05:00
|
|
|
end
|