2017-02-16 02:08:30 +00:00
|
|
|
module Gitlab
|
|
|
|
module GitalyClient
|
2017-07-18 07:59:36 +00:00
|
|
|
class RefService
|
2017-06-28 12:20:29 +00:00
|
|
|
include Gitlab::EncodingHelper
|
|
|
|
|
2017-04-07 09:19:20 +00:00
|
|
|
# 'repository' is a Gitlab::Git::Repository
|
|
|
|
def initialize(repository)
|
2017-06-28 12:20:29 +00:00
|
|
|
@repository = repository
|
2017-04-07 09:19:20 +00:00
|
|
|
@gitaly_repo = repository.gitaly_repository
|
2017-06-16 16:42:41 +00:00
|
|
|
@storage = repository.storage
|
2017-02-16 02:08:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def default_branch_name
|
2017-04-07 09:19:20 +00:00
|
|
|
request = Gitaly::FindDefaultBranchNameRequest.new(repository: @gitaly_repo)
|
2017-07-18 07:59:36 +00:00
|
|
|
response = GitalyClient.call(@storage, :ref_service, :find_default_branch_name, request)
|
2017-06-16 16:42:41 +00:00
|
|
|
Gitlab::Git.branch_name(response.name)
|
2017-02-16 02:08:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def branch_names
|
2017-04-07 09:19:20 +00:00
|
|
|
request = Gitaly::FindAllBranchNamesRequest.new(repository: @gitaly_repo)
|
2017-07-18 07:59:36 +00:00
|
|
|
response = GitalyClient.call(@storage, :ref_service, :find_all_branch_names, request)
|
2017-06-28 12:20:29 +00:00
|
|
|
consume_refs_response(response) { |name| Gitlab::Git.branch_name(name) }
|
2017-02-16 02:08:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def tag_names
|
2017-04-07 09:19:20 +00:00
|
|
|
request = Gitaly::FindAllTagNamesRequest.new(repository: @gitaly_repo)
|
2017-07-18 07:59:36 +00:00
|
|
|
response = GitalyClient.call(@storage, :ref_service, :find_all_tag_names, request)
|
2017-06-28 12:20:29 +00:00
|
|
|
consume_refs_response(response) { |name| Gitlab::Git.tag_name(name) }
|
2017-02-16 02:08:30 +00:00
|
|
|
end
|
|
|
|
|
2017-03-28 17:14:48 +00:00
|
|
|
def find_ref_name(commit_id, ref_prefix)
|
2017-04-06 16:47:35 +00:00
|
|
|
request = Gitaly::FindRefNameRequest.new(
|
2017-05-11 20:26:51 +00:00
|
|
|
repository: @gitaly_repo,
|
2017-03-28 17:14:48 +00:00
|
|
|
commit_id: commit_id,
|
|
|
|
prefix: ref_prefix
|
|
|
|
)
|
2017-07-18 07:59:36 +00:00
|
|
|
encode!(GitalyClient.call(@storage, :ref_service, :find_ref_name, request).name.dup)
|
2017-03-28 17:14:48 +00:00
|
|
|
end
|
|
|
|
|
2017-04-19 09:06:20 +00:00
|
|
|
def count_tag_names
|
|
|
|
tag_names.count
|
|
|
|
end
|
|
|
|
|
|
|
|
def count_branch_names
|
|
|
|
branch_names.count
|
|
|
|
end
|
|
|
|
|
2017-03-17 19:36:46 +00:00
|
|
|
def local_branches(sort_by: nil)
|
|
|
|
request = Gitaly::FindLocalBranchesRequest.new(repository: @gitaly_repo)
|
|
|
|
request.sort_by = sort_by_param(sort_by) if sort_by
|
2017-07-18 07:59:36 +00:00
|
|
|
response = GitalyClient.call(@storage, :ref_service, :find_local_branches, request)
|
2017-06-16 16:42:41 +00:00
|
|
|
consume_branches_response(response)
|
2017-03-17 19:36:46 +00:00
|
|
|
end
|
|
|
|
|
2017-02-16 02:08:30 +00:00
|
|
|
private
|
|
|
|
|
2017-06-28 12:20:29 +00:00
|
|
|
def consume_refs_response(response)
|
|
|
|
response.flat_map { |message| message.names.map { |name| yield(name) } }
|
2017-02-16 02:08:30 +00:00
|
|
|
end
|
2017-03-17 19:36:46 +00:00
|
|
|
|
|
|
|
def sort_by_param(sort_by)
|
2017-06-28 19:38:00 +00:00
|
|
|
sort_by = 'name' if sort_by == 'name_asc'
|
|
|
|
|
2017-03-17 19:36:46 +00:00
|
|
|
enum_value = Gitaly::FindLocalBranchesRequest::SortBy.resolve(sort_by.upcase.to_sym)
|
|
|
|
raise ArgumentError, "Invalid sort_by key `#{sort_by}`" unless enum_value
|
|
|
|
enum_value
|
|
|
|
end
|
|
|
|
|
|
|
|
def consume_branches_response(response)
|
2017-06-28 12:20:29 +00: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 13:30:58 +00:00
|
|
|
gitaly_branch.commit_id,
|
|
|
|
commit_from_local_branches_response(gitaly_branch)
|
2017-06-28 12:20:29 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2017-03-17 19:36:46 +00:00
|
|
|
end
|
2017-07-14 13:30:58 +00: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 21:33:16 +00:00
|
|
|
author_name: response.commit_author.name.dup,
|
|
|
|
author_email: response.commit_author.email.dup,
|
2017-07-14 13:30:58 +00:00
|
|
|
committed_date: Time.at(response.commit_committer.date.seconds),
|
2017-06-23 21:33:16 +00:00
|
|
|
committer_name: response.commit_committer.name.dup,
|
|
|
|
committer_email: response.commit_committer.email.dup
|
2017-07-14 13:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Gitlab::Git::Commit.decorate(hash)
|
|
|
|
end
|
2017-02-16 02:08:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|