Class: Gitlab::GitalyClient::RefService

Inherits:
Object
  • Object
show all
Includes:
EncodingHelper
Defined in:
lib/gitlab/gitaly_client/ref_service.rb

Constant Summary collapse

TAGS_SORT_KEY =
{
  'name' => Gitaly::FindAllTagsRequest::SortBy::Key::REFNAME,
  'updated' => Gitaly::FindAllTagsRequest::SortBy::Key::CREATORDATE
}.freeze
TAGS_SORT_DIRECTION =
{
  'asc' => Gitaly::SortDirection::ASCENDING,
  'desc' => Gitaly::SortDirection::DESCENDING
}.freeze

Constants included from EncodingHelper

EncodingHelper::ENCODING_CONFIDENCE_THRESHOLD, EncodingHelper::ESCAPED_CHARS, EncodingHelper::UNICODE_REPLACEMENT_CHARACTER

Instance Method Summary collapse

Methods included from EncodingHelper

#binary_io, #detect_binary?, #detect_encoding, #detect_libgit2_binary?, #encode!, #encode_binary, #encode_utf8, #encode_utf8_no_detect, #encode_utf8_with_replacement_character, #unquote_path

Constructor Details

#initialize(repository) ⇒ RefService

'repository' is a Gitlab::Git::Repository


19
20
21
22
23
# File 'lib/gitlab/gitaly_client/ref_service.rb', line 19

def initialize(repository)
  @repository = repository
  @gitaly_repo = repository.gitaly_repository
  @storage = repository.storage
end

Instance Method Details

#branch_namesObject


53
54
55
56
57
# File 'lib/gitlab/gitaly_client/ref_service.rb', line 53

def branch_names
  request = Gitaly::FindAllBranchNamesRequest.new(repository: @gitaly_repo)
  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) }
end

#branch_names_contains_sha(sha, limit: 0) ⇒ Object

Limit: 0 implies no limit, thus all tag names will be returned


150
151
152
153
154
155
156
157
158
159
# File 'lib/gitlab/gitaly_client/ref_service.rb', line 150

def branch_names_contains_sha(sha, limit: 0)
  request = Gitaly::ListBranchNamesContainingCommitRequest.new(
    repository: @gitaly_repo,
    commit_id: sha,
    limit: limit
  )

  response = GitalyClient.call(@storage, :ref_service, :list_branch_names_containing_commit, request, timeout: GitalyClient.medium_timeout)
  consume_ref_contains_sha_response(response, :branch_names)
end

#branchesObject


25
26
27
28
29
# File 'lib/gitlab/gitaly_client/ref_service.rb', line 25

def branches
  request = Gitaly::FindAllBranchesRequest.new(repository: @gitaly_repo)
  response = GitalyClient.call(@storage, :ref_service, :find_all_branches, request, timeout: GitalyClient.fast_timeout)
  consume_find_all_branches_response(response)
end

#count_branch_namesObject


69
70
71
# File 'lib/gitlab/gitaly_client/ref_service.rb', line 69

def count_branch_names
  branch_names.count
end

#count_tag_namesObject


65
66
67
# File 'lib/gitlab/gitaly_client/ref_service.rb', line 65

def count_tag_names
  tag_names.count
end

#default_branch_nameObject


47
48
49
50
51
# File 'lib/gitlab/gitaly_client/ref_service.rb', line 47

def default_branch_name
  request = Gitaly::FindDefaultBranchNameRequest.new(repository: @gitaly_repo)
  response = GitalyClient.call(@storage, :ref_service, :find_default_branch_name, request, timeout: GitalyClient.fast_timeout)
  Gitlab::Git.branch_name(response.name)
end

#delete_refs(refs: [], except_with_prefixes: []) ⇒ Object


125
126
127
128
129
130
131
132
133
134
135
# File 'lib/gitlab/gitaly_client/ref_service.rb', line 125

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) }
  )

  response = GitalyClient.call(@repository.storage, :ref_service, :delete_refs, request, timeout: GitalyClient.medium_timeout)

  raise Gitlab::Git::Repository::GitError, response.git_error if response.git_error.present?
end

#find_branch(branch_name) ⇒ Object


96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/gitlab/gitaly_client/ref_service.rb', line 96

def find_branch(branch_name)
  request = Gitaly::FindBranchRequest.new(
    repository: @gitaly_repo,
    name: encode_binary(branch_name)
  )

  response = GitalyClient.call(@repository.storage, :ref_service, :find_branch, request, timeout: GitalyClient.medium_timeout)
  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

#find_refs_by_oid(oid:, limit:) ⇒ Object


213
214
215
216
217
218
# File 'lib/gitlab/gitaly_client/ref_service.rb', line 213

def find_refs_by_oid(oid:, limit:)
  request = Gitaly::FindRefsByOIDRequest.new(repository: @gitaly_repo, sort_field: :refname, oid: oid, limit: limit)

  response = GitalyClient.call(@storage, :ref_service, :find_refs_by_oid, request, timeout: GitalyClient.medium_timeout)
  response&.refs&.to_a
end

#find_tag(tag_name) ⇒ Object


110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/gitlab/gitaly_client/ref_service.rb', line 110

def find_tag(tag_name)
  return if tag_name.blank?

  request = Gitaly::FindTagRequest.new(
    repository: @gitaly_repo,
    tag_name: encode_binary(tag_name)
  )

  response = GitalyClient.call(@repository.storage, :ref_service, :find_tag, request, timeout: GitalyClient.medium_timeout)
  tag = response.tag
  return unless tag

  Gitlab::Git::Tag.new(@repository, tag)
end

#get_tag_messages(tag_ids) ⇒ Object


161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/gitlab/gitaly_client/ref_service.rb', line 161

def get_tag_messages(tag_ids)
  request = Gitaly::GetTagMessagesRequest.new(repository: @gitaly_repo, tag_ids: tag_ids)
  messages = Hash.new { |h, k| h[k] = +''.b }
  current_tag_id = nil

  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?

    messages[current_tag_id] << rpc_message.message
  end

  messages
end

#get_tag_signatures(tag_ids) ⇒ Object


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/gitlab/gitaly_client/ref_service.rb', line 176

def get_tag_signatures(tag_ids)
  request = Gitaly::GetTagSignaturesRequest.new(repository: @gitaly_repo, tag_revisions: tag_ids)
  response = GitalyClient.call(@repository.storage, :ref_service, :get_tag_signatures, request, timeout: GitalyClient.fast_timeout)

  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

#list_refs(patterns = [Gitlab::Git::BRANCH_REF_PREFIX]) ⇒ Object


197
198
199
200
201
202
203
204
205
# File 'lib/gitlab/gitaly_client/ref_service.rb', line 197

def list_refs(patterns = [Gitlab::Git::BRANCH_REF_PREFIX])
  request = Gitaly::ListRefsRequest.new(
    repository: @gitaly_repo,
    patterns: patterns
  )

  response = GitalyClient.call(@storage, :ref_service, :list_refs, request, timeout: GitalyClient.fast_timeout)
  consume_list_refs_response(response)
end

#local_branches(sort_by: nil, pagination_params: nil) ⇒ Object


73
74
75
76
77
78
# File 'lib/gitlab/gitaly_client/ref_service.rb', line 73

def local_branches(sort_by: nil, pagination_params: nil)
  request = Gitaly::FindLocalBranchesRequest.new(repository: @gitaly_repo, pagination_params: pagination_params)
  request.sort_by = sort_local_branches_by_param(sort_by) if sort_by
  response = GitalyClient.call(@storage, :ref_service, :find_local_branches, request, timeout: GitalyClient.fast_timeout)
  consume_find_local_branches_response(response)
end

#merged_branches(branch_names = []) ⇒ Object


37
38
39
40
41
42
43
44
45
# File 'lib/gitlab/gitaly_client/ref_service.rb', line 37

def merged_branches(branch_names = [])
  request = Gitaly::FindAllBranchesRequest.new(
    repository: @gitaly_repo,
    merged_only: true,
    merged_branches: branch_names.map { |s| encode_binary(s) }
  )
  response = GitalyClient.call(@storage, :ref_service, :find_all_branches, request, timeout: GitalyClient.fast_timeout)
  consume_find_all_branches_response(response)
end

#pack_refsObject


207
208
209
210
211
# File 'lib/gitlab/gitaly_client/ref_service.rb', line 207

def pack_refs
  request = Gitaly::PackRefsRequest.new(repository: @gitaly_repo)

  GitalyClient.call(@storage, :ref_service, :pack_refs, request, timeout: GitalyClient.long_timeout)
end

#ref_exists?(ref_name) ⇒ Boolean

Returns:

  • (Boolean)

88
89
90
91
92
93
94
# File 'lib/gitlab/gitaly_client/ref_service.rb', line 88

def ref_exists?(ref_name)
  request = Gitaly::RefExistsRequest.new(repository: @gitaly_repo, ref: encode_binary(ref_name))
  response = GitalyClient.call(@storage, :ref_service, :ref_exists, request, timeout: GitalyClient.fast_timeout)
  response.value
rescue GRPC::InvalidArgument => e
  raise ArgumentError, e.message
end

#remote_branches(remote_name) ⇒ Object


31
32
33
34
35
# File 'lib/gitlab/gitaly_client/ref_service.rb', line 31

def remote_branches(remote_name)
  request = Gitaly::FindAllRemoteBranchesRequest.new(repository: @gitaly_repo, remote_name: remote_name)
  response = GitalyClient.call(@storage, :ref_service, :find_all_remote_branches, request, timeout: GitalyClient.medium_timeout)
  consume_find_all_remote_branches_response(remote_name, response)
end

#tag_namesObject


59
60
61
62
63
# File 'lib/gitlab/gitaly_client/ref_service.rb', line 59

def tag_names
  request = Gitaly::FindAllTagNamesRequest.new(repository: @gitaly_repo)
  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) }
end

#tag_names_contains_sha(sha, limit: 0) ⇒ Object

Limit: 0 implies no limit, thus all tag names will be returned


138
139
140
141
142
143
144
145
146
147
# File 'lib/gitlab/gitaly_client/ref_service.rb', line 138

def tag_names_contains_sha(sha, limit: 0)
  request = Gitaly::ListTagNamesContainingCommitRequest.new(
    repository: @gitaly_repo,
    commit_id: sha,
    limit: limit
  )

  response = GitalyClient.call(@storage, :ref_service, :list_tag_names_containing_commit, request, timeout: GitalyClient.medium_timeout)
  consume_ref_contains_sha_response(response, :tag_names)
end

#tags(sort_by: nil, pagination_params: nil) ⇒ Object


80
81
82
83
84
85
86
# File 'lib/gitlab/gitaly_client/ref_service.rb', line 80

def tags(sort_by: nil, pagination_params: nil)
  request = Gitaly::FindAllTagsRequest.new(repository: @gitaly_repo, pagination_params: pagination_params)
  request.sort_by = sort_tags_by_param(sort_by) if sort_by

  response = GitalyClient.call(@storage, :ref_service, :find_all_tags, request, timeout: GitalyClient.medium_timeout)
  consume_tags_response(response)
end