2017-02-15 21:08:30 -05:00
|
|
|
module Gitlab
|
|
|
|
module GitalyClient
|
|
|
|
class Ref
|
2017-04-07 05:19:20 -04:00
|
|
|
# 'repository' is a Gitlab::Git::Repository
|
|
|
|
def initialize(repository)
|
|
|
|
@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
|
|
|
|
|
|
|
|
def default_branch_name
|
2017-04-07 05:19:20 -04:00
|
|
|
request = Gitaly::FindDefaultBranchNameRequest.new(repository: @gitaly_repo)
|
2017-06-16 12:42:41 -04:00
|
|
|
response = GitalyClient.call(@storage, :ref, :find_default_branch_name, request)
|
|
|
|
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)
|
2017-06-16 12:42:41 -04:00
|
|
|
response = GitalyClient.call(@storage, :ref, :find_all_branch_names, request)
|
|
|
|
consume_refs_response(response, prefix: 'refs/heads/')
|
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)
|
2017-06-16 12:42:41 -04:00
|
|
|
response = GitalyClient.call(@storage, :ref, :find_all_tag_names, request)
|
|
|
|
consume_refs_response(response, prefix: 'refs/tags/')
|
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-06-16 12:42:41 -04:00
|
|
|
GitalyClient.call(@storage, :ref, :find_ref_name, request).name
|
2017-03-28 13:14:48 -04:00
|
|
|
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
|
|
|
|
|
2017-03-17 15:36:46 -04: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-06-16 12:42:41 -04:00
|
|
|
response = GitalyClient.call(@storage, :ref, :find_local_branches, request)
|
|
|
|
consume_branches_response(response)
|
2017-03-17 15:36:46 -04:00
|
|
|
end
|
|
|
|
|
2017-02-15 21:08:30 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def consume_refs_response(response, prefix:)
|
|
|
|
response.flat_map do |r|
|
|
|
|
r.names.map { |name| name.sub(/\A#{Regexp.escape(prefix)}/, '') }
|
|
|
|
end
|
|
|
|
end
|
2017-03-17 15:36:46 -04:00
|
|
|
|
|
|
|
def sort_by_param(sort_by)
|
|
|
|
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)
|
|
|
|
response.flat_map { |r| r.branches }
|
|
|
|
end
|
2017-02-15 21:08:30 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|