2018-11-09 13:39:43 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-01-04 13:43:06 -05:00
|
|
|
module Gitlab
|
|
|
|
module Git
|
|
|
|
class Tree
|
2017-06-01 17:21:14 -04:00
|
|
|
include Gitlab::EncodingHelper
|
2018-10-30 09:37:40 -04:00
|
|
|
extend Gitlab::Git::WrapsGitalyErrors
|
2017-01-04 13:43:06 -05:00
|
|
|
|
2020-07-22 14:09:27 -04:00
|
|
|
attr_accessor :id, :root_id, :type, :mode, :commit_id, :submodule_url
|
|
|
|
attr_writer :name, :path, :flat_path
|
2017-01-04 13:43:06 -05:00
|
|
|
|
|
|
|
class << self
|
|
|
|
# Get list of tree objects
|
|
|
|
# for repository based on commit sha and path
|
|
|
|
# Uses rugged for raw objects
|
2017-07-12 10:31:36 -04:00
|
|
|
#
|
|
|
|
# Gitaly migration: https://gitlab.com/gitlab-org/gitaly/issues/320
|
2021-07-29 05:08:46 -04:00
|
|
|
def where(repository, sha, path = nil, recursive = false, pagination_params = nil)
|
2017-01-04 13:43:06 -05:00
|
|
|
path = nil if path == '' || path == '/'
|
|
|
|
|
2021-07-29 05:08:46 -04:00
|
|
|
tree_entries(repository, sha, path, recursive, pagination_params)
|
2019-03-07 07:29:51 -05:00
|
|
|
end
|
|
|
|
|
2021-07-29 05:08:46 -04:00
|
|
|
def tree_entries(repository, sha, path, recursive, pagination_params = nil)
|
2018-10-30 09:37:40 -04:00
|
|
|
wrapped_gitaly_errors do
|
2021-07-29 05:08:46 -04:00
|
|
|
repository.gitaly_commit_client.tree_entries(repository, sha, path, recursive, pagination_params)
|
2017-01-04 13:43:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-12 10:31:36 -04:00
|
|
|
private
|
|
|
|
|
2017-01-04 13:43:06 -05:00
|
|
|
# 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
|
|
|
|
|
2019-02-08 07:19:53 -05:00
|
|
|
return unless entry
|
2017-01-04 13:43:06 -05:00
|
|
|
|
|
|
|
if path_arr.size > 1
|
|
|
|
path_arr.shift
|
|
|
|
find_id_by_path(repository, entry[:oid], path_arr.join('/'))
|
|
|
|
else
|
|
|
|
entry[:oid]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(options)
|
2017-09-06 16:47:25 -04:00
|
|
|
%w(id root_id name path flat_path type mode commit_id).each do |key|
|
2017-08-10 12:39:26 -04:00
|
|
|
self.send("#{key}=", options[key.to_sym]) # rubocop:disable GitlabSecurity/PublicSend
|
2017-01-04 13:43:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def name
|
|
|
|
encode! @name
|
|
|
|
end
|
|
|
|
|
2017-07-04 15:10:34 -04:00
|
|
|
def path
|
|
|
|
encode! @path
|
|
|
|
end
|
|
|
|
|
2017-09-06 16:47:25 -04:00
|
|
|
def flat_path
|
|
|
|
encode! @flat_path
|
|
|
|
end
|
|
|
|
|
2017-01-04 13:43:06 -05:00
|
|
|
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
|
2019-03-07 07:29:51 -05:00
|
|
|
|
|
|
|
Gitlab::Git::Tree.singleton_class.prepend Gitlab::Git::RuggedImpl::Tree::ClassMethods
|