2018-11-09 13:39:43 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-15 21:08:30 -05:00
|
|
|
module Gitlab
|
|
|
|
module GitalyClient
|
2017-07-18 03:59:36 -04:00
|
|
|
class RefService
|
2017-06-28 08:20:29 -04:00
|
|
|
include Gitlab::EncodingHelper
|
|
|
|
|
2017-04-07 05:19:20 -04:00
|
|
|
# 'repository' is a Gitlab::Git::Repository
|
|
|
|
def initialize(repository)
|
2017-06-28 08:20:29 -04:00
|
|
|
@repository = repository
|
2017-04-07 05:19:20 -04:00
|
|
|
@gitaly_repo = repository.gitaly_repository
|
2017-06-16 12:42:41 -04:00
|
|
|
@storage = repository.storage
|
2017-02-15 21:08:30 -05:00
|
|
|
end
|
|
|
|
|
2017-07-22 18:13:47 -04:00
|
|
|
def branches
|
|
|
|
request = Gitaly::FindAllBranchesRequest.new(repository: @gitaly_repo)
|
2020-06-30 20:09:02 -04:00
|
|
|
response = GitalyClient.call(@storage, :ref_service, :find_all_branches, request, timeout: GitalyClient.fast_timeout)
|
|
|
|
consume_find_all_branches_response(response)
|
2017-12-28 13:07:21 -05:00
|
|
|
end
|
|
|
|
|
2018-07-17 22:08:23 -04:00
|
|
|
def remote_branches(remote_name)
|
|
|
|
request = Gitaly::FindAllRemoteBranchesRequest.new(repository: @gitaly_repo, remote_name: remote_name)
|
2020-06-30 20:09:02 -04:00
|
|
|
response = GitalyClient.call(@storage, :ref_service, :find_all_remote_branches, request, timeout: GitalyClient.medium_timeout)
|
|
|
|
consume_find_all_remote_branches_response(remote_name, response)
|
2018-07-17 22:08:23 -04:00
|
|
|
end
|
|
|
|
|
2017-12-28 13:07:21 -05:00
|
|
|
def merged_branches(branch_names = [])
|
|
|
|
request = Gitaly::FindAllBranchesRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
merged_only: true,
|
|
|
|
merged_branches: branch_names.map { |s| encode_binary(s) }
|
|
|
|
)
|
2020-06-30 20:09:02 -04:00
|
|
|
response = GitalyClient.call(@storage, :ref_service, :find_all_branches, request, timeout: GitalyClient.fast_timeout)
|
|
|
|
consume_find_all_branches_response(response)
|
2017-07-22 18:13:47 -04:00
|
|
|
end
|
|
|
|
|
2017-02-15 21:08:30 -05:00
|
|
|
def default_branch_name
|
2017-04-07 05:19:20 -04:00
|
|
|
request = Gitaly::FindDefaultBranchNameRequest.new(repository: @gitaly_repo)
|
2018-07-05 12:05:24 -04:00
|
|
|
response = GitalyClient.call(@storage, :ref_service, :find_default_branch_name, request, timeout: GitalyClient.fast_timeout)
|
2017-06-16 12:42:41 -04:00
|
|
|
Gitlab::Git.branch_name(response.name)
|
2017-02-15 21:08:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def branch_names
|
2017-04-07 05:19:20 -04:00
|
|
|
request = Gitaly::FindAllBranchNamesRequest.new(repository: @gitaly_repo)
|
2020-06-30 20:09:02 -04:00
|
|
|
response = GitalyClient.call(@storage, :ref_service, :find_all_branch_names, request, timeout: GitalyClient.fast_timeout)
|
|
|
|
consume_refs_response(response) { |name| Gitlab::Git.branch_name(name) }
|
2017-02-15 21:08:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def tag_names
|
2017-04-07 05:19:20 -04:00
|
|
|
request = Gitaly::FindAllTagNamesRequest.new(repository: @gitaly_repo)
|
2020-06-30 20:09:02 -04:00
|
|
|
response = GitalyClient.call(@storage, :ref_service, :find_all_tag_names, request, timeout: GitalyClient.fast_timeout)
|
|
|
|
consume_refs_response(response) { |name| Gitlab::Git.tag_name(name) }
|
2017-02-15 21:08:30 -05:00
|
|
|
end
|
|
|
|
|
2017-03-28 13:14:48 -04:00
|
|
|
def find_ref_name(commit_id, ref_prefix)
|
2017-04-06 12:47:35 -04:00
|
|
|
request = Gitaly::FindRefNameRequest.new(
|
2017-05-11 16:26:51 -04:00
|
|
|
repository: @gitaly_repo,
|
2017-03-28 13:14:48 -04:00
|
|
|
commit_id: commit_id,
|
|
|
|
prefix: ref_prefix
|
|
|
|
)
|
2017-11-29 04:12:12 -05:00
|
|
|
response = GitalyClient.call(@storage, :ref_service, :find_ref_name, request, timeout: GitalyClient.medium_timeout)
|
|
|
|
encode!(response.name.dup)
|
2017-03-28 13:14:48 -04:00
|
|
|
end
|
|
|
|
|
2018-07-17 06:17:43 -04:00
|
|
|
def list_new_commits(newrev)
|
|
|
|
request = Gitaly::ListNewCommitsRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
commit_id: newrev
|
|
|
|
)
|
|
|
|
|
|
|
|
commits = []
|
2020-06-22 11:09:27 -04:00
|
|
|
|
2020-06-30 20:09:02 -04:00
|
|
|
response = GitalyClient.call(@storage, :ref_service, :list_new_commits, request, timeout: GitalyClient.medium_timeout)
|
|
|
|
response.each do |msg|
|
|
|
|
msg.commits.each do |c|
|
|
|
|
commits << Gitlab::Git::Commit.new(@repository, c)
|
2018-07-17 06:17:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
commits
|
|
|
|
end
|
|
|
|
|
2019-03-05 10:09:33 -05:00
|
|
|
def list_new_blobs(newrev, limit = 0, dynamic_timeout: nil)
|
2018-08-07 07:13:48 -04:00
|
|
|
request = Gitaly::ListNewBlobsRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
commit_id: newrev,
|
|
|
|
limit: limit
|
|
|
|
)
|
|
|
|
|
2019-03-05 10:09:33 -05:00
|
|
|
timeout =
|
|
|
|
if dynamic_timeout
|
|
|
|
[dynamic_timeout, GitalyClient.medium_timeout].min
|
|
|
|
else
|
|
|
|
GitalyClient.medium_timeout
|
|
|
|
end
|
|
|
|
|
2020-06-30 20:09:02 -04:00
|
|
|
response = GitalyClient.call(@storage, :ref_service, :list_new_blobs, request, timeout: timeout)
|
|
|
|
response.flat_map do |msg|
|
|
|
|
# Returns an Array of Gitaly::NewBlobObject objects
|
|
|
|
# Available methods are: #size, #oid and #path
|
|
|
|
msg.new_blob_objects
|
2018-08-07 07:13:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-19 05:06:20 -04:00
|
|
|
def count_tag_names
|
|
|
|
tag_names.count
|
|
|
|
end
|
|
|
|
|
|
|
|
def count_branch_names
|
|
|
|
branch_names.count
|
|
|
|
end
|
|
|
|
|
2020-07-17 05:09:43 -04:00
|
|
|
def local_branches(sort_by: nil, pagination_params: nil)
|
|
|
|
request = Gitaly::FindLocalBranchesRequest.new(repository: @gitaly_repo, pagination_params: pagination_params)
|
2017-03-17 15:36:46 -04:00
|
|
|
request.sort_by = sort_by_param(sort_by) if sort_by
|
2020-06-30 20:09:02 -04:00
|
|
|
response = GitalyClient.call(@storage, :ref_service, :find_local_branches, request, timeout: GitalyClient.fast_timeout)
|
|
|
|
consume_find_local_branches_response(response)
|
2017-03-17 15:36:46 -04:00
|
|
|
end
|
|
|
|
|
2017-07-25 15:49:12 -04:00
|
|
|
def tags
|
|
|
|
request = Gitaly::FindAllTagsRequest.new(repository: @gitaly_repo)
|
2020-06-30 20:09:02 -04:00
|
|
|
response = GitalyClient.call(@storage, :ref_service, :find_all_tags, request, timeout: GitalyClient.medium_timeout)
|
|
|
|
consume_tags_response(response)
|
2017-07-25 15:49:12 -04:00
|
|
|
end
|
|
|
|
|
2017-08-14 07:12:32 -04:00
|
|
|
def ref_exists?(ref_name)
|
2017-12-26 13:53:31 -05:00
|
|
|
request = Gitaly::RefExistsRequest.new(repository: @gitaly_repo, ref: encode_binary(ref_name))
|
2018-07-05 12:05:24 -04:00
|
|
|
response = GitalyClient.call(@storage, :ref_service, :ref_exists, request, timeout: GitalyClient.fast_timeout)
|
2017-08-14 07:12:32 -04:00
|
|
|
response.value
|
|
|
|
rescue GRPC::InvalidArgument => e
|
|
|
|
raise ArgumentError, e.message
|
|
|
|
end
|
|
|
|
|
2017-09-05 13:29:45 -04:00
|
|
|
def find_branch(branch_name)
|
2017-08-28 03:31:41 -04:00
|
|
|
request = Gitaly::FindBranchRequest.new(
|
2017-09-05 13:29:45 -04:00
|
|
|
repository: @gitaly_repo,
|
2017-12-26 13:53:31 -05:00
|
|
|
name: encode_binary(branch_name)
|
2017-09-05 13:29:45 -04:00
|
|
|
)
|
|
|
|
|
2018-07-05 12:05:24 -04:00
|
|
|
response = GitalyClient.call(@repository.storage, :ref_service, :find_branch, request, timeout: GitalyClient.medium_timeout)
|
2017-09-05 13:29:45 -04:00
|
|
|
branch = response.branch
|
|
|
|
return unless branch
|
|
|
|
|
|
|
|
target_commit = Gitlab::Git::Commit.decorate(@repository, branch.target_commit)
|
|
|
|
Gitlab::Git::Branch.new(@repository, encode!(branch.name.dup), branch.target_commit.id, target_commit)
|
|
|
|
end
|
|
|
|
|
2018-01-25 14:59:33 -05:00
|
|
|
def delete_refs(refs: [], except_with_prefixes: [])
|
2017-11-17 11:54:48 -05:00
|
|
|
request = Gitaly::DeleteRefsRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
2018-01-25 14:59:33 -05:00
|
|
|
refs: refs.map { |r| encode_binary(r) },
|
|
|
|
except_with_prefix: except_with_prefixes.map { |r| encode_binary(r) }
|
2017-11-17 11:54:48 -05:00
|
|
|
)
|
|
|
|
|
2019-09-19 14:06:18 -04:00
|
|
|
response = GitalyClient.call(@repository.storage, :ref_service, :delete_refs, request, timeout: GitalyClient.medium_timeout)
|
2018-01-25 14:59:33 -05:00
|
|
|
|
|
|
|
raise Gitlab::Git::Repository::GitError, response.git_error if response.git_error.present?
|
2017-11-17 11:54:48 -05:00
|
|
|
end
|
|
|
|
|
2018-01-30 03:59:45 -05:00
|
|
|
# Limit: 0 implies no limit, thus all tag names will be returned
|
2018-01-30 05:22:08 -05:00
|
|
|
def tag_names_contains_sha(sha, limit: 0)
|
2018-01-30 03:59:45 -05:00
|
|
|
request = Gitaly::ListTagNamesContainingCommitRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
commit_id: sha,
|
|
|
|
limit: limit
|
|
|
|
)
|
|
|
|
|
2020-06-30 20:09:02 -04:00
|
|
|
response = GitalyClient.call(@storage, :ref_service, :list_tag_names_containing_commit, request, timeout: GitalyClient.medium_timeout)
|
|
|
|
consume_ref_contains_sha_response(response, :tag_names)
|
2018-01-30 03:59:45 -05:00
|
|
|
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
|
|
|
|
)
|
|
|
|
|
2020-06-30 20:09:02 -04:00
|
|
|
response = GitalyClient.call(@storage, :ref_service, :list_branch_names_containing_commit, request, timeout: GitalyClient.medium_timeout)
|
|
|
|
consume_ref_contains_sha_response(response, :branch_names)
|
2018-01-30 03:59:45 -05:00
|
|
|
end
|
|
|
|
|
2018-03-20 15:20:12 -04:00
|
|
|
def get_tag_messages(tag_ids)
|
|
|
|
request = Gitaly::GetTagMessagesRequest.new(repository: @gitaly_repo, tag_ids: tag_ids)
|
2018-11-09 13:39:43 -05:00
|
|
|
messages = Hash.new { |h, k| h[k] = +''.b }
|
2018-03-20 15:20:12 -04:00
|
|
|
current_tag_id = nil
|
|
|
|
|
2020-06-30 20:09:02 -04:00
|
|
|
response = GitalyClient.call(@storage, :ref_service, :get_tag_messages, request, timeout: GitalyClient.fast_timeout)
|
|
|
|
response.each do |rpc_message|
|
|
|
|
current_tag_id = rpc_message.tag_id if rpc_message.tag_id.present?
|
2018-03-20 15:20:12 -04:00
|
|
|
|
2020-06-30 20:09:02 -04:00
|
|
|
messages[current_tag_id] << rpc_message.message
|
2018-03-20 15:20:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
messages
|
|
|
|
end
|
|
|
|
|
2019-05-02 17:41:05 -04:00
|
|
|
def pack_refs
|
|
|
|
request = Gitaly::PackRefsRequest.new(repository: @gitaly_repo)
|
|
|
|
|
2019-09-19 14:06:18 -04:00
|
|
|
GitalyClient.call(@storage, :ref_service, :pack_refs, request, timeout: GitalyClient.long_timeout)
|
2019-05-02 17:41:05 -04:00
|
|
|
end
|
|
|
|
|
2017-02-15 21:08:30 -05:00
|
|
|
private
|
|
|
|
|
2017-06-28 08:20:29 -04:00
|
|
|
def consume_refs_response(response)
|
|
|
|
response.flat_map { |message| message.names.map { |name| yield(name) } }
|
2017-02-15 21:08:30 -05:00
|
|
|
end
|
2017-03-17 15:36:46 -04:00
|
|
|
|
|
|
|
def sort_by_param(sort_by)
|
2017-06-28 15:38:00 -04:00
|
|
|
sort_by = 'name' if sort_by == 'name_asc'
|
|
|
|
|
2017-03-17 15:36:46 -04:00
|
|
|
enum_value = Gitaly::FindLocalBranchesRequest::SortBy.resolve(sort_by.upcase.to_sym)
|
|
|
|
raise ArgumentError, "Invalid sort_by key `#{sort_by}`" unless enum_value
|
2017-11-14 04:02:39 -05:00
|
|
|
|
2017-03-17 15:36:46 -04:00
|
|
|
enum_value
|
|
|
|
end
|
|
|
|
|
2017-12-28 13:07:21 -05:00
|
|
|
def consume_find_local_branches_response(response)
|
2017-06-28 08:20:29 -04:00
|
|
|
response.flat_map do |message|
|
|
|
|
message.branches.map do |gitaly_branch|
|
|
|
|
Gitlab::Git::Branch.new(
|
|
|
|
@repository,
|
|
|
|
encode!(gitaly_branch.name.dup),
|
2017-07-14 09:30:58 -04:00
|
|
|
gitaly_branch.commit_id,
|
|
|
|
commit_from_local_branches_response(gitaly_branch)
|
2017-06-28 08:20:29 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2017-03-17 15:36:46 -04:00
|
|
|
end
|
2017-07-14 09:30:58 -04:00
|
|
|
|
2017-12-28 13:07:21 -05:00
|
|
|
def consume_find_all_branches_response(response)
|
|
|
|
response.flat_map do |message|
|
|
|
|
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)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-17 22:08:23 -04:00
|
|
|
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
|
|
|
|
|
2017-07-25 15:49:12 -04:00
|
|
|
def consume_tags_response(response)
|
|
|
|
response.flat_map do |message|
|
2018-03-20 15:20:12 -04:00
|
|
|
message.tags.map { |gitaly_tag| Gitlab::Git::Tag.new(@repository, gitaly_tag) }
|
2017-07-25 15:49:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-14 09:30:58 -04:00
|
|
|
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),
|
2017-06-23 17:33:16 -04:00
|
|
|
author_name: response.commit_author.name.dup,
|
|
|
|
author_email: response.commit_author.email.dup,
|
2017-07-14 09:30:58 -04:00
|
|
|
committed_date: Time.at(response.commit_committer.date.seconds),
|
2017-06-23 17:33:16 -04:00
|
|
|
committer_name: response.commit_committer.name.dup,
|
|
|
|
committer_email: response.commit_committer.email.dup
|
2017-07-14 09:30:58 -04:00
|
|
|
}
|
|
|
|
|
2017-07-25 16:48:17 -04:00
|
|
|
Gitlab::Git::Commit.decorate(@repository, hash)
|
2017-07-14 09:30:58 -04:00
|
|
|
end
|
2017-08-28 03:31:41 -04:00
|
|
|
|
2018-01-30 05:22:08 -05:00
|
|
|
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
|
|
|
|
|
2017-08-28 03:31:41 -04:00
|
|
|
def invalid_ref!(message)
|
|
|
|
raise Gitlab::Git::Repository::InvalidRef.new(message)
|
|
|
|
end
|
2017-02-15 21:08:30 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|