Module: Gitlab::Git::RuggedImpl::Tree::ClassMethods

Extended by:
Utils::Override
Includes:
UseRugged
Defined in:
lib/gitlab/git/rugged_impl/tree.rb

Instance Method Summary collapse

Methods included from Utils::Override

extended, extensions, included, method_added, override, prepended, queue_verification, verify!

Methods included from UseRugged

#execute_rugged_call, #rugged_enabled_through_feature_flag?, #rugged_feature_keys, #running_puma_with_multiple_threads?, #use_rugged?

Instance Method Details

#get_tree_entries_from_rugged(repository, sha, path) ⇒ Object


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/gitlab/git/rugged_impl/tree.rb', line 77

def get_tree_entries_from_rugged(repository, sha, path)
  commit = repository.lookup(sha)
  root_tree = commit.tree

  tree = if path
           id = find_id_by_path(repository, root_tree.oid, path)
           if id
             repository.lookup(id)
           else
             []
           end
         else
           root_tree
         end

  tree.map do |entry|
    current_path = path ? File.join(path, entry[:name]) : entry[:name]

    new(
      id: entry[:oid],
      root_id: root_tree.oid,
      name: entry[:name],
      type: entry[:type],
      mode: entry[:filemode].to_s(8),
      path: current_path,
      commit_id: sha
    )
  end
rescue Rugged::ReferenceError
  []
end

#rugged_flatten_tree(repository, sha, tree, root_path) ⇒ Object

Returns the relative path of the first subdir that doesn't have only one directory descendant


67
68
69
70
71
72
73
74
75
# File 'lib/gitlab/git/rugged_impl/tree.rb', line 67

def rugged_flatten_tree(repository, sha, tree, root_path)
  subtree = tree_entries_from_rugged(repository, sha, tree.path, false)

  if subtree.count == 1 && subtree.first.dir?
    File.join(tree.name, rugged_flatten_tree(repository, sha, subtree.first, root_path))
  else
    tree.name
  end
end

#rugged_populate_flat_path(repository, sha, path, entries) ⇒ Object


51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gitlab/git/rugged_impl/tree.rb', line 51

def rugged_populate_flat_path(repository, sha, path, entries)
  entries.each do |entry|
    entry.flat_path = entry.path

    next unless entry.dir?

    entry.flat_path =
      if path
        File.join(path, rugged_flatten_tree(repository, sha, entry, path))
      else
        rugged_flatten_tree(repository, sha, entry, path)
      end
  end
end

#tree_entries(repository, sha, path, recursive) ⇒ Object


17
18
19
20
21
22
23
# File 'lib/gitlab/git/rugged_impl/tree.rb', line 17

def tree_entries(repository, sha, path, recursive)
  if use_rugged?(repository, :rugged_tree_entries)
    execute_rugged_call(:tree_entries_with_flat_path_from_rugged, repository, sha, path, recursive)
  else
    super
  end
end

#tree_entries_from_rugged(repository, sha, path, recursive) ⇒ Object


36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/gitlab/git/rugged_impl/tree.rb', line 36

def tree_entries_from_rugged(repository, sha, path, recursive)
  current_path_entries = get_tree_entries_from_rugged(repository, sha, path)
  ordered_entries = []

  current_path_entries.each do |entry|
    ordered_entries << entry

    if recursive && entry.dir?
      ordered_entries.concat(tree_entries_from_rugged(repository, sha, entry.path, true))
    end
  end

  ordered_entries
end

#tree_entries_with_flat_path_from_rugged(repository, sha, path, recursive) ⇒ Object


25
26
27
28
29
30
31
32
33
34
# File 'lib/gitlab/git/rugged_impl/tree.rb', line 25

def tree_entries_with_flat_path_from_rugged(repository, sha, path, recursive)
  tree_entries_from_rugged(repository, sha, path, recursive).tap do |entries|
    # This was an optimization to reduce N+1 queries for Gitaly
    # (https://gitlab.com/gitlab-org/gitaly/issues/530).  It
    # used to be done lazily in the view via
    # TreeHelper#flatten_tree, so it's possible there's a
    # performance impact by loading this eagerly.
    rugged_populate_flat_path(repository, sha, path, entries)
  end
end