debian-mirror-gitlab/lib/gitlab/gitaly_client/repository_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

437 lines
14 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
module Gitlab
module GitalyClient
class RepositoryService
2018-03-17 18:26:18 +05:30
include Gitlab::EncodingHelper
2023-01-13 00:05:48 +05:30
include WithFeatureFlagActors
2018-03-17 18:26:18 +05:30
2019-09-30 21:07:59 +05:30
MAX_MSG_SIZE = 128.kilobytes
2018-03-17 18:26:18 +05:30
2017-09-10 17:25:29 +05:30
def initialize(repository)
@repository = repository
@gitaly_repo = repository.gitaly_repository
@storage = repository.storage
2023-01-13 00:05:48 +05:30
self.repository_actor = repository
2017-09-10 17:25:29 +05:30
end
def exists?
request = Gitaly::RepositoryExistsRequest.new(repository: @gitaly_repo)
2023-01-13 00:05:48 +05:30
response = gitaly_client_call(@storage, :repository_service, :repository_exists, request, timeout: GitalyClient.fast_timeout)
2018-03-17 18:26:18 +05:30
response.exists
2017-09-10 17:25:29 +05:30
end
2022-05-07 20:08:51 +05:30
def optimize_repository
request = Gitaly::OptimizeRepositoryRequest.new(repository: @gitaly_repo)
2023-01-13 00:05:48 +05:30
gitaly_client_call(@storage, :repository_service, :optimize_repository, request, timeout: GitalyClient.long_timeout)
2022-05-07 20:08:51 +05:30
end
def prune_unreachable_objects
request = Gitaly::PruneUnreachableObjectsRequest.new(repository: @gitaly_repo)
2023-01-13 00:05:48 +05:30
gitaly_client_call(@storage, :repository_service, :prune_unreachable_objects, request, timeout: GitalyClient.long_timeout)
2022-05-07 20:08:51 +05:30
end
2017-09-10 17:25:29 +05:30
def repository_size
request = Gitaly::RepositorySizeRequest.new(repository: @gitaly_repo)
2023-01-13 00:05:48 +05:30
response = gitaly_client_call(@storage, :repository_service, :repository_size, request, timeout: GitalyClient.long_timeout)
2018-03-17 18:26:18 +05:30
response.size
end
2019-07-07 11:18:12 +05:30
def get_object_directory_size
request = Gitaly::GetObjectDirectorySizeRequest.new(repository: @gitaly_repo)
2023-01-13 00:05:48 +05:30
response = gitaly_client_call(@storage, :repository_service, :get_object_directory_size, request, timeout: GitalyClient.medium_timeout)
2019-07-07 11:18:12 +05:30
response.size
end
2018-03-17 18:26:18 +05:30
def apply_gitattributes(revision)
request = Gitaly::ApplyGitattributesRequest.new(repository: @gitaly_repo, revision: encode_binary(revision))
2023-01-13 00:05:48 +05:30
gitaly_client_call(@storage, :repository_service, :apply_gitattributes, request, timeout: GitalyClient.fast_timeout)
2018-11-08 19:23:39 +05:30
rescue GRPC::InvalidArgument => ex
raise Gitlab::Git::Repository::InvalidRef, ex
2018-03-17 18:26:18 +05:30
end
2018-10-15 14:42:47 +05:30
def info_attributes
request = Gitaly::GetInfoAttributesRequest.new(repository: @gitaly_repo)
2023-01-13 00:05:48 +05:30
response = gitaly_client_call(@storage, :repository_service, :get_info_attributes, request, timeout: GitalyClient.fast_timeout)
2019-02-15 15:39:39 +05:30
response.each_with_object([]) do |message, attributes|
2018-10-15 14:42:47 +05:30
attributes << message.attributes
2019-02-15 15:39:39 +05:30
end.join
2018-10-15 14:42:47 +05:30
end
2021-09-04 01:27:46 +05:30
# rubocop: disable Metrics/ParameterLists
# The `remote` parameter is going away soonish anyway, at which point the
# Rubocop warning can be enabled again.
2023-01-10 11:22:00 +05:30
def fetch_remote(url, refmap:, ssh_auth:, forced:, no_tags:, timeout:, prune: true, check_tags_changed: false, http_authorization_header: "", resolved_address: "")
2018-03-17 18:26:18 +05:30
request = Gitaly::FetchRemoteRequest.new(
2021-10-27 15:23:28 +05:30
repository: @gitaly_repo,
force: forced,
no_tags: no_tags,
timeout: timeout,
no_prune: !prune,
check_tags_changed: check_tags_changed,
remote_params: Gitaly::Remote.new(
url: url,
mirror_refmaps: Array.wrap(refmap).map(&:to_s),
2023-01-10 11:22:00 +05:30
http_authorization_header: http_authorization_header,
resolved_address: resolved_address
2021-10-27 15:23:28 +05:30
)
2018-03-17 18:26:18 +05:30
)
2019-02-15 15:39:39 +05:30
if ssh_auth&.ssh_mirror_url?
2018-03-17 18:26:18 +05:30
if ssh_auth.ssh_key_auth? && ssh_auth.ssh_private_key.present?
request.ssh_key = ssh_auth.ssh_private_key
end
if ssh_auth.ssh_known_hosts.present?
request.known_hosts = ssh_auth.ssh_known_hosts
end
end
2023-01-13 00:05:48 +05:30
gitaly_client_call(@storage, :repository_service, :fetch_remote, request, timeout: GitalyClient.long_timeout)
2018-03-17 18:26:18 +05:30
end
2021-09-04 01:27:46 +05:30
# rubocop: enable Metrics/ParameterLists
2018-03-17 18:26:18 +05:30
2022-05-07 20:08:51 +05:30
def create_repository(default_branch = nil)
request = Gitaly::CreateRepositoryRequest.new(repository: @gitaly_repo, default_branch: default_branch)
2023-01-13 00:05:48 +05:30
gitaly_client_call(@storage, :repository_service, :create_repository, request, timeout: GitalyClient.fast_timeout)
2018-03-17 18:26:18 +05:30
end
def has_local_branches?
request = Gitaly::HasLocalBranchesRequest.new(repository: @gitaly_repo)
2023-01-13 00:05:48 +05:30
response = gitaly_client_call(@storage, :repository_service, :has_local_branches, request, timeout: GitalyClient.fast_timeout)
2018-03-17 18:26:18 +05:30
response.value
end
def find_merge_base(*revisions)
request = Gitaly::FindMergeBaseRequest.new(
repository: @gitaly_repo,
revisions: revisions.map { |r| encode_binary(r) }
)
2023-01-13 00:05:48 +05:30
response = gitaly_client_call(@storage, :repository_service, :find_merge_base, request, timeout: GitalyClient.fast_timeout)
2018-03-17 18:26:18 +05:30
response.base.presence
end
def fork_repository(source_repository)
request = Gitaly::CreateForkRequest.new(
repository: @gitaly_repo,
source_repository: source_repository.gitaly_repository
)
2023-01-13 00:05:48 +05:30
gitaly_client_call(
2018-03-17 18:26:18 +05:30
@storage,
:repository_service,
:create_fork,
request,
remote_storage: source_repository.storage,
2019-12-21 20:55:43 +05:30
timeout: GitalyClient.long_timeout
2018-03-17 18:26:18 +05:30
)
end
2023-01-10 11:22:00 +05:30
def import_repository(source, http_authorization_header: '', mirror: false, resolved_address: '')
2018-03-17 18:26:18 +05:30
request = Gitaly::CreateRepositoryFromURLRequest.new(
repository: @gitaly_repo,
2022-06-21 17:19:12 +05:30
url: source,
http_authorization_header: http_authorization_header,
2023-01-10 11:22:00 +05:30
mirror: mirror,
resolved_address: resolved_address
2018-03-17 18:26:18 +05:30
)
2023-01-13 00:05:48 +05:30
gitaly_client_call(
2018-03-17 18:26:18 +05:30
@storage,
:repository_service,
:create_repository_from_url,
request,
2019-12-21 20:55:43 +05:30
timeout: GitalyClient.long_timeout
2018-03-17 18:26:18 +05:30
)
end
def fetch_source_branch(source_repository, source_branch, local_ref)
request = Gitaly::FetchSourceBranchRequest.new(
repository: @gitaly_repo,
source_repository: source_repository.gitaly_repository,
source_branch: source_branch.b,
target_ref: local_ref.b
)
2023-01-13 00:05:48 +05:30
response = gitaly_client_call(
2018-03-17 18:26:18 +05:30
@storage,
:repository_service,
:fetch_source_branch,
request,
2019-12-21 20:55:43 +05:30
timeout: GitalyClient.long_timeout,
2018-03-17 18:26:18 +05:30
remote_storage: source_repository.storage
)
response.result
end
def fsck
request = Gitaly::FsckRequest.new(repository: @gitaly_repo)
2023-01-13 00:05:48 +05:30
response = gitaly_client_call(@storage, :repository_service, :fsck, request, timeout: GitalyClient.long_timeout)
2018-03-17 18:26:18 +05:30
if response.error.empty?
2020-07-28 23:09:34 +05:30
["", 0]
2018-03-17 18:26:18 +05:30
else
2020-07-28 23:09:34 +05:30
[response.error.b, 1]
2018-03-17 18:26:18 +05:30
end
end
def create_bundle(save_path)
2018-11-08 19:23:39 +05:30
gitaly_fetch_stream_to_file(
save_path,
2018-03-17 18:26:18 +05:30
:create_bundle,
2018-11-08 19:23:39 +05:30
Gitaly::CreateBundleRequest,
2019-12-21 20:55:43 +05:30
GitalyClient.long_timeout
2018-03-17 18:26:18 +05:30
)
2018-11-08 19:23:39 +05:30
end
2018-03-17 18:26:18 +05:30
2018-11-08 19:23:39 +05:30
def backup_custom_hooks(save_path)
gitaly_fetch_stream_to_file(
save_path,
:backup_custom_hooks,
Gitaly::BackupCustomHooksRequest,
GitalyClient.default_timeout
)
2018-03-17 18:26:18 +05:30
end
def create_from_bundle(bundle_path)
2018-11-08 19:23:39 +05:30
gitaly_repo_stream_request(
bundle_path,
2018-03-17 18:26:18 +05:30
:create_repository_from_bundle,
2018-11-08 19:23:39 +05:30
Gitaly::CreateRepositoryFromBundleRequest,
2019-12-21 20:55:43 +05:30
GitalyClient.long_timeout
2018-11-08 19:23:39 +05:30
)
end
def restore_custom_hooks(custom_hooks_path)
gitaly_repo_stream_request(
custom_hooks_path,
:restore_custom_hooks,
Gitaly::RestoreCustomHooksRequest,
GitalyClient.default_timeout
2018-03-17 18:26:18 +05:30
)
end
2018-05-09 12:01:36 +05:30
def create_from_snapshot(http_url, http_auth)
request = Gitaly::CreateRepositoryFromSnapshotRequest.new(
repository: @gitaly_repo,
http_url: http_url,
http_auth: http_auth
)
2023-01-13 00:05:48 +05:30
gitaly_client_call(
2018-05-09 12:01:36 +05:30
@storage,
:repository_service,
:create_repository_from_snapshot,
request,
2019-12-21 20:55:43 +05:30
timeout: GitalyClient.long_timeout
2018-05-09 12:01:36 +05:30
)
end
2019-02-15 15:39:39 +05:30
def write_ref(ref_path, ref, old_ref)
2018-03-17 18:26:18 +05:30
request = Gitaly::WriteRefRequest.new(
repository: @gitaly_repo,
ref: ref_path.b,
2019-02-15 15:39:39 +05:30
revision: ref.b
2018-03-17 18:26:18 +05:30
)
request.old_revision = old_ref.b unless old_ref.nil?
2023-01-13 00:05:48 +05:30
gitaly_client_call(@storage, :repository_service, :write_ref, request, timeout: GitalyClient.fast_timeout)
2018-03-17 18:26:18 +05:30
end
2021-10-27 15:23:28 +05:30
def set_full_path(path)
2023-01-13 00:05:48 +05:30
gitaly_client_call(
2018-03-17 18:26:18 +05:30
@storage,
:repository_service,
2021-10-27 15:23:28 +05:30
:set_full_path,
Gitaly::SetFullPathRequest.new(
repository: @gitaly_repo,
path: path
),
2018-03-17 18:26:18 +05:30
timeout: GitalyClient.fast_timeout
)
2018-11-08 19:23:39 +05:30
nil
end
2022-08-27 11:52:29 +05:30
def full_path
2023-01-13 00:05:48 +05:30
response = gitaly_client_call(
2022-08-27 11:52:29 +05:30
@storage,
:repository_service,
:full_path,
Gitaly::FullPathRequest.new(repository: @gitaly_repo),
timeout: GitalyClient.fast_timeout
)
response.path.presence
end
2022-11-25 23:54:43 +05:30
def find_license
2018-03-27 19:54:05 +05:30
request = Gitaly::FindLicenseRequest.new(repository: @gitaly_repo)
2023-01-13 00:05:48 +05:30
gitaly_client_call(@storage, :repository_service, :find_license, request, timeout: GitalyClient.medium_timeout)
2018-03-27 19:54:05 +05:30
end
2018-05-09 12:01:36 +05:30
def calculate_checksum
request = Gitaly::CalculateChecksumRequest.new(repository: @gitaly_repo)
2023-01-13 00:05:48 +05:30
response = gitaly_client_call(@storage, :repository_service, :calculate_checksum, request, timeout: GitalyClient.fast_timeout)
2018-05-09 12:01:36 +05:30
response.checksum.presence
2018-10-15 14:42:47 +05:30
rescue GRPC::DataLoss => e
2021-06-08 01:23:25 +05:30
raise Gitlab::Git::Repository::InvalidRepository, e
2018-10-15 14:42:47 +05:30
end
def raw_changes_between(from, to)
request = Gitaly::GetRawChangesRequest.new(repository: @gitaly_repo, from_revision: from, to_revision: to)
2023-01-13 00:05:48 +05:30
gitaly_client_call(@storage, :repository_service, :get_raw_changes, request, timeout: GitalyClient.fast_timeout)
2018-11-08 19:23:39 +05:30
end
2023-01-13 00:05:48 +05:30
def search_files_by_name(ref, query, limit: 0, offset: 0)
request = Gitaly::SearchFilesByNameRequest.new(repository: @gitaly_repo, ref: ref, query: query, limit: limit, offset: offset)
gitaly_client_call(@storage, :repository_service, :search_files_by_name, request, timeout: GitalyClient.fast_timeout).flat_map(&:files)
2018-11-08 19:23:39 +05:30
end
2020-03-13 15:44:24 +05:30
def search_files_by_content(ref, query, options = {})
2019-07-07 11:18:12 +05:30
request = Gitaly::SearchFilesByContentRequest.new(repository: @gitaly_repo, ref: ref, query: query)
2023-01-13 00:05:48 +05:30
response = gitaly_client_call(@storage, :repository_service, :search_files_by_content, request, timeout: GitalyClient.default_timeout)
2020-03-13 15:44:24 +05:30
search_results_from_response(response, options)
2018-11-08 19:23:39 +05:30
end
2019-07-31 22:56:46 +05:30
2023-01-13 00:05:48 +05:30
def search_files_by_regexp(ref, filter, limit: 0, offset: 0)
request = Gitaly::SearchFilesByNameRequest.new(repository: @gitaly_repo, ref: ref, query: '.', filter: filter, limit: limit, offset: offset)
gitaly_client_call(@storage, :repository_service, :search_files_by_name, request, timeout: GitalyClient.fast_timeout).flat_map(&:files)
2021-04-29 21:17:54 +05:30
end
2019-07-31 22:56:46 +05:30
def disconnect_alternates
request = Gitaly::DisconnectGitAlternatesRequest.new(
repository: @gitaly_repo
)
2023-01-13 00:05:48 +05:30
gitaly_client_call(@storage, :object_pool_service, :disconnect_git_alternates, request, timeout: GitalyClient.long_timeout)
2019-12-21 20:55:43 +05:30
end
def rename(relative_path)
request = Gitaly::RenameRepositoryRequest.new(repository: @gitaly_repo, relative_path: relative_path)
2023-01-13 00:05:48 +05:30
gitaly_client_call(@storage, :repository_service, :rename_repository, request, timeout: GitalyClient.fast_timeout)
2019-12-21 20:55:43 +05:30
end
def remove
request = Gitaly::RemoveRepositoryRequest.new(repository: @gitaly_repo)
2023-01-13 00:05:48 +05:30
gitaly_client_call(@storage, :repository_service, :remove_repository, request, timeout: GitalyClient.long_timeout)
2019-07-31 22:56:46 +05:30
end
2018-11-08 19:23:39 +05:30
2020-04-08 14:13:33 +05:30
def replicate(source_repository)
request = Gitaly::ReplicateRepositoryRequest.new(
repository: @gitaly_repo,
source: source_repository.gitaly_repository
)
2023-01-13 00:05:48 +05:30
gitaly_client_call(
2020-04-08 14:13:33 +05:30
@storage,
:repository_service,
:replicate_repository,
request,
remote_storage: source_repository.storage,
timeout: GitalyClient.long_timeout
)
end
2018-11-08 19:23:39 +05:30
private
2020-03-13 15:44:24 +05:30
def search_results_from_response(gitaly_response, options = {})
limit = options[:limit]
2019-03-02 22:35:43 +05:30
matches = []
2020-03-13 15:44:24 +05:30
matches_count = 0
2019-03-02 22:35:43 +05:30
current_match = +""
gitaly_response.each do |message|
next if message.nil?
2020-03-13 15:44:24 +05:30
break if limit && matches_count >= limit
2019-07-07 11:18:12 +05:30
current_match << message.match_data
2023-01-13 00:05:48 +05:30
next unless message.end_of_match
matches << current_match
current_match = +""
matches_count += 1
2019-03-02 22:35:43 +05:30
end
matches
end
2018-11-08 19:23:39 +05:30
def gitaly_fetch_stream_to_file(save_path, rpc_name, request_class, timeout)
request = request_class.new(repository: @gitaly_repo)
2023-01-13 00:05:48 +05:30
response = gitaly_client_call(
2018-11-08 19:23:39 +05:30
@storage,
:repository_service,
rpc_name,
request,
timeout: timeout
)
2020-07-28 23:09:34 +05:30
write_stream_to_file(response, save_path)
end
2018-11-08 19:23:39 +05:30
2020-07-28 23:09:34 +05:30
def write_stream_to_file(response, save_path)
2018-11-08 19:23:39 +05:30
File.open(save_path, 'wb') do |f|
response.each do |message|
f.write(message.data)
end
end
2018-12-13 13:39:08 +05:30
# If the file is empty means that we received an empty stream, we delete the file
2018-11-08 19:23:39 +05:30
FileUtils.rm(save_path) if File.zero?(save_path)
end
def gitaly_repo_stream_request(file_path, rpc_name, request_class, timeout)
request = request_class.new(repository: @gitaly_repo)
enum = Enumerator.new do |y|
File.open(file_path, 'rb') do |f|
while data = f.read(MAX_MSG_SIZE)
request.data = data
y.yield request
request = request_class.new
end
end
end
2023-01-13 00:05:48 +05:30
gitaly_client_call(
2018-11-08 19:23:39 +05:30
@storage,
:repository_service,
rpc_name,
enum,
timeout: timeout
)
end
def build_set_config_entry(key, value)
entry = Gitaly::SetConfigRequest::Entry.new(key: key)
case value
when String
entry.value_str = value
when Integer
entry.value_int32 = value
when TrueClass, FalseClass
entry.value_bool = value
else
raise InvalidArgument, "invalid git config value: #{value.inspect}"
end
entry
2018-05-09 12:01:36 +05:30
end
2017-09-10 17:25:29 +05:30
end
end
end