2011-11-20 15:32:12 -05:00
|
|
|
class Tree
|
2015-05-12 19:40:11 -04:00
|
|
|
include Gitlab::MarkupHelper
|
2014-08-11 05:59:36 -04:00
|
|
|
|
2015-03-17 05:33:25 -04:00
|
|
|
attr_accessor :repository, :sha, :path, :entries
|
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?
|
2015-05-12 19:40:11 -04:00
|
|
|
|
2015-03-17 05:33:25 -04:00
|
|
|
@repository = repository
|
|
|
|
@sha = sha
|
|
|
|
@path = path
|
|
|
|
|
|
|
|
git_repo = @repository.raw_repository
|
|
|
|
@entries = Gitlab::Git::Tree.where(git_repo, @sha, @path)
|
2015-03-17 05:29:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def readme
|
|
|
|
return @readme if defined?(@readme)
|
|
|
|
|
2016-01-20 19:03:20 -05:00
|
|
|
available_readmes = blobs.select(&:readme?)
|
|
|
|
|
|
|
|
previewable_readmes = available_readmes.select do |blob|
|
|
|
|
previewable?(blob.name)
|
|
|
|
end
|
|
|
|
|
|
|
|
plain_readmes = available_readmes.select do |blob|
|
|
|
|
plain?(blob.name)
|
2016-01-05 08:00:10 -05:00
|
|
|
end
|
2013-10-01 10:00:28 -04:00
|
|
|
|
2016-01-20 19:03:20 -05:00
|
|
|
# Prioritize previewable over plain readmes
|
|
|
|
readme_tree = previewable_readmes.first || plain_readmes.first
|
|
|
|
|
|
|
|
# Return if we can't preview any of them
|
2016-01-05 08:00:10 -05:00
|
|
|
if readme_tree.nil?
|
2015-05-12 19:40:11 -04:00
|
|
|
return @readme = nil
|
2013-10-01 10:00:28 -04:00
|
|
|
end
|
2015-03-17 05:29:06 -04:00
|
|
|
|
|
|
|
readme_path = path == '/' ? readme_tree.name : File.join(path, readme_tree.name)
|
|
|
|
|
2015-03-17 05:33:25 -04:00
|
|
|
git_repo = repository.raw_repository
|
2015-03-17 05:29:06 -04:00
|
|
|
@readme = Gitlab::Git::Blob.find(git_repo, sha, readme_path)
|
2016-02-01 04:41:52 -05:00
|
|
|
@readme.load_all_data!(git_repo)
|
|
|
|
@readme
|
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
|