BigW Consortium Gitlab

tree.rb 1.49 KB
Newer Older
1
class Tree
2 3
  include Gitlab::MarkdownHelper

4
  attr_accessor :entries, :readme, :contribution_guide
5

6 7 8 9 10
  def initialize(repository, sha, path = '/')
    path = '/' if path.blank?
    git_repo = repository.raw_repository
    @entries = Gitlab::Git::Tree.where(git_repo, sha, path)

11 12 13 14 15 16 17
    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|
18
          previewable?(readme.name)
19 20 21 22 23 24 25 26 27
        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

28 29
      readme_path = path == '/' ? readme_tree.name : File.join(path, readme_tree.name)
      @readme = Gitlab::Git::Blob.find(git_repo, sha, readme_path)
30
    end
31

32
    if contribution_tree = @entries.find(&:contributing?)
33 34 35
      contribution_path = path == '/' ? contribution_tree.name : File.join(path, contribution_tree.name)
      @contribution_guide = Gitlab::Git::Blob.find(git_repo, sha, contribution_path)
    end
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