2019-11-13 03:06:31 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-24 15:53:44 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-18 07:59:36 +00:00
|
|
|
describe Gitlab::GitalyClient::CommitService do
|
2017-05-05 14:55:12 +00:00
|
|
|
let(:project) { create(:project, :repository) }
|
2017-07-18 05:02:56 +00:00
|
|
|
let(:storage_name) { project.repository_storage }
|
2017-07-22 00:37:22 +00:00
|
|
|
let(:relative_path) { project.disk_path + '.git' }
|
2017-05-05 14:55:12 +00:00
|
|
|
let(:repository) { project.repository }
|
|
|
|
let(:repository_message) { repository.gitaly_repository }
|
2017-07-18 05:02:56 +00:00
|
|
|
let(:revision) { '913c66a37b4a45b9769037c55c2d238bd0942d2e' }
|
|
|
|
let(:commit) { project.commit(revision) }
|
|
|
|
let(:client) { described_class.new(repository) }
|
2017-02-24 15:53:44 +00:00
|
|
|
|
2017-05-05 14:55:12 +00:00
|
|
|
describe '#diff_from_parent' do
|
2017-02-24 15:53:44 +00:00
|
|
|
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',
|
2017-07-13 22:22:09 +00:00
|
|
|
right_commit_id: commit.id,
|
2018-06-29 13:25:35 +00:00
|
|
|
collapse_diffs: false,
|
2017-07-13 22:22:09 +00:00
|
|
|
enforce_limits: true,
|
2018-09-06 20:56:06 +00:00
|
|
|
# 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: 102400
|
2017-02-24 15:53:44 +00:00
|
|
|
)
|
|
|
|
|
2017-07-18 07:59:36 +00:00
|
|
|
expect_any_instance_of(Gitaly::DiffService::Stub).to receive(:commit_diff).with(request, kind_of(Hash))
|
2017-02-24 15:53:44 +00:00
|
|
|
|
2017-07-18 05:02:56 +00:00
|
|
|
client.diff_from_parent(commit)
|
2017-02-24 15:53:44 +00:00
|
|
|
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
|
2017-07-25 20:48:17 +00:00
|
|
|
initial_commit = project.commit('1a0b36b3cdad1d2ee32457c102a8c0b7056fa863').raw
|
2017-02-24 15:53:44 +00:00
|
|
|
request = Gitaly::CommitDiffRequest.new(
|
|
|
|
repository: repository_message,
|
2018-04-12 03:05:07 +00:00
|
|
|
left_commit_id: Gitlab::Git::EMPTY_TREE_ID,
|
2017-07-13 22:22:09 +00:00
|
|
|
right_commit_id: initial_commit.id,
|
2018-06-29 13:25:35 +00:00
|
|
|
collapse_diffs: false,
|
2017-07-13 22:22:09 +00:00
|
|
|
enforce_limits: true,
|
2018-09-06 20:56:06 +00:00
|
|
|
# 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: 102400
|
2017-02-24 15:53:44 +00:00
|
|
|
)
|
|
|
|
|
2017-07-18 07:59:36 +00:00
|
|
|
expect_any_instance_of(Gitaly::DiffService::Stub).to receive(:commit_diff).with(request, kind_of(Hash))
|
2017-02-24 15:53:44 +00:00
|
|
|
|
2017-07-18 05:02:56 +00:00
|
|
|
client.diff_from_parent(initial_commit)
|
2017-02-24 15:53:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-25 21:26:52 +00:00
|
|
|
it 'returns a Gitlab::GitalyClient::DiffStitcher' do
|
2017-07-18 05:02:56 +00:00
|
|
|
ret = client.diff_from_parent(commit)
|
2017-02-24 15:53:44 +00:00
|
|
|
|
2017-07-25 21:26:52 +00:00
|
|
|
expect(ret).to be_kind_of(Gitlab::GitalyClient::DiffStitcher)
|
2017-05-05 14:55:12 +00:00
|
|
|
end
|
2017-09-19 11:35:21 +00:00
|
|
|
|
|
|
|
it 'encodes paths correctly' do
|
2017-10-03 08:03:19 +00:00
|
|
|
expect { client.diff_from_parent(commit, paths: ['encoding/test.txt', 'encoding/テスト.txt', nil]) }.not_to raise_error
|
2017-09-19 11:35:21 +00:00
|
|
|
end
|
2017-05-05 14:55:12 +00:00
|
|
|
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
|
|
|
|
)
|
|
|
|
|
2017-07-18 07:59:36 +00:00
|
|
|
expect_any_instance_of(Gitaly::DiffService::Stub).to receive(:commit_delta).with(request, kind_of(Hash)).and_return([])
|
2017-05-05 14:55:12 +00:00
|
|
|
|
2017-07-18 05:02:56 +00:00
|
|
|
client.commit_deltas(commit)
|
2017-05-05 14:55:12 +00:00
|
|
|
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,
|
2018-04-12 03:05:07 +00:00
|
|
|
left_commit_id: Gitlab::Git::EMPTY_TREE_ID,
|
2017-05-05 14:55:12 +00:00
|
|
|
right_commit_id: initial_commit.id
|
|
|
|
)
|
|
|
|
|
2017-07-18 07:59:36 +00:00
|
|
|
expect_any_instance_of(Gitaly::DiffService::Stub).to receive(:commit_delta).with(request, kind_of(Hash)).and_return([])
|
2017-05-05 14:55:12 +00:00
|
|
|
|
2017-07-18 05:02:56 +00:00
|
|
|
client.commit_deltas(initial_commit)
|
2017-05-05 14:55:12 +00:00
|
|
|
end
|
2017-02-24 15:53:44 +00:00
|
|
|
end
|
|
|
|
end
|
2017-06-23 21:52:51 +00:00
|
|
|
|
|
|
|
describe '#between' do
|
|
|
|
let(:from) { 'master' }
|
2018-04-12 03:05:07 +00:00
|
|
|
let(:to) { Gitlab::Git::EMPTY_TREE_ID }
|
2017-07-18 05:02:56 +00:00
|
|
|
|
2017-06-23 21:52:51 +00:00
|
|
|
it 'sends an RPC request' do
|
|
|
|
request = Gitaly::CommitsBetweenRequest.new(
|
|
|
|
repository: repository_message, from: from, to: to
|
|
|
|
)
|
|
|
|
|
|
|
|
expect_any_instance_of(Gitaly::CommitService::Stub).to receive(:commits_between)
|
|
|
|
.with(request, kind_of(Hash)).and_return([])
|
|
|
|
|
|
|
|
described_class.new(repository).between(from, to)
|
|
|
|
end
|
|
|
|
end
|
2017-07-18 05:02:56 +00:00
|
|
|
|
2018-09-14 01:11:35 +00:00
|
|
|
describe '#diff_stats' do
|
|
|
|
let(:left_commit_id) { 'master' }
|
|
|
|
let(:right_commit_id) { 'cfe32cf61b73a0d5e9f13e774abde7ff789b1660' }
|
|
|
|
|
2020-06-03 12:08:21 +00:00
|
|
|
it 'sends an RPC request and returns the stats' do
|
2018-09-14 01:11:35 +00:00
|
|
|
request = Gitaly::DiffStatsRequest.new(repository: repository_message,
|
|
|
|
left_commit_id: left_commit_id,
|
|
|
|
right_commit_id: right_commit_id)
|
|
|
|
|
2020-06-03 12:08:21 +00:00
|
|
|
diff_stat_response = Gitaly::DiffStatsResponse.new(
|
|
|
|
stats: [{ additions: 1, deletions: 2, path: 'test' }])
|
|
|
|
|
2018-09-14 01:11:35 +00:00
|
|
|
expect_any_instance_of(Gitaly::DiffService::Stub).to receive(:diff_stats)
|
2020-06-03 12:08:21 +00:00
|
|
|
.with(request, kind_of(Hash)).and_return([diff_stat_response])
|
|
|
|
|
|
|
|
returned_value = described_class.new(repository).diff_stats(left_commit_id, right_commit_id)
|
2018-09-14 01:11:35 +00:00
|
|
|
|
2020-06-03 12:08:21 +00:00
|
|
|
expect(returned_value).to eq(diff_stat_response.stats)
|
2018-09-14 01:11:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-18 05:02:56 +00:00
|
|
|
describe '#tree_entries' do
|
|
|
|
let(:path) { '/' }
|
|
|
|
|
|
|
|
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([])
|
|
|
|
|
2018-02-16 20:39:43 +00:00
|
|
|
client.tree_entries(repository, revision, path, false)
|
2017-07-18 05:02:56 +00:00
|
|
|
end
|
2017-08-21 18:13:40 +00:00
|
|
|
|
|
|
|
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([])
|
|
|
|
|
2018-02-16 20:39:43 +00:00
|
|
|
client.tree_entries(repository, revision, path, false)
|
2017-08-21 18:13:40 +00:00
|
|
|
end
|
|
|
|
end
|
2017-07-18 05:02:56 +00:00
|
|
|
end
|
2017-07-25 21:33:06 +00:00
|
|
|
|
2018-01-23 08:26:28 +00:00
|
|
|
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
|
|
|
|
|
2017-07-25 21:33:06 +00:00
|
|
|
describe '#find_commit' do
|
2018-04-12 03:05:07 +00:00
|
|
|
let(:revision) { Gitlab::Git::EMPTY_TREE_ID }
|
2019-12-12 12:07:33 +00:00
|
|
|
|
2017-07-25 21:33:06 +00:00
|
|
|
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
|
2018-02-06 15:32:34 +00:00
|
|
|
|
|
|
|
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
|
2019-03-17 06:23:11 +00:00
|
|
|
|
|
|
|
context 'when caching of the ref name is enabled' do
|
2019-06-23 05:51:52 +00:00
|
|
|
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
|
|
|
|
|
2019-03-17 06:23:11 +00:00
|
|
|
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
|
2018-02-06 15:32:34 +00:00
|
|
|
end
|
2017-07-25 21:33:06 +00:00
|
|
|
end
|
2017-08-09 21:47:11 +00:00
|
|
|
|
2017-09-06 10:55:16 +00:00
|
|
|
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
|
2020-02-13 21:08:59 +00:00
|
|
|
|
|
|
|
describe '#find_commits' do
|
2020-02-18 00:09:20 +00:00
|
|
|
it 'sends an RPC request with NONE when default' do
|
|
|
|
request = Gitaly::FindCommitsRequest.new(
|
|
|
|
repository: repository_message,
|
|
|
|
disable_walk: true,
|
|
|
|
order: 'NONE'
|
|
|
|
)
|
|
|
|
|
|
|
|
expect_any_instance_of(Gitaly::CommitService::Stub).to receive(:find_commits)
|
|
|
|
.with(request, kind_of(Hash)).and_return([])
|
|
|
|
|
|
|
|
client.find_commits(order: 'default')
|
|
|
|
end
|
|
|
|
|
2020-02-13 21:08:59 +00:00
|
|
|
it 'sends an RPC request' do
|
|
|
|
request = Gitaly::FindCommitsRequest.new(
|
|
|
|
repository: repository_message,
|
|
|
|
disable_walk: true,
|
|
|
|
order: 'TOPO'
|
|
|
|
)
|
|
|
|
|
|
|
|
expect_any_instance_of(Gitaly::CommitService::Stub).to receive(:find_commits)
|
|
|
|
.with(request, kind_of(Hash)).and_return([])
|
|
|
|
|
|
|
|
client.find_commits(order: 'topo')
|
|
|
|
end
|
2020-02-26 18:09:24 +00:00
|
|
|
|
|
|
|
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>"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2020-02-13 21:08:59 +00:00
|
|
|
end
|
2020-06-11 15:08:36 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2020-06-23 18:09:28 +00:00
|
|
|
|
|
|
|
describe '#list_commits_by_ref_name' do
|
|
|
|
it 'lists latest commits grouped by a ref name' do
|
|
|
|
response = client.list_commits_by_ref_name(%w[master feature v1.0.0 nonexistent])
|
|
|
|
|
|
|
|
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).not_to have_key 'nonexistent'
|
|
|
|
end
|
|
|
|
end
|
2017-02-24 15:53:44 +00:00
|
|
|
end
|