summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/gitaly_client/commit_service_spec.rb
blob: 50a0f20e775f10b6212b8c6dfdd5448514f89723 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
66
67
68
69
70
71
72
73
74
75
76
77
78
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
111
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::GitalyClient::CommitService do
  let(:project) { create(:project, :repository) }
  let(:storage_name) { project.repository_storage }
  let(:relative_path) { project.disk_path + '.git' }
  let(:repository) { project.repository }
  let(:repository_message) { repository.gitaly_repository }
  let(:revision) { '913c66a37b4a45b9769037c55c2d238bd0942d2e' }
  let(:commit) { project.commit(revision) }
  let(:client) { described_class.new(repository) }

  describe '#diff_from_parent' do
    context 'when a commit has a parent' do
      it 'sends an RPC request with the parent ID as left commit' do
        request = Gitaly::CommitDiffRequest.new(
          repository: repository_message,
          left_commit_id: 'cfe32cf61b73a0d5e9f13e774abde7ff789b1660',
          right_commit_id: commit.id,
          collapse_diffs: false,
          enforce_limits: true,
          # Tests limitation parameters explicitly
          max_files: 100,
          max_lines: 5000,
          max_bytes: 512000,
          safe_max_files: 100,
          safe_max_lines: 5000,
          safe_max_bytes: 512000,
          max_patch_bytes: 204800
        )

        expect_any_instance_of(Gitaly::DiffService::Stub).to receive(:commit_diff).with(request, kind_of(Hash))

        client.diff_from_parent(commit)
      end
    end

    context 'when a commit does not have a parent' do
      it 'sends an RPC request with empty tree ref as left commit' do
        initial_commit = project.commit('1a0b36b3cdad1d2ee32457c102a8c0b7056fa863').raw
        request        = Gitaly::CommitDiffRequest.new(
          repository: repository_message,
          left_commit_id: Gitlab::Git::EMPTY_TREE_ID,
          right_commit_id: initial_commit.id,
          collapse_diffs: false,
          enforce_limits: true,
          # Tests limitation parameters explicitly
          max_files: 100,
          max_lines: 5000,
          max_bytes: 512000,
          safe_max_files: 100,
          safe_max_lines: 5000,
          safe_max_bytes: 512000,
          max_patch_bytes: 204800
        )

        expect_any_instance_of(Gitaly::DiffService::Stub).to receive(:commit_diff).with(request, kind_of(Hash))

        client.diff_from_parent(initial_commit)
      end
    end

    it 'returns a Gitlab::GitalyClient::DiffStitcher' do
      ret = client.diff_from_parent(commit)

      expect(ret).to be_kind_of(Gitlab::GitalyClient::DiffStitcher)
    end

    it 'encodes paths correctly' do
      expect { client.diff_from_parent(commit, paths: ['encoding/test.txt', 'encoding/テスト.txt', nil]) }.not_to raise_error
    end
  end

  describe '#commit_deltas' do
    context 'when a commit has a parent' do
      it 'sends an RPC request with the parent ID as left commit' do
        request = Gitaly::CommitDeltaRequest.new(
          repository: repository_message,
          left_commit_id: 'cfe32cf61b73a0d5e9f13e774abde7ff789b1660',
          right_commit_id: commit.id
        )

        expect_any_instance_of(Gitaly::DiffService::Stub).to receive(:commit_delta).with(request, kind_of(Hash)).and_return([])

        client.commit_deltas(commit)
      end
    end

    context 'when a commit does not have a parent' do
      it 'sends an RPC request with empty tree ref as left commit' do
        initial_commit = project.commit('1a0b36b3cdad1d2ee32457c102a8c0b7056fa863')
        request        = Gitaly::CommitDeltaRequest.new(
          repository: repository_message,
          left_commit_id: Gitlab::Git::EMPTY_TREE_ID,
          right_commit_id: initial_commit.id
        )

        expect_any_instance_of(Gitaly::DiffService::Stub).to receive(:commit_delta).with(request, kind_of(Hash)).and_return([])

        client.commit_deltas(initial_commit)
      end
    end
  end

  describe '#diff_stats' do
    let(:left_commit_id) { 'master' }
    let(:right_commit_id) { 'cfe32cf61b73a0d5e9f13e774abde7ff789b1660' }

    it 'sends an RPC request and returns the stats' do
      request = Gitaly::DiffStatsRequest.new(repository: repository_message,
                                             left_commit_id: left_commit_id,
                                             right_commit_id: right_commit_id)

      diff_stat_response = Gitaly::DiffStatsResponse.new(
        stats: [{ additions: 1, deletions: 2, path: 'test' }])

      expect_any_instance_of(Gitaly::DiffService::Stub).to receive(:diff_stats)
        .with(request, kind_of(Hash)).and_return([diff_stat_response])

      returned_value = described_class.new(repository).diff_stats(left_commit_id, right_commit_id)

      expect(returned_value).to eq(diff_stat_response.stats)
    end
  end

  describe '#find_changed_paths' do
    let(:commits) { %w[1a0b36b3cdad1d2ee32457c102a8c0b7056fa863 cfe32cf61b73a0d5e9f13e774abde7ff789b1660] }

    it 'sends an RPC request and returns the stats' do
      request = Gitaly::FindChangedPathsRequest.new(repository: repository_message,
                                                    commits: commits)

      changed_paths_response = Gitaly::FindChangedPathsResponse.new(
        paths: [{
          path: "app/assets/javascripts/boards/components/project_select.vue",
          status: :MODIFIED
        }])

      expect_any_instance_of(Gitaly::DiffService::Stub).to receive(:find_changed_paths)
        .with(request, kind_of(Hash)).and_return([changed_paths_response])

      returned_value = described_class.new(repository).find_changed_paths(commits)
      mapped_expected_value = changed_paths_response.paths.map { |path| Gitlab::Git::ChangedPath.new(status: path.status, path: path.path) }

      expect(returned_value.as_json).to eq(mapped_expected_value.as_json)
    end
  end

  describe '#tree_entries' do
    subject { client.tree_entries(repository, revision, path, recursive, pagination_params) }

    let(:path) { '/' }
    let(:recursive) { false }
    let(:pagination_params) { nil }

    it 'sends a get_tree_entries message' do
      expect_any_instance_of(Gitaly::CommitService::Stub)
        .to receive(:get_tree_entries)
        .with(gitaly_request_with_path(storage_name, relative_path), kind_of(Hash))
        .and_return([])

      is_expected.to eq([[], nil])
    end

    context 'with UTF-8 params strings' do
      let(:revision) { "branch\u011F" }
      let(:path) { "foo/\u011F.txt" }

      it 'handles string encodings correctly' do
        expect_any_instance_of(Gitaly::CommitService::Stub)
          .to receive(:get_tree_entries)
          .with(gitaly_request_with_path(storage_name, relative_path), kind_of(Hash))
          .and_return([])

        is_expected.to eq([[], nil])
      end
    end

    context 'with pagination parameters' do
      let(:pagination_params) { { limit: 3, page_token: nil } }

      it 'responds with a pagination cursor' do
        pagination_cursor = Gitaly::PaginationCursor.new(next_cursor: 'aabbccdd')
        response = Gitaly::GetTreeEntriesResponse.new(
          entries: [],
          pagination_cursor: pagination_cursor
        )

        expect_any_instance_of(Gitaly::CommitService::Stub)
          .to receive(:get_tree_entries)
          .with(gitaly_request_with_path(storage_name, relative_path), kind_of(Hash))
          .and_return([response])

        is_expected.to eq([[], pagination_cursor])
      end
    end
  end

  describe '#commit_count' do
    before do
      expect_any_instance_of(Gitaly::CommitService::Stub)
        .to receive(:count_commits)
        .with(gitaly_request_with_path(storage_name, relative_path),
              kind_of(Hash))
        .and_return([])
    end

    it 'sends a commit_count message' do
      client.commit_count(revision)
    end

    context 'with UTF-8 params strings' do
      let(:revision) { "branch\u011F" }
      let(:path) { "foo/\u011F.txt" }

      it 'handles string encodings correctly' do
        client.commit_count(revision, path: path)
      end
    end
  end

  describe '#find_commit' do
    let(:revision) { Gitlab::Git::EMPTY_TREE_ID }

    it 'sends an RPC request' do
      request = Gitaly::FindCommitRequest.new(
        repository: repository_message, revision: revision
      )

      expect_any_instance_of(Gitaly::CommitService::Stub).to receive(:find_commit)
        .with(request, kind_of(Hash)).and_return(double(commit: nil))

      described_class.new(repository).find_commit(revision)
    end

    describe 'caching', :request_store do
      let(:commit_dbl) { double(id: 'f01b' * 10) }

      context 'when passed revision is a branch name' do
        it 'calls Gitaly' do
          expect_any_instance_of(Gitaly::CommitService::Stub).to receive(:find_commit).twice.and_return(double(commit: commit_dbl))

          commit = nil
          2.times { commit = described_class.new(repository).find_commit('master') }

          expect(commit).to eq(commit_dbl)
        end
      end

      context 'when passed revision is a commit ID' do
        it 'returns a cached commit' do
          expect_any_instance_of(Gitaly::CommitService::Stub).to receive(:find_commit).once.and_return(double(commit: commit_dbl))

          commit = nil
          2.times { commit = described_class.new(repository).find_commit('f01b' * 10) }

          expect(commit).to eq(commit_dbl)
        end
      end

      context 'when caching of the ref name is enabled' do
        it 'caches negative entries' do
          expect_any_instance_of(Gitaly::CommitService::Stub).to receive(:find_commit).once.and_return(double(commit: nil))

          commit = nil
          2.times do
            ::Gitlab::GitalyClient.allow_ref_name_caching do
              commit = described_class.new(repository).find_commit('master')
            end
          end

          expect(commit).to eq(nil)
        end

        it 'returns a cached commit' do
          expect_any_instance_of(Gitaly::CommitService::Stub).to receive(:find_commit).once.and_return(double(commit: commit_dbl))

          commit = nil
          2.times do
            ::Gitlab::GitalyClient.allow_ref_name_caching do
              commit = described_class.new(repository).find_commit('master')
            end
          end

          expect(commit).to eq(commit_dbl)
        end
      end
    end
  end

  describe '#list_commits' do
    let(:revisions) { 'master' }
    let(:reverse) { false }
    let(:pagination_params) { nil }

    shared_examples 'a ListCommits request' do
      before do
        ::Gitlab::GitalyClient.clear_stubs!
      end

      it 'sends a list_commits message' do
        expect_next_instance_of(Gitaly::CommitService::Stub) do |service|
          expected_request = gitaly_request_with_params(
            Array.wrap(revisions),
            reverse: reverse,
            pagination_params: pagination_params
          )

          expect(service).to receive(:list_commits).with(expected_request, kind_of(Hash)).and_return([])
        end

        client.list_commits(revisions, reverse: reverse, pagination_params: pagination_params)
      end
    end

    it_behaves_like 'a ListCommits request'

    context 'with multiple revisions' do
      let(:revisions) { %w[master --not --all] }

      it_behaves_like 'a ListCommits request'
    end

    context 'with reverse: true' do
      let(:reverse) { true }

      it_behaves_like 'a ListCommits request'
    end

    context 'with pagination params' do
      let(:pagination_params) { { limit: 1, page_token: 'foo' } }

      it_behaves_like 'a ListCommits request'
    end
  end

  describe '#list_new_commits' do
    let(:revisions) { [revision] }
    let(:gitaly_commits) { create_list(:gitaly_commit, 3) }
    let(:commits) { gitaly_commits.map { |c| Gitlab::Git::Commit.new(repository, c) }}

    subject { client.list_new_commits(revisions, allow_quarantine: allow_quarantine) }

    shared_examples 'a #list_all_commits message' do
      it 'sends a list_all_commits message' do
        expected_repository = repository.gitaly_repository.dup
        expected_repository.git_alternate_object_directories = Google::Protobuf::RepeatedField.new(:string)

        expect_next_instance_of(Gitaly::CommitService::Stub) do |service|
          expect(service).to receive(:list_all_commits)
            .with(gitaly_request_with_params(repository: expected_repository), kind_of(Hash))
            .and_return([Gitaly::ListAllCommitsResponse.new(commits: gitaly_commits)])
        end

        expect(subject).to eq(commits)
      end
    end

    shared_examples 'a #list_commits message' do
      it 'sends a list_commits message' do
        expect_next_instance_of(Gitaly::CommitService::Stub) do |service|
          expect(service).to receive(:list_commits)
            .with(gitaly_request_with_params(revisions: revisions + %w[--not --all]), kind_of(Hash))
            .and_return([Gitaly::ListCommitsResponse.new(commits: gitaly_commits)])
        end

        expect(subject).to eq(commits)
      end
    end

    before do
      ::Gitlab::GitalyClient.clear_stubs!

      allow(Gitlab::Git::HookEnv)
        .to receive(:all)
        .with(repository.gl_repository)
        .and_return(git_env)
    end

    context 'with hook environment' do
      let(:git_env) do
        {
          'GIT_OBJECT_DIRECTORY_RELATIVE' => '.git/objects',
          'GIT_ALTERNATE_OBJECT_DIRECTORIES_RELATIVE' => ['/dir/one', '/dir/two']
        }
      end

      context 'with allowed quarantine' do
        let(:allow_quarantine) { true }

        it_behaves_like 'a #list_all_commits message'
      end

      context 'with disallowed quarantine' do
        let(:allow_quarantine) { false }

        it_behaves_like 'a #list_commits message'
      end
    end

    context 'without hook environment' do
      let(:git_env) do
        {
          'GIT_OBJECT_DIRECTORY_RELATIVE' => '',
          'GIT_ALTERNATE_OBJECT_DIRECTORIES_RELATIVE' => []
        }
      end

      context 'with allowed quarantine' do
        let(:allow_quarantine) { true }

        it_behaves_like 'a #list_commits message'
      end

      context 'with disallowed quarantine' do
        let(:allow_quarantine) { false }

        it_behaves_like 'a #list_commits message'
      end
    end
  end

  describe '#commit_stats' do
    let(:request) do
      Gitaly::CommitStatsRequest.new(
        repository: repository_message, revision: revision
      )
    end

    let(:response) do
      Gitaly::CommitStatsResponse.new(
        oid: revision,
        additions: 11,
        deletions: 15
      )
    end

    subject { described_class.new(repository).commit_stats(revision) }

    it 'sends an RPC request' do
      expect_any_instance_of(Gitaly::CommitService::Stub).to receive(:commit_stats)
        .with(request, kind_of(Hash)).and_return(response)

      expect(subject.additions).to eq(11)
      expect(subject.deletions).to eq(15)
    end
  end

  describe '#find_commits' do
    it 'sends an RPC request with NONE when default' do
      request = Gitaly::FindCommitsRequest.new(
        repository: repository_message,
        disable_walk: true,
        order: 'NONE',
        global_options: Gitaly::GlobalOptions.new(literal_pathspecs: false)
      )

      expect_any_instance_of(Gitaly::CommitService::Stub).to receive(:find_commits)
        .with(request, kind_of(Hash)).and_return([])

      client.find_commits(order: 'default')
    end

    it 'sends an RPC request' do
      request = Gitaly::FindCommitsRequest.new(
        repository: repository_message,
        disable_walk: true,
        order: 'TOPO',
        global_options: Gitaly::GlobalOptions.new(literal_pathspecs: false)
      )

      expect_any_instance_of(Gitaly::CommitService::Stub).to receive(:find_commits)
        .with(request, kind_of(Hash)).and_return([])

      client.find_commits(order: 'topo')
    end

    it 'sends an RPC request with an author' do
      request = Gitaly::FindCommitsRequest.new(
        repository: repository_message,
        disable_walk: true,
        order: 'NONE',
        author: "Billy Baggins <bilbo@shire.com>",
        global_options: Gitaly::GlobalOptions.new(literal_pathspecs: false)
      )

      expect_any_instance_of(Gitaly::CommitService::Stub).to receive(:find_commits)
        .with(request, kind_of(Hash)).and_return([])

      client.find_commits(order: 'default', author: "Billy Baggins <bilbo@shire.com>")
    end
  end

  describe '#commits_by_message' do
    shared_examples 'a CommitsByMessageRequest' do
      let(:commits) { create_list(:gitaly_commit, 2) }

      before do
        request = Gitaly::CommitsByMessageRequest.new(
          repository: repository_message,
          query: query,
          revision: (options[:revision] || '').dup.force_encoding(Encoding::ASCII_8BIT),
          path: (options[:path] || '').dup.force_encoding(Encoding::ASCII_8BIT),
          limit: (options[:limit] || 1000).to_i,
          offset: (options[:offset] || 0).to_i,
          global_options: Gitaly::GlobalOptions.new(literal_pathspecs: true)
        )

        allow_any_instance_of(Gitaly::CommitService::Stub)
          .to receive(:commits_by_message)
          .with(request, kind_of(Hash))
          .and_return([Gitaly::CommitsByMessageResponse.new(commits: commits)])
      end

      it 'sends an RPC request with the correct payload' do
        expect(client.commits_by_message(query, **options)).to match_array(wrap_commits(commits))
      end
    end

    let(:query) { 'Add a feature' }
    let(:options) { {} }

    context 'when only the query is provided' do
      include_examples 'a CommitsByMessageRequest'
    end

    context 'when all arguments are provided' do
      let(:options) { { revision: 'feature-branch', path: 'foo.txt', limit: 10, offset: 20 } }

      include_examples 'a CommitsByMessageRequest'
    end

    context 'when limit and offset are not integers' do
      let(:options) { { limit: '10', offset: '60' } }

      include_examples 'a CommitsByMessageRequest'
    end

    context 'when revision and path contain non-ASCII characters' do
      let(:options) { { revision: "branch\u011F", path: "foo/\u011F.txt" } }

      include_examples 'a CommitsByMessageRequest'
    end

    def wrap_commits(commits)
      commits.map { |commit| Gitlab::Git::Commit.new(repository, commit) }
    end
  end

  describe '#list_commits_by_ref_name' do
    let(:project) { create(:project, :repository, create_branch: 'ü/unicode/multi-byte') }

    it 'lists latest commits grouped by a ref name' do
      response = client.list_commits_by_ref_name(%w[master feature v1.0.0 nonexistent ü/unicode/multi-byte])

      expect(response.keys.count).to eq 4
      expect(response.fetch('master').id).to eq 'b83d6e391c22777fca1ed3012fce84f633d7fed0'
      expect(response.fetch('feature').id).to eq '0b4bc9a49b562e85de7cc9e834518ea6828729b9'
      expect(response.fetch('v1.0.0').id).to eq '6f6d7e7ed97bb5f0054f2b1df789b39ca89b6ff9'
      expect(response.fetch('ü/unicode/multi-byte')).to be_present
      expect(response).not_to have_key 'nonexistent'
    end
  end

  describe '#raw_blame' do
    let(:project) { create(:project, :test_repo) }
    let(:revision) { 'blame-on-renamed' }
    let(:path) { 'files/plain_text/renamed' }

    let(:blame_headers) do
      [
        '405a45736a75e439bb059e638afaa9a3c2eeda79 1 1 2',
        '405a45736a75e439bb059e638afaa9a3c2eeda79 2 2',
        'bed1d1610ebab382830ee888288bf939c43873bb 3 3 1',
        '3685515c40444faf92774e72835e1f9c0e809672 4 4 1',
        '32c33da59f8a1a9f90bdeda570337888b00b244d 5 5 1'
      ]
    end

    subject(:blame) { client.raw_blame(revision, path, range: range).split("\n") }

    context 'without a range' do
      let(:range) { nil }

      it 'blames a whole file' do
        is_expected.to include(*blame_headers)
      end
    end

    context 'with a range' do
      let(:range) { '3,4' }

      it 'blames part of a file' do
        is_expected.to include(blame_headers[2], blame_headers[3])
        is_expected.not_to include(blame_headers[0], blame_headers[1], blame_headers[4])
      end
    end
  end
end