2017-09-10 17:25:29 +05:30
|
|
|
# Gitaly note: JV: needs 1 RPC, migration is in progress.
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
module Gitlab
|
|
|
|
module Git
|
|
|
|
class Tree
|
2017-09-10 17:25:29 +05:30
|
|
|
include Gitlab::EncodingHelper
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
attr_accessor :id, :root_id, :name, :path, :flat_path, :type,
|
2017-08-17 22:00:37 +05:30
|
|
|
:mode, :commit_id, :submodule_url
|
|
|
|
|
|
|
|
class << self
|
|
|
|
# Get list of tree objects
|
|
|
|
# for repository based on commit sha and path
|
|
|
|
# Uses rugged for raw objects
|
2017-09-10 17:25:29 +05:30
|
|
|
#
|
|
|
|
# Gitaly migration: https://gitlab.com/gitlab-org/gitaly/issues/320
|
2018-03-27 19:54:05 +05:30
|
|
|
def where(repository, sha, path = nil, recursive = false)
|
2017-08-17 22:00:37 +05:30
|
|
|
path = nil if path == '' || path == '/'
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
Gitlab::GitalyClient.migrate(:tree_entries) do |is_enabled|
|
|
|
|
if is_enabled
|
2018-03-27 19:54:05 +05:30
|
|
|
repository.gitaly_commit_client.tree_entries(repository, sha, path, recursive)
|
2017-09-10 17:25:29 +05:30
|
|
|
else
|
2018-03-27 19:54:05 +05:30
|
|
|
tree_entries_from_rugged(repository, sha, path, recursive)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
private
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
# Recursive search of tree id for path
|
|
|
|
#
|
|
|
|
# Ex.
|
|
|
|
# blog/ # oid: 1a
|
|
|
|
# app/ # oid: 2a
|
|
|
|
# models/ # oid: 3a
|
|
|
|
# views/ # oid: 4a
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# Tree.find_id_by_path(repo, '1a', 'app/models') # => '3a'
|
|
|
|
#
|
|
|
|
def find_id_by_path(repository, root_id, path)
|
|
|
|
root_tree = repository.lookup(root_id)
|
|
|
|
path_arr = path.split('/')
|
|
|
|
|
|
|
|
entry = root_tree.find do |entry|
|
|
|
|
entry[:name] == path_arr[0] && entry[:type] == :tree
|
|
|
|
end
|
|
|
|
|
|
|
|
return nil unless entry
|
|
|
|
|
|
|
|
if path_arr.size > 1
|
|
|
|
path_arr.shift
|
|
|
|
find_id_by_path(repository, entry[:oid], path_arr.join('/'))
|
|
|
|
else
|
|
|
|
entry[:oid]
|
|
|
|
end
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
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
|
|
|
|
|
|
|
|
def get_tree_entries_from_rugged(repository, sha, path)
|
2017-09-10 17:25:29 +05:30
|
|
|
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|
|
|
|
|
new(
|
|
|
|
id: entry[:oid],
|
|
|
|
root_id: root_tree.oid,
|
|
|
|
name: entry[:name],
|
|
|
|
type: entry[:type],
|
|
|
|
mode: entry[:filemode].to_s(8),
|
|
|
|
path: path ? File.join(path, entry[:name]) : entry[:name],
|
|
|
|
commit_id: sha
|
|
|
|
)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
rescue Rugged::ReferenceError
|
|
|
|
[]
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(options)
|
2018-03-17 18:26:18 +05:30
|
|
|
%w(id root_id name path flat_path type mode commit_id).each do |key|
|
|
|
|
self.send("#{key}=", options[key.to_sym]) # rubocop:disable GitlabSecurity/PublicSend
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def name
|
|
|
|
encode! @name
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def path
|
|
|
|
encode! @path
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def flat_path
|
|
|
|
encode! @flat_path
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def dir?
|
|
|
|
type == :tree
|
|
|
|
end
|
|
|
|
|
|
|
|
def file?
|
|
|
|
type == :blob
|
|
|
|
end
|
|
|
|
|
|
|
|
def submodule?
|
|
|
|
type == :commit
|
|
|
|
end
|
|
|
|
|
|
|
|
def readme?
|
|
|
|
name =~ /^readme/i
|
|
|
|
end
|
|
|
|
|
|
|
|
def contributing?
|
|
|
|
name =~ /^contributing/i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|