Checksum calculation is handled by Gitaly when feature is enabled

parent e892eeb5
......@@ -7,13 +7,14 @@ module Gitlab
Failure = Class.new(StandardError)
attr_reader :path, :relative_path, :storage, :storage_path
attr_reader :path, :relative_path, :storage, :storage_path, :gl_repository
def initialize(storage, relative_path)
def initialize(storage, relative_path, gl_repository)
@storage = storage
@storage_path = Gitlab.config.repositories.storages[storage].legacy_disk_path
@relative_path = "#{relative_path}.git"
@path = File.join(storage_path, @relative_path)
@gl_repository = gl_repository
end
def calculate
......@@ -21,7 +22,13 @@ module Gitlab
failure!(Gitlab::Git::Repository::NoRepository, 'No repository for such path')
end
calculate_checksum_by_shelling_out
raw_repository.gitaly_migrate(:calculate_checksum) do |is_enabled|
if is_enabled
calculate_checksum_gitaly
else
calculate_checksum_by_shelling_out
end
end
end
private
......@@ -30,6 +37,10 @@ module Gitlab
raw_repository.exists?
end
def calculate_checksum_gitaly
gitaly_repository_client.calculate_checksum
end
def calculate_checksum_by_shelling_out
args = %W(--git-dir=#{path} show-ref --heads --tags)
output, status = run_git(args)
......@@ -69,7 +80,11 @@ module Gitlab
end
def raw_repository
Gitlab::Git::Repository.new(storage, relative_path, nil)
@raw_repository ||= Gitlab::Git::Repository.new(storage, relative_path, gl_repository)
end
def gitaly_repository_client
raw_repository.gitaly_repository_client
end
def run_git(args)
......
......@@ -2,37 +2,48 @@ require 'spec_helper'
describe Gitlab::Git::Checksum, seed_helper: true do
let(:storage) { 'default' }
let(:gl_repository) { 'project-123' }
it 'raises Gitlab::Git::Repository::NoRepository when there is no repo' do
checksum = described_class.new(storage, 'nonexistent-repo')
shared_examples 'calculating checksum' do
it 'raises Gitlab::Git::Repository::NoRepository when there is no repo' do
checksum = described_class.new(storage, 'nonexistent-repo', gl_repository)
expect { checksum.calculate }.to raise_error Gitlab::Git::Repository::NoRepository
end
expect { checksum.calculate }.to raise_error Gitlab::Git::Repository::NoRepository
end
it 'pretends that checksum is 000000... when the repo is empty' do
FileUtils.rm_rf(File.join(SEED_STORAGE_PATH, 'empty-repo.git'))
it 'pretends that checksum is 000000... when the repo is empty' do
FileUtils.rm_rf(File.join(SEED_STORAGE_PATH, 'empty-repo.git'))
system(git_env, *%W(#{Gitlab.config.git.bin_path} init --bare empty-repo.git),
chdir: SEED_STORAGE_PATH,
out: '/dev/null',
err: '/dev/null')
system(git_env, *%W(#{Gitlab.config.git.bin_path} init --bare empty-repo.git),
chdir: SEED_STORAGE_PATH,
out: '/dev/null',
err: '/dev/null')
checksum = described_class.new(storage, 'empty-repo')
checksum = described_class.new(storage, 'empty-repo', gl_repository)
expect(checksum.calculate).to eq '0000000000000000000000000000000000000000'
end
expect(checksum.calculate).to eq '0000000000000000000000000000000000000000'
end
it 'raises Gitlab::Git::Repository::Failure when shelling out to git return non-zero status' do
checksum = described_class.new(storage, 'gitlab-git-test')
it 'calculates the checksum when there is a repo' do
checksum = described_class.new(storage, 'gitlab-git-test', gl_repository)
allow(checksum).to receive(:popen).and_return(['output', nil])
expect(checksum.calculate).to eq '54f21be4c32c02f6788d72207fa03ad3bce725e4'
end
end
expect { checksum.calculate }.to raise_error Gitlab::Git::Checksum::Failure
context 'when calculate_checksum Gitaly feature is enabled' do
it_behaves_like 'calculating checksum'
end
it 'calculates the checksum when there is a repo' do
checksum = described_class.new(storage, 'gitlab-git-test')
context 'when calculate_checksum Gitaly feature is disabled', :disable_gitaly do
it_behaves_like 'calculating checksum'
it "raises a Gitlab::Git::Repository::Failure error if the `popen` call to git returns a non-zero exit code" do
checksum = described_class.new(storage, 'gitlab-git-test', gl_repository)
allow(checksum).to receive(:popen).and_return(['output', nil])
expect(checksum.calculate).to eq '54f21be4c32c02f6788d72207fa03ad3bce725e4'
expect { checksum.calculate }.to raise_error Gitlab::Git::Checksum::Failure
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment