BigW Consortium Gitlab

tree.rb 1.14 KB
Newer Older
1
class Tree
2
  include Gitlab::MarkupHelper
3

Douwe Maan committed
4
  attr_accessor :repository, :sha, :path, :entries
5

6 7
  def initialize(repository, sha, path = '/')
    path = '/' if path.blank?
8

Douwe Maan committed
9 10 11 12 13 14
    @repository = repository
    @sha = sha
    @path = path

    git_repo = @repository.raw_repository
    @entries = Gitlab::Git::Tree.where(git_repo, @sha, @path)
Douwe Maan committed
15 16 17 18 19
  end

  def readme
    return @readme if defined?(@readme)

Douwe Maan committed
20
    available_readmes = blobs.select(&:readme?)
21

Douwe Maan committed
22
    if available_readmes.count == 0
23
      return @readme = nil
24
    end
Douwe Maan committed
25 26 27 28 29 30 31 32 33

    # 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)

Douwe Maan committed
34
    git_repo = repository.raw_repository
Douwe Maan committed
35
    @readme = Gitlab::Git::Blob.find(git_repo, sha, readme_path)
36
  end
gitlabhq committed
37

38 39
  def trees
    @entries.select(&:dir?)
gitlabhq committed
40
  end
41

42 43 44
  def blobs
    @entries.select(&:file?)
  end
45

46 47
  def submodules
    @entries.select(&:submodule?)
48
  end
49 50 51 52

  def sorted_entries
    trees + blobs + submodules
  end
53
end