Class: Tree

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository, sha, path = '/', recursive: false, skip_flat_paths: true, pagination_params: nil, ref_type: nil, rescue_not_found: true) ⇒ Tree

Returns a new instance of Tree.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/models/tree.rb', line 8

def initialize(
  repository, sha, path = '/', recursive: false, skip_flat_paths: true, pagination_params: nil,
  ref_type: nil, rescue_not_found: true)
  path = '/' if path.blank?

  @repository = repository
  @sha = sha
  @path = path
  @ref_type = ExtractsRef.ref_type(ref_type)
  git_repo = @repository.raw_repository

  ref = ExtractsRef.qualify_ref(@sha, ref_type)

  @entries, @cursor = Gitlab::Git::Tree.where(git_repo, ref, @path, recursive, skip_flat_paths, rescue_not_found,
    pagination_params)

  @entries.each do |entry|
    entry.ref_type = self.ref_type
  end
end

Instance Attribute Details

#cursorObject

Returns the value of attribute cursor.



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

def cursor
  @cursor
end

#entriesObject

Returns the value of attribute entries.



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

def entries
  @entries
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

#ref_typeObject

Returns the value of attribute ref_type.



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

def ref_type
  @ref_type
end

#repositoryObject

Returns the value of attribute repository.



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

def repository
  @repository
end

#shaObject

Returns the value of attribute sha.



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

def sha
  @sha
end

Instance Method Details

#blobsObject



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

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

#readmeObject



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

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

#readme_pathObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/tree.rb', line 29

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|
      Gitlab::MarkupHelper.previewable?(blob.name)
    end

    plain_readmes = available_readmes.select do |blob|
      Gitlab::MarkupHelper.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



73
74
75
# File 'app/models/tree.rb', line 73

def sorted_entries
  trees + blobs + submodules
end

#submodulesObject



69
70
71
# File 'app/models/tree.rb', line 69

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

#treesObject



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

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