Class: Tree

Inherits:
Object
  • Object
show all
Includes:
Gitlab::MarkupHelper, Gitlab::Utils::StrongMemoize
Defined in:
app/models/tree.rb

Constant Summary

Constants included from Gitlab::MarkupHelper

Gitlab::MarkupHelper::ASCIIDOC_EXTENSIONS, Gitlab::MarkupHelper::EXTENSIONS, Gitlab::MarkupHelper::MARKDOWN_EXTENSIONS, Gitlab::MarkupHelper::OTHER_EXTENSIONS, Gitlab::MarkupHelper::PLAIN_FILENAMES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods included from Gitlab::MarkupHelper

#asciidoc?, #gitlab_markdown?, #markup?, #plain?, #previewable?

Constructor Details

#initialize(repository, sha, path = '/', recursive: false) ⇒ Tree

Returns a new instance of Tree.


9
10
11
12
13
14
15
16
17
18
# File 'app/models/tree.rb', line 9

def initialize(repository, sha, path = '/', recursive: false)
  path = '/' if path.blank?

  @repository = repository
  @sha = sha
  @path = path

  git_repo = @repository.raw_repository
  @entries = Gitlab::Git::Tree.where(git_repo, @sha, @path, recursive)
end

Instance Attribute Details

#entriesObject

Returns the value of attribute entries


7
8
9
# File 'app/models/tree.rb', line 7

def entries
  @entries
end

#pathObject

Returns the value of attribute path


7
8
9
# File 'app/models/tree.rb', line 7

def path
  @path
end

#repositoryObject

Returns the value of attribute repository


7
8
9
# File 'app/models/tree.rb', line 7

def repository
  @repository
end

#shaObject

Returns the value of attribute sha


7
8
9
# File 'app/models/tree.rb', line 7

def sha
  @sha
end

Instance Method Details

#blobsObject


56
57
58
# File 'app/models/tree.rb', line 56

def blobs
  @entries.select(&:file?)
end

#readmeObject


46
47
48
49
50
# File 'app/models/tree.rb', line 46

def readme
  strong_memoize(:readme) do
    repository.blob_at(sha, readme_path) if readme_path
  end
end

#readme_pathObject


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/tree.rb', line 20

def readme_path
  strong_memoize(:readme_path) do
    available_readmes = blobs.select do |blob|
      Gitlab::FileDetector.type_of(blob.name) == :readme
    end

    previewable_readmes = available_readmes.select do |blob|
      previewable?(blob.name)
    end

    plain_readmes = available_readmes.select do |blob|
      plain?(blob.name)
    end

    # Prioritize previewable over plain readmes
    entry = previewable_readmes.first || plain_readmes.first
    next nil unless entry

    if path == '/'
      entry.name
    else
      File.join(path, entry.name)
    end
  end
end

#sorted_entriesObject


64
65
66
# File 'app/models/tree.rb', line 64

def sorted_entries
  trees + blobs + submodules
end

#submodulesObject


60
61
62
# File 'app/models/tree.rb', line 60

def submodules
  @entries.select(&:submodule?)
end

#treesObject


52
53
54
# File 'app/models/tree.rb', line 52

def trees
  @entries.select(&:dir?)
end