Checksum calculation is handled by Gitaly when feature is enabled

上级 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,7 +22,13 @@ module Gitlab ...@@ -21,7 +22,13 @@ module Gitlab
failure!(Gitlab::Git::Repository::NoRepository, 'No repository for such path') failure!(Gitlab::Git::Repository::NoRepository, 'No repository for such path')
end 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 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,37 +2,48 @@ require 'spec_helper' ...@@ -2,37 +2,48 @@ 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' }
it 'raises Gitlab::Git::Repository::NoRepository when there is no repo' do shared_examples 'calculating checksum' do
checksum = described_class.new(storage, 'nonexistent-repo') 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 expect { checksum.calculate }.to raise_error Gitlab::Git::Repository::NoRepository
end end
it 'pretends that checksum is 000000... when the repo is empty' do it 'pretends that checksum is 000000... when the repo is empty' do
FileUtils.rm_rf(File.join(SEED_STORAGE_PATH, 'empty-repo.git')) 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), system(git_env, *%W(#{Gitlab.config.git.bin_path} init --bare empty-repo.git),
chdir: SEED_STORAGE_PATH, chdir: SEED_STORAGE_PATH,
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'
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
end end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册