Class: Gitlab::GitalyClient::RepositoryService

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

Constant Summary collapse

MAX_MSG_SIZE =
128.kilobytes

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) ⇒ RepositoryService

Returns a new instance of RepositoryService.


10
11
12
13
14
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 10

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

Instance Method Details

#apply_gitattributes(revision) ⇒ Object


62
63
64
65
66
67
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 62

def apply_gitattributes(revision)
  request = Gitaly::ApplyGitattributesRequest.new(repository: @gitaly_repo, revision: encode_binary(revision))
  GitalyClient.call(@storage, :repository_service, :apply_gitattributes, request, timeout: GitalyClient.fast_timeout)
rescue GRPC::InvalidArgument => ex
  raise Gitlab::Git::Repository::InvalidRef, ex
end

#backup_custom_hooks(save_path) ⇒ Object


205
206
207
208
209
210
211
212
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 205

def backup_custom_hooks(save_path)
  gitaly_fetch_stream_to_file(
    save_path,
    :backup_custom_hooks,
    Gitaly::BackupCustomHooksRequest,
    GitalyClient.default_timeout
  )
end

#calculate_checksumObject


282
283
284
285
286
287
288
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 282

def calculate_checksum
  request  = Gitaly::CalculateChecksumRequest.new(repository: @gitaly_repo)
  response = GitalyClient.call(@storage, :repository_service, :calculate_checksum, request, timeout: GitalyClient.fast_timeout)
  response.checksum.presence
rescue GRPC::DataLoss => e
  raise Gitlab::Git::Repository::InvalidRepository, e
end

#create_bundle(save_path) ⇒ Object


196
197
198
199
200
201
202
203
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 196

def create_bundle(save_path)
  gitaly_fetch_stream_to_file(
    save_path,
    :create_bundle,
    Gitaly::CreateBundleRequest,
    GitalyClient.long_timeout
  )
end

#create_from_bundle(bundle_path) ⇒ Object


214
215
216
217
218
219
220
221
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 214

def create_from_bundle(bundle_path)
  gitaly_repo_stream_request(
    bundle_path,
    :create_repository_from_bundle,
    Gitaly::CreateRepositoryFromBundleRequest,
    GitalyClient.long_timeout
  )
end

#create_from_snapshot(http_url, http_auth) ⇒ Object


232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 232

def create_from_snapshot(http_url, http_auth)
  request = Gitaly::CreateRepositoryFromSnapshotRequest.new(
    repository: @gitaly_repo,
    http_url: http_url,
    http_auth: http_auth
  )

  GitalyClient.call(
    @storage,
    :repository_service,
    :create_repository_from_snapshot,
    request,
    timeout: GitalyClient.long_timeout
  )
end

#create_repository(default_branch = nil) ⇒ Object

rubocop: enable Metrics/ParameterLists


110
111
112
113
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 110

def create_repository(default_branch = nil)
  request = Gitaly::CreateRepositoryRequest.new(repository: @gitaly_repo, default_branch: default_branch)
  GitalyClient.call(@storage, :repository_service, :create_repository, request, timeout: GitalyClient.fast_timeout)
end

#disconnect_alternatesObject


312
313
314
315
316
317
318
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 312

def disconnect_alternates
  request = Gitaly::DisconnectGitAlternatesRequest.new(
    repository: @gitaly_repo
  )

  GitalyClient.call(@storage, :object_pool_service, :disconnect_git_alternates, request, timeout: GitalyClient.long_timeout)
end

#exists?Boolean

Returns:

  • (Boolean)

16
17
18
19
20
21
22
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 16

def exists?
  request = Gitaly::RepositoryExistsRequest.new(repository: @gitaly_repo)

  response = GitalyClient.call(@storage, :repository_service, :repository_exists, request, timeout: GitalyClient.fast_timeout)

  response.exists
end

#fetch_remote(url, refmap:, ssh_auth:, forced:, no_tags:, timeout:, prune: true, check_tags_changed: false, http_authorization_header: "") ⇒ Object

rubocop: disable Metrics/ParameterLists The `remote` parameter is going away soonish anyway, at which point the Rubocop warning can be enabled again.


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 81

def fetch_remote(url, refmap:, ssh_auth:, forced:, no_tags:, timeout:, prune: true, check_tags_changed: false, http_authorization_header: "")
  request = Gitaly::FetchRemoteRequest.new(
    repository: @gitaly_repo,
    force: forced,
    no_tags: no_tags,
    timeout: timeout,
    no_prune: !prune,
    check_tags_changed: check_tags_changed,
    remote_params: Gitaly::Remote.new(
      url: url,
      mirror_refmaps: Array.wrap(refmap).map(&:to_s),
      http_authorization_header: http_authorization_header
    )
  )

  if ssh_auth&.ssh_mirror_url?
    if ssh_auth.ssh_key_auth? && ssh_auth.ssh_private_key.present?
      request.ssh_key = ssh_auth.ssh_private_key
    end

    if ssh_auth.ssh_known_hosts.present?
      request.known_hosts = ssh_auth.ssh_known_hosts
    end
  end

  GitalyClient.call(@storage, :repository_service, :fetch_remote, request, timeout: GitalyClient.long_timeout)
end

#fetch_source_branch(source_repository, source_branch, local_ref) ⇒ Object


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 165

def fetch_source_branch(source_repository, source_branch, local_ref)
  request = Gitaly::FetchSourceBranchRequest.new(
    repository: @gitaly_repo,
    source_repository: source_repository.gitaly_repository,
    source_branch: source_branch.b,
    target_ref: local_ref.b
  )

  response = GitalyClient.call(
    @storage,
    :repository_service,
    :fetch_source_branch,
    request,
    timeout: GitalyClient.long_timeout,
    remote_storage: source_repository.storage
  )

  response.result
end

#find_merge_base(*revisions) ⇒ Object


122
123
124
125
126
127
128
129
130
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 122

def find_merge_base(*revisions)
  request = Gitaly::FindMergeBaseRequest.new(
    repository: @gitaly_repo,
    revisions: revisions.map { |r| encode_binary(r) }
  )

  response = GitalyClient.call(@storage, :repository_service, :find_merge_base, request, timeout: GitalyClient.fast_timeout)
  response.base.presence
end

#fork_repository(source_repository) ⇒ Object


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 132

def fork_repository(source_repository)
  request = Gitaly::CreateForkRequest.new(
    repository: @gitaly_repo,
    source_repository: source_repository.gitaly_repository
  )

  GitalyClient.call(
    @storage,
    :repository_service,
    :create_fork,
    request,
    remote_storage: source_repository.storage,
    timeout: GitalyClient.long_timeout
  )
end

#fsckObject


185
186
187
188
189
190
191
192
193
194
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 185

def fsck
  request = Gitaly::FsckRequest.new(repository: @gitaly_repo)
  response = GitalyClient.call(@storage, :repository_service, :fsck, request, timeout: GitalyClient.long_timeout)

  if response.error.empty?
    ["", 0]
  else
    [response.error.b, 1]
  end
end

#garbage_collect(create_bitmap, prune:) ⇒ Object


34
35
36
37
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 34

def garbage_collect(create_bitmap, prune:)
  request = Gitaly::GarbageCollectRequest.new(repository: @gitaly_repo, create_bitmap: create_bitmap, prune: prune)
  GitalyClient.call(@storage, :repository_service, :garbage_collect, request, timeout: GitalyClient.long_timeout)
end

#get_object_directory_sizeObject


55
56
57
58
59
60
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 55

def get_object_directory_size
  request = Gitaly::GetObjectDirectorySizeRequest.new(repository: @gitaly_repo)
  response = GitalyClient.call(@storage, :repository_service, :get_object_directory_size, request, timeout: GitalyClient.medium_timeout)

  response.size
end

#has_local_branches?Boolean

Returns:

  • (Boolean)

115
116
117
118
119
120
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 115

def has_local_branches?
  request = Gitaly::HasLocalBranchesRequest.new(repository: @gitaly_repo)
  response = GitalyClient.call(@storage, :repository_service, :has_local_branches, request, timeout: GitalyClient.fast_timeout)

  response.value
end

#import_repository(source, http_authorization_header: '', mirror: false) ⇒ Object


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 148

def import_repository(source, http_authorization_header: '', mirror: false)
  request = Gitaly::CreateRepositoryFromURLRequest.new(
    repository: @gitaly_repo,
    url: source,
    http_authorization_header: http_authorization_header,
    mirror: mirror
  )

  GitalyClient.call(
    @storage,
    :repository_service,
    :create_repository_from_url,
    request,
    timeout: GitalyClient.long_timeout
  )
end

#info_attributesObject


69
70
71
72
73
74
75
76
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 69

def info_attributes
  request = Gitaly::GetInfoAttributesRequest.new(repository: @gitaly_repo)

  response = GitalyClient.call(@storage, :repository_service, :get_info_attributes, request, timeout: GitalyClient.fast_timeout)
  response.each_with_object([]) do |message, attributes|
    attributes << message.attributes
  end.join
end

#license_short_nameObject


274
275
276
277
278
279
280
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 274

def license_short_name
  request = Gitaly::FindLicenseRequest.new(repository: @gitaly_repo)

  response = GitalyClient.call(@storage, :repository_service, :find_license, request, timeout: GitalyClient.fast_timeout)

  response.license_short_name.presence
end

#optimize_repositoryObject


24
25
26
27
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 24

def optimize_repository
  request = Gitaly::OptimizeRepositoryRequest.new(repository: @gitaly_repo)
  GitalyClient.call(@storage, :repository_service, :optimize_repository, request, timeout: GitalyClient.long_timeout)
end

#prune_unreachable_objectsObject


29
30
31
32
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 29

def prune_unreachable_objects
  request = Gitaly::PruneUnreachableObjectsRequest.new(repository: @gitaly_repo)
  GitalyClient.call(@storage, :repository_service, :prune_unreachable_objects, request, timeout: GitalyClient.long_timeout)
end

#raw_changes_between(from, to) ⇒ Object


290
291
292
293
294
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 290

def raw_changes_between(from, to)
  request = Gitaly::GetRawChangesRequest.new(repository: @gitaly_repo, from_revision: from, to_revision: to)

  GitalyClient.call(@storage, :repository_service, :get_raw_changes, request, timeout: GitalyClient.fast_timeout)
end

#removeObject


326
327
328
329
330
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 326

def remove
  request = Gitaly::RemoveRepositoryRequest.new(repository: @gitaly_repo)

  GitalyClient.call(@storage, :repository_service, :remove_repository, request, timeout: GitalyClient.long_timeout)
end

#rename(relative_path) ⇒ Object


320
321
322
323
324
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 320

def rename(relative_path)
  request = Gitaly::RenameRepositoryRequest.new(repository: @gitaly_repo, relative_path: relative_path)

  GitalyClient.call(@storage, :repository_service, :rename_repository, request, timeout: GitalyClient.fast_timeout)
end

#repack_full(create_bitmap) ⇒ Object


39
40
41
42
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 39

def repack_full(create_bitmap)
  request = Gitaly::RepackFullRequest.new(repository: @gitaly_repo, create_bitmap: create_bitmap)
  GitalyClient.call(@storage, :repository_service, :repack_full, request, timeout: GitalyClient.long_timeout)
end

#repack_incrementalObject


44
45
46
47
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 44

def repack_incremental
  request = Gitaly::RepackIncrementalRequest.new(repository: @gitaly_repo)
  GitalyClient.call(@storage, :repository_service, :repack_incremental, request, timeout: GitalyClient.long_timeout)
end

#replicate(source_repository) ⇒ Object


332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 332

def replicate(source_repository)
  request = Gitaly::ReplicateRepositoryRequest.new(
    repository: @gitaly_repo,
    source: source_repository.gitaly_repository
  )

  GitalyClient.call(
    @storage,
    :repository_service,
    :replicate_repository,
    request,
    remote_storage: source_repository.storage,
    timeout: GitalyClient.long_timeout
  )
end

#repository_sizeObject


49
50
51
52
53
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 49

def repository_size
  request = Gitaly::RepositorySizeRequest.new(repository: @gitaly_repo)
  response = GitalyClient.call(@storage, :repository_service, :repository_size, request, timeout: GitalyClient.long_timeout)
  response.size
end

#restore_custom_hooks(custom_hooks_path) ⇒ Object


223
224
225
226
227
228
229
230
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 223

def restore_custom_hooks(custom_hooks_path)
  gitaly_repo_stream_request(
    custom_hooks_path,
    :restore_custom_hooks,
    Gitaly::RestoreCustomHooksRequest,
    GitalyClient.default_timeout
  )
end

#search_files_by_content(ref, query, options = {}) ⇒ Object


301
302
303
304
305
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 301

def search_files_by_content(ref, query, options = {})
  request = Gitaly::SearchFilesByContentRequest.new(repository: @gitaly_repo, ref: ref, query: query)
  response = GitalyClient.call(@storage, :repository_service, :search_files_by_content, request, timeout: GitalyClient.default_timeout)
  search_results_from_response(response, options)
end

#search_files_by_name(ref, query) ⇒ Object


296
297
298
299
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 296

def search_files_by_name(ref, query)
  request = Gitaly::SearchFilesByNameRequest.new(repository: @gitaly_repo, ref: ref, query: query)
  GitalyClient.call(@storage, :repository_service, :search_files_by_name, request, timeout: GitalyClient.fast_timeout).flat_map(&:files)
end

#search_files_by_regexp(ref, filter) ⇒ Object


307
308
309
310
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 307

def search_files_by_regexp(ref, filter)
  request = Gitaly::SearchFilesByNameRequest.new(repository: @gitaly_repo, ref: ref, query: '.', filter: filter)
  GitalyClient.call(@storage, :repository_service, :search_files_by_name, request, timeout: GitalyClient.fast_timeout).flat_map(&:files)
end

#set_full_path(path) ⇒ Object


259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 259

def set_full_path(path)
  GitalyClient.call(
    @storage,
    :repository_service,
    :set_full_path,
    Gitaly::SetFullPathRequest.new(
      repository: @gitaly_repo,
      path: path
    ),
    timeout: GitalyClient.fast_timeout
  )

  nil
end

#write_ref(ref_path, ref, old_ref) ⇒ Object


248
249
250
251
252
253
254
255
256
257
# File 'lib/gitlab/gitaly_client/repository_service.rb', line 248

def write_ref(ref_path, ref, old_ref)
  request = Gitaly::WriteRefRequest.new(
    repository: @gitaly_repo,
    ref: ref_path.b,
    revision: ref.b
  )
  request.old_revision = old_ref.b unless old_ref.nil?

  GitalyClient.call(@storage, :repository_service, :write_ref, request, timeout: GitalyClient.fast_timeout)
end