diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-07-20 15:40:28 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-07-20 15:40:28 +0000 |
commit | b595cb0c1dec83de5bdee18284abe86614bed33b (patch) | |
tree | 8c3d4540f193c5ff98019352f554e921b3a41a72 /spec/lib/gitlab/git | |
parent | 2f9104a328fc8a4bddeaa4627b595166d24671d0 (diff) | |
download | gitlab-ce-b595cb0c1dec83de5bdee18284abe86614bed33b.tar.gz |
Add latest changes from gitlab-org/gitlab@15-2-stable-eev15.2.0-rc42
Diffstat (limited to 'spec/lib/gitlab/git')
-rw-r--r-- | spec/lib/gitlab/git/attributes_at_ref_parser_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/gitlab/git/attributes_parser_spec.rb | 14 | ||||
-rw-r--r-- | spec/lib/gitlab/git/blame_spec.rb | 16 | ||||
-rw-r--r-- | spec/lib/gitlab/git/branch_spec.rb | 25 | ||||
-rw-r--r-- | spec/lib/gitlab/git/commit_spec.rb | 75 | ||||
-rw-r--r-- | spec/lib/gitlab/git/conflict/parser_spec.rb | 79 | ||||
-rw-r--r-- | spec/lib/gitlab/git/object_pool_spec.rb | 12 | ||||
-rw-r--r-- | spec/lib/gitlab/git/repository_spec.rb | 266 | ||||
-rw-r--r-- | spec/lib/gitlab/git/tree_spec.rb | 56 |
9 files changed, 269 insertions, 276 deletions
diff --git a/spec/lib/gitlab/git/attributes_at_ref_parser_spec.rb b/spec/lib/gitlab/git/attributes_at_ref_parser_spec.rb index 96cd70b4ff1..a5115989e6b 100644 --- a/spec/lib/gitlab/git/attributes_at_ref_parser_spec.rb +++ b/spec/lib/gitlab/git/attributes_at_ref_parser_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe Gitlab::Git::AttributesAtRefParser, :seed_helper do +RSpec.describe Gitlab::Git::AttributesAtRefParser do let(:project) { create(:project, :repository) } let(:repository) { project.repository } diff --git a/spec/lib/gitlab/git/attributes_parser_spec.rb b/spec/lib/gitlab/git/attributes_parser_spec.rb index 4bc39921e85..295d62fa052 100644 --- a/spec/lib/gitlab/git/attributes_parser_spec.rb +++ b/spec/lib/gitlab/git/attributes_parser_spec.rb @@ -2,9 +2,8 @@ require 'spec_helper' -RSpec.describe Gitlab::Git::AttributesParser, :seed_helper do - let(:attributes_path) { File.join(SEED_STORAGE_PATH, 'with-git-attributes.git', 'info', 'attributes') } - let(:data) { File.read(attributes_path) } +RSpec.describe Gitlab::Git::AttributesParser do + let(:data) { fixture_file('gitlab/git/gitattributes') } subject { described_class.new(data) } @@ -141,11 +140,12 @@ RSpec.describe Gitlab::Git::AttributesParser, :seed_helper do expect { |b| subject.each_line(&b) }.to yield_successive_args(*args) end - it 'does not yield when the attributes file has an unsupported encoding' do - path = File.join(SEED_STORAGE_PATH, 'with-invalid-git-attributes.git', 'info', 'attributes') - attrs = described_class.new(File.read(path)) + context 'unsupported encoding' do + let(:data) { fixture_file('gitlab/git/gitattributes_invalid') } - expect { |b| attrs.each_line(&b) }.not_to yield_control + it 'does not yield' do + expect { |b| subject.each_line(&b) }.not_to yield_control + end end end end diff --git a/spec/lib/gitlab/git/blame_spec.rb b/spec/lib/gitlab/git/blame_spec.rb index 7dd7460b142..e514e128785 100644 --- a/spec/lib/gitlab/git/blame_spec.rb +++ b/spec/lib/gitlab/git/blame_spec.rb @@ -2,10 +2,10 @@ require "spec_helper" -RSpec.describe Gitlab::Git::Blame, :seed_helper do - let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH, '', 'group/project') } - - let(:sha) { SeedRepo::Commit::ID } +RSpec.describe Gitlab::Git::Blame do + let(:project) { create(:project, :repository) } + let(:repository) { project.repository.raw } + let(:sha) { TestEnv::BRANCH_SHA['master'] } let(:path) { 'CONTRIBUTING.md' } let(:range) { nil } @@ -37,19 +37,17 @@ RSpec.describe Gitlab::Git::Blame, :seed_helper do end context "ISO-8859 encoding" do - let(:sha) { SeedRepo::EncodingCommit::ID } let(:path) { 'encoding/iso8859.txt' } it 'converts to UTF-8' do expect(result.size).to eq(1) expect(result.first[:commit]).to be_kind_of(Gitlab::Git::Commit) - expect(result.first[:line]).to eq("Ä ü") + expect(result.first[:line]).to eq("Äü") expect(result.first[:line]).to be_utf8 end end context "unknown encoding" do - let(:sha) { SeedRepo::EncodingCommit::ID } let(:path) { 'encoding/iso8859.txt' } it 'converts to UTF-8' do @@ -59,14 +57,12 @@ RSpec.describe Gitlab::Git::Blame, :seed_helper do expect(result.size).to eq(1) expect(result.first[:commit]).to be_kind_of(Gitlab::Git::Commit) - expect(result.first[:line]).to eq(" ") + expect(result.first[:line]).to eq("") expect(result.first[:line]).to be_utf8 end end context "renamed file" do - let(:project) { create(:project, :repository) } - let(:repository) { project.repository.raw_repository } let(:commit) { project.commit('blame-on-renamed') } let(:sha) { commit.id } let(:path) { 'files/plain_text/renamed' } diff --git a/spec/lib/gitlab/git/branch_spec.rb b/spec/lib/gitlab/git/branch_spec.rb index 3cc52863976..97cd4777b4d 100644 --- a/spec/lib/gitlab/git/branch_spec.rb +++ b/spec/lib/gitlab/git/branch_spec.rb @@ -4,9 +4,6 @@ require "spec_helper" RSpec.describe Gitlab::Git::Branch, :seed_helper do let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH, '', 'group/project') } - let(:rugged) do - Rugged::Repository.new(File.join(TestEnv.repos_path, repository.relative_path)) - end subject { repository.branches } @@ -81,20 +78,6 @@ RSpec.describe Gitlab::Git::Branch, :seed_helper do end let(:user) { create(:user) } - let(:committer) { { email: user.email, name: user.name } } - let(:params) do - parents = [rugged.head.target] - tree = parents.first.tree - - { - message: +'commit message', - author: committer, - committer: committer, - tree: tree, - parents: parents - } - end - let(:stale_sha) { travel_to(Gitlab::Git::Branch::STALE_BRANCH_THRESHOLD.ago - 5.days) { create_commit } } let(:active_sha) { travel_to(Gitlab::Git::Branch::STALE_BRANCH_THRESHOLD.ago + 5.days) { create_commit } } let(:future_sha) { travel_to(100.days.since) { create_commit } } @@ -137,7 +120,11 @@ RSpec.describe Gitlab::Git::Branch, :seed_helper do it { expect(repository.branches.size).to eq(SeedRepo::Repo::BRANCHES.size) } def create_commit - params[:message].delete!(+"\r") - Rugged::Commit.create(rugged, params.merge(committer: committer.merge(time: Time.now))) + repository.multi_action( + user, + branch_name: 'HEAD', + message: 'commit message', + actions: [] + ).newrev end end diff --git a/spec/lib/gitlab/git/commit_spec.rb b/spec/lib/gitlab/git/commit_spec.rb index de342444c15..da77d8ee5d6 100644 --- a/spec/lib/gitlab/git/commit_spec.rb +++ b/spec/lib/gitlab/git/commit_spec.rb @@ -3,68 +3,8 @@ require "spec_helper" RSpec.describe Gitlab::Git::Commit, :seed_helper do - include GitHelpers - let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH, '', 'group/project') } - let(:rugged_repo) do - Rugged::Repository.new(File.join(TestEnv.repos_path, TEST_REPO_PATH)) - end - let(:commit) { described_class.find(repository, SeedRepo::Commit::ID) } - let(:rugged_commit) { rugged_repo.lookup(SeedRepo::Commit::ID) } - - describe "Commit info" do - before do - @committer = { - email: 'mike@smith.com', - name: "Mike Smith", - time: Time.new(2000, 1, 1, 0, 0, 0, "+08:00") - } - - @author = { - email: 'john@smith.com', - name: "John Smith", - time: Time.new(2000, 1, 1, 0, 0, 0, "-08:00") - } - - @parents = [rugged_repo.head.target] - @gitlab_parents = @parents.map { |c| described_class.find(repository, c.oid) } - @tree = @parents.first.tree - - sha = Rugged::Commit.create( - rugged_repo, - author: @author, - committer: @committer, - tree: @tree, - parents: @parents, - message: "Refactoring specs", - update_ref: "HEAD" - ) - - @raw_commit = rugged_repo.lookup(sha) - @commit = described_class.find(repository, sha) - end - - it { expect(@commit.short_id).to eq(@raw_commit.oid[0..10]) } - it { expect(@commit.id).to eq(@raw_commit.oid) } - it { expect(@commit.sha).to eq(@raw_commit.oid) } - it { expect(@commit.safe_message).to eq(@raw_commit.message) } - it { expect(@commit.created_at).to eq(@raw_commit.committer[:time]) } - it { expect(@commit.date).to eq(@raw_commit.committer[:time]) } - it { expect(@commit.author_email).to eq(@author[:email]) } - it { expect(@commit.author_name).to eq(@author[:name]) } - it { expect(@commit.committer_name).to eq(@committer[:name]) } - it { expect(@commit.committer_email).to eq(@committer[:email]) } - it { expect(@commit.different_committer?).to be_truthy } - it { expect(@commit.parents).to eq(@gitlab_parents) } - it { expect(@commit.parent_id).to eq(@parents.first.oid) } - it { expect(@commit.no_commit_message).to eq("No commit message") } - - after do - # Erase the new commit so other tests get the original repo - rugged_repo.references.update("refs/heads/master", SeedRepo::LastCommit::ID) - end - end describe "Commit info from gitaly commit" do let(:subject) { (+"My commit").force_encoding('ASCII-8BIT') } @@ -132,7 +72,7 @@ RSpec.describe Gitlab::Git::Commit, :seed_helper do shared_examples '.find' do it "returns first head commit if without params" do expect(described_class.last(repository).id).to eq( - rugged_repo.head.target.oid + repository.commit.sha ) end @@ -622,19 +562,6 @@ RSpec.describe Gitlab::Git::Commit, :seed_helper do end end - skip 'move this test to gitaly-ruby' do - RSpec.describe '#init_from_rugged' do - let(:gitlab_commit) { described_class.new(repository, rugged_commit) } - - subject { gitlab_commit } - - describe '#id' do - subject { super().id } - it { is_expected.to eq(SeedRepo::Commit::ID) } - end - end - end - describe '#init_from_hash' do let(:commit) { described_class.new(repository, sample_commit_hash) } diff --git a/spec/lib/gitlab/git/conflict/parser_spec.rb b/spec/lib/gitlab/git/conflict/parser_spec.rb index 02b00f711b4..7d81af92412 100644 --- a/spec/lib/gitlab/git/conflict/parser_spec.rb +++ b/spec/lib/gitlab/git/conflict/parser_spec.rb @@ -86,43 +86,68 @@ RSpec.describe Gitlab::Git::Conflict::Parser do CONFLICT end - let(:lines) do - described_class.parse(text, our_path: 'files/ruby/regex.rb', their_path: 'files/ruby/regex.rb') - end + shared_examples_for 'successful parsing' do + let(:lines) do + described_class.parse(content, our_path: 'files/ruby/regex.rb', their_path: 'files/ruby/regex.rb') + end - let(:old_line_numbers) do - lines.select { |line| line[:type] != 'new' }.map { |line| line[:line_old] } - end + let(:old_line_numbers) do + lines.select { |line| line[:type] != 'new' }.map { |line| line[:line_old] } + end - let(:new_line_numbers) do - lines.select { |line| line[:type] != 'old' }.map { |line| line[:line_new] } - end + let(:new_line_numbers) do + lines.select { |line| line[:type] != 'old' }.map { |line| line[:line_new] } + end + + let(:line_indexes) { lines.map { |line| line[:line_obj_index] } } - let(:line_indexes) { lines.map { |line| line[:line_obj_index] } } + it 'sets our lines as new lines' do + expect(lines[8..13]).to all(include(type: 'new')) + expect(lines[26..27]).to all(include(type: 'new')) + expect(lines[56..57]).to all(include(type: 'new')) + end - it 'sets our lines as new lines' do - expect(lines[8..13]).to all(include(type: 'new')) - expect(lines[26..27]).to all(include(type: 'new')) - expect(lines[56..57]).to all(include(type: 'new')) + it 'sets their lines as old lines' do + expect(lines[14..19]).to all(include(type: 'old')) + expect(lines[28..29]).to all(include(type: 'old')) + expect(lines[58..59]).to all(include(type: 'old')) + end + + it 'sets non-conflicted lines as both' do + expect(lines[0..7]).to all(include(type: nil)) + expect(lines[20..25]).to all(include(type: nil)) + expect(lines[30..55]).to all(include(type: nil)) + expect(lines[60..62]).to all(include(type: nil)) + end + + it 'sets consecutive line numbers for line_obj_index, line_old, and line_new' do + expect(line_indexes).to eq(0.upto(62).to_a) + expect(old_line_numbers).to eq(1.upto(53).to_a) + expect(new_line_numbers).to eq(1.upto(53).to_a) + end end - it 'sets their lines as old lines' do - expect(lines[14..19]).to all(include(type: 'old')) - expect(lines[28..29]).to all(include(type: 'old')) - expect(lines[58..59]).to all(include(type: 'old')) + context 'content has LF endings' do + let(:content) { text } + + it_behaves_like 'successful parsing' end - it 'sets non-conflicted lines as both' do - expect(lines[0..7]).to all(include(type: nil)) - expect(lines[20..25]).to all(include(type: nil)) - expect(lines[30..55]).to all(include(type: nil)) - expect(lines[60..62]).to all(include(type: nil)) + context 'content has CRLF endings' do + let(:content) { text.gsub("\n", "\r\n") } + + it_behaves_like 'successful parsing' end - it 'sets consecutive line numbers for line_obj_index, line_old, and line_new' do - expect(line_indexes).to eq(0.upto(62).to_a) - expect(old_line_numbers).to eq(1.upto(53).to_a) - expect(new_line_numbers).to eq(1.upto(53).to_a) + context 'content has mixed LF and CRLF endings' do + # Simulate mixed line endings by only changing some of the lines to CRLF + let(:content) do + text.each_line.map.with_index do |line, index| + index.odd? ? line.gsub("\n", "\r\n") : line + end.join + end + + it_behaves_like 'successful parsing' end end diff --git a/spec/lib/gitlab/git/object_pool_spec.rb b/spec/lib/gitlab/git/object_pool_spec.rb index 91960ebbede..3b1eb0319f8 100644 --- a/spec/lib/gitlab/git/object_pool_spec.rb +++ b/spec/lib/gitlab/git/object_pool_spec.rb @@ -3,8 +3,6 @@ require 'spec_helper' RSpec.describe Gitlab::Git::ObjectPool do - include RepoHelpers - let(:pool_repository) { create(:pool_repository) } let(:source_repository) { pool_repository.source_project.repository } @@ -80,8 +78,6 @@ RSpec.describe Gitlab::Git::ObjectPool do end describe '#fetch' do - let(:source_repository_path) { File.join(TestEnv.repos_path, source_repository.relative_path) } - let(:source_repository_rugged) { Rugged::Repository.new(source_repository_path) } let(:commit_count) { source_repository.commit_count } context "when the object's pool repository exists" do @@ -106,7 +102,13 @@ RSpec.describe Gitlab::Git::ObjectPool do end it 'fetches objects from the source repository' do - new_commit_id = new_commit_edit_old_file(source_repository_rugged).oid + new_commit_id = source_repository.create_file( + pool_repository.source_project.owner, + 'a.file', + 'This is a file', + branch_name: source_repository.root_ref, + message: 'Add a file' + ) expect(subject.repository.exists?).to be false diff --git a/spec/lib/gitlab/git/repository_spec.rb b/spec/lib/gitlab/git/repository_spec.rb index 47688c4b3e6..e20d5b928c4 100644 --- a/spec/lib/gitlab/git/repository_spec.rb +++ b/spec/lib/gitlab/git/repository_spec.rb @@ -352,12 +352,30 @@ RSpec.describe Gitlab::Git::Repository, :seed_helper do repository.create_branch('left-branch') repository.create_branch('right-branch') - left.times do - new_commit_edit_new_file_on_branch(repository_rugged, 'encoding/CHANGELOG', 'left-branch', 'some more content for a', 'some stuff') + left.times do |i| + repository.multi_action( + user, + branch_name: 'left-branch', + message: 'some more content for a', + actions: [{ + action: i == 0 ? :create : :update, + file_path: 'encoding/CHANGELOG', + content: 'some stuff' + }] + ) end - right.times do - new_commit_edit_new_file_on_branch(repository_rugged, 'encoding/CHANGELOG', 'right-branch', 'some more content for b', 'some stuff') + right.times do |i| + repository.multi_action( + user, + branch_name: 'right-branch', + message: 'some more content for b', + actions: [{ + action: i == 0 ? :create : :update, + file_path: 'encoding/CHANGELOG', + content: 'some stuff' + }] + ) end end @@ -367,8 +385,8 @@ RSpec.describe Gitlab::Git::Repository, :seed_helper do end it 'returns the correct count bounding at max_count' do - branch_a_sha = repository_rugged.branches['left-branch'].target.oid - branch_b_sha = repository_rugged.branches['right-branch'].target.oid + branch_a_sha = repository.find_branch('left-branch').dereferenced_target.sha + branch_b_sha = repository.find_branch('right-branch').dereferenced_target.sha count = repository.diverging_commit_count(branch_a_sha, branch_b_sha, max_count: 1000) @@ -392,12 +410,30 @@ RSpec.describe Gitlab::Git::Repository, :seed_helper do repository.create_branch('left-branch') repository.create_branch('right-branch') - left.times do - new_commit_edit_new_file_on_branch(repository_rugged, 'encoding/CHANGELOG', 'left-branch', 'some more content for a', 'some stuff') + left.times do |i| + repository.multi_action( + user, + branch_name: 'left-branch', + message: 'some more content for a', + actions: [{ + action: i == 0 ? :create : :update, + file_path: 'encoding/CHANGELOG', + content: 'some stuff' + }] + ) end - right.times do - new_commit_edit_new_file_on_branch(repository_rugged, 'encoding/CHANGELOG', 'right-branch', 'some more content for b', 'some stuff') + right.times do |i| + repository.multi_action( + user, + branch_name: 'right-branch', + message: 'some more content for b', + actions: [{ + action: i == 0 ? :create : :update, + file_path: 'encoding/CHANGELOG', + content: 'some stuff' + }] + ) end end @@ -407,8 +443,8 @@ RSpec.describe Gitlab::Git::Repository, :seed_helper do end it 'returns the correct count bounding at max_count' do - branch_a_sha = repository_rugged.branches['left-branch'].target.oid - branch_b_sha = repository_rugged.branches['right-branch'].target.oid + branch_a_sha = repository.find_branch('left-branch').dereferenced_target.sha + branch_b_sha = repository.find_branch('right-branch').dereferenced_target.sha results = repository.diverging_commit_count(branch_a_sha, branch_b_sha, max_count: max_count) @@ -469,16 +505,14 @@ RSpec.describe Gitlab::Git::Repository, :seed_helper do it 'deletes the ref' do repository.delete_refs('refs/heads/feature') - expect(repository_rugged.references['refs/heads/feature']).to be_nil + expect(repository.find_branch('feature')).to be_nil end it 'deletes all refs' do refs = %w[refs/heads/wip refs/tags/v1.1.0] repository.delete_refs(*refs) - refs.each do |ref| - expect(repository_rugged.references[ref]).to be_nil - end + expect(repository.list_refs(refs)).to be_empty end it 'does not fail when deleting an empty list of refs' do @@ -491,7 +525,7 @@ RSpec.describe Gitlab::Git::Repository, :seed_helper do end describe '#branch_names_contains_sha' do - let(:head_id) { repository_rugged.head.target.oid } + let(:head_id) { repository.commit.id } let(:new_branch) { head_id } let(:utf8_branch) { 'branch-é' } @@ -525,7 +559,7 @@ RSpec.describe Gitlab::Git::Repository, :seed_helper do it 'does not error when dereferenced_target is nil' do blob_id = repository.blob_at('master', 'README.md').id - repository_rugged.tags.create("refs/tags/blob-tag", blob_id) + repository.add_tag("refs/tags/blob-tag", user: user, target: blob_id) expect { subject }.not_to raise_error end @@ -559,14 +593,31 @@ RSpec.describe Gitlab::Git::Repository, :seed_helper do describe '#search_files_by_content' do let(:repository) { mutable_repository } - let(:repository_rugged) { mutable_repository_rugged } let(:ref) { 'search-files-by-content-branch' } let(:content) { 'foobarbazmepmep' } before do repository.create_branch(ref) - new_commit_edit_new_file_on_branch(repository_rugged, 'encoding/CHANGELOG', ref, 'committing something', content) - new_commit_edit_new_file_on_branch(repository_rugged, 'anotherfile', ref, 'committing something', content) + repository.multi_action( + user, + branch_name: ref, + message: 'committing something', + actions: [{ + action: :create, + file_path: 'encoding/CHANGELOG', + content: content + }] + ) + repository.multi_action( + user, + branch_name: ref, + message: 'committing something', + actions: [{ + action: :create, + file_path: 'anotherfile', + content: content + }] + ) end after do @@ -669,14 +720,42 @@ RSpec.describe Gitlab::Git::Repository, :seed_helper do before do # Add new commits so that there's a renamed file in the commit history - @commit_with_old_name_id = new_commit_edit_old_file(repository_rugged).oid - @rename_commit_id = new_commit_move_file(repository_rugged).oid - @commit_with_new_name_id = new_commit_edit_new_file(repository_rugged, "encoding/CHANGELOG", "Edit encoding/CHANGELOG", "I'm a new changelog with different text").oid + @commit_with_old_name_id = repository.multi_action( + user, + branch_name: repository.root_ref, + message: 'Update CHANGELOG', + actions: [{ + action: :update, + file_path: 'CHANGELOG', + content: 'CHANGELOG' + }] + ).newrev + @rename_commit_id = repository.multi_action( + user, + branch_name: repository.root_ref, + message: 'Move CHANGELOG to encoding/', + actions: [{ + action: :move, + previous_path: 'CHANGELOG', + file_path: 'encoding/CHANGELOG', + content: 'CHANGELOG' + }] + ).newrev + @commit_with_new_name_id = repository.multi_action( + 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 end after do # Erase our commits so other tests get the original repo - repository_rugged.references.update("refs/heads/master", SeedRepo::LastCommit::ID) + repository.write_ref(repository.root_ref, SeedRepo::LastCommit::ID) end context "where 'follow' == true" do @@ -1649,27 +1728,28 @@ RSpec.describe Gitlab::Git::Repository, :seed_helper do end describe '#gitattribute' do - let(:repository) { Gitlab::Git::Repository.new('default', TEST_GITATTRIBUTES_REPO_PATH, '', 'group/project') } + let(:project) { create(:project, :repository) } + let(:repository) { project.repository } - after do - ensure_seeds - end + context 'with gitattributes' do + before do + repository.copy_gitattributes('gitattributes') + end - it 'returns matching language attribute' do - expect(repository.gitattribute("custom-highlighting/test.gitlab-custom", 'gitlab-language')).to eq('ruby') - end + it 'returns matching language attribute' do + expect(repository.gitattribute("custom-highlighting/test.gitlab-custom", 'gitlab-language')).to eq('ruby') + end - 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 + 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 - it 'returns nil if nothing matches' do - expect(repository.gitattribute("report.xslt", 'gitlab-language')).to eq(nil) + it 'returns nil if nothing matches' do + expect(repository.gitattribute("report.xslt", 'gitlab-language')).to eq(nil) + end end - context 'without gitattributes file' do - let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH, '', 'group/project') } - + context 'without gitattributes' do it 'returns nil' do expect(repository.gitattribute("README.md", 'gitlab-language')).to eq(nil) end @@ -1760,25 +1840,13 @@ RSpec.describe Gitlab::Git::Repository, :seed_helper do describe '#languages' do it 'returns exactly the expected results' do languages = repository.languages('4b4918a572fa86f9771e5ba40fbd48e1eb03e2c6') - expected_languages = [ - { value: 66.63, label: "Ruby", color: "#701516", highlight: "#701516" }, - { value: 22.96, label: "JavaScript", color: "#f1e05a", highlight: "#f1e05a" }, - { value: 7.9, label: "HTML", color: "#e34c26", highlight: "#e34c26" }, - { value: 2.51, label: "CoffeeScript", color: "#244776", highlight: "#244776" } - ] - - expect(languages.size).to eq(expected_languages.size) - - expected_languages.size.times do |i| - a = expected_languages[i] - b = languages[i] - expect(a.keys.sort).to eq(b.keys.sort) - expect(a[:value]).to be_within(0.1).of(b[:value]) - - non_float_keys = a.keys - [:value] - expect(a.values_at(*non_float_keys)).to eq(b.values_at(*non_float_keys)) - end + 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" } + ]) end it "uses the repository's HEAD when no ref is passed" do @@ -1818,12 +1886,18 @@ RSpec.describe Gitlab::Git::Repository, :seed_helper do context 'when the branch exists' do context 'when the commit does not exist locally' do let(:source_branch) { 'new-branch-for-fetch-source-branch' } - let(:source_path) { File.join(TestEnv.repos_path, source_repository.relative_path) } - let(:source_rugged) { Rugged::Repository.new(source_path) } - let(:new_oid) { new_commit_edit_old_file(source_rugged).oid } - before do - source_rugged.branches.create(source_branch, new_oid) + let!(:new_oid) do + source_repository.multi_action( + user, + branch_name: source_branch, + message: 'Add a file', + actions: [{ + action: :create, + file_path: 'a.file', + content: 'This is a file.' + }] + ).newrev end it 'writes the ref' do @@ -1869,7 +1943,7 @@ RSpec.describe Gitlab::Git::Repository, :seed_helper do it "removes the branch from the repo" do repository.rm_branch(branch_name, user: user) - expect(repository_rugged.branches[branch_name]).to be_nil + expect(repository.find_branch(branch_name)).to be_nil end end @@ -2290,11 +2364,23 @@ RSpec.describe Gitlab::Git::Repository, :seed_helper do end context 'when the diff contains a rename' do - let(:end_sha) { new_commit_move_file(repository_rugged).oid } + let(:end_sha) do + repository.multi_action( + user, + branch_name: repository.root_ref, + message: 'Move CHANGELOG to encoding/', + actions: [{ + action: :move, + previous_path: 'CHANGELOG', + file_path: 'encoding/CHANGELOG', + content: 'CHANGELOG' + }] + ).newrev + end after do # Erase our commits so other tests get the original repo - repository_rugged.references.update('refs/heads/master', SeedRepo::LastCommit::ID) + repository.write_ref(repository.root_ref, SeedRepo::LastCommit::ID) end it 'does not include the renamed file in the sparse checkout' do @@ -2342,24 +2428,15 @@ RSpec.describe Gitlab::Git::Repository, :seed_helper do end def create_remote_branch(remote_name, branch_name, source_branch_name) - source_branch = repository.branches.find { |branch| branch.name == source_branch_name } - repository_rugged.references.create("refs/remotes/#{remote_name}/#{branch_name}", source_branch.dereferenced_target.sha) - end - - def refs(dir) - IO.popen(%W[git -C #{dir} for-each-ref], &:read).split("\n").map do |line| - line.split("\t").last - end + source_branch = repository.find_branch(source_branch_name) + repository.write_ref("refs/remotes/#{remote_name}/#{branch_name}", source_branch.dereferenced_target.sha) end describe '#disconnect_alternates' do let(:project) { create(:project, :repository) } let(:pool_repository) { create(:pool_repository) } let(:repository) { project.repository } - let(:repository_path) { File.join(TestEnv.repos_path, repository.relative_path) } let(:object_pool) { pool_repository.object_pool } - let(:object_pool_path) { File.join(TestEnv.repos_path, object_pool.repository.relative_path) } - let(:object_pool_rugged) { Rugged::Repository.new(object_pool_path) } before do object_pool.create # rubocop:disable Rails/SaveBang @@ -2369,25 +2446,24 @@ RSpec.describe Gitlab::Git::Repository, :seed_helper do expect { repository.disconnect_alternates }.not_to raise_error end - it 'removes the alternates file' do - object_pool.link(repository) - - alternates_file = File.join(repository_path, "objects", "info", "alternates") - expect(File.exist?(alternates_file)).to be_truthy - - repository.disconnect_alternates - - expect(File.exist?(alternates_file)).to be_falsey - end - it 'can still access objects in the object pool' do object_pool.link(repository) - new_commit = new_commit_edit_old_file(object_pool_rugged) - expect(repository.commit(new_commit.oid).id).to eq(new_commit.oid) + new_commit_id = object_pool.repository.multi_action( + 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) repository.disconnect_alternates - expect(repository.commit(new_commit.oid).id).to eq(new_commit.oid) + expect(repository.commit(new_commit_id).id).to eq(new_commit_id) end end @@ -2483,7 +2559,7 @@ RSpec.describe Gitlab::Git::Repository, :seed_helper do it 'mirrors the source repository' do subject - expect(refs(new_repository_path)).to eq(refs(repository_path)) + expect(new_repository.list_refs(['refs/'])).to eq(repository.list_refs(['refs/'])) end end @@ -2495,7 +2571,7 @@ RSpec.describe Gitlab::Git::Repository, :seed_helper do it 'mirrors the source repository' do subject - expect(refs(new_repository_path)).to eq(refs(repository_path)) + expect(new_repository.list_refs(['refs/'])).to eq(repository.list_refs(['refs/'])) end context 'with keep-around refs' do @@ -2511,8 +2587,8 @@ RSpec.describe Gitlab::Git::Repository, :seed_helper do it 'includes the temporary and keep-around refs' do subject - expect(refs(new_repository_path)).to include(keep_around_ref) - expect(refs(new_repository_path)).to include(tmp_ref) + 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]) end end end diff --git a/spec/lib/gitlab/git/tree_spec.rb b/spec/lib/gitlab/git/tree_spec.rb index 97ba177da71..172d7a3f27b 100644 --- a/spec/lib/gitlab/git/tree_spec.rb +++ b/spec/lib/gitlab/git/tree_spec.rb @@ -3,6 +3,8 @@ require "spec_helper" RSpec.describe Gitlab::Git::Tree, :seed_helper do + let_it_be(:user) { create(:user) } + let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH, '', 'group/project') } shared_examples :repo do @@ -85,51 +87,29 @@ RSpec.describe Gitlab::Git::Tree, :seed_helper do context :flat_path do let(:filename) { 'files/flat/path/correct/content.txt' } - let(:sha) { create_file(filename) } let(:path) { 'files/flat' } # rubocop: disable Rails/FindBy # This is not ActiveRecord where..first let(:subdir_file) { entries.first } # rubocop: enable Rails/FindBy - let(:repository_rugged) { Rugged::Repository.new(File.join(SEED_STORAGE_PATH, TEST_REPO_PATH)) } - - it { expect(subdir_file.flat_path).to eq('files/flat/path/correct') } - end - - def create_file(path) - oid = repository_rugged.write('test', :blob) - index = repository_rugged.index - index.add(path: filename, oid: oid, mode: 0100644) - - options = commit_options( - repository_rugged, - index, - repository_rugged.head.target, - 'HEAD', - 'Add new file') + let!(:sha) do + repository.multi_action( + user, + branch_name: 'HEAD', + message: "Create #{filename}", + actions: [{ + action: :create, + file_path: filename, + contents: 'test' + }] + ).newrev + end - Rugged::Commit.create(repository_rugged, options) - end + after do + ensure_seeds + end - # Build the options hash that's passed to Rugged::Commit#create - def commit_options(repo, index, target, ref, message) - options = {} - options[:tree] = index.write_tree(repo) - options[:author] = { - email: "test@example.com", - name: "Test Author", - time: Time.gm(2014, "mar", 3, 20, 15, 1) - } - options[:committer] = { - email: "test@example.com", - name: "Test Author", - time: Time.gm(2014, "mar", 3, 20, 15, 1) - } - options[:message] ||= message - options[:parents] = repo.empty? ? [] : [target].compact - options[:update_ref] = ref - - options + it { expect(subdir_file.flat_path).to eq('files/flat/path/correct') } end end |