repository_spec.rb 42.7 KB
Newer Older
R
Robert Speicher 已提交
1 2 3
require "spec_helper"

describe Gitlab::Git::Repository, seed_helper: true do
4
  include Gitlab::EncodingHelper
R
Robert Speicher 已提交
5

6
  let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH) }
R
Robert Speicher 已提交
7 8 9 10 11 12 13 14 15 16

  describe "Respond to" do
    subject { repository }

    it { is_expected.to respond_to(:raw) }
    it { is_expected.to respond_to(:rugged) }
    it { is_expected.to respond_to(:root_ref) }
    it { is_expected.to respond_to(:tags) }
  end

17 18
  describe '#root_ref' do
    context 'with gitaly disabled' do
19 20 21
      before do
        allow(Gitlab::GitalyClient).to receive(:feature_enabled?).and_return(false)
      end
22 23 24 25 26 27 28

      it 'calls #discover_default_branch' do
        expect(repository).to receive(:discover_default_branch)
        repository.root_ref
      end
    end

J
Jacob Vosmaer 已提交
29 30 31 32
    it 'returns UTF-8' do
      expect(repository.root_ref.encoding).to eq(Encoding.find('UTF-8'))
    end

33
    context 'with gitaly enabled' do
34 35 36 37 38 39 40
      before do
        stub_gitaly
      end

      after do
        Gitlab::GitalyClient.clear_stubs!
      end
41 42 43 44 45 46 47

      it 'gets the branch name from GitalyClient' do
        expect_any_instance_of(Gitlab::GitalyClient::Ref).to receive(:default_branch_name)
        repository.root_ref
      end

      it 'wraps GRPC not found' do
48 49
        expect_any_instance_of(Gitlab::GitalyClient::Ref).to receive(:default_branch_name)
          .and_raise(GRPC::NotFound)
50 51 52 53
        expect { repository.root_ref }.to raise_error(Gitlab::Git::Repository::NoRepository)
      end

      it 'wraps GRPC exceptions' do
54 55
        expect_any_instance_of(Gitlab::GitalyClient::Ref).to receive(:default_branch_name)
          .and_raise(GRPC::Unknown)
56 57 58
        expect { repository.root_ref }.to raise_error(Gitlab::Git::CommandError)
      end
    end
59 60
  end

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  describe "#rugged" do
    context 'with no Git env stored' do
      before do
        expect(Gitlab::Git::Env).to receive(:all).and_return({})
      end

      it "whitelist some variables and pass them via the alternates keyword argument" do
        expect(Rugged::Repository).to receive(:new).with(repository.path, alternates: [])

        repository.rugged
      end
    end

    context 'with some Git env stored' do
      before do
        expect(Gitlab::Git::Env).to receive(:all).and_return({
          'GIT_OBJECT_DIRECTORY' => 'foo',
          'GIT_ALTERNATE_OBJECT_DIRECTORIES' => 'bar',
          'GIT_OTHER' => 'another_env'
        })
      end

      it "whitelist some variables and pass them via the alternates keyword argument" do
        expect(Rugged::Repository).to receive(:new).with(repository.path, alternates: %w[foo bar])

        repository.rugged
      end
    end
  end

R
Robert Speicher 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
  describe "#discover_default_branch" do
    let(:master) { 'master' }
    let(:feature) { 'feature' }
    let(:feature2) { 'feature2' }

    it "returns 'master' when master exists" do
      expect(repository).to receive(:branch_names).at_least(:once).and_return([feature, master])
      expect(repository.discover_default_branch).to eq('master')
    end

    it "returns non-master when master exists but default branch is set to something else" do
      File.write(File.join(repository.path, 'HEAD'), 'ref: refs/heads/feature')
      expect(repository).to receive(:branch_names).at_least(:once).and_return([feature, master])
      expect(repository.discover_default_branch).to eq('feature')
      File.write(File.join(repository.path, 'HEAD'), 'ref: refs/heads/master')
    end

    it "returns a non-master branch when only one exists" do
      expect(repository).to receive(:branch_names).at_least(:once).and_return([feature])
      expect(repository.discover_default_branch).to eq('feature')
    end

    it "returns a non-master branch when more than one exists and master does not" do
      expect(repository).to receive(:branch_names).at_least(:once).and_return([feature, feature2])
      expect(repository.discover_default_branch).to eq('feature')
    end

    it "returns nil when no branch exists" do
      expect(repository).to receive(:branch_names).at_least(:once).and_return([])
      expect(repository.discover_default_branch).to be_nil
    end
  end

124
  describe '#branch_names' do
R
Robert Speicher 已提交
125 126 127 128 129
    subject { repository.branch_names }

    it 'has SeedRepo::Repo::BRANCHES.size elements' do
      expect(subject.size).to eq(SeedRepo::Repo::BRANCHES.size)
    end
J
Jacob Vosmaer 已提交
130 131 132 133 134

    it 'returns UTF-8' do
      expect(subject.first.encoding).to eq(Encoding.find('UTF-8'))
    end

R
Robert Speicher 已提交
135 136
    it { is_expected.to include("master") }
    it { is_expected.not_to include("branch-from-space") }
137

138
    context 'with gitaly enabled' do
139 140 141 142 143 144 145
      before do
        stub_gitaly
      end

      after do
        Gitlab::GitalyClient.clear_stubs!
      end
146 147 148 149 150 151 152

      it 'gets the branch names from GitalyClient' do
        expect_any_instance_of(Gitlab::GitalyClient::Ref).to receive(:branch_names)
        subject
      end

      it 'wraps GRPC not found' do
153 154
        expect_any_instance_of(Gitlab::GitalyClient::Ref).to receive(:branch_names)
          .and_raise(GRPC::NotFound)
155 156 157 158
        expect { subject }.to raise_error(Gitlab::Git::Repository::NoRepository)
      end

      it 'wraps GRPC other exceptions' do
159 160
        expect_any_instance_of(Gitlab::GitalyClient::Ref).to receive(:branch_names)
          .and_raise(GRPC::Unknown)
161 162 163
        expect { subject }.to raise_error(Gitlab::Git::CommandError)
      end
    end
R
Robert Speicher 已提交
164 165
  end

166
  describe '#tag_names' do
R
Robert Speicher 已提交
167 168 169
    subject { repository.tag_names }

    it { is_expected.to be_kind_of Array }
J
Jacob Vosmaer 已提交
170

R
Robert Speicher 已提交
171 172 173 174
    it 'has SeedRepo::Repo::TAGS.size elements' do
      expect(subject.size).to eq(SeedRepo::Repo::TAGS.size)
    end

J
Jacob Vosmaer 已提交
175 176 177 178
    it 'returns UTF-8' do
      expect(subject.first.encoding).to eq(Encoding.find('UTF-8'))
    end

R
Robert Speicher 已提交
179 180 181 182 183 184
    describe '#last' do
      subject { super().last }
      it { is_expected.to eq("v1.2.1") }
    end
    it { is_expected.to include("v1.0.0") }
    it { is_expected.not_to include("v5.0.0") }
185

186
    context 'with gitaly enabled' do
187 188 189 190 191 192 193
      before do
        stub_gitaly
      end

      after do
        Gitlab::GitalyClient.clear_stubs!
      end
194 195 196 197 198 199 200

      it 'gets the tag names from GitalyClient' do
        expect_any_instance_of(Gitlab::GitalyClient::Ref).to receive(:tag_names)
        subject
      end

      it 'wraps GRPC not found' do
201 202
        expect_any_instance_of(Gitlab::GitalyClient::Ref).to receive(:tag_names)
          .and_raise(GRPC::NotFound)
203 204 205 206
        expect { subject }.to raise_error(Gitlab::Git::Repository::NoRepository)
      end

      it 'wraps GRPC exceptions' do
207 208
        expect_any_instance_of(Gitlab::GitalyClient::Ref).to receive(:tag_names)
          .and_raise(GRPC::Unknown)
209 210 211
        expect { subject }.to raise_error(Gitlab::Git::CommandError)
      end
    end
R
Robert Speicher 已提交
212 213 214 215 216 217 218
  end

  shared_examples 'archive check' do |extenstion|
    it { expect(metadata['ArchivePath']).to match(/tmp\/gitlab-git-test.git\/gitlab-git-test-master-#{SeedRepo::LastCommit::ID}/) }
    it { expect(metadata['ArchivePath']).to end_with extenstion }
  end

219 220 221 222 223 224 225 226 227 228 229 230
  describe '#archive_prefix' do
    let(:project_name) { 'project-name'}

    before do
      expect(repository).to receive(:name).once.and_return(project_name)
    end

    it 'returns parameterised string for a ref containing slashes' do
      prefix = repository.archive_prefix('test/branch', 'SHA')

      expect(prefix).to eq("#{project_name}-test-branch-SHA")
    end
231 232 233 234 235 236

    it 'returns correct string for a ref containing dots' do
      prefix = repository.archive_prefix('test.branch', 'SHA')

      expect(prefix).to eq("#{project_name}-test.branch-SHA")
    end
237 238 239
  end

  describe '#archive' do
R
Robert Speicher 已提交
240 241 242 243 244
    let(:metadata) { repository.archive_metadata('master', '/tmp') }

    it_should_behave_like 'archive check', '.tar.gz'
  end

245
  describe '#archive_zip' do
R
Robert Speicher 已提交
246 247 248 249 250
    let(:metadata) { repository.archive_metadata('master', '/tmp', 'zip') }

    it_should_behave_like 'archive check', '.zip'
  end

251
  describe '#archive_bz2' do
R
Robert Speicher 已提交
252 253 254 255 256
    let(:metadata) { repository.archive_metadata('master', '/tmp', 'tbz2') }

    it_should_behave_like 'archive check', '.tar.bz2'
  end

257
  describe '#archive_fallback' do
R
Robert Speicher 已提交
258 259 260 261 262
    let(:metadata) { repository.archive_metadata('master', '/tmp', 'madeup') }

    it_should_behave_like 'archive check', '.tar.gz'
  end

263
  describe '#size' do
R
Robert Speicher 已提交
264 265 266 267 268
    subject { repository.size }

    it { is_expected.to be < 2 }
  end

269
  describe '#has_commits?' do
R
Robert Speicher 已提交
270 271 272
    it { expect(repository.has_commits?).to be_truthy }
  end

273
  describe '#empty?' do
R
Robert Speicher 已提交
274 275 276
    it { expect(repository.empty?).to be_falsey }
  end

277
  describe '#bare?' do
R
Robert Speicher 已提交
278 279 280
    it { expect(repository.bare?).to be_truthy }
  end

281
  describe '#heads' do
R
Robert Speicher 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
    let(:heads) { repository.heads }
    subject { heads }

    it { is_expected.to be_kind_of Array }

    describe '#size' do
      subject { super().size }
      it { is_expected.to eq(SeedRepo::Repo::BRANCHES.size) }
    end

    context :head do
      subject { heads.first }

      describe '#name' do
        subject { super().name }
        it { is_expected.to eq("feature") }
      end

      context :commit do
        subject { heads.first.dereferenced_target.sha }

        it { is_expected.to eq("0b4bc9a49b562e85de7cc9e834518ea6828729b9") }
      end
    end
  end

308
  describe '#ref_names' do
R
Robert Speicher 已提交
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
    let(:ref_names) { repository.ref_names }
    subject { ref_names }

    it { is_expected.to be_kind_of Array }

    describe '#first' do
      subject { super().first }
      it { is_expected.to eq('feature') }
    end

    describe '#last' do
      subject { super().last }
      it { is_expected.to eq('v1.2.1') }
    end
  end

325
  describe '#search_files' do
R
Robert Speicher 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
    let(:results) { repository.search_files('rails', 'master') }
    subject { results }

    it { is_expected.to be_kind_of Array }

    describe '#first' do
      subject { super().first }
      it { is_expected.to be_kind_of Gitlab::Git::BlobSnippet }
    end

    context 'blob result' do
      subject { results.first }

      describe '#ref' do
        subject { super().ref }
        it { is_expected.to eq('master') }
      end

      describe '#filename' do
        subject { super().filename }
        it { is_expected.to eq('CHANGELOG') }
      end

      describe '#startline' do
        subject { super().startline }
        it { is_expected.to eq(35) }
      end

      describe '#data' do
        subject { super().data }
        it { is_expected.to include "Ability to filter by multiple labels" }
      end
    end
  end

361
  context '#submodules' do
362
    let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH) }
R
Robert Speicher 已提交
363 364

    context 'where repo has submodules' do
365
      let(:submodules) { repository.send(:submodules, 'master') }
R
Robert Speicher 已提交
366 367 368 369 370 371 372 373 374
      let(:submodule) { submodules.first }

      it { expect(submodules).to be_kind_of Hash }
      it { expect(submodules.empty?).to be_falsey }

      it 'should have valid data' do
        expect(submodule).to eq([
          "six", {
            "id" => "409f37c4f05865e4fb208c771485f211a22c4c2d",
375
            "name" => "six",
R
Robert Speicher 已提交
376 377 378 379 380 381 382
            "url" => "git://github.com/randx/six.git"
          }
        ])
      end

      it 'should handle nested submodules correctly' do
        nested = submodules['nested/six']
383
        expect(nested['name']).to eq('nested/six')
R
Robert Speicher 已提交
384 385 386 387 388 389
        expect(nested['url']).to eq('git://github.com/randx/six.git')
        expect(nested['id']).to eq('24fb71c79fcabc63dfd8832b12ee3bf2bf06b196')
      end

      it 'should handle deeply nested submodules correctly' do
        nested = submodules['deeper/nested/six']
390
        expect(nested['name']).to eq('deeper/nested/six')
R
Robert Speicher 已提交
391 392 393 394 395 396 397 398 399
        expect(nested['url']).to eq('git://github.com/randx/six.git')
        expect(nested['id']).to eq('24fb71c79fcabc63dfd8832b12ee3bf2bf06b196')
      end

      it 'should not have an entry for an invalid submodule' do
        expect(submodules).not_to have_key('invalid/path')
      end

      it 'should not have an entry for an uncommited submodule dir' do
400
        submodules = repository.send(:submodules, 'fix-existing-submodule-dir')
R
Robert Speicher 已提交
401 402 403 404
        expect(submodules).not_to have_key('submodule-existing-dir')
      end

      it 'should handle tags correctly' do
405
        submodules = repository.send(:submodules, 'v1.2.1')
R
Robert Speicher 已提交
406 407 408 409

        expect(submodules.first).to eq([
          "six", {
            "id" => "409f37c4f05865e4fb208c771485f211a22c4c2d",
410
            "name" => "six",
R
Robert Speicher 已提交
411 412 413 414
            "url" => "git://github.com/randx/six.git"
          }
        ])
      end
415 416 417 418 419 420 421 422 423 424 425 426 427

      it 'should not break on invalid syntax' do
        allow(repository).to receive(:blob_content).and_return(<<-GITMODULES.strip_heredoc)
          [submodule "six"]
          path = six
          url = git://github.com/randx/six.git

          [submodule]
          foo = bar
        GITMODULES

        expect(submodules).to have_key('six')
      end
R
Robert Speicher 已提交
428 429 430
    end

    context 'where repo doesn\'t have submodules' do
431
      let(:submodules) { repository.send(:submodules, '6d39438') }
R
Robert Speicher 已提交
432 433 434 435 436 437
      it 'should return an empty hash' do
        expect(submodules).to be_empty
      end
    end
  end

438
  describe '#commit_count' do
R
Robert Speicher 已提交
439 440 441 442 443
    it { expect(repository.commit_count("master")).to eq(25) }
    it { expect(repository.commit_count("feature")).to eq(9) }
  end

  describe "#reset" do
444 445 446
    change_path = File.join(SEED_STORAGE_PATH, TEST_NORMAL_REPO_PATH, "CHANGELOG")
    untracked_path = File.join(SEED_STORAGE_PATH, TEST_NORMAL_REPO_PATH, "UNTRACKED")
    tracked_path = File.join(SEED_STORAGE_PATH, TEST_NORMAL_REPO_PATH, "files", "ruby", "popen.rb")
R
Robert Speicher 已提交
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464

    change_text = "New changelog text"
    untracked_text = "This file is untracked"

    reset_commit = SeedRepo::LastCommit::ID

    context "--hard" do
      before(:all) do
        # Modify a tracked file
        File.open(change_path, "w") do |f|
          f.write(change_text)
        end

        # Add an untracked file to the working directory
        File.open(untracked_path, "w") do |f|
          f.write(untracked_text)
        end

465
        @normal_repo = Gitlab::Git::Repository.new('default', TEST_NORMAL_REPO_PATH)
R
Robert Speicher 已提交
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
        @normal_repo.reset("HEAD", :hard)
      end

      it "should replace the working directory with the content of the index" do
        File.open(change_path, "r") do |f|
          expect(f.each_line.first).not_to eq(change_text)
        end

        File.open(tracked_path, "r") do |f|
          expect(f.each_line.to_a[8]).to include('raise RuntimeError, "System commands')
        end
      end

      it "should not touch untracked files" do
        expect(File.exist?(untracked_path)).to be_truthy
      end

      it "should move the HEAD to the correct commit" do
        new_head = @normal_repo.rugged.head.target.oid
        expect(new_head).to eq(reset_commit)
      end

      it "should move the tip of the master branch to the correct commit" do
489 490
        new_tip = @normal_repo.rugged.references["refs/heads/master"]
          .target.oid
R
Robert Speicher 已提交
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507

        expect(new_tip).to eq(reset_commit)
      end

      after(:all) do
        # Fast-forward to the original HEAD
        FileUtils.rm_rf(TEST_NORMAL_REPO_PATH)
        ensure_seeds
      end
    end
  end

  describe "#checkout" do
    new_branch = "foo_branch"

    context "-b" do
      before(:all) do
508
        @normal_repo = Gitlab::Git::Repository.new('default', TEST_NORMAL_REPO_PATH)
R
Robert Speicher 已提交
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
        @normal_repo.checkout(new_branch, { b: true }, "origin/feature")
      end

      it "should create a new branch" do
        expect(@normal_repo.rugged.branches[new_branch]).not_to be_nil
      end

      it "should move the HEAD to the correct commit" do
        expect(@normal_repo.rugged.head.target.oid).to(
          eq(@normal_repo.rugged.branches["origin/feature"].target.oid)
        )
      end

      it "should refresh the repo's #heads collection" do
        head_names = @normal_repo.heads.map { |h| h.name }
        expect(head_names).to include(new_branch)
      end

      after(:all) do
        FileUtils.rm_rf(TEST_NORMAL_REPO_PATH)
        ensure_seeds
      end
    end

    context "without -b" do
      context "and specifying a nonexistent branch" do
        it "should not do anything" do
536
          normal_repo = Gitlab::Git::Repository.new('default', TEST_NORMAL_REPO_PATH)
R
Robert Speicher 已提交
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555

          expect { normal_repo.checkout(new_branch) }.to raise_error(Rugged::ReferenceError)
          expect(normal_repo.rugged.branches[new_branch]).to be_nil
          expect(normal_repo.rugged.head.target.oid).to(
            eq(normal_repo.rugged.branches["master"].target.oid)
          )

          head_names = normal_repo.heads.map { |h| h.name }
          expect(head_names).not_to include(new_branch)
        end

        after(:all) do
          FileUtils.rm_rf(TEST_NORMAL_REPO_PATH)
          ensure_seeds
        end
      end

      context "and with a valid branch" do
        before(:all) do
556
          @normal_repo = Gitlab::Git::Repository.new('default', TEST_NORMAL_REPO_PATH)
R
Robert Speicher 已提交
557 558 559 560 561 562 563 564 565 566 567
          @normal_repo.rugged.branches.create("feature", "origin/feature")
          @normal_repo.checkout("feature")
        end

        it "should move the HEAD to the correct commit" do
          expect(@normal_repo.rugged.head.target.oid).to(
            eq(@normal_repo.rugged.branches["feature"].target.oid)
          )
        end

        it "should update the working directory" do
568
          File.open(File.join(SEED_STORAGE_PATH, TEST_NORMAL_REPO_PATH, ".gitignore"), "r") do |f|
R
Robert Speicher 已提交
569 570 571 572 573
            expect(f.read.each_line.to_a).not_to include(".DS_Store\n")
          end
        end

        after(:all) do
574
          FileUtils.rm_rf(SEED_STORAGE_PATH, TEST_NORMAL_REPO_PATH)
R
Robert Speicher 已提交
575 576 577 578 579 580 581 582
          ensure_seeds
        end
      end
    end
  end

  describe "#delete_branch" do
    before(:all) do
583
      @repo = Gitlab::Git::Repository.new('default', TEST_MUTABLE_REPO_PATH)
R
Robert Speicher 已提交
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
      @repo.delete_branch("feature")
    end

    it "should remove the branch from the repo" do
      expect(@repo.rugged.branches["feature"]).to be_nil
    end

    it "should update the repo's #heads collection" do
      expect(@repo.heads).not_to include("feature")
    end

    after(:all) do
      FileUtils.rm_rf(TEST_MUTABLE_REPO_PATH)
      ensure_seeds
    end
  end

  describe "#create_branch" do
    before(:all) do
603
      @repo = Gitlab::Git::Repository.new('default', TEST_MUTABLE_REPO_PATH)
R
Robert Speicher 已提交
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
    end

    it "should create a new branch" do
      expect(@repo.create_branch('new_branch', 'master')).not_to be_nil
    end

    it "should create a new branch with the right name" do
      expect(@repo.create_branch('another_branch', 'master').name).to eq('another_branch')
    end

    it "should fail if we create an existing branch" do
      @repo.create_branch('duplicated_branch', 'master')
      expect{@repo.create_branch('duplicated_branch', 'master')}.to raise_error("Branch duplicated_branch already exists")
    end

    it "should fail if we create a branch from a non existing ref" do
      expect{@repo.create_branch('branch_based_in_wrong_ref', 'master_2_the_revenge')}.to raise_error("Invalid reference master_2_the_revenge")
    end

    after(:all) do
      FileUtils.rm_rf(TEST_MUTABLE_REPO_PATH)
      ensure_seeds
    end
  end

  describe "#remote_names" do
    let(:remotes) { repository.remote_names }

    it "should have one entry: 'origin'" do
      expect(remotes.size).to eq(1)
      expect(remotes.first).to eq("origin")
    end
  end

  describe "#refs_hash" do
    let(:refs) { repository.refs_hash }

    it "should have as many entries as branches and tags" do
      expected_refs = SeedRepo::Repo::BRANCHES + SeedRepo::Repo::TAGS
      # We flatten in case a commit is pointed at by more than one branch and/or tag
      expect(refs.values.flatten.size).to eq(expected_refs.size)
    end
  end

  describe "#remote_delete" do
    before(:all) do
650
      @repo = Gitlab::Git::Repository.new('default', TEST_MUTABLE_REPO_PATH)
R
Robert Speicher 已提交
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
      @repo.remote_delete("expendable")
    end

    it "should remove the remote" do
      expect(@repo.rugged.remotes).not_to include("expendable")
    end

    after(:all) do
      FileUtils.rm_rf(TEST_MUTABLE_REPO_PATH)
      ensure_seeds
    end
  end

  describe "#remote_add" do
    before(:all) do
666
      @repo = Gitlab::Git::Repository.new('default', TEST_MUTABLE_REPO_PATH)
667
      @repo.remote_add("new_remote", SeedHelper::GITLAB_GIT_TEST_REPO_URL)
R
Robert Speicher 已提交
668 669 670 671 672 673 674 675 676 677 678 679 680 681
    end

    it "should add the remote" do
      expect(@repo.rugged.remotes.each_name.to_a).to include("new_remote")
    end

    after(:all) do
      FileUtils.rm_rf(TEST_MUTABLE_REPO_PATH)
      ensure_seeds
    end
  end

  describe "#remote_update" do
    before(:all) do
682
      @repo = Gitlab::Git::Repository.new('default', TEST_MUTABLE_REPO_PATH)
R
Robert Speicher 已提交
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
      @repo.remote_update("expendable", url: TEST_NORMAL_REPO_PATH)
    end

    it "should add the remote" do
      expect(@repo.rugged.remotes["expendable"].url).to(
        eq(TEST_NORMAL_REPO_PATH)
      )
    end

    after(:all) do
      FileUtils.rm_rf(TEST_MUTABLE_REPO_PATH)
      ensure_seeds
    end
  end

  describe "#log" do
    commit_with_old_name = nil
    commit_with_new_name = nil
    rename_commit = nil

703
    before(:context) do
R
Robert Speicher 已提交
704
      # Add new commits so that there's a renamed file in the commit history
705
      repo = Gitlab::Git::Repository.new('default', TEST_REPO_PATH).rugged
R
Robert Speicher 已提交
706 707 708 709 710 711

      commit_with_old_name = new_commit_edit_old_file(repo)
      rename_commit = new_commit_move_file(repo)
      commit_with_new_name = new_commit_edit_new_file(repo)
    end

712 713
    after(:context) do
      # Erase our commits so other tests get the original repo
714
      repo = Gitlab::Git::Repository.new('default', TEST_REPO_PATH).rugged
715 716 717
      repo.references.update("refs/heads/master", SeedRepo::LastCommit::ID)
    end

R
Robert Speicher 已提交
718
    context "where 'follow' == true" do
719
      let(:options) { { ref: "master", follow: true } }
R
Robert Speicher 已提交
720 721

      context "and 'path' is a directory" do
722
        it "does not follow renames" do
723
          log_commits = repository.log(options.merge(path: "encoding"))
R
Robert Speicher 已提交
724

725 726 727 728 729
          aggregate_failures 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
R
Robert Speicher 已提交
730 731 732 733
        end
      end

      context "and 'path' is a file that matches the new filename" do
734 735
        context 'without offset' do
          it "follows renames" do
736 737 738 739 740 741 742
            log_commits = repository.log(options.merge(path: "encoding/CHANGELOG"))

            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
743
          end
R
Robert Speicher 已提交
744 745
        end

746 747
        context 'with offset=1' do
          it "follows renames and skip the latest commit" do
748 749 750 751 752 753 754
            log_commits = repository.log(options.merge(path: "encoding/CHANGELOG", offset: 1))

            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
755 756 757 758 759
          end
        end

        context 'with offset=1', 'and limit=1' do
          it "follows renames, skip the latest commit and return only one commit" do
760 761 762
            log_commits = repository.log(options.merge(path: "encoding/CHANGELOG", offset: 1, limit: 1))

            expect(log_commits).to contain_exactly(rename_commit)
763 764 765 766 767
          end
        end

        context 'with offset=1', 'and limit=2' do
          it "follows renames, skip the latest commit and return only two commits" do
768 769 770 771 772
            log_commits = repository.log(options.merge(path: "encoding/CHANGELOG", offset: 1, limit: 2))

            aggregate_failures do
              expect(log_commits).to contain_exactly(rename_commit, commit_with_old_name)
            end
773 774 775 776 777
          end
        end

        context 'with offset=2' do
          it "follows renames and skip the latest commit" do
778 779 780 781 782 783 784
            log_commits = repository.log(options.merge(path: "encoding/CHANGELOG", offset: 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
785 786 787 788 789
          end
        end

        context 'with offset=2', 'and limit=1' do
          it "follows renames, skip the two latest commit and return only one commit" do
790 791 792
            log_commits = repository.log(options.merge(path: "encoding/CHANGELOG", offset: 2, limit: 1))

            expect(log_commits).to contain_exactly(commit_with_old_name)
793 794 795 796 797
          end
        end

        context 'with offset=2', 'and limit=2' do
          it "follows renames, skip the two latest commit and return only one commit" do
798 799 800 801 802 803 804
            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
805
          end
R
Robert Speicher 已提交
806 807 808 809
        end
      end

      context "and 'path' is a file that matches the old filename" do
810
        it "does not follow renames" do
811
          log_commits = repository.log(options.merge(path: "CHANGELOG"))
R
Robert Speicher 已提交
812

813 814 815 816 817
          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
R
Robert Speicher 已提交
818 819 820 821
        end
      end

      context "unknown ref" do
822 823
        it "returns an empty array" do
          log_commits = repository.log(options.merge(ref: 'unknown'))
R
Robert Speicher 已提交
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924

          expect(log_commits).to eq([])
        end
      end
    end

    context "where 'follow' == false" do
      options = { follow: false }

      context "and 'path' is a directory" do
        let(:log_commits) do
          repository.log(options.merge(path: "encoding"))
        end

        it "should 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
      end

      context "and 'path' is a file that matches the new filename" do
        let(:log_commits) do
          repository.log(options.merge(path: "encoding/CHANGELOG"))
        end

        it "should 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
      end

      context "and 'path' is a file that matches the old filename" do
        let(:log_commits) do
          repository.log(options.merge(path: "CHANGELOG"))
        end

        it "should 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
      end

      context "and 'path' includes a directory that used to be a file" do
        let(:log_commits) do
          repository.log(options.merge(ref: "refs/heads/fix-blob-path", path: "files/testdir/file.txt"))
        end

        it "should return a list of commits" do
          expect(log_commits.size).to eq(1)
        end
      end
    end

    context "compare results between log_by_walk and log_by_shell" do
      let(:options) { { ref: "master" } }
      let(:commits_by_walk) { repository.log(options).map(&:oid) }
      let(:commits_by_shell) { repository.log(options.merge({ disable_walk: true })).map(&:oid) }

      it { expect(commits_by_walk).to eq(commits_by_shell) }

      context "with limit" do
        let(:options) { { ref: "master", limit: 1 } }

        it { expect(commits_by_walk).to eq(commits_by_shell) }
      end

      context "with offset" do
        let(:options) { { ref: "master", offset: 1 } }

        it { expect(commits_by_walk).to eq(commits_by_shell) }
      end

      context "with skip_merges" do
        let(:options) { { ref: "master", skip_merges: true } }

        it { expect(commits_by_walk).to eq(commits_by_shell) }
      end

      context "with path" do
        let(:options) { { ref: "master", path: "encoding" } }

        it { expect(commits_by_walk).to eq(commits_by_shell) }

        context "with follow" do
          let(:options) { { ref: "master", path: "encoding", follow: true } }

          it { expect(commits_by_walk).to eq(commits_by_shell) }
        end
      end
    end

    context "where provides 'after' timestamp" do
      options = { after: Time.iso8601('2014-03-03T20:15:01+00:00') }

      it "should returns commits on or after that timestamp" do
        commits = repository.log(options)

        expect(commits.size).to be > 0
925 926
        expect(commits).to satisfy do |commits|
          commits.all? { |commit| commit.time >= options[:after] }
R
Robert Speicher 已提交
927 928 929 930 931 932 933 934 935 936 937
        end
      end
    end

    context "where provides 'before' timestamp" do
      options = { before: Time.iso8601('2014-03-03T20:15:01+00:00') }

      it "should returns commits on or before that timestamp" do
        commits = repository.log(options)

        expect(commits.size).to be > 0
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
        expect(commits).to satisfy do |commits|
          commits.all? { |commit| commit.time <= options[:before] }
        end
      end
    end

    context 'when multiple paths are provided' do
      let(:options) { { ref: 'master', path: ['PROCESS.md', 'README.md'] } }

      def commit_files(commit)
        commit.diff(commit.parent_ids.first).deltas.flat_map do |delta|
          [delta.old_file[:path], delta.new_file[:path]].uniq.compact
        end
      end

      it 'only returns commits matching at least one path' do
        commits = repository.log(options)

        expect(commits.size).to be > 0
        expect(commits).to satisfy do |commits|
          commits.none? { |commit| (commit_files(commit) & options[:path]).empty? }
R
Robert Speicher 已提交
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
        end
      end
    end
  end

  describe "#commits_between" do
    context 'two SHAs' do
      let(:first_sha) { 'b0e52af38d7ea43cf41d8a6f2471351ac036d6c9' }
      let(:second_sha) { '0e50ec4d3c7ce42ab74dda1d422cb2cbffe1e326' }

      it 'returns the number of commits between' do
        expect(repository.commits_between(first_sha, second_sha).count).to eq(3)
      end
    end

    context 'SHA and master branch' do
      let(:sha) { 'b0e52af38d7ea43cf41d8a6f2471351ac036d6c9' }
      let(:branch) { 'master' }

      it 'returns the number of commits between a sha and a branch' do
        expect(repository.commits_between(sha, branch).count).to eq(5)
      end

      it 'returns the number of commits between a branch and a sha' do
        expect(repository.commits_between(branch, sha).count).to eq(0) # sha is before branch
      end
    end

    context 'two branches' do
      let(:first_branch) { 'feature' }
      let(:second_branch) { 'master' }

      it 'returns the number of commits between' do
        expect(repository.commits_between(first_branch, second_branch).count).to eq(17)
      end
    end
  end

  describe '#count_commits_between' do
    subject { repository.count_commits_between('feature', 'master') }

    it { is_expected.to eq(17) }
  end

1003 1004 1005
  describe '#count_commits' do
    context 'with after timestamp' do
      it 'returns the number of commits after timestamp' do
1006
        options = { ref: 'master', limit: nil, after: Time.iso8601('2013-03-03T20:15:01+00:00') }
1007

1008
        expect(repository.count_commits(options)).to eq(25)
1009 1010 1011 1012 1013
      end
    end

    context 'with before timestamp' do
      it 'returns the number of commits after timestamp' do
1014
        options = { ref: 'feature', limit: nil, before: Time.iso8601('2015-03-03T20:15:01+00:00') }
1015

1016
        expect(repository.count_commits(options)).to eq(9)
1017 1018 1019 1020 1021
      end
    end

    context 'with path' do
      it 'returns the number of commits with path ' do
1022
        options = { ref: 'master', limit: nil, path: "encoding" }
1023

1024
        expect(repository.count_commits(options)).to eq(2)
1025 1026 1027 1028
      end
    end
  end

R
Robert Speicher 已提交
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
  describe "branch_names_contains" do
    subject { repository.branch_names_contains(SeedRepo::LastCommit::ID) }

    it { is_expected.to include('master') }
    it { is_expected.not_to include('feature') }
    it { is_expected.not_to include('fix') }
  end

  describe '#autocrlf' do
    before(:all) do
1039
      @repo = Gitlab::Git::Repository.new('default', TEST_MUTABLE_REPO_PATH)
R
Robert Speicher 已提交
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
      @repo.rugged.config['core.autocrlf'] = true
    end

    it 'return the value of the autocrlf option' do
      expect(@repo.autocrlf).to be(true)
    end

    after(:all) do
      @repo.rugged.config.delete('core.autocrlf')
    end
  end

  describe '#autocrlf=' do
    before(:all) do
1054
      @repo = Gitlab::Git::Repository.new('default', TEST_MUTABLE_REPO_PATH)
R
Robert Speicher 已提交
1055 1056 1057 1058 1059 1060
      @repo.rugged.config['core.autocrlf'] = false
    end

    it 'should set the autocrlf option to the provided option' do
      @repo.autocrlf = :input

1061
      File.open(File.join(SEED_STORAGE_PATH, TEST_MUTABLE_REPO_PATH, '.git', 'config')) do |config_file|
R
Robert Speicher 已提交
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
        expect(config_file.read).to match('autocrlf = input')
      end
    end

    after(:all) do
      @repo.rugged.config.delete('core.autocrlf')
    end
  end

  describe '#find_branch' do
    it 'should return a Branch for master' do
      branch = repository.find_branch('master')

      expect(branch).to be_a_kind_of(Gitlab::Git::Branch)
      expect(branch.name).to eq('master')
    end

    it 'should handle non-existent branch' do
      branch = repository.find_branch('this-is-garbage')

      expect(branch).to eq(nil)
    end

    it 'should reload Rugged::Repository and return master' do
      expect(Rugged::Repository).to receive(:new).twice.and_call_original

      repository.find_branch('master')
      branch = repository.find_branch('master', force_reload: true)

      expect(branch).to be_a_kind_of(Gitlab::Git::Branch)
      expect(branch.name).to eq('master')
    end
1094 1095
  end

1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
  describe '#ref_name_for_sha' do
    let(:ref_path) { 'refs/heads' }
    let(:sha) { repository.find_branch('master').dereferenced_target.id }
    let(:ref_name) { 'refs/heads/master' }

    it 'returns the ref name for the given sha' do
      expect(repository.ref_name_for_sha(ref_path, sha)).to eq(ref_name)
    end

    it "returns an empty name if the ref doesn't exist" do
      expect(repository.ref_name_for_sha(ref_path, "000000")).to eq("")
    end

    it "raise an exception if the ref is empty" do
      expect { repository.ref_name_for_sha(ref_path, "") }.to raise_error(ArgumentError)
    end

    it "raise an exception if the ref is nil" do
      expect { repository.ref_name_for_sha(ref_path, nil) }.to raise_error(ArgumentError)
    end
  end

R
Robert Speicher 已提交
1118 1119 1120 1121 1122
  describe '#branches with deleted branch' do
    before(:each) do
      ref = double()
      allow(ref).to receive(:name) { 'bad-branch' }
      allow(ref).to receive(:target) { raise Rugged::ReferenceError }
1123 1124 1125
      branches = double()
      allow(branches).to receive(:each) { [ref].each }
      allow(repository.rugged).to receive(:branches) { branches }
R
Robert Speicher 已提交
1126 1127 1128 1129 1130 1131 1132 1133 1134
    end

    it 'should return empty branches' do
      expect(repository.branches).to eq([])
    end
  end

  describe '#branch_count' do
    it 'returns the number of branches' do
K
Kim "BKC" Carlbäcker 已提交
1135
      expect(repository.branch_count).to eq(9)
R
Robert Speicher 已提交
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
    end
  end

  describe "#ls_files" do
    let(:master_file_paths) { repository.ls_files("master") }
    let(:not_existed_branch) { repository.ls_files("not_existed_branch") }

    it "read every file paths of master branch" do
      expect(master_file_paths.length).to equal(40)
    end

    it "reads full file paths of master branch" do
      expect(master_file_paths).to include("files/html/500.html")
    end

    it "dose 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
  end

  describe "#copy_gitattributes" do
1165
    let(:attributes_path) { File.join(SEED_STORAGE_PATH, TEST_REPO_PATH, 'info/attributes') }
R
Robert Speicher 已提交
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267

    it "raises an error with invalid ref" do
      expect { repository.copy_gitattributes("invalid") }.to raise_error(Gitlab::Git::Repository::InvalidRef)
    end

    context "with no .gitattrbutes" do
      before(:each) do
        repository.copy_gitattributes("master")
      end

      it "does not have an info/attributes" do
        expect(File.exist?(attributes_path)).to be_falsey
      end

      after(:each) do
        FileUtils.rm_rf(attributes_path)
      end
    end

    context "with .gitattrbutes" do
      before(:each) do
        repository.copy_gitattributes("gitattributes")
      end

      it "has an info/attributes" do
        expect(File.exist?(attributes_path)).to be_truthy
      end

      it "has the same content in info/attributes as .gitattributes" do
        contents = File.open(attributes_path, "rb") { |f| f.read }
        expect(contents).to eq("*.md binary\n")
      end

      after(:each) do
        FileUtils.rm_rf(attributes_path)
      end
    end

    context "with updated .gitattrbutes" do
      before(:each) do
        repository.copy_gitattributes("gitattributes")
        repository.copy_gitattributes("gitattributes-updated")
      end

      it "has an info/attributes" do
        expect(File.exist?(attributes_path)).to be_truthy
      end

      it "has the updated content in info/attributes" do
        contents = File.read(attributes_path)
        expect(contents).to eq("*.txt binary\n")
      end

      after(:each) do
        FileUtils.rm_rf(attributes_path)
      end
    end

    context "with no .gitattrbutes in HEAD but with previous info/attributes" do
      before(:each) do
        repository.copy_gitattributes("gitattributes")
        repository.copy_gitattributes("master")
      end

      it "does not have an info/attributes" do
        expect(File.exist?(attributes_path)).to be_falsey
      end

      after(:each) do
        FileUtils.rm_rf(attributes_path)
      end
    end
  end

  describe '#tag_exists?' do
    it 'returns true for an existing tag' do
      tag = repository.tag_names.first

      expect(repository.tag_exists?(tag)).to eq(true)
    end

    it 'returns false for a non-existing tag' do
      expect(repository.tag_exists?('v9000')).to eq(false)
    end
  end

  describe '#branch_exists?' do
    it 'returns true for an existing branch' do
      expect(repository.branch_exists?('master')).to eq(true)
    end

    it 'returns false for a non-existing branch' do
      expect(repository.branch_exists?('kittens')).to eq(false)
    end

    it 'returns false when using an invalid branch name' do
      expect(repository.branch_exists?('.bla')).to eq(false)
    end
  end

  describe '#local_branches' do
    before(:all) do
1268
      @repo = Gitlab::Git::Repository.new('default', File.join(TEST_MUTABLE_REPO_PATH, '.git'))
R
Robert Speicher 已提交
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
    end

    after(:all) do
      FileUtils.rm_rf(TEST_MUTABLE_REPO_PATH)
      ensure_seeds
    end

    it 'returns the local branches' do
      create_remote_branch('joe', 'remote_branch', 'master')
      @repo.create_branch('local_branch', 'master')

      expect(@repo.local_branches.any? { |branch| branch.name == 'remote_branch' }).to eq(false)
      expect(@repo.local_branches.any? { |branch| branch.name == 'local_branch' }).to eq(true)
    end
1283 1284

    context 'with gitaly enabled' do
1285 1286 1287 1288 1289 1290 1291
      before do
        stub_gitaly
      end

      after do
        Gitlab::GitalyClient.clear_stubs!
      end
1292

J
Jacob Vosmaer 已提交
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
      it 'returns a Branch with UTF-8 fields' do
        branches = @repo.local_branches.to_a
        expect(branches.size).to be > 0
        utf_8 = Encoding.find('utf-8')
        branches.each do |branch|
          expect(branch.name.encoding).to eq(utf_8)
          expect(branch.target.encoding).to eq(utf_8) unless branch.target.nil?
        end
      end

1303
      it 'gets the branches from GitalyClient' do
1304 1305
        expect_any_instance_of(Gitlab::GitalyClient::Ref).to receive(:local_branches)
          .and_return([])
1306 1307 1308 1309
        @repo.local_branches
      end

      it 'wraps GRPC not found' do
1310 1311
        expect_any_instance_of(Gitlab::GitalyClient::Ref).to receive(:local_branches)
          .and_raise(GRPC::NotFound)
1312 1313 1314 1315
        expect { @repo.local_branches }.to raise_error(Gitlab::Git::Repository::NoRepository)
      end

      it 'wraps GRPC exceptions' do
1316 1317
        expect_any_instance_of(Gitlab::GitalyClient::Ref).to receive(:local_branches)
          .and_raise(GRPC::Unknown)
1318 1319 1320
        expect { @repo.local_branches }.to raise_error(Gitlab::Git::CommandError)
      end
    end
R
Robert Speicher 已提交
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
  end

  def create_remote_branch(remote_name, branch_name, source_branch_name)
    source_branch = @repo.branches.find { |branch| branch.name == source_branch_name }
    rugged = @repo.rugged
    rugged.references.create("refs/remotes/#{remote_name}/#{branch_name}", source_branch.dereferenced_target.sha)
  end

  # Build the options hash that's passed to Rugged::Commit#create
  def commit_options(repo, index, 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? ? [] : [repo.head.target].compact
    options[:update_ref] = "HEAD"

    options
  end

  # Writes a new commit to the repo and returns a Rugged::Commit.  Replaces the
  # contents of CHANGELOG with a single new line of text.
  def new_commit_edit_old_file(repo)
    oid = repo.write("I replaced the changelog with this text", :blob)
    index = repo.index
    index.read_tree(repo.head.target.tree)
    index.add(path: "CHANGELOG", oid: oid, mode: 0100644)

    options = commit_options(
      repo,
      index,
      "Edit CHANGELOG in its original location"
    )

    sha = Rugged::Commit.create(repo, options)
    repo.lookup(sha)
  end

  # Writes a new commit to the repo and returns a Rugged::Commit.  Replaces the
  # contents of encoding/CHANGELOG with new text.
  def new_commit_edit_new_file(repo)
    oid = repo.write("I'm a new changelog with different text", :blob)
    index = repo.index
    index.read_tree(repo.head.target.tree)
    index.add(path: "encoding/CHANGELOG", oid: oid, mode: 0100644)

    options = commit_options(repo, index, "Edit encoding/CHANGELOG")

    sha = Rugged::Commit.create(repo, options)
    repo.lookup(sha)
  end

  # Writes a new commit to the repo and returns a Rugged::Commit.  Moves the
  # CHANGELOG file to the encoding/ directory.
  def new_commit_move_file(repo)
    blob_oid = repo.head.target.tree.detect { |i| i[:name] == "CHANGELOG" }[:oid]
    file_content = repo.lookup(blob_oid).content
    oid = repo.write(file_content, :blob)
    index = repo.index
    index.read_tree(repo.head.target.tree)
    index.add(path: "encoding/CHANGELOG", oid: oid, mode: 0100644)
    index.remove("CHANGELOG")

    options = commit_options(repo, index, "Move CHANGELOG to encoding/")

    sha = Rugged::Commit.create(repo, options)
    repo.lookup(sha)
  end
1398 1399 1400 1401 1402 1403 1404

  def stub_gitaly
    allow(Gitlab::GitalyClient).to receive(:feature_enabled?).and_return(true)

    stub = double(:stub)
    allow(Gitaly::Ref::Stub).to receive(:new).and_return(stub)
  end
R
Robert Speicher 已提交
1405
end