Class: Tree

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::MarkupHelper

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

Constructor Details

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

Returns a new instance of Tree


6
7
8
9
10
11
12
13
14
15
# File 'app/models/tree.rb', line 6

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

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

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

Instance Attribute Details

#entriesObject

Returns the value of attribute entries


4
5
6
# File 'app/models/tree.rb', line 4

def entries
  @entries
end

#pathObject

Returns the value of attribute path


4
5
6
# File 'app/models/tree.rb', line 4

def path
  @path
end

#repositoryObject

Returns the value of attribute repository


4
5
6
# File 'app/models/tree.rb', line 4

def repository
  @repository
end

#shaObject

Returns the value of attribute sha


4
5
6
# File 'app/models/tree.rb', line 4

def sha
  @sha
end

Instance Method Details

#blobsObject


50
51
52
# File 'app/models/tree.rb', line 50

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

#readmeObject


17
18
19
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 17

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

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

  # Prioritize previewable over plain readmes
  readme_tree = previewable_readmes.first || plain_readmes.first

  # Return if we can't preview any of them
  if readme_tree.nil?
    return @readme = nil
  end

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

  git_repo = repository.raw_repository
  @readme = Gitlab::Git::Blob.find(git_repo, sha, readme_path)
  @readme.load_all_data!(git_repo)
  @readme
end

#sorted_entriesObject


58
59
60
# File 'app/models/tree.rb', line 58

def sorted_entries
  trees + blobs + submodules
end

#submodulesObject


54
55
56
# File 'app/models/tree.rb', line 54

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

#treesObject


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

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