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 RefService
|
|
|
|
include Gitlab::EncodingHelper
|
2023-01-13 00:05:48 +05:30
|
|
|
include WithFeatureFlagActors
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
TAGS_SORT_KEY = {
|
|
|
|
'name' => Gitaly::FindAllTagsRequest::SortBy::Key::REFNAME,
|
2022-10-11 01:57:18 +05:30
|
|
|
'updated' => Gitaly::FindAllTagsRequest::SortBy::Key::CREATORDATE,
|
|
|
|
'version' => Gitaly::FindAllTagsRequest::SortBy::Key::VERSION_REFNAME
|
2021-11-11 11:23:49 +05:30
|
|
|
}.freeze
|
|
|
|
|
|
|
|
TAGS_SORT_DIRECTION = {
|
|
|
|
'asc' => Gitaly::SortDirection::ASCENDING,
|
|
|
|
'desc' => Gitaly::SortDirection::DESCENDING
|
|
|
|
}.freeze
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
AMBIGUOUS_REFERENCE = 'reference is ambiguous'
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
# 'repository' is a Gitlab::Git::Repository
|
|
|
|
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 branches
|
|
|
|
request = Gitaly::FindAllBranchesRequest.new(repository: @gitaly_repo)
|
2023-01-13 00:05:48 +05:30
|
|
|
response = gitaly_client_call(@storage, :ref_service, :find_all_branches, request, timeout: GitalyClient.fast_timeout)
|
2018-03-17 18:26:18 +05:30
|
|
|
consume_find_all_branches_response(response)
|
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
def remote_branches(remote_name)
|
|
|
|
request = Gitaly::FindAllRemoteBranchesRequest.new(repository: @gitaly_repo, remote_name: remote_name)
|
2023-01-13 00:05:48 +05:30
|
|
|
response = gitaly_client_call(@storage, :ref_service, :find_all_remote_branches, request, timeout: GitalyClient.medium_timeout)
|
2018-11-18 11:00:15 +05:30
|
|
|
consume_find_all_remote_branches_response(remote_name, response)
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def merged_branches(branch_names = [])
|
|
|
|
request = Gitaly::FindAllBranchesRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
merged_only: true,
|
|
|
|
merged_branches: branch_names.map { |s| encode_binary(s) }
|
|
|
|
)
|
2023-01-13 00:05:48 +05:30
|
|
|
response = gitaly_client_call(@storage, :ref_service, :find_all_branches, request, timeout: GitalyClient.fast_timeout)
|
2018-03-17 18:26:18 +05:30
|
|
|
consume_find_all_branches_response(response)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def default_branch_name
|
|
|
|
request = Gitaly::FindDefaultBranchNameRequest.new(repository: @gitaly_repo)
|
2023-01-13 00:05:48 +05:30
|
|
|
response = gitaly_client_call(@storage, :ref_service, :find_default_branch_name, request, timeout: GitalyClient.fast_timeout)
|
2017-09-10 17:25:29 +05:30
|
|
|
Gitlab::Git.branch_name(response.name)
|
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
def local_branches(sort_by: nil, pagination_params: nil)
|
|
|
|
request = Gitaly::FindLocalBranchesRequest.new(repository: @gitaly_repo, pagination_params: pagination_params)
|
2021-11-11 11:23:49 +05:30
|
|
|
request.sort_by = sort_local_branches_by_param(sort_by) if sort_by
|
2023-01-13 00:05:48 +05:30
|
|
|
response = gitaly_client_call(@storage, :ref_service, :find_local_branches, request, timeout: GitalyClient.fast_timeout)
|
2018-03-17 18:26:18 +05:30
|
|
|
consume_find_local_branches_response(response)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
def tags(sort_by: nil, pagination_params: nil)
|
|
|
|
request = Gitaly::FindAllTagsRequest.new(repository: @gitaly_repo, pagination_params: pagination_params)
|
2021-11-11 11:23:49 +05:30
|
|
|
request.sort_by = sort_tags_by_param(sort_by) if sort_by
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
response = gitaly_client_call(@storage, :ref_service, :find_all_tags, request, timeout: GitalyClient.medium_timeout)
|
2017-09-10 17:25:29 +05:30
|
|
|
consume_tags_response(response)
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def ref_exists?(ref_name)
|
|
|
|
request = Gitaly::RefExistsRequest.new(repository: @gitaly_repo, ref: encode_binary(ref_name))
|
2023-01-13 00:05:48 +05:30
|
|
|
response = gitaly_client_call(@storage, :ref_service, :ref_exists, request, timeout: GitalyClient.fast_timeout)
|
2018-03-17 18:26:18 +05:30
|
|
|
response.value
|
|
|
|
rescue GRPC::InvalidArgument => e
|
|
|
|
raise ArgumentError, e.message
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_branch(branch_name)
|
|
|
|
request = Gitaly::FindBranchRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
name: encode_binary(branch_name)
|
|
|
|
)
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
response = gitaly_client_call(@repository.storage, :ref_service, :find_branch, request, timeout: GitalyClient.medium_timeout)
|
2018-03-17 18:26:18 +05:30
|
|
|
branch = response.branch
|
|
|
|
return unless branch
|
|
|
|
|
|
|
|
target_commit = Gitlab::Git::Commit.decorate(@repository, branch.target_commit)
|
2022-10-11 01:57:18 +05:30
|
|
|
Gitlab::Git::Branch.new(@repository, branch.name.dup, branch.target_commit.id, target_commit)
|
2023-03-04 22:38:38 +05:30
|
|
|
rescue GRPC::BadStatus => e
|
|
|
|
raise e unless e.message.include?(AMBIGUOUS_REFERENCE)
|
|
|
|
|
|
|
|
raise Gitlab::Git::AmbiguousRef, "branch is ambiguous: #{branch_name}"
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
def find_tag(tag_name)
|
|
|
|
return if tag_name.blank?
|
|
|
|
|
|
|
|
request = Gitaly::FindTagRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
tag_name: encode_binary(tag_name)
|
|
|
|
)
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
response = gitaly_client_call(@repository.storage, :ref_service, :find_tag, request, timeout: GitalyClient.medium_timeout)
|
2021-11-11 11:23:49 +05:30
|
|
|
tag = response.tag
|
|
|
|
return unless tag
|
|
|
|
|
|
|
|
Gitlab::Git::Tag.new(@repository, tag)
|
2022-08-27 11:52:29 +05:30
|
|
|
rescue GRPC::BadStatus => e
|
|
|
|
detailed_error = GitalyClient.decode_detailed_error(e)
|
|
|
|
|
|
|
|
case detailed_error&.error
|
|
|
|
when :tag_not_found
|
|
|
|
raise Gitlab::Git::UnknownRef, "tag does not exist: #{tag_name}"
|
|
|
|
else
|
|
|
|
# When this is not a know structured error we simply re-raise the exception.
|
|
|
|
raise e
|
|
|
|
end
|
2021-11-11 11:23:49 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def delete_refs(refs: [], except_with_prefixes: [])
|
|
|
|
request = Gitaly::DeleteRefsRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
refs: refs.map { |r| encode_binary(r) },
|
|
|
|
except_with_prefix: except_with_prefixes.map { |r| encode_binary(r) }
|
|
|
|
)
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
response = gitaly_client_call(@repository.storage, :ref_service, :delete_refs, request, timeout: GitalyClient.medium_timeout)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
raise Gitlab::Git::Repository::GitError, response.git_error if response.git_error.present?
|
2022-08-13 15:12:31 +05:30
|
|
|
rescue GRPC::BadStatus => e
|
|
|
|
detailed_error = GitalyClient.decode_detailed_error(e)
|
|
|
|
|
|
|
|
case detailed_error&.error
|
|
|
|
when :invalid_format
|
|
|
|
raise Gitlab::Git::InvalidRefFormatError, "references have an invalid format: #{detailed_error.invalid_format.refs.join(",")}"
|
|
|
|
when :references_locked
|
|
|
|
raise Gitlab::Git::ReferencesLockedError
|
|
|
|
else
|
|
|
|
raise e
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# Limit: 0 implies no limit, thus all tag names will be returned
|
|
|
|
def tag_names_contains_sha(sha, limit: 0)
|
|
|
|
request = Gitaly::ListTagNamesContainingCommitRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
commit_id: sha,
|
|
|
|
limit: limit
|
|
|
|
)
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
response = gitaly_client_call(@storage, :ref_service, :list_tag_names_containing_commit, request, timeout: GitalyClient.medium_timeout)
|
2020-07-28 23:09:34 +05:30
|
|
|
consume_ref_contains_sha_response(response, :tag_names)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# Limit: 0 implies no limit, thus all tag names will be returned
|
|
|
|
def branch_names_contains_sha(sha, limit: 0)
|
|
|
|
request = Gitaly::ListBranchNamesContainingCommitRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
commit_id: sha,
|
|
|
|
limit: limit
|
|
|
|
)
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
response = gitaly_client_call(@storage, :ref_service, :list_branch_names_containing_commit, request, timeout: GitalyClient.medium_timeout)
|
2020-07-28 23:09:34 +05:30
|
|
|
consume_ref_contains_sha_response(response, :branch_names)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def get_tag_messages(tag_ids)
|
|
|
|
request = Gitaly::GetTagMessagesRequest.new(repository: @gitaly_repo, tag_ids: tag_ids)
|
2019-02-15 15:39:39 +05:30
|
|
|
messages = Hash.new { |h, k| h[k] = +''.b }
|
2018-11-08 19:23:39 +05:30
|
|
|
current_tag_id = nil
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
response = gitaly_client_call(@storage, :ref_service, :get_tag_messages, request, timeout: GitalyClient.fast_timeout)
|
2018-11-08 19:23:39 +05:30
|
|
|
response.each do |rpc_message|
|
|
|
|
current_tag_id = rpc_message.tag_id if rpc_message.tag_id.present?
|
|
|
|
|
|
|
|
messages[current_tag_id] << rpc_message.message
|
|
|
|
end
|
|
|
|
|
|
|
|
messages
|
|
|
|
end
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
def get_tag_signatures(tag_ids)
|
|
|
|
request = Gitaly::GetTagSignaturesRequest.new(repository: @gitaly_repo, tag_revisions: tag_ids)
|
2023-01-13 00:05:48 +05:30
|
|
|
response = gitaly_client_call(@repository.storage, :ref_service, :get_tag_signatures, request, timeout: GitalyClient.fast_timeout)
|
2021-10-27 15:23:28 +05:30
|
|
|
|
|
|
|
signatures = Hash.new { |h, k| h[k] = [+''.b, +''.b] }
|
|
|
|
current_tag_id = nil
|
|
|
|
|
|
|
|
response.each do |message|
|
|
|
|
message.signatures.each do |tag_signature|
|
|
|
|
current_tag_id = tag_signature.tag_id if tag_signature.tag_id.present?
|
|
|
|
|
|
|
|
signatures[current_tag_id].first << tag_signature.signature
|
|
|
|
signatures[current_tag_id].last << tag_signature.content
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
signatures
|
|
|
|
rescue GRPC::InvalidArgument => ex
|
|
|
|
raise ArgumentError, ex
|
|
|
|
end
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
# peel_tags slows down the request by a factor of 3-4
|
|
|
|
def list_refs(patterns = [Gitlab::Git::BRANCH_REF_PREFIX], pointing_at_oids: [], peel_tags: false)
|
2021-12-11 22:18:48 +05:30
|
|
|
request = Gitaly::ListRefsRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
2023-04-23 21:23:45 +05:30
|
|
|
patterns: patterns,
|
|
|
|
pointing_at_oids: pointing_at_oids,
|
|
|
|
peel_tags: peel_tags
|
2021-12-11 22:18:48 +05:30
|
|
|
)
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
response = gitaly_client_call(@storage, :ref_service, :list_refs, request, timeout: GitalyClient.fast_timeout)
|
2021-12-11 22:18:48 +05:30
|
|
|
consume_list_refs_response(response)
|
|
|
|
end
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
def find_refs_by_oid(oid:, limit:, ref_patterns: nil)
|
|
|
|
request = Gitaly::FindRefsByOIDRequest.new(repository: @gitaly_repo, sort_field: :refname, oid: oid, limit: limit, ref_patterns: ref_patterns)
|
2021-12-11 22:18:48 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
response = gitaly_client_call(@storage, :ref_service, :find_refs_by_oid, request, timeout: GitalyClient.medium_timeout)
|
2021-12-11 22:18:48 +05:30
|
|
|
response&.refs&.to_a
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def consume_refs_response(response)
|
|
|
|
response.flat_map { |message| message.names.map { |name| yield(name) } }
|
|
|
|
end
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
def consume_list_refs_response(response)
|
2023-03-04 22:38:38 +05:30
|
|
|
response.flat_map { |res| res.references.to_ary }
|
2021-12-11 22:18:48 +05:30
|
|
|
end
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
def sort_local_branches_by_param(sort_by)
|
2017-09-10 17:25:29 +05:30
|
|
|
sort_by = 'name' if sort_by == 'name_asc'
|
|
|
|
|
|
|
|
enum_value = Gitaly::FindLocalBranchesRequest::SortBy.resolve(sort_by.upcase.to_sym)
|
2023-05-27 22:25:52 +05:30
|
|
|
return Gitaly::FindLocalBranchesRequest::SortBy::NAME unless enum_value
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
enum_value
|
|
|
|
end
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
def sort_tags_by_param(sort_by)
|
2022-10-11 01:57:18 +05:30
|
|
|
match = sort_by.match(/^(?<key>name|updated|version)_(?<direction>asc|desc)$/)
|
2021-11-11 11:23:49 +05:30
|
|
|
|
|
|
|
return unless match
|
|
|
|
|
|
|
|
Gitaly::FindAllTagsRequest::SortBy.new(
|
|
|
|
key: TAGS_SORT_KEY[match[:key]],
|
|
|
|
direction: TAGS_SORT_DIRECTION[match[:direction]]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def consume_find_local_branches_response(response)
|
2022-11-25 23:54:43 +05:30
|
|
|
response.flat_map do |message|
|
|
|
|
if message.local_branches.present?
|
2022-10-11 01:57:18 +05:30
|
|
|
message.local_branches.map do |branch|
|
|
|
|
target_commit = Gitlab::Git::Commit.decorate(@repository, branch.target_commit)
|
|
|
|
Gitlab::Git::Branch.new(@repository, branch.name, branch.target_commit.id, target_commit)
|
|
|
|
end
|
2022-11-25 23:54:43 +05:30
|
|
|
else
|
2022-10-11 01:57:18 +05:30
|
|
|
message.branches.map do |gitaly_branch|
|
|
|
|
Gitlab::Git::Branch.new(
|
|
|
|
@repository,
|
|
|
|
gitaly_branch.name.dup,
|
|
|
|
gitaly_branch.commit_id,
|
|
|
|
commit_from_local_branches_response(gitaly_branch)
|
|
|
|
)
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def consume_find_all_branches_response(response)
|
2017-09-10 17:25:29 +05:30
|
|
|
response.flat_map do |message|
|
2018-03-17 18:26:18 +05:30
|
|
|
message.branches.map do |branch|
|
|
|
|
target_commit = Gitlab::Git::Commit.decorate(@repository, branch.target)
|
|
|
|
Gitlab::Git::Branch.new(@repository, branch.name, branch.target.id, target_commit)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
def consume_find_all_remote_branches_response(remote_name, response)
|
|
|
|
remote_name += '/' unless remote_name.ends_with?('/')
|
|
|
|
|
|
|
|
response.flat_map do |message|
|
|
|
|
message.branches.map do |branch|
|
|
|
|
target_commit = Gitlab::Git::Commit.decorate(@repository, branch.target_commit)
|
|
|
|
branch_name = branch.name.sub(remote_name, '')
|
|
|
|
Gitlab::Git::Branch.new(@repository, branch_name, branch.target_commit.id, target_commit)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def consume_tags_response(response)
|
|
|
|
response.flat_map do |message|
|
2018-11-08 19:23:39 +05:30
|
|
|
message.tags.map { |gitaly_tag| Gitlab::Git::Tag.new(@repository, gitaly_tag) }
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def commit_from_local_branches_response(response)
|
|
|
|
# Git messages have no encoding enforcements. However, in the UI we only
|
|
|
|
# handle UTF-8, so basically we cross our fingers that the message force
|
|
|
|
# encoded to UTF-8 is readable.
|
|
|
|
message = response.commit_subject.dup.force_encoding('UTF-8')
|
|
|
|
|
|
|
|
# NOTE: For ease of parsing in Gitaly, we have only the subject of
|
|
|
|
# the commit and not the full message. This is ok, since all the
|
|
|
|
# code that uses `local_branches` only cares at most about the
|
|
|
|
# commit message.
|
|
|
|
# TODO: Once gitaly "takes over" Rugged consider separating the
|
|
|
|
# subject from the message to make it clearer when there's one
|
|
|
|
# available but not the other.
|
|
|
|
hash = {
|
|
|
|
id: response.commit_id,
|
|
|
|
message: message,
|
|
|
|
authored_date: Time.at(response.commit_author.date.seconds),
|
|
|
|
author_name: response.commit_author.name.dup,
|
|
|
|
author_email: response.commit_author.email.dup,
|
|
|
|
committed_date: Time.at(response.commit_committer.date.seconds),
|
|
|
|
committer_name: response.commit_committer.name.dup,
|
|
|
|
committer_email: response.commit_committer.email.dup
|
|
|
|
}
|
|
|
|
|
|
|
|
Gitlab::Git::Commit.decorate(@repository, hash)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
def consume_ref_contains_sha_response(stream, collection_name)
|
|
|
|
stream.each_with_object([]) do |response, array|
|
|
|
|
encoded_names = response.send(collection_name).map { |b| Gitlab::Git.ref_name(b) } # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
array.concat(encoded_names)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def invalid_ref!(message)
|
2021-06-08 01:23:25 +05:30
|
|
|
raise Gitlab::Git::Repository::InvalidRef, message
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|