Class: Gitlab::GitalyClient::CommitService

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

Constant Summary

Constants included from EncodingHelper

EncodingHelper::ENCODING_CONFIDENCE_THRESHOLD

Instance Method Summary collapse

Methods included from EncodingHelper

#binary_io, #detect_binary?, #detect_libgit2_binary?, #encode!, #encode_binary, #encode_utf8

Constructor Details

#initialize(repository) ⇒ CommitService

Returns a new instance of CommitService.


8
9
10
11
# File 'lib/gitlab/gitaly_client/commit_service.rb', line 8

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

Instance Method Details

#ancestor?(ancestor_id, child_id) ⇒ Boolean

Returns:

  • (Boolean)

25
26
27
28
29
30
31
32
33
# File 'lib/gitlab/gitaly_client/commit_service.rb', line 25

def ancestor?(ancestor_id, child_id)
  request = Gitaly::CommitIsAncestorRequest.new(
    repository: @gitaly_repo,
    ancestor_id: ancestor_id,
    child_id: child_id
  )

  GitalyClient.call(@repository.storage, :commit_service, :commit_is_ancestor, request, timeout: GitalyClient.fast_timeout).value
end

#between(from, to) ⇒ Object


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

def between(from, to)
  request = Gitaly::CommitsBetweenRequest.new(
    repository: @gitaly_repo,
    from: from,
    to: to
  )

  response = GitalyClient.call(@repository.storage, :commit_service, :commits_between, request, timeout: GitalyClient.medium_timeout)
  consume_commits_response(response)
end

#commit_count(ref, options = {}) ⇒ Object


138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/gitlab/gitaly_client/commit_service.rb', line 138

def commit_count(ref, options = {})
  request = Gitaly::CountCommitsRequest.new(
    repository: @gitaly_repo,
    revision: encode_binary(ref),
    all: !!options[:all],
    first_parent: !!options[:first_parent]
  )
  request.after = Google::Protobuf::Timestamp.new(seconds: options[:after].to_i) if options[:after].present?
  request.before = Google::Protobuf::Timestamp.new(seconds: options[:before].to_i) if options[:before].present?
  request.path = encode_binary(options[:path]) if options[:path].present?
  request.max_count = options[:max_count] if options[:max_count].present?

  GitalyClient.call(@repository.storage, :commit_service, :count_commits, request, timeout: GitalyClient.medium_timeout).count
end

#commit_deltas(commit) ⇒ Object


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

def commit_deltas(commit)
  request = Gitaly::CommitDeltaRequest.new(diff_from_parent_request_params(commit))
  response = GitalyClient.call(@repository.storage, :diff_service, :commit_delta, request, timeout: GitalyClient.fast_timeout)
  response.flat_map { |msg| msg.deltas }
end

#commit_stats(revision) ⇒ Object


303
304
305
306
307
308
309
# File 'lib/gitlab/gitaly_client/commit_service.rb', line 303

def commit_stats(revision)
  request = Gitaly::CommitStatsRequest.new(
    repository: @gitaly_repo,
    revision: encode_binary(revision)
  )
  GitalyClient.call(@repository.storage, :commit_service, :commit_stats, request, timeout: GitalyClient.medium_timeout)
end

#commits_by_message(query, revision: '', path: '', limit: 1000, offset: 0, literal_pathspec: true) ⇒ Object


243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/gitlab/gitaly_client/commit_service.rb', line 243

def commits_by_message(query, revision: '', path: '', limit: 1000, offset: 0, literal_pathspec: true)
  request = Gitaly::CommitsByMessageRequest.new(
    repository: @gitaly_repo,
    query: query,
    revision: encode_binary(revision),
    path: encode_binary(path),
    limit: limit.to_i,
    offset: offset.to_i,
    global_options: parse_global_options!(literal_pathspec: literal_pathspec)
  )

  response = GitalyClient.call(@repository.storage, :commit_service, :commits_by_message, request, timeout: GitalyClient.medium_timeout)
  consume_commits_response(response)
end

#diff(from, to, options = {}) ⇒ Object


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/gitlab/gitaly_client/commit_service.rb', line 35

def diff(from, to, options = {})
  from_id = case from
            when NilClass
              Gitlab::Git::EMPTY_TREE_ID
            else
              if from.respond_to?(:oid)
                # This is meant to match a Rugged::Commit. This should be impossible in
                # the future.
                from.oid
              else
                from
              end
            end

  to_id = case to
          when NilClass
            Gitlab::Git::EMPTY_TREE_ID
          else
            if to.respond_to?(:oid)
              # This is meant to match a Rugged::Commit. This should be impossible in
              # the future.
              to.oid
            else
              to
            end
          end

  request_params = diff_between_commits_request_params(from_id, to_id, options)

  call_commit_diff(request_params, options)
end

#diff_from_parent(commit, options = {}) ⇒ Object


67
68
69
70
71
# File 'lib/gitlab/gitaly_client/commit_service.rb', line 67

def diff_from_parent(commit, options = {})
  request_params = diff_from_parent_request_params(commit, options)

  call_commit_diff(request_params, options)
end

#diff_stats(left_commit_sha, right_commit_sha) ⇒ Object


208
209
210
211
212
213
214
215
216
217
# File 'lib/gitlab/gitaly_client/commit_service.rb', line 208

def diff_stats(left_commit_sha, right_commit_sha)
  request = Gitaly::DiffStatsRequest.new(
    repository: @gitaly_repo,
    left_commit_id: left_commit_sha,
    right_commit_id: right_commit_sha
  )

  response = GitalyClient.call(@repository.storage, :diff_service, :diff_stats, request, timeout: GitalyClient.medium_timeout)
  response.flat_map(&:stats)
end

#diverging_commit_count(from, to, max_count:) ⇒ Object


153
154
155
156
157
158
159
160
161
162
# File 'lib/gitlab/gitaly_client/commit_service.rb', line 153

def diverging_commit_count(from, to, max_count:)
  request = Gitaly::CountDivergingCommitsRequest.new(
    repository: @gitaly_repo,
    from: encode_binary(from),
    to: encode_binary(to),
    max_count: max_count
  )
  response = GitalyClient.call(@repository.storage, :commit_service, :count_diverging_commits, request, timeout: GitalyClient.medium_timeout)
  [response.left_count, response.right_count]
end

#filter_shas_with_signatures(shas) ⇒ Object


335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/gitlab/gitaly_client/commit_service.rb', line 335

def filter_shas_with_signatures(shas)
  request = Gitaly::FilterShasWithSignaturesRequest.new(repository: @gitaly_repo)

  enum = Enumerator.new do |y|
    shas.each_slice(20) do |revs|
      request.shas = encode_repeated(revs)

      y.yield request

      request = Gitaly::FilterShasWithSignaturesRequest.new
    end
  end

  response = GitalyClient.call(@repository.storage, :commit_service, :filter_shas_with_signatures, enum, timeout: GitalyClient.fast_timeout)
  response.flat_map do |msg|
    msg.shas.map { |sha| EncodingHelper.encode!(sha) }
  end
end

#find_all_commits(opts = {}) ⇒ Object


219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/gitlab/gitaly_client/commit_service.rb', line 219

def find_all_commits(opts = {})
  request = Gitaly::FindAllCommitsRequest.new(
    repository: @gitaly_repo,
    revision: opts[:ref].to_s,
    max_count: opts[:max_count].to_i,
    skip: opts[:skip].to_i
  )
  request.order = opts[:order].upcase if opts[:order].present?

  response = GitalyClient.call(@repository.storage, :commit_service, :find_all_commits, request, timeout: GitalyClient.medium_timeout)
  consume_commits_response(response)
end

#find_commit(revision) ⇒ Object


276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/gitlab/gitaly_client/commit_service.rb', line 276

def find_commit(revision)
  return call_find_commit(revision) unless Gitlab::SafeRequestStore.active?

  # We don't use Gitlab::SafeRequestStore.fetch(key) { ... } directly
  # because `revision` can be a branch name, so we can't use it as a key
  # as it could point to another commit later on (happens a lot in
  # tests).
  key = {
    storage: @gitaly_repo.storage_name,
    relative_path: @gitaly_repo.relative_path,
    commit_id: revision
  }
  return Gitlab::SafeRequestStore[key] if Gitlab::SafeRequestStore.exist?(key)

  commit = call_find_commit(revision)

  if GitalyClient.ref_name_caching_allowed?
    Gitlab::SafeRequestStore[key] = commit
    return commit
  end

  return unless commit

  key[:commit_id] = commit.id
  Gitlab::SafeRequestStore[key] = commit
end

#find_commits(options) ⇒ Object


311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/gitlab/gitaly_client/commit_service.rb', line 311

def find_commits(options)
  request = Gitaly::FindCommitsRequest.new(
    repository:   @gitaly_repo,
    limit:        options[:limit],
    offset:       options[:offset],
    follow:       options[:follow],
    skip_merges:  options[:skip_merges],
    all:          !!options[:all],
    first_parent: !!options[:first_parent],
    global_options: parse_global_options!(options),
    disable_walk: true # This option is deprecated. The 'walk' implementation is being removed.
  )
  request.after    = GitalyClient.timestamp(options[:after]) if options[:after]
  request.before   = GitalyClient.timestamp(options[:before]) if options[:before]
  request.revision = encode_binary(options[:ref]) if options[:ref]
  request.author   = encode_binary(options[:author]) if options[:author]
  request.order    = options[:order].upcase.sub('DEFAULT', 'NONE') if options[:order].present?

  request.paths = encode_repeated(Array(options[:path])) if options[:path].present?

  response = GitalyClient.call(@repository.storage, :commit_service, :find_commits, request, timeout: GitalyClient.medium_timeout)
  consume_commits_response(response)
end

#get_commit_messages(commit_ids) ⇒ Object


373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/gitlab/gitaly_client/commit_service.rb', line 373

def get_commit_messages(commit_ids)
  request = Gitaly::GetCommitMessagesRequest.new(repository: @gitaly_repo, commit_ids: commit_ids)
  response = GitalyClient.call(@repository.storage, :commit_service, :get_commit_messages, request, timeout: GitalyClient.fast_timeout)

  messages = Hash.new { |h, k| h[k] = +''.b }
  current_commit_id = nil

  response.each do |rpc_message|
    current_commit_id = rpc_message.commit_id if rpc_message.commit_id.present?

    messages[current_commit_id] << rpc_message.message
  end

  messages
end

#get_commit_signatures(commit_ids) ⇒ Object


354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/gitlab/gitaly_client/commit_service.rb', line 354

def get_commit_signatures(commit_ids)
  request = Gitaly::GetCommitSignaturesRequest.new(repository: @gitaly_repo, commit_ids: commit_ids)
  response = GitalyClient.call(@repository.storage, :commit_service, :get_commit_signatures, request, timeout: GitalyClient.fast_timeout)

  signatures = Hash.new { |h, k| h[k] = [+''.b, +''.b] }
  current_commit_id = nil

  response.each do |message|
    current_commit_id = message.commit_id if message.commit_id.present?

    signatures[current_commit_id].first << message.signature
    signatures[current_commit_id].last << message.signed_text
  end

  signatures
rescue GRPC::InvalidArgument => ex
  raise ArgumentError, ex
end

#languages(ref = nil) ⇒ Object


258
259
260
261
262
263
# File 'lib/gitlab/gitaly_client/commit_service.rb', line 258

def languages(ref = nil)
  request = Gitaly::CommitLanguagesRequest.new(repository: @gitaly_repo, revision: ref || '')
  response = GitalyClient.call(@repository.storage, :commit_service, :commit_languages, request, timeout: GitalyClient.long_timeout)

  response.languages.map { |l| { value: l.share.round(2), label: l.name, color: l.color, highlight: l.color } }
end

#last_commit_for_path(revision, path, literal_pathspec: false) ⇒ Object


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

def last_commit_for_path(revision, path, literal_pathspec: false)
  request = Gitaly::LastCommitForPathRequest.new(
    repository: @gitaly_repo,
    revision: encode_binary(revision),
    path: encode_binary(path.to_s),
    global_options: parse_global_options!(literal_pathspec: literal_pathspec)
  )

  gitaly_commit = GitalyClient.call(@repository.storage, :commit_service, :last_commit_for_path, request, timeout: GitalyClient.fast_timeout).commit
  return unless gitaly_commit

  Gitlab::Git::Commit.new(@repository, gitaly_commit)
end

#list_commits_by_oid(oids) ⇒ Object


232
233
234
235
236
237
238
239
240
241
# File 'lib/gitlab/gitaly_client/commit_service.rb', line 232

def list_commits_by_oid(oids)
  return [] if oids.empty?

  request = Gitaly::ListCommitsByOidRequest.new(repository: @gitaly_repo, oid: oids)

  response = GitalyClient.call(@repository.storage, :commit_service, :list_commits_by_oid, request, timeout: GitalyClient.medium_timeout)
  consume_commits_response(response)
rescue GRPC::NotFound # If no repository is found, happens mainly during testing
  []
end

#list_commits_by_ref_name(refs) ⇒ Object


389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/gitlab/gitaly_client/commit_service.rb', line 389

def list_commits_by_ref_name(refs)
  request = Gitaly::ListCommitsByRefNameRequest
    .new(repository: @gitaly_repo, ref_names: refs.map { |ref| encode_binary(ref) })

  response = GitalyClient.call(@repository.storage, :commit_service, :list_commits_by_ref_name, request, timeout: GitalyClient.medium_timeout)

  commit_refs = response.flat_map do |message|
    message.commit_refs.map do |commit_ref|
      [encode_utf8(commit_ref.ref_name), Gitlab::Git::Commit.new(@repository, commit_ref.commit)]
    end
  end

  Hash[commit_refs]
end

#list_last_commits_for_tree(revision, path, offset: 0, limit: 25, literal_pathspec: false) ⇒ Object


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

def list_last_commits_for_tree(revision, path, offset: 0, limit: 25, literal_pathspec: false)
  request = Gitaly::ListLastCommitsForTreeRequest.new(
    repository: @gitaly_repo,
    revision: encode_binary(revision),
    path: encode_binary(path.to_s),
    offset: offset,
    limit: limit,
    global_options: parse_global_options!(literal_pathspec: literal_pathspec)
  )

  response = GitalyClient.call(@repository.storage, :commit_service, :list_last_commits_for_tree, request, timeout: GitalyClient.medium_timeout)

  response.each_with_object({}) do |gitaly_response, hsh|
    gitaly_response.commits.each do |commit_for_tree|
      hsh[commit_for_tree.path_bytes] = Gitlab::Git::Commit.new(@repository, commit_for_tree.commit)
    end
  end
end

#ls_files(revision) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
# File 'lib/gitlab/gitaly_client/commit_service.rb', line 13

def ls_files(revision)
  request = Gitaly::ListFilesRequest.new(
    repository: @gitaly_repo,
    revision: encode_binary(revision)
  )

  response = GitalyClient.call(@repository.storage, :commit_service, :list_files, request, timeout: GitalyClient.medium_timeout)
  response.flat_map do |msg|
    msg.paths.map { |d| EncodingHelper.encode!(d.dup) }
  end
end

#raw_blame(revision, path) ⇒ Object


265
266
267
268
269
270
271
272
273
274
# File 'lib/gitlab/gitaly_client/commit_service.rb', line 265

def raw_blame(revision, path)
  request = Gitaly::RawBlameRequest.new(
    repository: @gitaly_repo,
    revision: encode_binary(revision),
    path: encode_binary(path)
  )

  response = GitalyClient.call(@repository.storage, :commit_service, :raw_blame, request, timeout: GitalyClient.medium_timeout)
  response.reduce([]) { |memo, msg| memo << msg.data }.join
end

#tree_entries(repository, revision, path, recursive) ⇒ Object


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/gitlab/gitaly_client/commit_service.rb', line 112

def tree_entries(repository, revision, path, recursive)
  request = Gitaly::GetTreeEntriesRequest.new(
    repository: @gitaly_repo,
    revision: encode_binary(revision),
    path: path.present? ? encode_binary(path) : '.',
    recursive: recursive
  )

  response = GitalyClient.call(@repository.storage, :commit_service, :get_tree_entries, request, timeout: GitalyClient.medium_timeout)

  response.flat_map do |message|
    message.entries.map do |gitaly_tree_entry|
      Gitlab::Git::Tree.new(
        id: gitaly_tree_entry.oid,
        root_id: gitaly_tree_entry.root_oid,
        type: gitaly_tree_entry.type.downcase,
        mode: gitaly_tree_entry.mode.to_s(8),
        name: File.basename(gitaly_tree_entry.path),
        path: encode_binary(gitaly_tree_entry.path),
        flat_path: encode_binary(gitaly_tree_entry.flat_path),
        commit_id: gitaly_tree_entry.commit_oid
      )
    end
  end
end

#tree_entry(ref, path, limit = nil) ⇒ Object


79
80
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
108
109
110
# File 'lib/gitlab/gitaly_client/commit_service.rb', line 79

def tree_entry(ref, path, limit = nil)
  if Pathname.new(path).cleanpath.to_s.start_with?('../')
    # The TreeEntry RPC should return an empty response in this case but in
    # Gitaly 0.107.0 and earlier we get an exception instead. This early return
    # saves us a Gitaly roundtrip while also avoiding the exception.
    return
  end

  request = Gitaly::TreeEntryRequest.new(
    repository: @gitaly_repo,
    revision: encode_binary(ref),
    path: encode_binary(path),
    limit: limit.to_i
  )

  response = GitalyClient.call(@repository.storage, :commit_service, :tree_entry, request, timeout: GitalyClient.medium_timeout)

  entry = nil
  data = []
  response.each do |msg|
    if entry.nil?
      entry = msg

      break unless entry.type == :BLOB
    end

    data << msg.data
  end
  entry.data = data.join

  entry unless entry.oid.blank?
end