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

Douwe Maan's avatar
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's avatar
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's avatar
Douwe Maan committed
15 16 17 18 19
  end

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

20 21 22 23 24
    # Take the first previewable readme, or return nil if none is available or
    # we can't preview any of them
    readme_tree = blobs.find do |blob|
      blob.readme? && (previewable?(blob.name) || plain?(blob.name))
    end
25

26
    if readme_tree.nil?
27
      return @readme = nil
28
    end
Douwe Maan's avatar
Douwe Maan committed
29 30 31

    readme_path = path == '/' ? readme_tree.name : File.join(path, readme_tree.name)

Douwe Maan's avatar
Douwe Maan committed
32
    git_repo = repository.raw_repository
Douwe Maan's avatar
Douwe Maan committed
33
    @readme = Gitlab::Git::Blob.find(git_repo, sha, readme_path)
34
  end
gitlabhq's avatar
gitlabhq committed
35

36 37
  def trees
    @entries.select(&:dir?)
gitlabhq's avatar
gitlabhq committed
38
  end
39

40 41 42
  def blobs
    @entries.select(&:file?)
  end
43

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

  def sorted_entries
    trees + blobs + submodules
  end
51
end