BigW Consortium Gitlab

tree.rb 1.29 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)

20 21 22 23 24 25 26 27
    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)
28
    end
29

30 31 32 33
    # Prioritize previewable over plain readmes
    readme_tree = previewable_readmes.first || plain_readmes.first

    # Return if we can't preview any of them
34
    if readme_tree.nil?
35
      return @readme = nil
36
    end
Douwe Maan committed
37 38 39

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

Douwe Maan committed
40
    git_repo = repository.raw_repository
Douwe Maan committed
41
    @readme = Gitlab::Git::Blob.find(git_repo, sha, readme_path)
Jacob Vosmaer committed
42 43
    @readme.load_all_data!(git_repo)
    @readme
44
  end
gitlabhq committed
45

46 47
  def trees
    @entries.select(&:dir?)
gitlabhq committed
48
  end
49

50 51 52
  def blobs
    @entries.select(&:file?)
  end
53

54 55
  def submodules
    @entries.select(&:submodule?)
56
  end
57 58 59 60

  def sorted_entries
    trees + blobs + submodules
  end
61
end