2020-01-01 13:55:28 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
require "spec_helper"
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
RSpec.describe Gitlab::Git::Repository, feature_category: :source_code_management do
|
2017-09-10 17:25:29 +05:30
|
|
|
include Gitlab::EncodingHelper
|
2019-07-31 22:56:46 +05:30
|
|
|
include RepoHelpers
|
2018-03-17 18:26:18 +05:30
|
|
|
using RSpec::Parameterized::TableSyntax
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
shared_examples 'wrapping gRPC errors' do |gitaly_client_class, gitaly_client_method|
|
|
|
|
it 'wraps gRPC not found error' do
|
|
|
|
expect_any_instance_of(gitaly_client_class).to receive(gitaly_client_method)
|
|
|
|
.and_raise(GRPC::NotFound)
|
|
|
|
expect { subject }.to raise_error(Gitlab::Git::Repository::NoRepository)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'wraps gRPC unknown error' do
|
|
|
|
expect_any_instance_of(gitaly_client_class).to receive(gitaly_client_method)
|
|
|
|
.and_raise(GRPC::Unknown)
|
|
|
|
expect { subject }.to raise_error(Gitlab::Git::CommandError)
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
let_it_be(:project) { create(:project, :repository) }
|
|
|
|
let_it_be(:repository) { project.repository.raw }
|
|
|
|
|
|
|
|
let(:mutable_project) { create(:project, :repository) }
|
|
|
|
let(:mutable_repository) { mutable_project.repository.raw }
|
2018-03-17 18:26:18 +05:30
|
|
|
let(:user) { build(:user) }
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe "Respond to" do
|
|
|
|
subject { repository }
|
|
|
|
|
|
|
|
it { is_expected.to respond_to(:root_ref) }
|
|
|
|
it { is_expected.to respond_to(:tags) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#root_ref' do
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'returns UTF-8' do
|
|
|
|
expect(repository.root_ref).to be_utf8
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'gets the branch name from GitalyClient' do
|
|
|
|
expect_any_instance_of(Gitlab::GitalyClient::RefService).to receive(:default_branch_name)
|
|
|
|
repository.root_ref
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it_behaves_like 'wrapping gRPC errors', Gitlab::GitalyClient::RefService, :default_branch_name do
|
|
|
|
subject { repository.root_ref }
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
describe '#create_repository' do
|
|
|
|
it_behaves_like 'wrapping gRPC errors', Gitlab::GitalyClient::RepositoryService, :create_repository do
|
|
|
|
subject { repository.create_repository }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe '#branch_names' do
|
|
|
|
subject { repository.branch_names }
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
it 'has TestRepo::BRANCH_SHA.size elements' do
|
|
|
|
expect(subject.size).to eq(TestEnv::BRANCH_SHA.size)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'returns UTF-8' do
|
|
|
|
expect(subject.first).to be_utf8
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it { is_expected.to include("master") }
|
|
|
|
it { is_expected.not_to include("branch-from-space") }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
it_behaves_like 'wrapping gRPC errors', Gitlab::GitalyClient::RefService, :list_refs
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '#tag_names' do
|
|
|
|
subject { repository.tag_names }
|
|
|
|
|
|
|
|
it { is_expected.to be_kind_of Array }
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
it 'has some elements' do
|
|
|
|
expect(subject.size).to be >= 1
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'returns UTF-8' do
|
|
|
|
expect(subject.first).to be_utf8
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe '#last' do
|
|
|
|
subject { super().last }
|
2019-12-21 20:55:43 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
it { is_expected.to eq("v1.1.1") }
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2022-10-11 01:57:18 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it { is_expected.to include("v1.0.0") }
|
|
|
|
it { is_expected.not_to include("v5.0.0") }
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
it_behaves_like 'wrapping gRPC errors', Gitlab::GitalyClient::RefService, :list_refs
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
describe '#tags' do
|
|
|
|
subject { repository.tags }
|
|
|
|
|
|
|
|
it_behaves_like 'wrapping gRPC errors', Gitlab::GitalyClient::RefService, :tags
|
|
|
|
end
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
describe '#archive_metadata' do
|
|
|
|
let(:storage_path) { '/tmp' }
|
2022-10-11 01:57:18 +05:30
|
|
|
let(:cache_key) { File.join(repository.gl_repository, TestEnv::BRANCH_SHA['master']) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
let(:append_sha) { true }
|
|
|
|
let(:ref) { 'master' }
|
|
|
|
let(:format) { nil }
|
2019-07-31 22:56:46 +05:30
|
|
|
let(:path) { nil }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
let(:expected_extension) { 'tar.gz' }
|
|
|
|
let(:expected_filename) { "#{expected_prefix}.#{expected_extension}" }
|
2021-01-03 14:25:43 +05:30
|
|
|
let(:expected_path) { File.join(storage_path, cache_key, "@v2", expected_filename) }
|
2023-05-08 21:46:49 +05:30
|
|
|
let(:expected_prefix) { "gitlab-git-test-#{ref.tr('/', '-')}-#{expected_prefix_sha}" }
|
|
|
|
let(:expected_prefix_sha) { TestEnv::BRANCH_SHA['master'] }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
subject(:metadata) { repository.archive_metadata(ref, storage_path, 'gitlab-git-test', format, append_sha: append_sha, path: path) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
it 'sets CommitId to the commit SHA' do
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(metadata['CommitId']).to start_with(TestEnv::BRANCH_SHA['master'])
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
it 'sets ArchivePrefix to the expected prefix' do
|
|
|
|
expect(metadata['ArchivePrefix']).to eq(expected_prefix)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
it 'sets ArchivePath to the expected globally-unique path' do
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(expected_path).to include(File.join(repository.gl_repository, TestEnv::BRANCH_SHA['master']))
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
expect(metadata['ArchivePath']).to eq(expected_path)
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
context 'path is set' do
|
|
|
|
let(:path) { 'foo/bar' }
|
|
|
|
|
|
|
|
it 'appends the path to the prefix' do
|
|
|
|
expect(metadata['ArchivePrefix']).to eq("#{expected_prefix}-foo-bar")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
context 'append_sha varies archive path and filename' do
|
|
|
|
where(:append_sha, :ref, :expected_prefix) do
|
2022-10-11 01:57:18 +05:30
|
|
|
sha = TestEnv::BRANCH_SHA['master']
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
true | 'master' | "gitlab-git-test-master-#{sha}"
|
|
|
|
true | sha | "gitlab-git-test-#{sha}-#{sha}"
|
|
|
|
false | 'master' | "gitlab-git-test-master"
|
|
|
|
false | sha | "gitlab-git-test-#{sha}"
|
|
|
|
nil | 'master' | "gitlab-git-test-master-#{sha}"
|
|
|
|
nil | sha | "gitlab-git-test-#{sha}"
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
with_them do
|
|
|
|
it { expect(metadata['ArchivePrefix']).to eq(expected_prefix) }
|
|
|
|
it { expect(metadata['ArchivePath']).to eq(expected_path) }
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
context 'format varies archive path and filename' do
|
|
|
|
where(:format, :expected_extension) do
|
|
|
|
nil | 'tar.gz'
|
|
|
|
'madeup' | 'tar.gz'
|
|
|
|
'tbz2' | 'tar.bz2'
|
|
|
|
'zip' | 'zip'
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
with_them do
|
|
|
|
it { expect(metadata['ArchivePrefix']).to eq(expected_prefix) }
|
|
|
|
it { expect(metadata['ArchivePath']).to eq(expected_path) }
|
|
|
|
end
|
|
|
|
end
|
2023-05-08 21:46:49 +05:30
|
|
|
|
|
|
|
context 'when references are ambiguous' do
|
|
|
|
let_it_be(:ambiguous_project) { create(:project, :repository) }
|
|
|
|
let_it_be(:repository) { ambiguous_project.repository.raw }
|
|
|
|
let_it_be(:branch_merged_commit_id) { ambiguous_project.repository.find_branch('branch-merged').dereferenced_target.id }
|
|
|
|
let_it_be(:branch_master_commit_id) { ambiguous_project.repository.find_branch('master').dereferenced_target.id }
|
|
|
|
let_it_be(:tag_1_0_0_commit_id) { ambiguous_project.repository.find_tag('v1.0.0').dereferenced_target.id }
|
|
|
|
|
|
|
|
context 'when tag is ambiguous' do
|
|
|
|
before do
|
|
|
|
ambiguous_project.repository.add_tag(user, ref, 'master', 'foo')
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
ambiguous_project.repository.rm_tag(user, ref)
|
|
|
|
end
|
|
|
|
|
|
|
|
where(:ref, :expected_commit_id, :desc) do
|
|
|
|
'refs/heads/branch-merged' | ref(:branch_master_commit_id) | 'when tag looks like a branch'
|
|
|
|
'branch-merged' | ref(:branch_master_commit_id) | 'when tag has the same name as a branch'
|
|
|
|
ref(:branch_merged_commit_id) | ref(:branch_merged_commit_id) | 'when tag looks like a commit id'
|
|
|
|
'v0.0.0' | ref(:branch_master_commit_id) | 'when tag looks like a normal tag'
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
it 'selects the correct commit' do
|
|
|
|
expect(metadata['CommitId']).to eq(expected_commit_id)
|
|
|
|
end
|
|
|
|
end
|
2023-05-27 22:25:52 +05:30
|
|
|
|
|
|
|
context 'when resolve_ambiguous_archives is disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(resolve_ambiguous_archives: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
where(:ref, :expected_commit_id, :desc) do
|
|
|
|
'refs/heads/branch-merged' | ref(:branch_merged_commit_id) | 'when tag looks like a branch (difference!)'
|
|
|
|
'branch-merged' | ref(:branch_master_commit_id) | 'when tag has the same name as a branch'
|
|
|
|
ref(:branch_merged_commit_id) | ref(:branch_merged_commit_id) | 'when tag looks like a commit id'
|
|
|
|
'v0.0.0' | ref(:branch_master_commit_id) | 'when tag looks like a normal tag'
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
it 'selects the correct commit' do
|
|
|
|
expect(metadata['CommitId']).to eq(expected_commit_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-05-08 21:46:49 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when branch is ambiguous' do
|
|
|
|
before do
|
|
|
|
ambiguous_project.repository.add_branch(user, ref, 'master')
|
|
|
|
end
|
|
|
|
|
|
|
|
where(:ref, :expected_commit_id, :desc) do
|
|
|
|
'refs/tags/v1.0.0' | ref(:branch_master_commit_id) | 'when branch looks like a tag'
|
|
|
|
'v1.0.0' | ref(:tag_1_0_0_commit_id) | 'when branch has the same name as a tag'
|
|
|
|
ref(:branch_merged_commit_id) | ref(:branch_merged_commit_id) | 'when branch looks like a commit id'
|
|
|
|
'just-a-normal-branch' | ref(:branch_master_commit_id) | 'when branch looks like a normal branch'
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
it 'selects the correct commit' do
|
|
|
|
expect(metadata['CommitId']).to eq(expected_commit_id)
|
|
|
|
end
|
|
|
|
end
|
2023-05-27 22:25:52 +05:30
|
|
|
|
|
|
|
context 'when resolve_ambiguous_archives is disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(resolve_ambiguous_archives: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
where(:ref, :expected_commit_id, :desc) do
|
|
|
|
'refs/tags/v1.0.0' | ref(:tag_1_0_0_commit_id) | 'when branch looks like a tag (difference!)'
|
|
|
|
'v1.0.0' | ref(:tag_1_0_0_commit_id) | 'when branch has the same name as a tag'
|
|
|
|
ref(:branch_merged_commit_id) | ref(:branch_merged_commit_id) | 'when branch looks like a commit id'
|
|
|
|
'just-a-normal-branch' | ref(:branch_master_commit_id) | 'when branch looks like a normal branch'
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
it 'selects the correct commit' do
|
|
|
|
expect(metadata['CommitId']).to eq(expected_commit_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-05-08 21:46:49 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when ref is HEAD' do
|
|
|
|
let(:ref) { 'HEAD' }
|
|
|
|
|
|
|
|
it 'selects commit id from HEAD ref' do
|
|
|
|
expect(metadata['CommitId']).to eq(branch_master_commit_id)
|
|
|
|
expect(metadata['ArchivePrefix']).to eq(expected_prefix)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when ref is not found' do
|
|
|
|
let(:ref) { 'unknown-ref-cannot-be-found' }
|
|
|
|
|
|
|
|
it 'returns empty metadata' do
|
|
|
|
expect(metadata).to eq({})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '#size' do
|
|
|
|
subject { repository.size }
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
it { is_expected.to be > 0 }
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
describe '#to_s' do
|
|
|
|
subject { repository.to_s }
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
it { is_expected.to eq("<Gitlab::Git::Repository: #{project.full_path}>") }
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
describe '#object_directory_size' do
|
|
|
|
before do
|
|
|
|
allow(repository.gitaly_repository_client)
|
|
|
|
.to receive(:get_object_directory_size)
|
|
|
|
.and_return(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { repository.object_directory_size }
|
|
|
|
|
|
|
|
it { is_expected.to eq 2048 }
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe '#empty?' do
|
2018-03-17 18:26:18 +05:30
|
|
|
it { expect(repository).not_to be_empty }
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '#ref_names' do
|
|
|
|
let(:ref_names) { repository.ref_names }
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
subject { ref_names }
|
|
|
|
|
|
|
|
it { is_expected.to be_kind_of Array }
|
|
|
|
|
|
|
|
describe '#first' do
|
|
|
|
subject { super().first }
|
2019-12-21 20:55:43 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
it { is_expected.to eq(TestEnv::BRANCH_SHA.keys.min) }
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '#last' do
|
|
|
|
subject { super().last }
|
2019-12-21 20:55:43 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
it { is_expected.to eq('v1.1.1') }
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
describe '#submodule_url_for' do
|
2022-10-11 01:57:18 +05:30
|
|
|
let(:ref) { 'submodule_inside_folder' }
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
def submodule_url(path)
|
|
|
|
repository.submodule_url_for(ref, path)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it { expect(submodule_url('six')).to eq('git://github.com/randx/six.git') }
|
2022-10-11 01:57:18 +05:30
|
|
|
it { expect(submodule_url('test_inside_folder/another_folder/six')).to eq('git://github.com/randx/six.git') }
|
2017-09-10 17:25:29 +05:30
|
|
|
it { expect(submodule_url('invalid/path')).to eq(nil) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'uncommitted submodule dir' do
|
|
|
|
let(:ref) { 'fix-existing-submodule-dir' }
|
|
|
|
|
|
|
|
it { expect(submodule_url('submodule-existing-dir')).to eq(nil) }
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'tags' do
|
2022-10-11 01:57:18 +05:30
|
|
|
let(:ref) { 'v1.1.1' }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it { expect(submodule_url('six')).to eq('git://github.com/randx/six.git') }
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'no .gitmodules at commit' do
|
|
|
|
let(:ref) { '9596bc54a6f0c0c98248fe97077eb5ccf48a98d0' }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it { expect(submodule_url('six')).to eq(nil) }
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'no gitlink entry' do
|
|
|
|
let(:ref) { '6d39438' }
|
|
|
|
|
|
|
|
it { expect(submodule_url('six')).to eq(nil) }
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
describe '#submodule_urls_for' do
|
|
|
|
let(:ref) { 'master' }
|
|
|
|
|
|
|
|
it 'returns url mappings for submodules' do
|
|
|
|
urls = repository.submodule_urls_for(ref)
|
|
|
|
|
|
|
|
expect(urls).to eq({
|
2022-10-11 01:57:18 +05:30
|
|
|
"gitlab-grack" => "https://gitlab.com/gitlab-org/gitlab-grack.git",
|
|
|
|
"gitlab-shell" => "https://github.com/gitlabhq/gitlab-shell.git",
|
2019-09-30 21:07:59 +05:30
|
|
|
"six" => "git://github.com/randx/six.git"
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe '#commit_count' do
|
2022-10-11 01:57:18 +05:30
|
|
|
it { expect(repository.commit_count("master")).to eq(37) }
|
2018-12-05 23:21:45 +05:30
|
|
|
it { expect(repository.commit_count("feature")).to eq(9) }
|
|
|
|
it { expect(repository.commit_count("does-not-exist")).to eq(0) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
it_behaves_like 'wrapping gRPC errors', Gitlab::GitalyClient::CommitService, :commit_count do
|
|
|
|
subject { repository.commit_count('master') }
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
describe '#diverging_commit_count' do
|
|
|
|
it 'counts 0 for the same branch' do
|
|
|
|
expect(repository.diverging_commit_count('master', 'master', max_count: 1000)).to eq([0, 0])
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'max count does not truncate results' do
|
|
|
|
where(:left, :right, :expected) do
|
|
|
|
1 | 1 | [1, 1]
|
|
|
|
4 | 4 | [4, 4]
|
|
|
|
2 | 2 | [2, 2]
|
|
|
|
2 | 4 | [2, 4]
|
|
|
|
4 | 2 | [4, 2]
|
|
|
|
10 | 10 | [10, 10]
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
before do
|
2020-03-13 15:44:24 +05:30
|
|
|
repository.create_branch('left-branch')
|
|
|
|
repository.create_branch('right-branch')
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
left.times do |i|
|
2022-10-11 01:57:18 +05:30
|
|
|
repository.commit_files(
|
2022-08-13 15:12:31 +05:30
|
|
|
user,
|
|
|
|
branch_name: 'left-branch',
|
|
|
|
message: 'some more content for a',
|
|
|
|
actions: [{
|
|
|
|
action: i == 0 ? :create : :update,
|
|
|
|
file_path: 'encoding/CHANGELOG',
|
|
|
|
content: 'some stuff'
|
|
|
|
}]
|
|
|
|
)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
right.times do |i|
|
2022-10-11 01:57:18 +05:30
|
|
|
repository.commit_files(
|
2022-08-13 15:12:31 +05:30
|
|
|
user,
|
|
|
|
branch_name: 'right-branch',
|
|
|
|
message: 'some more content for b',
|
|
|
|
actions: [{
|
|
|
|
action: i == 0 ? :create : :update,
|
|
|
|
file_path: 'encoding/CHANGELOG',
|
|
|
|
content: 'some stuff'
|
|
|
|
}]
|
|
|
|
)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
repository.delete_branch('left-branch')
|
|
|
|
repository.delete_branch('right-branch')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the correct count bounding at max_count' do
|
2022-08-13 15:12:31 +05:30
|
|
|
branch_a_sha = repository.find_branch('left-branch').dereferenced_target.sha
|
|
|
|
branch_b_sha = repository.find_branch('right-branch').dereferenced_target.sha
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
count = repository.diverging_commit_count(branch_a_sha, branch_b_sha, max_count: 1000)
|
|
|
|
|
|
|
|
expect(count).to eq(expected)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'max count truncates results' do
|
|
|
|
where(:left, :right, :max_count) do
|
|
|
|
1 | 1 | 1
|
|
|
|
4 | 4 | 4
|
|
|
|
2 | 2 | 3
|
|
|
|
2 | 4 | 3
|
|
|
|
4 | 2 | 5
|
|
|
|
10 | 10 | 10
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
before do
|
2020-03-13 15:44:24 +05:30
|
|
|
repository.create_branch('left-branch')
|
|
|
|
repository.create_branch('right-branch')
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
left.times do |i|
|
2022-10-11 01:57:18 +05:30
|
|
|
repository.commit_files(
|
2022-08-13 15:12:31 +05:30
|
|
|
user,
|
|
|
|
branch_name: 'left-branch',
|
|
|
|
message: 'some more content for a',
|
|
|
|
actions: [{
|
|
|
|
action: i == 0 ? :create : :update,
|
|
|
|
file_path: 'encoding/CHANGELOG',
|
|
|
|
content: 'some stuff'
|
|
|
|
}]
|
|
|
|
)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
right.times do |i|
|
2022-10-11 01:57:18 +05:30
|
|
|
repository.commit_files(
|
2022-08-13 15:12:31 +05:30
|
|
|
user,
|
|
|
|
branch_name: 'right-branch',
|
|
|
|
message: 'some more content for b',
|
|
|
|
actions: [{
|
|
|
|
action: i == 0 ? :create : :update,
|
|
|
|
file_path: 'encoding/CHANGELOG',
|
|
|
|
content: 'some stuff'
|
|
|
|
}]
|
|
|
|
)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
repository.delete_branch('left-branch')
|
|
|
|
repository.delete_branch('right-branch')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the correct count bounding at max_count' do
|
2022-08-13 15:12:31 +05:30
|
|
|
branch_a_sha = repository.find_branch('left-branch').dereferenced_target.sha
|
|
|
|
branch_b_sha = repository.find_branch('right-branch').dereferenced_target.sha
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
results = repository.diverging_commit_count(branch_a_sha, branch_b_sha, max_count: max_count)
|
|
|
|
|
|
|
|
expect(results[0] + results[1]).to eq(max_count)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'wrapping gRPC errors', Gitlab::GitalyClient::CommitService, :diverging_commit_count do
|
|
|
|
subject { repository.diverging_commit_count('master', 'master', max_count: 1000) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
describe '#has_local_branches?' do
|
2018-11-08 19:23:39 +05:30
|
|
|
context 'check for local branches' do
|
2018-03-17 18:26:18 +05:30
|
|
|
it { expect(repository.has_local_branches?).to eq(true) }
|
2022-10-11 01:57:18 +05:30
|
|
|
end
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
describe '#delete_branch' do
|
|
|
|
let(:repository) { mutable_repository }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
it 'deletes a branch' do
|
|
|
|
expect(repository.find_branch('feature')).not_to be_nil
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
repository.delete_branch('feature')
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(repository.find_branch('feature')).to be_nil
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
it 'deletes a fully qualified branch' do
|
|
|
|
expect(repository.find_branch('feature')).not_to be_nil
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
repository.delete_branch('refs/heads/feature')
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(repository.find_branch('feature')).to be_nil
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#delete_refs' do
|
2018-12-05 23:21:45 +05:30
|
|
|
let(:repository) { mutable_repository }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
it 'deletes the ref' do
|
|
|
|
repository.delete_refs('refs/heads/feature')
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
expect(repository.find_branch('feature')).to be_nil
|
2018-12-05 23:21:45 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
it 'deletes all refs' do
|
|
|
|
refs = %w[refs/heads/wip refs/tags/v1.1.0]
|
|
|
|
repository.delete_refs(*refs)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
expect(repository.list_refs(refs)).to be_empty
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
it 'does not fail when deleting an empty list of refs' do
|
|
|
|
expect { repository.delete_refs(*[]) }.not_to raise_error
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
it 'raises an error if it failed' do
|
2023-01-13 00:05:48 +05:30
|
|
|
expect { repository.delete_refs('refs\heads\fix') }.to raise_error(Gitlab::Git::InvalidRefFormatError)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
describe '#branch_names_contains_sha' do
|
2022-08-13 15:12:31 +05:30
|
|
|
let(:head_id) { repository.commit.id }
|
2018-11-08 19:23:39 +05:30
|
|
|
let(:new_branch) { head_id }
|
|
|
|
let(:utf8_branch) { 'branch-é' }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
before do
|
2020-03-13 15:44:24 +05:30
|
|
|
repository.create_branch(new_branch)
|
|
|
|
repository.create_branch(utf8_branch)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
after do
|
|
|
|
repository.delete_branch(new_branch)
|
|
|
|
repository.delete_branch(utf8_branch)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it 'displays that branch' do
|
|
|
|
expect(repository.branch_names_contains_sha(head_id)).to include('master', new_branch, utf8_branch)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2022-11-25 23:54:43 +05:30
|
|
|
|
|
|
|
context 'when limit is provided' do
|
|
|
|
it 'displays limited number of branches' do
|
|
|
|
expect(repository.branch_names_contains_sha(head_id, limit: 1)).to match_array(['2-mb-file'])
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe "#refs_hash" do
|
2018-03-17 18:26:18 +05:30
|
|
|
subject { repository.refs_hash }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
it "has as many entries as branches and tags" do
|
2017-08-17 22:00:37 +05:30
|
|
|
# We flatten in case a commit is pointed at by more than one branch and/or tag
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(subject.values.flatten.size).to be > 0
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'has valid commit ids as keys' do
|
|
|
|
expect(subject.keys).to all( match(Commit::COMMIT_SHA_PATTERN) )
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
it 'does not error when dereferenced_target is nil' do
|
|
|
|
blob_id = repository.blob_at('master', 'README.md').id
|
2022-08-13 15:12:31 +05:30
|
|
|
repository.add_tag("refs/tags/blob-tag", user: user, target: blob_id)
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
expect { subject }.not_to raise_error
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
describe '#fetch_remote' do
|
2021-10-27 15:23:28 +05:30
|
|
|
let(:url) { 'http://example.clom' }
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
it 'delegates to the gitaly RepositoryService' do
|
|
|
|
ssh_auth = double(:ssh_auth)
|
|
|
|
expected_opts = {
|
|
|
|
ssh_auth: ssh_auth,
|
|
|
|
forced: true,
|
|
|
|
no_tags: true,
|
|
|
|
timeout: described_class::GITLAB_PROJECTS_TIMEOUT,
|
2021-03-08 18:12:59 +05:30
|
|
|
prune: false,
|
2021-09-04 01:27:46 +05:30
|
|
|
check_tags_changed: false,
|
2021-10-27 15:23:28 +05:30
|
|
|
refmap: nil,
|
2023-01-10 11:22:00 +05:30
|
|
|
http_authorization_header: "",
|
|
|
|
resolved_address: '172.16.123.1'
|
2018-12-13 13:39:08 +05:30
|
|
|
}
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
expect(repository.gitaly_repository_client).to receive(:fetch_remote).with(url, expected_opts)
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2023-01-10 11:22:00 +05:30
|
|
|
repository.fetch_remote(url, ssh_auth: ssh_auth, forced: true, no_tags: true, prune: false, check_tags_changed: false, resolved_address: '172.16.123.1')
|
2018-12-13 13:39:08 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'wrapping gRPC errors', Gitlab::GitalyClient::RepositoryService, :fetch_remote do
|
2021-10-27 15:23:28 +05:30
|
|
|
subject { repository.fetch_remote(url) }
|
2018-12-13 13:39:08 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
describe '#search_files_by_content' do
|
|
|
|
let(:repository) { mutable_repository }
|
2020-03-13 15:44:24 +05:30
|
|
|
let(:ref) { 'search-files-by-content-branch' }
|
|
|
|
let(:content) { 'foobarbazmepmep' }
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
before do
|
2020-03-13 15:44:24 +05:30
|
|
|
repository.create_branch(ref)
|
2022-10-11 01:57:18 +05:30
|
|
|
repository.commit_files(
|
2022-08-13 15:12:31 +05:30
|
|
|
user,
|
|
|
|
branch_name: ref,
|
|
|
|
message: 'committing something',
|
|
|
|
actions: [{
|
|
|
|
action: :create,
|
|
|
|
file_path: 'encoding/CHANGELOG',
|
|
|
|
content: content
|
|
|
|
}]
|
|
|
|
)
|
2022-10-11 01:57:18 +05:30
|
|
|
repository.commit_files(
|
2022-08-13 15:12:31 +05:30
|
|
|
user,
|
|
|
|
branch_name: ref,
|
|
|
|
message: 'committing something',
|
|
|
|
actions: [{
|
|
|
|
action: :create,
|
|
|
|
file_path: 'anotherfile',
|
|
|
|
content: content
|
|
|
|
}]
|
|
|
|
)
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
subject do
|
|
|
|
repository.search_files_by_content(content, ref)
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
it 'has 2 items' do
|
|
|
|
expect(subject.size).to eq(2)
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
it 'has the correct matching line' do
|
|
|
|
expect(subject).to contain_exactly("#{ref}:encoding/CHANGELOG\u00001\u0000#{content}\n",
|
|
|
|
"#{ref}:anotherfile\u00001\u0000#{content}\n")
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
describe '#search_files_by_regexp' do
|
|
|
|
let(:ref) { 'master' }
|
|
|
|
|
|
|
|
subject(:result) { mutable_repository.search_files_by_regexp(filter, ref) }
|
|
|
|
|
|
|
|
context 'when sending a valid regexp' do
|
|
|
|
let(:filter) { 'files\/.*\/.*\.rb' }
|
|
|
|
|
|
|
|
it 'returns matched files' do
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(result).to contain_exactly('files/ruby/popen.rb',
|
2021-04-29 21:17:54 +05:30
|
|
|
'files/ruby/regex.rb',
|
|
|
|
'files/ruby/version_info.rb')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when sending an ivalid regexp' do
|
|
|
|
let(:filter) { '*.rb' }
|
|
|
|
|
|
|
|
it 'raises error' do
|
|
|
|
expect { result }.to raise_error(GRPC::InvalidArgument,
|
|
|
|
/missing argument to repetition operator: `*`/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the ref doesn't exist" do
|
|
|
|
let(:filter) { 'files\/.*\/.*\.rb' }
|
|
|
|
let(:ref) { 'non-existing-branch' }
|
|
|
|
|
|
|
|
it 'returns an empty array' do
|
|
|
|
expect(result).to eq([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
describe '#search_files_by_name' do
|
|
|
|
let(:ref) { 'master' }
|
|
|
|
|
|
|
|
subject(:result) { mutable_repository.search_files_by_name(query, ref) }
|
|
|
|
|
|
|
|
context 'when sending a valid name' do
|
|
|
|
let(:query) { 'files/ruby/popen.rb' }
|
|
|
|
|
|
|
|
it 'returns matched files' do
|
|
|
|
expect(result).to contain_exactly('files/ruby/popen.rb')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when sending a name with space' do
|
|
|
|
let(:query) { 'file with space.md' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
mutable_repository.commit_files(
|
|
|
|
user,
|
|
|
|
actions: [{ action: :create, file_path: "file with space.md", content: "Test content" }],
|
|
|
|
branch_name: ref, message: "Test"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns matched files' do
|
|
|
|
expect(result).to contain_exactly('file with space.md')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when sending a name with special ASCII characters' do
|
|
|
|
let(:file_name) { 'Hello !@#$%^&*()' }
|
|
|
|
let(:query) { file_name }
|
|
|
|
|
|
|
|
before do
|
|
|
|
mutable_repository.commit_files(
|
|
|
|
user,
|
|
|
|
actions: [{ action: :create, file_path: file_name, content: "Test content" }],
|
|
|
|
branch_name: ref, message: "Test"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns matched files' do
|
|
|
|
expect(result).to contain_exactly(file_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when sending a non-existing name' do
|
|
|
|
let(:query) { 'please do not exist.md' }
|
|
|
|
|
|
|
|
it 'raises error' do
|
|
|
|
expect(result).to eql([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
describe '#find_remote_root_ref' do
|
|
|
|
it 'gets the remote root ref from GitalyClient' do
|
|
|
|
expect_any_instance_of(Gitlab::GitalyClient::RemoteService)
|
|
|
|
.to receive(:find_remote_root_ref).and_call_original
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(repository.find_remote_root_ref(TestEnv.factory_repo_path.to_s)).to eq 'master'
|
2018-11-20 20:47:30 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns UTF-8' do
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(repository.find_remote_root_ref(TestEnv.factory_repo_path.to_s)).to be_utf8
|
2018-11-20 20:47:30 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil when remote name is nil' do
|
|
|
|
expect_any_instance_of(Gitlab::GitalyClient::RemoteService)
|
|
|
|
.not_to receive(:find_remote_root_ref)
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
expect(repository.find_remote_root_ref(nil)).to be_nil
|
2018-11-20 20:47:30 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil when remote name is empty' do
|
|
|
|
expect_any_instance_of(Gitlab::GitalyClient::RemoteService)
|
|
|
|
.not_to receive(:find_remote_root_ref)
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
expect(repository.find_remote_root_ref('')).to be_nil
|
2018-11-20 20:47:30 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'wrapping gRPC errors', Gitlab::GitalyClient::RemoteService, :find_remote_root_ref do
|
2022-11-25 23:54:43 +05:30
|
|
|
subject { repository.find_remote_root_ref(TestEnv.factory_repo_path.to_s) }
|
2018-11-20 20:47:30 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe "#log" do
|
2018-03-27 19:54:05 +05:30
|
|
|
shared_examples 'repository log' do
|
|
|
|
let(:commit_with_old_name) do
|
2018-11-18 11:00:15 +05:30
|
|
|
Gitlab::Git::Commit.find(repository, @commit_with_old_name_id)
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
2020-10-24 23:57:45 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
let(:commit_with_new_name) do
|
2018-11-18 11:00:15 +05:30
|
|
|
Gitlab::Git::Commit.find(repository, @commit_with_new_name_id)
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
2020-10-24 23:57:45 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
let(:rename_commit) do
|
2018-11-18 11:00:15 +05:30
|
|
|
Gitlab::Git::Commit.find(repository, @rename_commit_id)
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
before do
|
2018-03-27 19:54:05 +05:30
|
|
|
# Add new commits so that there's a renamed file in the commit history
|
2022-10-11 01:57:18 +05:30
|
|
|
@commit_with_old_name_id = repository.commit_files(
|
2022-08-13 15:12:31 +05:30
|
|
|
user,
|
|
|
|
branch_name: repository.root_ref,
|
|
|
|
message: 'Update CHANGELOG',
|
|
|
|
actions: [{
|
|
|
|
action: :update,
|
|
|
|
file_path: 'CHANGELOG',
|
|
|
|
content: 'CHANGELOG'
|
|
|
|
}]
|
|
|
|
).newrev
|
2022-10-11 01:57:18 +05:30
|
|
|
@rename_commit_id = repository.commit_files(
|
2022-08-13 15:12:31 +05:30
|
|
|
user,
|
|
|
|
branch_name: repository.root_ref,
|
|
|
|
message: 'Move CHANGELOG to encoding/',
|
|
|
|
actions: [{
|
|
|
|
action: :move,
|
|
|
|
previous_path: 'CHANGELOG',
|
|
|
|
file_path: 'encoding/CHANGELOG',
|
|
|
|
content: 'CHANGELOG'
|
|
|
|
}]
|
|
|
|
).newrev
|
2022-10-11 01:57:18 +05:30
|
|
|
@commit_with_new_name_id = repository.commit_files(
|
2022-08-13 15:12:31 +05:30
|
|
|
user,
|
|
|
|
branch_name: repository.root_ref,
|
|
|
|
message: 'Edit encoding/CHANGELOG',
|
|
|
|
actions: [{
|
|
|
|
action: :update,
|
|
|
|
file_path: 'encoding/CHANGELOG',
|
|
|
|
content: "I'm a new changelog with different text"
|
|
|
|
}]
|
|
|
|
).newrev
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
after do
|
2018-03-27 19:54:05 +05:30
|
|
|
# Erase our commits so other tests get the original repo
|
2022-10-11 01:57:18 +05:30
|
|
|
repository.write_ref(repository.root_ref, TestEnv::BRANCH_SHA['master'])
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
context "where 'follow' == true" do
|
|
|
|
let(:options) { { ref: "master", follow: true } }
|
|
|
|
|
|
|
|
context "and 'path' is a directory" do
|
|
|
|
it "does not follow renames" do
|
|
|
|
log_commits = repository.log(options.merge(path: "encoding"))
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
aggregate_failures do
|
|
|
|
expect(log_commits).to include(commit_with_new_name)
|
|
|
|
expect(log_commits).to include(rename_commit)
|
2018-03-27 19:54:05 +05:30
|
|
|
expect(log_commits).not_to include(commit_with_old_name)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
context "and 'path' is a file that matches the new filename" do
|
|
|
|
context 'without offset' do
|
|
|
|
it "follows renames" do
|
|
|
|
log_commits = repository.log(options.merge(path: "encoding/CHANGELOG"))
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
aggregate_failures do
|
|
|
|
expect(log_commits).to include(commit_with_new_name)
|
|
|
|
expect(log_commits).to include(rename_commit)
|
|
|
|
expect(log_commits).to include(commit_with_old_name)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
context 'with offset=1' do
|
|
|
|
it "follows renames and skip the latest commit" do
|
|
|
|
log_commits = repository.log(options.merge(path: "encoding/CHANGELOG", offset: 1))
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
aggregate_failures do
|
|
|
|
expect(log_commits).not_to include(commit_with_new_name)
|
|
|
|
expect(log_commits).to include(rename_commit)
|
|
|
|
expect(log_commits).to include(commit_with_old_name)
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
context 'with offset=1', 'and limit=1' do
|
|
|
|
it "follows renames, skip the latest commit and return only one commit" do
|
|
|
|
log_commits = repository.log(options.merge(path: "encoding/CHANGELOG", offset: 1, limit: 1))
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
expect(log_commits).to contain_exactly(rename_commit)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
context 'with offset=1', 'and limit=2' do
|
|
|
|
it "follows renames, skip the latest commit and return only two commits" do
|
|
|
|
log_commits = repository.log(options.merge(path: "encoding/CHANGELOG", offset: 1, limit: 2))
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
aggregate_failures do
|
|
|
|
expect(log_commits).to contain_exactly(rename_commit, commit_with_old_name)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
context 'with offset=2' do
|
|
|
|
it "follows renames and skip the latest commit" do
|
|
|
|
log_commits = repository.log(options.merge(path: "encoding/CHANGELOG", offset: 2))
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
aggregate_failures do
|
|
|
|
expect(log_commits).not_to include(commit_with_new_name)
|
|
|
|
expect(log_commits).not_to include(rename_commit)
|
|
|
|
expect(log_commits).to include(commit_with_old_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with offset=2', 'and limit=1' do
|
|
|
|
it "follows renames, skip the two latest commit and return only one commit" do
|
|
|
|
log_commits = repository.log(options.merge(path: "encoding/CHANGELOG", offset: 2, limit: 1))
|
|
|
|
|
|
|
|
expect(log_commits).to contain_exactly(commit_with_old_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with offset=2', 'and limit=2' do
|
|
|
|
it "follows renames, skip the two latest commit and return only one commit" do
|
|
|
|
log_commits = repository.log(options.merge(path: "encoding/CHANGELOG", offset: 2, limit: 2))
|
|
|
|
|
|
|
|
aggregate_failures do
|
|
|
|
expect(log_commits).not_to include(commit_with_new_name)
|
|
|
|
expect(log_commits).not_to include(rename_commit)
|
|
|
|
expect(log_commits).to include(commit_with_old_name)
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
context "and 'path' is a file that matches the old filename" do
|
|
|
|
it "does not follow renames" do
|
|
|
|
log_commits = repository.log(options.merge(path: "CHANGELOG"))
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
aggregate_failures do
|
|
|
|
expect(log_commits).not_to include(commit_with_new_name)
|
2018-03-27 19:54:05 +05:30
|
|
|
expect(log_commits).to include(rename_commit)
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(log_commits).to include(commit_with_old_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
context "unknown ref" do
|
|
|
|
it "returns an empty array" do
|
|
|
|
log_commits = repository.log(options.merge(ref: 'unknown'))
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
expect(log_commits).to eq([])
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
context "where 'follow' == false" do
|
|
|
|
options = { follow: false }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
context "and 'path' is a directory" do
|
|
|
|
let(:log_commits) do
|
|
|
|
repository.log(options.merge(path: "encoding"))
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
it "does not follow renames" do
|
|
|
|
expect(log_commits).to include(commit_with_new_name)
|
|
|
|
expect(log_commits).to include(rename_commit)
|
|
|
|
expect(log_commits).not_to include(commit_with_old_name)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
context "and 'path' is a file that matches the new filename" do
|
|
|
|
let(:log_commits) do
|
|
|
|
repository.log(options.merge(path: "encoding/CHANGELOG"))
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
it "does not follow renames" do
|
|
|
|
expect(log_commits).to include(commit_with_new_name)
|
|
|
|
expect(log_commits).to include(rename_commit)
|
|
|
|
expect(log_commits).not_to include(commit_with_old_name)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
context "and 'path' is a file that matches the old filename" do
|
|
|
|
let(:log_commits) do
|
|
|
|
repository.log(options.merge(path: "CHANGELOG"))
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
it "does not follow renames" do
|
|
|
|
expect(log_commits).to include(commit_with_old_name)
|
|
|
|
expect(log_commits).to include(rename_commit)
|
|
|
|
expect(log_commits).not_to include(commit_with_new_name)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
context "where provides 'after' timestamp" do
|
|
|
|
options = { after: Time.iso8601('2014-03-03T20:15:01+00:00') }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
it "returns commits on or after that timestamp" do
|
2018-03-27 19:54:05 +05:30
|
|
|
commits = repository.log(options)
|
|
|
|
|
|
|
|
expect(commits.size).to be > 0
|
|
|
|
expect(commits).to satisfy do |commits|
|
|
|
|
commits.all? { |commit| commit.committed_date >= options[:after] }
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
context "where provides 'before' timestamp" do
|
|
|
|
options = { before: Time.iso8601('2014-03-03T20:15:01+00:00') }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
it "returns commits on or before that timestamp" do
|
2018-03-27 19:54:05 +05:30
|
|
|
commits = repository.log(options)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
expect(commits.size).to be > 0
|
|
|
|
expect(commits).to satisfy do |commits|
|
|
|
|
commits.all? { |commit| commit.committed_date <= options[:before] }
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
context 'when multiple paths are provided' do
|
|
|
|
let(:options) { { ref: 'master', path: ['PROCESS.md', 'README.md'] } }
|
|
|
|
|
|
|
|
def commit_files(commit)
|
2023-01-13 00:05:48 +05:30
|
|
|
commit.deltas.flat_map do |delta|
|
|
|
|
[delta.old_path, delta.new_path].uniq.compact
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
it 'only returns commits matching at least one path' do
|
|
|
|
commits = repository.log(options)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
expect(commits.size).to be > 0
|
|
|
|
expect(commits).to satisfy do |commits|
|
|
|
|
commits.none? { |commit| (commit_files(commit) & options[:path]).empty? }
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
context 'limit validation' do
|
|
|
|
where(:limit) do
|
|
|
|
[0, nil, '', 'foo']
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
with_them do
|
|
|
|
it { expect { repository.log(limit: limit) }.to raise_error(ArgumentError) }
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
context 'with all' do
|
|
|
|
it 'returns a list of commits' do
|
|
|
|
commits = repository.log({ all: true, limit: 50 })
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(commits.size).to eq(50)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
context 'when Gitaly find_commits feature is enabled' do
|
|
|
|
it_behaves_like 'repository log'
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
describe '#blobs' do
|
2022-10-11 01:57:18 +05:30
|
|
|
let_it_be(:commit_oid) { TestEnv::BRANCH_SHA['master'] }
|
2021-09-30 23:02:18 +05:30
|
|
|
|
|
|
|
shared_examples 'a blob enumeration' do
|
|
|
|
it 'enumerates blobs' do
|
|
|
|
blobs = repository.blobs(revisions).to_a
|
|
|
|
|
|
|
|
expect(blobs.size).to eq(expected_blobs)
|
|
|
|
blobs.each do |blob|
|
|
|
|
expect(blob.data).to be_empty
|
|
|
|
expect(blob.id.size).to be(40)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'single revision' do
|
|
|
|
let(:revisions) { [commit_oid] }
|
2022-10-11 01:57:18 +05:30
|
|
|
let(:expected_blobs) { 52 }
|
2021-09-30 23:02:18 +05:30
|
|
|
|
|
|
|
it_behaves_like 'a blob enumeration'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'multiple revisions' do
|
|
|
|
let(:revisions) { ["^#{commit_oid}~", commit_oid] }
|
|
|
|
let(:expected_blobs) { 1 }
|
|
|
|
|
|
|
|
it_behaves_like 'a blob enumeration'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'pseudo revisions' do
|
|
|
|
let(:revisions) { ['master', '--not', '--all'] }
|
|
|
|
let(:expected_blobs) { 0 }
|
|
|
|
|
|
|
|
it_behaves_like 'a blob enumeration'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'blank revisions' do
|
|
|
|
let(:revisions) { [::Gitlab::Git::BLANK_SHA] }
|
|
|
|
let(:expected_blobs) { 0 }
|
|
|
|
|
|
|
|
before do
|
|
|
|
expect_any_instance_of(Gitlab::GitalyClient::BlobService)
|
|
|
|
.not_to receive(:list_blobs)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a blob enumeration'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
describe '#new_blobs' do
|
|
|
|
let(:repository) { mutable_repository }
|
2022-10-11 01:57:18 +05:30
|
|
|
let(:commit) { create_commit('nested/new-blob.txt' => 'This is a new blob') }
|
2021-11-11 11:23:49 +05:30
|
|
|
|
|
|
|
def create_commit(blobs)
|
2022-10-11 01:57:18 +05:30
|
|
|
commit_result = repository.commit_files(
|
|
|
|
user,
|
|
|
|
branch_name: 'a-new-branch',
|
|
|
|
message: 'Add a file',
|
|
|
|
actions: blobs.map do |path, content|
|
|
|
|
{
|
|
|
|
action: :create,
|
|
|
|
file_path: path,
|
|
|
|
content: content
|
|
|
|
}
|
|
|
|
end
|
|
|
|
)
|
2021-11-11 11:23:49 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
# new_blobs only returns unreferenced blobs because it is used for hooks.
|
|
|
|
# Gitaly does not allow us to create loose objects via the RPC.
|
|
|
|
repository.delete_branch('a-new-branch')
|
2021-11-11 11:23:49 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
commit_result.newrev
|
2021-11-11 11:23:49 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
subject { repository.new_blobs(newrevs).to_a }
|
|
|
|
|
|
|
|
shared_examples '#new_blobs with revisions' do
|
|
|
|
before do
|
|
|
|
expect_next_instance_of(Gitlab::GitalyClient::BlobService) do |service|
|
|
|
|
expect(service)
|
|
|
|
.to receive(:list_blobs)
|
|
|
|
.with(expected_newrevs,
|
|
|
|
limit: Gitlab::Git::Repository::REV_LIST_COMMIT_LIMIT,
|
|
|
|
with_paths: true,
|
|
|
|
dynamic_timeout: nil)
|
|
|
|
.once
|
|
|
|
.and_call_original
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'enumerates new blobs' do
|
|
|
|
expect(subject).to match_array(expected_blobs)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'memoizes results' do
|
|
|
|
expect(subject).to match_array(expected_blobs)
|
|
|
|
expect(subject).to match_array(expected_blobs)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a single revision' do
|
|
|
|
let(:newrevs) { commit }
|
|
|
|
let(:expected_newrevs) { ['--not', '--all', '--not', newrevs] }
|
|
|
|
let(:expected_blobs) do
|
2022-10-11 01:57:18 +05:30
|
|
|
[have_attributes(class: Gitlab::Git::Blob, id: an_instance_of(String), path: 'nested/new-blob.txt', size: 18)]
|
2021-11-11 11:23:49 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like '#new_blobs with revisions'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a single-entry array' do
|
|
|
|
let(:newrevs) { [commit] }
|
|
|
|
let(:expected_newrevs) { ['--not', '--all', '--not'] + newrevs }
|
|
|
|
let(:expected_blobs) do
|
2022-10-11 01:57:18 +05:30
|
|
|
[have_attributes(class: Gitlab::Git::Blob, id: an_instance_of(String), path: 'nested/new-blob.txt', size: 18)]
|
2021-11-11 11:23:49 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like '#new_blobs with revisions'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with multiple revisions' do
|
2022-10-11 01:57:18 +05:30
|
|
|
let(:newrevs) { [commit, create_commit('another_path.txt' => 'Another blob')] }
|
2021-11-11 11:23:49 +05:30
|
|
|
let(:expected_newrevs) { ['--not', '--all', '--not'] + newrevs.sort }
|
|
|
|
let(:expected_blobs) do
|
|
|
|
[
|
2022-10-11 01:57:18 +05:30
|
|
|
have_attributes(class: Gitlab::Git::Blob, id: an_instance_of(String), path: 'nested/new-blob.txt', size: 18),
|
|
|
|
have_attributes(class: Gitlab::Git::Blob, id: an_instance_of(String), path: 'another_path.txt', size: 12)
|
2021-11-11 11:23:49 +05:30
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like '#new_blobs with revisions'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with partially blank revisions' do
|
|
|
|
let(:newrevs) { [nil, commit, Gitlab::Git::BLANK_SHA] }
|
|
|
|
let(:expected_newrevs) { ['--not', '--all', '--not', commit] }
|
|
|
|
let(:expected_blobs) do
|
|
|
|
[
|
2022-10-11 01:57:18 +05:30
|
|
|
have_attributes(class: Gitlab::Git::Blob, id: an_instance_of(String), path: 'nested/new-blob.txt', size: 18)
|
2021-11-11 11:23:49 +05:30
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like '#new_blobs with revisions'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with repeated revisions' do
|
|
|
|
let(:newrevs) { [commit, commit, commit] }
|
|
|
|
let(:expected_newrevs) { ['--not', '--all', '--not', commit] }
|
|
|
|
let(:expected_blobs) do
|
|
|
|
[
|
2022-10-11 01:57:18 +05:30
|
|
|
have_attributes(class: Gitlab::Git::Blob, id: an_instance_of(String), path: 'nested/new-blob.txt', size: 18)
|
2021-11-11 11:23:49 +05:30
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like '#new_blobs with revisions'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with preexisting commits' do
|
|
|
|
let(:newrevs) { ['refs/heads/master'] }
|
|
|
|
let(:expected_newrevs) { ['--not', '--all', '--not'] + newrevs }
|
|
|
|
let(:expected_blobs) { [] }
|
|
|
|
|
|
|
|
it_behaves_like '#new_blobs with revisions'
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples '#new_blobs without revisions' do
|
|
|
|
before do
|
|
|
|
expect(Gitlab::GitalyClient::BlobService).not_to receive(:new)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an empty array' do
|
|
|
|
expect(subject).to eq([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a single nil newrev' do
|
|
|
|
let(:newrevs) { nil }
|
|
|
|
|
|
|
|
it_behaves_like '#new_blobs without revisions'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a single zero newrev' do
|
|
|
|
let(:newrevs) { Gitlab::Git::BLANK_SHA }
|
|
|
|
|
|
|
|
it_behaves_like '#new_blobs without revisions'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an empty array' do
|
|
|
|
let(:newrevs) { [] }
|
|
|
|
|
|
|
|
it_behaves_like '#new_blobs without revisions'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with array containing only empty refs' do
|
|
|
|
let(:newrevs) { [nil, Gitlab::Git::BLANK_SHA] }
|
|
|
|
|
|
|
|
it_behaves_like '#new_blobs without revisions'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
describe '#new_commits' do
|
|
|
|
let(:repository) { mutable_repository }
|
|
|
|
let(:new_commit) do
|
2022-10-11 01:57:18 +05:30
|
|
|
commit_result = repository.commit_files(
|
|
|
|
user,
|
|
|
|
branch_name: 'a-new-branch',
|
|
|
|
message: 'Message',
|
|
|
|
actions: [{
|
|
|
|
action: :create,
|
|
|
|
file_path: 'some_file.txt',
|
|
|
|
content: 'This is a file'
|
|
|
|
}]
|
|
|
|
)
|
|
|
|
|
|
|
|
# new_commits only returns unreferenced commits because it is used for
|
|
|
|
# hooks. Gitaly does not allow us to create loose objects via the RPC.
|
|
|
|
repository.delete_branch('a-new-branch')
|
2021-09-30 23:02:18 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
commit_result.newrev
|
2021-09-30 23:02:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
let(:expected_commits) { 1 }
|
|
|
|
let(:revisions) { [new_commit] }
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
before do
|
|
|
|
expect_next_instance_of(Gitlab::GitalyClient::CommitService) do |service|
|
|
|
|
expect(service)
|
|
|
|
.to receive(:list_commits)
|
|
|
|
.with([new_commit, '--not', '--all'])
|
|
|
|
.and_call_original
|
2021-09-30 23:02:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
it 'enumerates commits' do
|
|
|
|
commits = repository.new_commits(revisions).to_a
|
2021-09-30 23:02:18 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
expect(commits.size).to eq(expected_commits)
|
|
|
|
commits.each do |commit|
|
|
|
|
expect(commit.id).to eq(new_commit)
|
|
|
|
expect(commit.message).to eq("Message")
|
2021-09-30 23:02:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe '#count_commits_between' do
|
|
|
|
subject { repository.count_commits_between('feature', 'master') }
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
it { is_expected.to eq(29) }
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
describe '#raw_changes_between' do
|
2022-08-27 11:52:29 +05:30
|
|
|
let(:old_rev) {}
|
|
|
|
let(:new_rev) {}
|
2018-11-08 19:23:39 +05:30
|
|
|
let(:changes) { repository.raw_changes_between(old_rev, new_rev) }
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
context 'initial commit' do
|
|
|
|
let(:old_rev) { Gitlab::Git::BLANK_SHA }
|
|
|
|
let(:new_rev) { '1a0b36b3cdad1d2ee32457c102a8c0b7056fa863' }
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it 'returns the changes' do
|
|
|
|
expect(changes).to be_present
|
|
|
|
expect(changes.size).to eq(3)
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
context 'with an invalid rev' do
|
|
|
|
let(:old_rev) { 'foo' }
|
|
|
|
let(:new_rev) { 'bar' }
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it 'returns an error' do
|
|
|
|
expect { changes }.to raise_error(Gitlab::Git::Repository::GitError)
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
context 'with valid revs', :aggregate_failures do
|
|
|
|
let(:old_rev) { TestEnv::BRANCH_SHA['feature'] }
|
|
|
|
let(:new_rev) { TestEnv::BRANCH_SHA['master'] }
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it 'returns the changes' do
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(changes.size).to eq(21)
|
|
|
|
expect(changes.first.operation).to eq(:deleted)
|
|
|
|
expect(changes.first.old_path).to eq('.DS_Store')
|
2018-11-08 19:23:39 +05:30
|
|
|
expect(changes.last.operation).to eq(:added)
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(changes.last.new_path).to eq('with space/README.md')
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
describe '#merge_base' do
|
2018-12-05 23:21:45 +05:30
|
|
|
where(:from, :to, :result) do
|
2022-10-11 01:57:18 +05:30
|
|
|
'master' | 'feature' | 'ae73cb07c9eeaf35924a10f713b364d32b2dd34f'
|
|
|
|
'feature' | 'master' | 'ae73cb07c9eeaf35924a10f713b364d32b2dd34f'
|
|
|
|
'master' | 'foobar' | nil
|
|
|
|
'foobar' | 'master' | nil
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
with_them do
|
|
|
|
it { expect(repository.merge_base(from, to)).to eq(result) }
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe '#count_commits' do
|
2018-11-08 19:23:39 +05:30
|
|
|
describe 'extended commit counting' do
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'with after timestamp' do
|
|
|
|
it 'returns the number of commits after timestamp' do
|
2018-03-17 18:26:18 +05:30
|
|
|
options = { ref: 'master', after: Time.iso8601('2013-03-03T20:15:01+00:00') }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(repository.count_commits(options)).to eq(37)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'with before timestamp' do
|
|
|
|
it 'returns the number of commits before timestamp' do
|
2018-03-17 18:26:18 +05:30
|
|
|
options = { ref: 'feature', before: Time.iso8601('2015-03-03T20:15:01+00:00') }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(repository.count_commits(options)).to eq(9)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
context 'with max_count' do
|
2021-02-22 17:27:13 +05:30
|
|
|
it 'returns the number of commits with path' do
|
2018-03-17 18:26:18 +05:30
|
|
|
options = { ref: 'master', max_count: 5 }
|
|
|
|
|
|
|
|
expect(repository.count_commits(options)).to eq(5)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'with path' do
|
2021-02-22 17:27:13 +05:30
|
|
|
it 'returns the number of commits with path' do
|
2018-03-17 18:26:18 +05:30
|
|
|
options = { ref: 'master', path: 'encoding' }
|
|
|
|
|
|
|
|
expect(repository.count_commits(options)).to eq(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with option :from and option :to' do
|
2022-10-11 01:57:18 +05:30
|
|
|
it 'returns the number of commits ahead for master..feature' do
|
|
|
|
options = { from: 'master', to: 'feature' }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(repository.count_commits(options)).to eq(1)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
it 'returns the number of commits ahead for feature..master' do
|
|
|
|
options = { from: 'feature', to: 'master' }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(repository.count_commits(options)).to eq(29)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'with option :left_right' do
|
2022-10-11 01:57:18 +05:30
|
|
|
it 'returns the number of commits for master..feature' do
|
|
|
|
options = { from: 'master', to: 'feature', left_right: true }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(repository.count_commits(options)).to eq([29, 1])
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'with max_count' do
|
2022-10-11 01:57:18 +05:30
|
|
|
it 'returns the number of commits' do
|
|
|
|
options = { from: 'feature', to: 'master', left_right: true, max_count: 1 }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
expect(repository.count_commits(options)).to eq([1, 1])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with max_count' do
|
|
|
|
it 'returns the number of commits up to the passed limit' do
|
|
|
|
options = { ref: 'master', max_count: 10, after: Time.iso8601('2013-03-03T20:15:01+00:00') }
|
|
|
|
|
|
|
|
expect(repository.count_commits(options)).to eq(10)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2018-03-27 19:54:05 +05:30
|
|
|
|
|
|
|
context "with all" do
|
|
|
|
it "returns the number of commits in the whole repository" do
|
|
|
|
options = { all: true }
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
expect(repository.count_commits(options)).to eq(315)
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without all or ref being specified' do
|
|
|
|
it "raises an ArgumentError" do
|
|
|
|
expect { repository.count_commits({}) }.to raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#find_branch' do
|
2019-07-07 11:18:12 +05:30
|
|
|
it 'returns a Branch for master' do
|
2018-11-18 11:00:15 +05:30
|
|
|
branch = repository.find_branch('master')
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
expect(branch).to be_a_kind_of(Gitlab::Git::Branch)
|
|
|
|
expect(branch.name).to eq('master')
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
it 'handles non-existent branch' do
|
2018-11-18 11:00:15 +05:30
|
|
|
branch = repository.find_branch('this-is-garbage')
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
expect(branch).to eq(nil)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2023-03-04 22:38:38 +05:30
|
|
|
|
|
|
|
context 'when branch is ambiguous' do
|
|
|
|
let(:ambiguous_branch) { 'prefix' }
|
|
|
|
let(:branch_with_prefix) { 'prefix/branch' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
repository.create_branch(branch_with_prefix)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
repository.delete_branch(branch_with_prefix)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil for ambiguous branch' do
|
|
|
|
expect(repository.find_branch(branch_with_prefix)).to be_a_kind_of(Gitlab::Git::Branch)
|
|
|
|
expect(repository.find_branch(ambiguous_branch)).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
describe '#branches' do
|
|
|
|
subject { repository.branches }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'with local and remote branches' do
|
2018-12-05 23:21:45 +05:30
|
|
|
let(:repository) { mutable_repository }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
before do
|
2018-12-05 23:21:45 +05:30
|
|
|
create_remote_branch('joe', 'remote_branch', 'master')
|
2020-03-13 15:44:24 +05:30
|
|
|
repository.create_branch('local_branch')
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'returns the local and remote branches' do
|
|
|
|
expect(subject.any? { |b| b.name == 'joe/remote_branch' }).to eq(true)
|
|
|
|
expect(subject.any? { |b| b.name == 'local_branch' }).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it_behaves_like 'wrapping gRPC errors', Gitlab::GitalyClient::RefService, :branches
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '#branch_count' do
|
|
|
|
it 'returns the number of branches' do
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(repository.branch_count).to eq(TestEnv::BRANCH_SHA.size)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
context 'with local and remote branches' do
|
2018-12-05 23:21:45 +05:30
|
|
|
let(:repository) { mutable_repository }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
before do
|
2018-12-05 23:21:45 +05:30
|
|
|
create_remote_branch('joe', 'remote_branch', 'master')
|
2020-03-13 15:44:24 +05:30
|
|
|
repository.create_branch('local_branch')
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it 'returns the count of local branches' do
|
|
|
|
expect(repository.branch_count).to eq(repository.local_branches.count)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
describe '#merged_branch_names' do
|
2018-12-05 23:21:45 +05:30
|
|
|
context 'when branch names are passed' do
|
|
|
|
it 'only returns the names we are asking' do
|
|
|
|
names = repository.merged_branch_names(%w[merge-test])
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
expect(names).to contain_exactly('merge-test')
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
it 'does not return unmerged branch names' do
|
|
|
|
names = repository.merged_branch_names(%w[feature])
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
expect(names).to be_empty
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
context 'when no root ref is available' do
|
|
|
|
it 'returns empty list' do
|
|
|
|
project = create(:project, :empty_repo)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
names = project.repository.merged_branch_names(%w[feature])
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
expect(names).to be_empty
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
context 'when no branch names are specified' do
|
2022-10-11 01:57:18 +05:30
|
|
|
let(:repository) { mutable_repository }
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
before do
|
2020-03-13 15:44:24 +05:30
|
|
|
repository.create_branch('identical')
|
2018-12-05 23:21:45 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
it 'returns all merged branch names except for identical one' do
|
|
|
|
names = repository.merged_branch_names
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(names).to match_array(["'test'", "branch-merged", "flatten-dir", "improve/awesome", "merge-test"])
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '#diff_stats' do
|
|
|
|
let(:left_commit_id) { 'feature' }
|
|
|
|
let(:right_commit_id) { 'master' }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
it 'returns a DiffStatsCollection' do
|
|
|
|
collection = repository.diff_stats(left_commit_id, right_commit_id)
|
|
|
|
|
|
|
|
expect(collection).to be_a(Gitlab::Git::DiffStatsCollection)
|
|
|
|
expect(collection).to be_a(Enumerable)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
it 'yields Gitaly::DiffStats objects' do
|
|
|
|
collection = repository.diff_stats(left_commit_id, right_commit_id)
|
|
|
|
|
|
|
|
expect(collection.to_a).to all(be_a(Gitaly::DiffStats))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns no Gitaly::DiffStats when SHAs are invalid' do
|
|
|
|
collection = repository.diff_stats('foo', 'bar')
|
|
|
|
|
|
|
|
expect(collection).to be_a(Gitlab::Git::DiffStatsCollection)
|
|
|
|
expect(collection).to be_a(Enumerable)
|
|
|
|
expect(collection.to_a).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns no Gitaly::DiffStats when there is a nil SHA' do
|
2019-02-15 15:39:39 +05:30
|
|
|
expect_any_instance_of(Gitlab::GitalyClient::CommitService)
|
|
|
|
.not_to receive(:diff_stats)
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
collection = repository.diff_stats(nil, 'master')
|
|
|
|
|
|
|
|
expect(collection).to be_a(Gitlab::Git::DiffStatsCollection)
|
|
|
|
expect(collection).to be_a(Enumerable)
|
|
|
|
expect(collection.to_a).to be_empty
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
it 'returns no Gitaly::DiffStats when there is a BLANK_SHA' do
|
|
|
|
expect_any_instance_of(Gitlab::GitalyClient::CommitService)
|
|
|
|
.not_to receive(:diff_stats)
|
|
|
|
|
|
|
|
collection = repository.diff_stats(Gitlab::Git::BLANK_SHA, 'master')
|
|
|
|
|
|
|
|
expect(collection).to be_a(Gitlab::Git::DiffStatsCollection)
|
|
|
|
expect(collection).to be_a(Enumerable)
|
|
|
|
expect(collection.to_a).to be_empty
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
describe '#find_changed_paths' do
|
2022-10-11 01:57:18 +05:30
|
|
|
let(:commit_1) { TestEnv::BRANCH_SHA['with-executables'] }
|
|
|
|
let(:commit_2) { TestEnv::BRANCH_SHA['master'] }
|
2021-02-22 17:27:13 +05:30
|
|
|
let(:commit_3) { '6f6d7e7ed97bb5f0054f2b1df789b39ca89b6ff9' }
|
|
|
|
let(:commit_1_files) do
|
2022-10-11 01:57:18 +05:30
|
|
|
[Gitlab::Git::ChangedPath.new(status: :ADDED, path: "files/executables/ls")]
|
2021-02-22 17:27:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
let(:commit_2_files) do
|
2022-10-11 01:57:18 +05:30
|
|
|
[Gitlab::Git::ChangedPath.new(status: :ADDED, path: "bar/branch-test.txt")]
|
2021-02-22 17:27:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
let(:commit_3_files) do
|
|
|
|
[
|
2021-03-08 18:12:59 +05:30
|
|
|
Gitlab::Git::ChangedPath.new(status: :MODIFIED, path: ".gitmodules"),
|
|
|
|
Gitlab::Git::ChangedPath.new(status: :ADDED, path: "gitlab-shell")
|
2021-02-22 17:27:13 +05:30
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a list of paths' do
|
|
|
|
collection = repository.find_changed_paths([commit_1, commit_2, commit_3])
|
|
|
|
|
|
|
|
expect(collection).to be_a(Enumerable)
|
2021-03-08 18:12:59 +05:30
|
|
|
expect(collection.as_json).to eq((commit_1_files + commit_2_files + commit_3_files).as_json)
|
2021-02-22 17:27:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns no paths when SHAs are invalid' do
|
|
|
|
collection = repository.find_changed_paths(['invalid', commit_1])
|
|
|
|
|
|
|
|
expect(collection).to be_a(Enumerable)
|
|
|
|
expect(collection.to_a).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a list of paths even when containing a blank ref' do
|
|
|
|
collection = repository.find_changed_paths([nil, commit_1])
|
|
|
|
|
|
|
|
expect(collection).to be_a(Enumerable)
|
2021-03-08 18:12:59 +05:30
|
|
|
expect(collection.as_json).to eq(commit_1_files.as_json)
|
2021-02-22 17:27:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns no paths when the commits are nil' do
|
|
|
|
expect_any_instance_of(Gitlab::GitalyClient::CommitService)
|
|
|
|
.not_to receive(:find_changed_paths)
|
|
|
|
|
|
|
|
collection = repository.find_changed_paths([nil, nil])
|
|
|
|
|
|
|
|
expect(collection).to be_a(Enumerable)
|
|
|
|
expect(collection.to_a).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
describe "#ls_files" do
|
|
|
|
let(:master_file_paths) { repository.ls_files("master") }
|
|
|
|
let(:utf8_file_paths) { repository.ls_files("ls-files-utf8") }
|
|
|
|
let(:not_existed_branch) { repository.ls_files("not_existed_branch") }
|
|
|
|
|
|
|
|
it "read every file paths of master branch" do
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(master_file_paths.length).to equal(38)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "reads full file paths of master branch" do
|
|
|
|
expect(master_file_paths).to include("files/html/500.html")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not read submodule directory and empty directory of master branch" do
|
|
|
|
expect(master_file_paths).not_to include("six")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not include 'nil'" do
|
|
|
|
expect(master_file_paths).not_to include(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns empty array when not existed branch" do
|
|
|
|
expect(not_existed_branch.length).to equal(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns valid utf-8 data" do
|
|
|
|
expect(utf8_file_paths.map { |file| file.force_encoding('utf-8') }).to all(be_valid_encoding)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#copy_gitattributes" do
|
2022-10-11 01:57:18 +05:30
|
|
|
let(:repository) { mutable_repository }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it "raises an error with invalid ref" do
|
|
|
|
expect { repository.copy_gitattributes("invalid") }.to raise_error(Gitlab::Git::Repository::InvalidRef)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
context 'when forcing encoding issues' do
|
|
|
|
let(:branch_name) { "ʕ•ᴥ•ʔ" }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
before do
|
2020-03-13 15:44:24 +05:30
|
|
|
repository.create_branch(branch_name)
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
after do
|
|
|
|
repository.rm_branch(branch_name, user: build(:admin))
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it "doesn't raise with a valid unicode ref" do
|
|
|
|
expect { repository.copy_gitattributes(branch_name) }.not_to raise_error
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
repository
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
describe '#gitattribute' do
|
2022-10-11 01:57:18 +05:30
|
|
|
let(:repository) { mutable_repository }
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
context 'with gitattributes' do
|
|
|
|
before do
|
|
|
|
repository.copy_gitattributes('gitattributes')
|
|
|
|
end
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
it 'returns matching language attribute' do
|
|
|
|
expect(repository.gitattribute("custom-highlighting/test.gitlab-custom", 'gitlab-language')).to eq('ruby')
|
|
|
|
end
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
it 'returns matching language attribute with additional options' do
|
|
|
|
expect(repository.gitattribute("custom-highlighting/test.gitlab-cgi", 'gitlab-language')).to eq('erb?parent=json')
|
|
|
|
end
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
it 'returns nil if nothing matches' do
|
|
|
|
expect(repository.gitattribute("report.xslt", 'gitlab-language')).to eq(nil)
|
|
|
|
end
|
2018-12-13 13:39:08 +05:30
|
|
|
end
|
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
context 'without gitattributes' do
|
2018-12-13 13:39:08 +05:30
|
|
|
it 'returns nil' do
|
|
|
|
expect(repository.gitattribute("README.md", 'gitlab-language')).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
describe '#ref_exists?' do
|
2018-12-05 23:21:45 +05:30
|
|
|
it 'returns true for an existing tag' do
|
|
|
|
expect(repository.ref_exists?('refs/heads/master')).to eq(true)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
it 'returns false for a non-existing tag' do
|
|
|
|
expect(repository.ref_exists?('refs/tags/THIS_TAG_DOES_NOT_EXIST')).to eq(false)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
it 'raises an ArgumentError for an empty string' do
|
|
|
|
expect { repository.ref_exists?('') }.to raise_error(ArgumentError)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
it 'raises an ArgumentError for an invalid ref' do
|
|
|
|
expect { repository.ref_exists?('INVALID') }.to raise_error(ArgumentError)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '#tag_exists?' do
|
2018-12-05 23:21:45 +05:30
|
|
|
it 'returns true for an existing tag' do
|
|
|
|
tag = repository.tag_names.first
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
expect(repository.tag_exists?(tag)).to eq(true)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
it 'returns false for a non-existing tag' do
|
|
|
|
expect(repository.tag_exists?('v9000')).to eq(false)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#branch_exists?' do
|
2018-12-05 23:21:45 +05:30
|
|
|
it 'returns true for an existing branch' do
|
|
|
|
expect(repository.branch_exists?('master')).to eq(true)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
it 'returns false for a non-existing branch' do
|
|
|
|
expect(repository.branch_exists?('kittens')).to eq(false)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
it 'returns false when using an invalid branch name' do
|
|
|
|
expect(repository.branch_exists?('.bla')).to eq(false)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe '#local_branches' do
|
2018-12-05 23:21:45 +05:30
|
|
|
let(:repository) { mutable_repository }
|
|
|
|
|
|
|
|
before do
|
|
|
|
create_remote_branch('joe', 'remote_branch', 'master')
|
2020-03-13 15:44:24 +05:30
|
|
|
repository.create_branch('local_branch')
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the local branches' do
|
2018-12-05 23:21:45 +05:30
|
|
|
expect(repository.local_branches.any? { |branch| branch.name == 'remote_branch' }).to eq(false)
|
|
|
|
expect(repository.local_branches.any? { |branch| branch.name == 'local_branch' }).to eq(true)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
it 'returns a Branch with UTF-8 fields' do
|
2018-12-05 23:21:45 +05:30
|
|
|
branches = repository.local_branches.to_a
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(branches.size).to be > 0
|
|
|
|
branches.each do |branch|
|
|
|
|
expect(branch.name).to be_utf8
|
|
|
|
expect(branch.target).to be_utf8 unless branch.target.nil?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'gets the branches from GitalyClient' do
|
|
|
|
expect_any_instance_of(Gitlab::GitalyClient::RefService).to receive(:local_branches)
|
|
|
|
.and_return([])
|
2018-12-05 23:21:45 +05:30
|
|
|
repository.local_branches
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'wrapping gRPC errors', Gitlab::GitalyClient::RefService, :local_branches do
|
2018-12-05 23:21:45 +05:30
|
|
|
subject { repository.local_branches }
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
describe '#find_tag' do
|
|
|
|
it 'returns a tag' do
|
|
|
|
tag = repository.find_tag('v1.0.0')
|
|
|
|
|
|
|
|
expect(tag).to be_a_kind_of(Gitlab::Git::Tag)
|
|
|
|
expect(tag.name).to eq('v1.0.0')
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'a nonexistent tag' do
|
|
|
|
it 'returns nil' do
|
|
|
|
expect(repository.find_tag('this-is-garbage')).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when asking for a non-existent tag' do
|
|
|
|
it_behaves_like 'a nonexistent tag'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when Gitaly returns Internal error' do
|
|
|
|
before do
|
2023-07-09 08:55:56 +05:30
|
|
|
expect(Gitlab::GitalyClient)
|
|
|
|
.to receive(:call)
|
2022-08-27 11:52:29 +05:30
|
|
|
.and_raise(GRPC::Internal, "tag not found")
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a nonexistent tag'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when Gitaly returns tag_not_found error' do
|
|
|
|
before do
|
2023-07-09 08:55:56 +05:30
|
|
|
expect(Gitlab::GitalyClient)
|
|
|
|
.to receive(:call)
|
2022-08-27 11:52:29 +05:30
|
|
|
.and_raise(new_detailed_error(GRPC::Core::StatusCodes::NOT_FOUND,
|
|
|
|
"tag was not found",
|
|
|
|
Gitaly::FindTagError.new(tag_not_found: Gitaly::ReferenceNotFoundError.new)))
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a nonexistent tag'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
describe '#languages' do
|
2018-11-08 19:23:39 +05:30
|
|
|
it 'returns exactly the expected results' do
|
2022-10-11 01:57:18 +05:30
|
|
|
languages = repository.languages(TestEnv::BRANCH_SHA['master'])
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(languages).to match_array(
|
|
|
|
[
|
|
|
|
{ value: a_value_within(0.1).of(66.7), label: "Ruby", color: "#701516", highlight: "#701516" },
|
|
|
|
{ value: a_value_within(0.1).of(22.96), label: "JavaScript", color: "#f1e05a", highlight: "#f1e05a" },
|
|
|
|
{ value: a_value_within(0.1).of(7.9), label: "HTML", color: "#e34c26", highlight: "#e34c26" },
|
|
|
|
{ value: a_value_within(0.1).of(2.51), label: "CoffeeScript", color: "#244776", highlight: "#244776" }
|
|
|
|
])
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it "uses the repository's HEAD when no ref is passed" do
|
|
|
|
lang = repository.languages.first
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
expect(lang[:label]).to eq('Ruby')
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
describe '#license' do
|
2023-05-27 22:25:52 +05:30
|
|
|
subject(:license) { repository.license }
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
context 'when no license file can be found' do
|
|
|
|
let_it_be(:project) { create(:project, :repository) }
|
|
|
|
let(:repository) { project.repository.raw_repository }
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
before do
|
|
|
|
project.repository.delete_file(project.owner, 'LICENSE', message: 'remove license', branch_name: 'master')
|
2022-11-25 23:54:43 +05:30
|
|
|
end
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
it { is_expected.to be_nil }
|
|
|
|
end
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
context 'when an mit license is found' do
|
|
|
|
it { is_expected.to have_attributes(key: 'mit') }
|
|
|
|
end
|
2022-11-25 23:54:43 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
context 'when license is not recognized ' do
|
|
|
|
let_it_be(:project) { create(:project, :repository) }
|
|
|
|
let(:repository) { project.repository.raw_repository }
|
2022-11-25 23:54:43 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
before do
|
|
|
|
project.repository.update_file(
|
|
|
|
project.owner,
|
|
|
|
'LICENSE',
|
|
|
|
'This software is licensed under the Dummy license.',
|
|
|
|
message: 'Update license',
|
|
|
|
branch_name: 'master')
|
2022-11-25 23:54:43 +05:30
|
|
|
end
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
it { is_expected.to have_attributes(key: 'other', nickname: 'LICENSE') }
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
describe '#fetch_source_branch!' do
|
2018-11-18 11:00:15 +05:30
|
|
|
let(:local_ref) { 'refs/merge-requests/1/head' }
|
2022-10-11 01:57:18 +05:30
|
|
|
let(:repository) { create(:project, :repository).repository.raw }
|
2018-12-05 23:21:45 +05:30
|
|
|
let(:source_repository) { mutable_repository }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
context 'when the branch exists' do
|
|
|
|
context 'when the commit does not exist locally' do
|
|
|
|
let(:source_branch) { 'new-branch-for-fetch-source-branch' }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
let!(:new_oid) do
|
2022-10-11 01:57:18 +05:30
|
|
|
source_repository.commit_files(
|
2022-08-13 15:12:31 +05:30
|
|
|
user,
|
|
|
|
branch_name: source_branch,
|
|
|
|
message: 'Add a file',
|
|
|
|
actions: [{
|
|
|
|
action: :create,
|
|
|
|
file_path: 'a.file',
|
|
|
|
content: 'This is a file.'
|
|
|
|
}]
|
|
|
|
).newrev
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
it 'writes the ref' do
|
|
|
|
expect(repository.fetch_source_branch!(source_repository, source_branch, local_ref)).to eq(true)
|
|
|
|
expect(repository.commit(local_ref).sha).to eq(new_oid)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
context 'when the commit exists locally' do
|
|
|
|
let(:source_branch) { 'master' }
|
2022-10-11 01:57:18 +05:30
|
|
|
let(:expected_oid) { TestEnv::BRANCH_SHA['master'] }
|
2018-11-18 11:00:15 +05:30
|
|
|
|
|
|
|
it 'writes the ref' do
|
|
|
|
# Sanity check: the commit should already exist
|
|
|
|
expect(repository.commit(expected_oid)).not_to be_nil
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
expect(repository.fetch_source_branch!(source_repository, source_branch, local_ref)).to eq(true)
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(repository.commit(local_ref).sha).to start_with(expected_oid)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
context 'when the branch does not exist' do
|
|
|
|
let(:source_branch) { 'definitely-not-master' }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
it 'does not write the ref' do
|
|
|
|
expect(repository.fetch_source_branch!(source_repository, source_branch, local_ref)).to eq(false)
|
|
|
|
expect(repository.commit(local_ref)).to be_nil
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#rm_branch' do
|
2018-12-05 23:21:45 +05:30
|
|
|
let(:project) { create(:project, :repository) }
|
|
|
|
let(:repository) { project.repository.raw }
|
|
|
|
let(:branch_name) { "to-be-deleted-soon" }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
repository.create_branch(branch_name)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
it "removes the branch from the repo" do
|
|
|
|
repository.rm_branch(branch_name, user: user)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
expect(repository.find_branch(branch_name)).to be_nil
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#write_ref' do
|
|
|
|
context 'validations' do
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
where(:ref_path, :ref) do
|
|
|
|
'foo bar' | '123'
|
|
|
|
'foobar' | "12\x003"
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
it 'raises ArgumentError' do
|
|
|
|
expect { repository.write_ref(ref_path, ref) }.to raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
it 'writes the HEAD' do
|
|
|
|
repository.write_ref('HEAD', 'refs/heads/feature')
|
|
|
|
|
|
|
|
expect(repository.commit('HEAD')).to eq(repository.commit('feature'))
|
|
|
|
expect(repository.root_ref).to eq('feature')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'writes other refs' do
|
2022-10-11 01:57:18 +05:30
|
|
|
repository.write_ref('refs/heads/feature', TestEnv::BRANCH_SHA['master'])
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(repository.commit('feature').sha).to start_with(TestEnv::BRANCH_SHA['master'])
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
describe '#list_refs' do
|
|
|
|
it 'returns a list of branches with their head commit' do
|
|
|
|
refs = repository.list_refs
|
|
|
|
reference = refs.first
|
|
|
|
|
|
|
|
expect(refs).to be_an(Enumerable)
|
|
|
|
expect(reference).to be_a(Gitaly::ListRefsResponse::Reference)
|
|
|
|
expect(reference.name).to be_a(String)
|
|
|
|
expect(reference.target).to be_a(String)
|
|
|
|
end
|
2023-04-23 21:23:45 +05:30
|
|
|
|
|
|
|
it 'filters by pattern' do
|
|
|
|
refs = repository.list_refs([Gitlab::Git::TAG_REF_PREFIX])
|
|
|
|
|
|
|
|
refs.each do |reference|
|
|
|
|
expect(reference.name).to include(Gitlab::Git::TAG_REF_PREFIX)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with pointing_at_oids and peel_tags options' do
|
|
|
|
let(:commit_id) { mutable_repository.commit.id }
|
|
|
|
let!(:annotated_tag) { mutable_repository.add_tag('annotated-tag', user: user, target: commit_id, message: 'Tag message') }
|
|
|
|
let!(:lw_tag) { mutable_repository.add_tag('lw-tag', user: user, target: commit_id) }
|
|
|
|
|
|
|
|
it 'filters by target OIDs' do
|
|
|
|
refs = mutable_repository.list_refs([Gitlab::Git::TAG_REF_PREFIX], pointing_at_oids: [commit_id])
|
|
|
|
|
|
|
|
expect(refs.length).to eq(2)
|
|
|
|
expect(refs).to contain_exactly(
|
|
|
|
Gitaly::ListRefsResponse::Reference.new(
|
|
|
|
name: "#{Gitlab::Git::TAG_REF_PREFIX}#{lw_tag.name}",
|
|
|
|
target: commit_id
|
|
|
|
),
|
|
|
|
Gitaly::ListRefsResponse::Reference.new(
|
|
|
|
name: "#{Gitlab::Git::TAG_REF_PREFIX}#{annotated_tag.name}",
|
|
|
|
target: annotated_tag.id
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns peeled_target for annotated tags' do
|
|
|
|
refs = mutable_repository.list_refs([Gitlab::Git::TAG_REF_PREFIX], pointing_at_oids: [commit_id], peel_tags: true)
|
|
|
|
|
|
|
|
expect(refs.length).to eq(2)
|
|
|
|
expect(refs).to contain_exactly(
|
|
|
|
Gitaly::ListRefsResponse::Reference.new(
|
|
|
|
name: "#{Gitlab::Git::TAG_REF_PREFIX}#{lw_tag.name}",
|
|
|
|
target: commit_id
|
|
|
|
),
|
|
|
|
Gitaly::ListRefsResponse::Reference.new(
|
|
|
|
name: "#{Gitlab::Git::TAG_REF_PREFIX}#{annotated_tag.name}",
|
|
|
|
target: annotated_tag.id,
|
|
|
|
peeled_target: commit_id
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2021-12-11 22:18:48 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '#refs_by_oid' do
|
|
|
|
it 'returns a list of refs from a OID' do
|
|
|
|
refs = repository.refs_by_oid(oid: repository.commit.id)
|
|
|
|
|
|
|
|
expect(refs).to be_an(Array)
|
|
|
|
expect(refs).to include(Gitlab::Git::BRANCH_REF_PREFIX + repository.root_ref)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a single ref from a OID' do
|
|
|
|
refs = repository.refs_by_oid(oid: repository.commit.id, limit: 1)
|
|
|
|
|
|
|
|
expect(refs).to be_an(Array)
|
|
|
|
expect(refs).to eq([Gitlab::Git::BRANCH_REF_PREFIX + repository.root_ref])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns empty for unknown ID' do
|
|
|
|
expect(repository.refs_by_oid(oid: Gitlab::Git::BLANK_SHA, limit: 0)).to eq([])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil for an empty repo' do
|
|
|
|
project = create(:project)
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(project.repository.refs_by_oid(oid: TestEnv::BRANCH_SHA['master'], limit: 0)).to be_nil
|
2021-12-11 22:18:48 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
describe '#set_full_path' do
|
2022-10-11 01:57:18 +05:30
|
|
|
let(:full_path) { 'some/path' }
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
before do
|
2022-10-11 01:57:18 +05:30
|
|
|
repository.set_full_path(full_path: full_path)
|
2021-11-11 11:23:49 +05:30
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
it 'writes full_path to gitaly' do
|
|
|
|
repository.set_full_path(full_path: "not-the/real-path.git")
|
2021-10-27 15:23:28 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(repository.full_path).to eq('not-the/real-path.git')
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
context 'it is given an empty path' do
|
|
|
|
it 'does not write it to disk' do
|
|
|
|
repository.set_full_path(full_path: "")
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(repository.full_path).to eq(full_path)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
context 'repository does not exist' do
|
2022-08-27 11:52:29 +05:30
|
|
|
it 'raises NoRepository and does not call SetFullPath' do
|
2021-11-11 11:23:49 +05:30
|
|
|
repository = Gitlab::Git::Repository.new('default', 'does/not/exist.git', '', 'group/project')
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
expect(repository.gitaly_repository_client).not_to receive(:set_full_path)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
expect do
|
|
|
|
repository.set_full_path(full_path: 'foo/bar.git')
|
|
|
|
end.to raise_error(Gitlab::Git::Repository::NoRepository)
|
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
describe '#full_path' do
|
|
|
|
let(:full_path) { 'some/path' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
repository.set_full_path(full_path: full_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the full path' do
|
|
|
|
expect(repository.full_path).to eq(full_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
describe '#merge_to_ref' do
|
|
|
|
let(:repository) { mutable_repository }
|
|
|
|
let(:branch_head) { '6d394385cf567f80a8fd85055db1ab4c5295806f' }
|
|
|
|
let(:left_sha) { 'cfe32cf61b73a0d5e9f13e774abde7ff789b1660' }
|
|
|
|
let(:right_branch) { 'test-master' }
|
2019-09-30 21:07:59 +05:30
|
|
|
let(:first_parent_ref) { 'refs/heads/test-master' }
|
2019-07-07 11:18:12 +05:30
|
|
|
let(:target_ref) { 'refs/merge-requests/999/merge' }
|
|
|
|
|
|
|
|
before do
|
2019-09-30 21:07:59 +05:30
|
|
|
repository.create_branch(right_branch, branch_head) unless repository.ref_exists?(first_parent_ref)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def merge_to_ref
|
2021-04-29 21:17:54 +05:30
|
|
|
repository.merge_to_ref(user,
|
|
|
|
source_sha: left_sha, branch: right_branch, target_ref: target_ref,
|
|
|
|
message: 'Merge message', first_parent_ref: first_parent_ref)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'generates a commit in the target_ref' do
|
|
|
|
expect(repository.ref_exists?(target_ref)).to be(false)
|
|
|
|
|
|
|
|
commit_sha = merge_to_ref
|
|
|
|
ref_head = repository.commit(target_ref)
|
|
|
|
|
|
|
|
expect(commit_sha).to be_present
|
|
|
|
expect(repository.ref_exists?(target_ref)).to be(true)
|
|
|
|
expect(ref_head.id).to eq(commit_sha)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not change the right branch HEAD' do
|
2019-09-30 21:07:59 +05:30
|
|
|
expect { merge_to_ref }.not_to change { repository.commit(first_parent_ref).sha }
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
describe '#merge' do
|
2018-12-05 23:21:45 +05:30
|
|
|
let(:repository) { mutable_repository }
|
2018-03-17 18:26:18 +05:30
|
|
|
let(:source_sha) { '913c66a37b4a45b9769037c55c2d238bd0942d2e' }
|
|
|
|
let(:target_branch) { 'test-merge-target-branch' }
|
2023-04-23 21:23:45 +05:30
|
|
|
let(:target_sha) { '6d394385cf567f80a8fd85055db1ab4c5295806f' }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
before do
|
2023-04-23 21:23:45 +05:30
|
|
|
repository.create_branch(target_branch, target_sha)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
it 'can perform a merge' do
|
|
|
|
merge_commit_id = nil
|
2023-04-23 21:23:45 +05:30
|
|
|
result =
|
|
|
|
repository.merge(user,
|
|
|
|
source_sha: source_sha,
|
|
|
|
target_branch: target_branch,
|
|
|
|
target_sha: target_sha,
|
|
|
|
message: 'Test merge') do |commit_id|
|
|
|
|
merge_commit_id = commit_id
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
expect(result.newrev).to eq(merge_commit_id)
|
|
|
|
expect(result.repo_created).to eq(false)
|
|
|
|
expect(result.branch_created).to eq(false)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
it 'returns nil if there was a concurrent branch update' do
|
|
|
|
concurrent_update_id = '33f3729a45c02fc67d00adb1b8bca394b0e761d9'
|
2023-04-23 21:23:45 +05:30
|
|
|
result =
|
|
|
|
repository.merge(user,
|
|
|
|
source_sha: source_sha,
|
|
|
|
target_branch: target_branch,
|
|
|
|
target_sha: target_sha,
|
|
|
|
message: 'Test merge') do |_commit_id|
|
|
|
|
# This ref update should make the merge fail
|
|
|
|
repository.write_ref(Gitlab::Git::BRANCH_REF_PREFIX + target_branch, concurrent_update_id)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# This 'nil' signals that the merge was not applied
|
|
|
|
expect(result).to be_nil
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# Our concurrent ref update should not have been undone
|
|
|
|
expect(repository.find_branch(target_branch).target).to eq(concurrent_update_id)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#ff_merge' do
|
2018-12-05 23:21:45 +05:30
|
|
|
let(:repository) { mutable_repository }
|
2018-03-17 18:26:18 +05:30
|
|
|
let(:branch_head) { '6d394385cf567f80a8fd85055db1ab4c5295806f' }
|
|
|
|
let(:source_sha) { 'cfe32cf61b73a0d5e9f13e774abde7ff789b1660' }
|
|
|
|
let(:target_branch) { 'test-ff-target-branch' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
repository.create_branch(target_branch, branch_head)
|
|
|
|
end
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
subject do
|
|
|
|
repository.ff_merge(user,
|
|
|
|
source_sha: source_sha,
|
|
|
|
target_branch: target_branch,
|
|
|
|
target_sha: branch_head
|
|
|
|
)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
shared_examples '#ff_merge' do
|
|
|
|
it 'performs a ff_merge' do
|
|
|
|
expect(subject.newrev).to eq(source_sha)
|
|
|
|
expect(subject.repo_created).to be(false)
|
|
|
|
expect(subject.branch_created).to be(false)
|
|
|
|
|
|
|
|
expect(repository.commit(target_branch).id).to eq(source_sha)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a non-existing target branch' do
|
2023-04-23 21:23:45 +05:30
|
|
|
subject { repository.ff_merge(user, source_sha: source_sha, target_branch: 'this-isnt-real') }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
it 'throws an ArgumentError' do
|
|
|
|
expect { subject }.to raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a non-existing source commit' do
|
|
|
|
let(:source_sha) { 'f001' }
|
|
|
|
|
|
|
|
it 'throws an ArgumentError' do
|
|
|
|
expect { subject }.to raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the source sha is not a descendant of the branch head' do
|
|
|
|
let(:source_sha) { '1a0b36b3cdad1d2ee32457c102a8c0b7056fa863' }
|
|
|
|
|
|
|
|
it "doesn't perform the ff_merge" do
|
|
|
|
expect { subject }.to raise_error(Gitlab::Git::CommitError)
|
|
|
|
|
|
|
|
expect(repository.commit(target_branch).id).to eq(branch_head)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it "calls Gitaly's OperationService" do
|
|
|
|
expect_any_instance_of(Gitlab::GitalyClient::OperationService)
|
2023-04-23 21:23:45 +05:30
|
|
|
.to receive(:user_ff_branch).with(
|
|
|
|
user, source_sha: source_sha, target_branch: target_branch, target_sha: branch_head
|
|
|
|
).and_return(nil)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
subject
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it_behaves_like '#ff_merge'
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '#delete_all_refs_except' do
|
2018-12-05 23:21:45 +05:30
|
|
|
let(:repository) { mutable_repository }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
before do
|
2022-10-11 01:57:18 +05:30
|
|
|
repository.write_ref("refs/delete/a", TestEnv::BRANCH_SHA['master'])
|
|
|
|
repository.write_ref("refs/also-delete/b", TestEnv::BRANCH_SHA['master'])
|
|
|
|
repository.write_ref("refs/keep/c", TestEnv::BRANCH_SHA['master'])
|
|
|
|
repository.write_ref("refs/also-keep/d", TestEnv::BRANCH_SHA['master'])
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'deletes all refs except those with the specified prefixes' do
|
|
|
|
repository.delete_all_refs_except(%w(refs/keep refs/also-keep refs/heads))
|
|
|
|
expect(repository.ref_exists?("refs/delete/a")).to be(false)
|
|
|
|
expect(repository.ref_exists?("refs/also-delete/b")).to be(false)
|
|
|
|
expect(repository.ref_exists?("refs/keep/c")).to be(true)
|
|
|
|
expect(repository.ref_exists?("refs/also-keep/d")).to be(true)
|
|
|
|
expect(repository.ref_exists?("refs/heads/master")).to be(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#bundle_to_disk' do
|
2018-12-05 23:21:45 +05:30
|
|
|
let(:save_path) { File.join(Dir.tmpdir, "repo-#{SecureRandom.hex}.bundle") }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
after do
|
|
|
|
FileUtils.rm_rf(save_path)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
it 'saves a bundle to disk' do
|
|
|
|
repository.bundle_to_disk(save_path)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(File).to exist(save_path)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#create_from_bundle' do
|
2019-01-20 21:35:32 +05:30
|
|
|
let(:valid_bundle_path) { File.join(Dir.tmpdir, "repo-#{SecureRandom.hex}.bundle") }
|
|
|
|
let(:malicious_bundle_path) { Rails.root.join('spec/fixtures/malicious.bundle') }
|
2018-11-08 19:23:39 +05:30
|
|
|
let(:project) { create(:project) }
|
|
|
|
let(:imported_repo) { project.repository.raw }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
before do
|
2019-01-20 21:35:32 +05:30
|
|
|
expect(repository.bundle_to_disk(valid_bundle_path)).to be_truthy
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
after do
|
2019-01-20 21:35:32 +05:30
|
|
|
FileUtils.rm_rf(valid_bundle_path)
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it 'creates a repo from a bundle file' do
|
|
|
|
expect(imported_repo).not_to exist
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2019-01-20 21:35:32 +05:30
|
|
|
result = imported_repo.create_from_bundle(valid_bundle_path)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
expect(result).to be_truthy
|
|
|
|
expect(imported_repo).to exist
|
|
|
|
expect { imported_repo.fsck }.not_to raise_exception
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2019-01-20 21:35:32 +05:30
|
|
|
it 'raises an error if the bundle is an attempted malicious payload' do
|
|
|
|
expect do
|
|
|
|
imported_repo.create_from_bundle(malicious_bundle_path)
|
|
|
|
end.to raise_error(::Gitlab::Git::BundleFile::InvalidBundleError)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
describe '#compare_source_branch' do
|
2023-03-04 22:38:38 +05:30
|
|
|
it 'compares two branches cross repo' do
|
|
|
|
mutable_repository.commit_files(
|
|
|
|
user,
|
|
|
|
branch_name: mutable_repository.root_ref, message: 'Committing something',
|
|
|
|
actions: [{ action: :create, file_path: 'encoding/CHANGELOG', content: 'New file' }]
|
|
|
|
)
|
|
|
|
|
|
|
|
repository.commit_files(
|
|
|
|
user,
|
|
|
|
branch_name: repository.root_ref, message: 'Commit to root ref',
|
|
|
|
actions: [{ action: :create, file_path: 'encoding/CHANGELOG', content: 'One more' }]
|
|
|
|
)
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
[
|
|
|
|
[repository, mutable_repository, true],
|
|
|
|
[repository, mutable_repository, false],
|
|
|
|
[mutable_repository, repository, true],
|
|
|
|
[mutable_repository, repository, false]
|
|
|
|
].each do |source_repo, target_repo, straight|
|
|
|
|
raw_compare = target_repo.compare_source_branch(
|
|
|
|
target_repo.root_ref, source_repo, source_repo.root_ref, straight: straight)
|
|
|
|
|
|
|
|
expect(raw_compare).to be_a(::Gitlab::Git::Compare)
|
|
|
|
|
|
|
|
expect(raw_compare.commits).to eq([source_repo.commit])
|
|
|
|
expect(raw_compare.head).to eq(source_repo.commit)
|
|
|
|
expect(raw_compare.base).to eq(target_repo.commit)
|
|
|
|
expect(raw_compare.straight).to eq(straight)
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
2023-03-04 22:38:38 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'source ref does not exist in source repo' do
|
|
|
|
it 'returns an empty comparison' do
|
|
|
|
expect_next_instance_of(::Gitlab::Git::CrossRepo) do |instance|
|
|
|
|
expect(instance).not_to receive(:fetch_source_branch!)
|
|
|
|
end
|
|
|
|
|
|
|
|
raw_compare = repository.compare_source_branch(
|
|
|
|
repository.root_ref, mutable_repository, 'does-not-exist', straight: true)
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
expect(raw_compare).to be_a(::Gitlab::Git::Compare)
|
|
|
|
expect(raw_compare.commits.size).to eq(0)
|
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
describe '#checksum' do
|
2018-11-08 19:23:39 +05:30
|
|
|
it 'calculates the checksum for non-empty repo' do
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(repository.checksum.length).to be(40)
|
|
|
|
expect(Gitlab::Git.blank_ref?(repository.checksum)).to be false
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
it 'returns a blank sha for an empty repo' do
|
|
|
|
repository = create(:project, :empty_repo).repository
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(Gitlab::Git.blank_ref?(repository.checksum)).to be true
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
it 'raises NoRepository for a non-existent repo' do
|
|
|
|
repository = create(:project).repository
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
expect do
|
|
|
|
repository.checksum
|
|
|
|
end.to raise_error(described_class::NoRepository)
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
describe '#replicas', :praefect do
|
|
|
|
it 'gets the replica checksum through praefect' do
|
|
|
|
resp = repository.replicas
|
|
|
|
|
|
|
|
expect(resp.replicas).to be_empty
|
|
|
|
expect(resp.primary.checksum).to eq(repository.checksum)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
def create_remote_branch(remote_name, branch_name, source_branch_name)
|
2022-08-13 15:12:31 +05:30
|
|
|
source_branch = repository.find_branch(source_branch_name)
|
|
|
|
repository.write_ref("refs/remotes/#{remote_name}/#{branch_name}", source_branch.dereferenced_target.sha)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
|
|
|
|
describe '#disconnect_alternates' do
|
2022-10-11 01:57:18 +05:30
|
|
|
let(:project) { mutable_project }
|
|
|
|
let(:repository) { mutable_repository }
|
2019-07-31 22:56:46 +05:30
|
|
|
let(:pool_repository) { create(:pool_repository) }
|
|
|
|
let(:object_pool) { pool_repository.object_pool }
|
|
|
|
|
|
|
|
before do
|
2021-01-03 14:25:43 +05:30
|
|
|
object_pool.create # rubocop:disable Rails/SaveBang
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not raise an error when disconnecting a non-linked repository' do
|
|
|
|
expect { repository.disconnect_alternates }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can still access objects in the object pool' do
|
|
|
|
object_pool.link(repository)
|
2022-10-11 01:57:18 +05:30
|
|
|
new_commit_id = object_pool.repository.commit_files(
|
2022-08-13 15:12:31 +05:30
|
|
|
project.owner,
|
|
|
|
branch_name: object_pool.repository.root_ref,
|
|
|
|
message: 'Add a file',
|
|
|
|
actions: [{
|
|
|
|
action: :create,
|
|
|
|
file_path: 'a.file',
|
|
|
|
content: 'This is a file.'
|
|
|
|
}]
|
|
|
|
).newrev
|
|
|
|
|
|
|
|
expect(repository.commit(new_commit_id).id).to eq(new_commit_id)
|
2019-07-31 22:56:46 +05:30
|
|
|
|
|
|
|
repository.disconnect_alternates
|
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
expect(repository.commit(new_commit_id).id).to eq(new_commit_id)
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
|
|
|
end
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
describe '#rename' do
|
2022-10-11 01:57:18 +05:30
|
|
|
let(:repository) { mutable_repository }
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
it 'moves the repository' do
|
|
|
|
checksum = repository.checksum
|
|
|
|
new_relative_path = "rename_test/relative/path"
|
|
|
|
renamed_repository = Gitlab::Git::Repository.new(repository.storage, new_relative_path, nil, nil)
|
|
|
|
|
|
|
|
repository.rename(new_relative_path)
|
|
|
|
|
|
|
|
expect(renamed_repository.checksum).to eq(checksum)
|
|
|
|
expect(repository.exists?).to be false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#remove' do
|
2022-10-11 01:57:18 +05:30
|
|
|
let(:repository) { mutable_repository }
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
it 'removes the repository' do
|
|
|
|
expect(repository.exists?).to be true
|
|
|
|
|
|
|
|
repository.remove
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(repository.exists?).to be false
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the repository does not exist' do
|
|
|
|
let(:repository) { create(:project).repository }
|
|
|
|
|
|
|
|
it 'is idempotent' do
|
|
|
|
expect(repository.exists?).to be false
|
|
|
|
|
|
|
|
repository.remove
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(repository.exists?).to be false
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
describe '#import_repository' do
|
2022-10-11 01:57:18 +05:30
|
|
|
let_it_be(:repository) { create(:project).repository }
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
let(:url) { 'http://invalid.invalid' }
|
|
|
|
|
|
|
|
it 'raises an error if a relative path is provided' do
|
|
|
|
expect { repository.import_repository('/foo') }.to raise_error(ArgumentError, /disk path/)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error if an absolute path is provided' do
|
|
|
|
expect { repository.import_repository('./foo') }.to raise_error(ArgumentError, /disk path/)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'delegates to Gitaly' do
|
|
|
|
expect_next_instance_of(Gitlab::GitalyClient::RepositoryService) do |svc|
|
2023-01-10 11:22:00 +05:30
|
|
|
expect(svc).to receive(:import_repository).with(url, http_authorization_header: '', mirror: false, resolved_address: '').and_return(nil)
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
repository.import_repository(url)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'wrapping gRPC errors', Gitlab::GitalyClient::RepositoryService, :import_repository do
|
|
|
|
subject { repository.import_repository('http://invalid.invalid') }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#replicate' do
|
|
|
|
let(:new_repository) do
|
2022-10-11 01:57:18 +05:30
|
|
|
Gitlab::Git::Repository.new('test_second_storage', repository.relative_path, '', 'group/project')
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
2020-10-24 23:57:45 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
subject { new_repository.replicate(repository) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_storage_settings('test_second_storage' => {
|
|
|
|
'gitaly_address' => Gitlab.config.repositories.storages.default.gitaly_address,
|
|
|
|
'path' => TestEnv::SECOND_STORAGE_PATH
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
new_repository.remove
|
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
context 'destination does not exist' do
|
|
|
|
it 'mirrors the source repository' do
|
|
|
|
subject
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
expect(new_repository.list_refs(['refs/'])).to eq(repository.list_refs(['refs/']))
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
context 'destination exists' do
|
2020-04-08 14:13:33 +05:30
|
|
|
before do
|
2020-07-28 23:09:34 +05:30
|
|
|
new_repository.create_repository
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
it 'mirrors the source repository' do
|
2020-04-08 14:13:33 +05:30
|
|
|
subject
|
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
expect(new_repository.list_refs(['refs/'])).to eq(repository.list_refs(['refs/']))
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'with keep-around refs' do
|
2022-10-11 01:57:18 +05:30
|
|
|
let(:repository) { mutable_repository }
|
|
|
|
let(:sha) { TestEnv::BRANCH_SHA['master'] }
|
2020-07-28 23:09:34 +05:30
|
|
|
let(:keep_around_ref) { "refs/keep-around/#{sha}" }
|
|
|
|
let(:tmp_ref) { "refs/tmp/#{SecureRandom.hex}" }
|
|
|
|
|
|
|
|
before do
|
|
|
|
repository.write_ref(keep_around_ref, sha)
|
|
|
|
repository.write_ref(tmp_ref, sha)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes the temporary and keep-around refs' do
|
|
|
|
subject
|
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
expect(new_repository.list_refs([keep_around_ref]).map(&:name)).to match_array([keep_around_ref])
|
|
|
|
expect(new_repository.list_refs([tmp_ref]).map(&:name)).to match_array([tmp_ref])
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-03-04 22:38:38 +05:30
|
|
|
|
|
|
|
describe '#check_objects_exist' do
|
|
|
|
it 'returns hash specifying which object exists in repo' do
|
|
|
|
refs_exist = %w(
|
|
|
|
b83d6e391c22777fca1ed3012fce84f633d7fed0
|
|
|
|
498214de67004b1da3d820901307bed2a68a8ef6
|
|
|
|
1b12f15a11fc6e62177bef08f47bc7b5ce50b141
|
|
|
|
)
|
|
|
|
refs_dont_exist = %w(
|
|
|
|
1111111111111111111111111111111111111111
|
|
|
|
2222222222222222222222222222222222222222
|
|
|
|
)
|
|
|
|
object_existence_map = repository.check_objects_exist(refs_exist + refs_dont_exist)
|
|
|
|
expect(object_existence_map).to eq({
|
|
|
|
'b83d6e391c22777fca1ed3012fce84f633d7fed0' => true,
|
|
|
|
'498214de67004b1da3d820901307bed2a68a8ef6' => true,
|
|
|
|
'1b12f15a11fc6e62177bef08f47bc7b5ce50b141' => true,
|
|
|
|
'1111111111111111111111111111111111111111' => false,
|
|
|
|
'2222222222222222222222222222222222222222' => false
|
|
|
|
})
|
|
|
|
expect(object_existence_map.keys).to eq(refs_exist + refs_dont_exist)
|
|
|
|
|
|
|
|
single_sha = 'b83d6e391c22777fca1ed3012fce84f633d7fed0'
|
|
|
|
expect(repository.check_objects_exist(single_sha)).to eq({ single_sha => true })
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|