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
28
29
30
31
32
33
34
# 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::RefExtractor.ref_type(ref_type)
  git_repo = @repository.raw_repository

  ref = ExtractsRef::RefExtractor.qualify_ref(@sha, ref_type)

  @entries, @cursor = Gitlab::Git::Tree.tree_entries(
    repository: git_repo,
    sha: ref,
    path: @path,
    recursive: recursive,
    skip_flat_paths: skip_flat_paths,
    rescue_not_found: rescue_not_found,
    pagination_params: 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



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

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

#readmeObject



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

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

#readme_pathObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/tree.rb', line 36

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



80
81
82
# File 'app/models/tree.rb', line 80

def sorted_entries
  trees + blobs + submodules
end

#submodulesObject



76
77
78
# File 'app/models/tree.rb', line 76

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

#treesObject



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

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