Checksum calculation is handled by Gitaly when feature is enabled

parent e892eeb5
...@@ -7,13 +7,14 @@ module Gitlab ...@@ -7,13 +7,14 @@ module Gitlab
Failure = Class.new(StandardError) 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 = storage
@storage_path = Gitlab.config.repositories.storages[storage].legacy_disk_path @storage_path = Gitlab.config.repositories.storages[storage].legacy_disk_path
@relative_path = "#{relative_path}.git" @relative_path = "#{relative_path}.git"
@path = File.join(storage_path, @relative_path) @path = File.join(storage_path, @relative_path)
@gl_repository = gl_repository
end end
def calculate def calculate
...@@ -21,8 +22,14 @@ module Gitlab ...@@ -21,8 +22,14 @@ module Gitlab
failure!(Gitlab::Git::Repository::NoRepository, 'No repository for such path') failure!(Gitlab::Git::Repository::NoRepository, 'No repository for such path')
end end
raw_repository.gitaly_migrate(:calculate_checksum) do |is_enabled|
if is_enabled
calculate_checksum_gitaly
else
calculate_checksum_by_shelling_out calculate_checksum_by_shelling_out
end end
end
end
private private
...@@ -30,6 +37,10 @@ module Gitlab ...@@ -30,6 +37,10 @@ module Gitlab
raw_repository.exists? raw_repository.exists?
end end
def calculate_checksum_gitaly
gitaly_repository_client.calculate_checksum
end
def calculate_checksum_by_shelling_out def calculate_checksum_by_shelling_out
args = %W(--git-dir=#{path} show-ref --heads --tags) args = %W(--git-dir=#{path} show-ref --heads --tags)
output, status = run_git(args) output, status = run_git(args)
...@@ -69,7 +80,11 @@ module Gitlab ...@@ -69,7 +80,11 @@ module Gitlab
end end
def raw_repository 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 end
def run_git(args) def run_git(args)
......
...@@ -2,9 +2,11 @@ require 'spec_helper' ...@@ -2,9 +2,11 @@ require 'spec_helper'
describe Gitlab::Git::Checksum, seed_helper: true do describe Gitlab::Git::Checksum, seed_helper: true do
let(:storage) { 'default' } let(:storage) { 'default' }
let(:gl_repository) { 'project-123' }
shared_examples 'calculating checksum' do
it 'raises Gitlab::Git::Repository::NoRepository when there is no repo' do it 'raises Gitlab::Git::Repository::NoRepository when there is no repo' do
checksum = described_class.new(storage, 'nonexistent-repo') checksum = described_class.new(storage, 'nonexistent-repo', gl_repository)
expect { checksum.calculate }.to raise_error Gitlab::Git::Repository::NoRepository expect { checksum.calculate }.to raise_error Gitlab::Git::Repository::NoRepository
end end
...@@ -17,22 +19,31 @@ describe Gitlab::Git::Checksum, seed_helper: true do ...@@ -17,22 +19,31 @@ describe Gitlab::Git::Checksum, seed_helper: true do
out: '/dev/null', out: '/dev/null',
err: '/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' expect(checksum.calculate).to eq '0000000000000000000000000000000000000000'
end end
it 'raises Gitlab::Git::Repository::Failure when shelling out to git return non-zero status' do it 'calculates the checksum when there is a repo' do
checksum = described_class.new(storage, 'gitlab-git-test') 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 end
it 'calculates the checksum when there is a repo' do context 'when calculate_checksum Gitaly feature is disabled', :disable_gitaly do
checksum = described_class.new(storage, 'gitlab-git-test') it_behaves_like 'calculating checksum'
expect(checksum.calculate).to eq '54f21be4c32c02f6788d72207fa03ad3bce725e4' 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 raise_error Gitlab::Git::Checksum::Failure
end
end 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