Raise InvalidRepository error for non-valid git repositories

parent 68b71df6
...@@ -30,6 +30,7 @@ module Gitlab ...@@ -30,6 +30,7 @@ module Gitlab
EMPTY_REPOSITORY_CHECKSUM = '0000000000000000000000000000000000000000'.freeze EMPTY_REPOSITORY_CHECKSUM = '0000000000000000000000000000000000000000'.freeze
NoRepository = Class.new(StandardError) NoRepository = Class.new(StandardError)
InvalidRepository = Class.new(StandardError)
InvalidBlobName = Class.new(StandardError) InvalidBlobName = Class.new(StandardError)
InvalidRef = Class.new(StandardError) InvalidRef = Class.new(StandardError)
GitError = Class.new(StandardError) GitError = Class.new(StandardError)
...@@ -1584,7 +1585,7 @@ module Gitlab ...@@ -1584,7 +1585,7 @@ module Gitlab
def checksum def checksum
gitaly_migrate(:calculate_checksum, gitaly_migrate(:calculate_checksum,
status: Gitlab::GitalyClient::MigrationStatus::OPT_OUT) do |is_enabled| status: Gitlab::GitalyClient::MigrationStatus::OPT_OUT) do |is_enabled|
if is_enabled if is_enabled
gitaly_repository_client.calculate_checksum gitaly_repository_client.calculate_checksum
else else
...@@ -2533,10 +2534,12 @@ module Gitlab ...@@ -2533,10 +2534,12 @@ module Gitlab
output, status = run_git(args) output, status = run_git(args)
if status.nil? || !status.zero? if status.nil? || !status.zero?
# Empty repositories return with a non-zero status and an empty output. # Non-valid git repositories return 128 as the status code and an error output
return EMPTY_REPOSITORY_CHECKSUM if output&.empty? raise InvalidRepository if status == 128 && output.to_s.downcase =~ /not a git repository/
# Empty repositories returns with a non-zero status and an empty output.
raise ChecksumError, output unless output.blank?
raise ChecksumError, output return EMPTY_REPOSITORY_CHECKSUM
end end
refs = output.split("\n") refs = output.split("\n")
......
...@@ -292,6 +292,8 @@ module Gitlab ...@@ -292,6 +292,8 @@ module Gitlab
request = Gitaly::CalculateChecksumRequest.new(repository: @gitaly_repo) request = Gitaly::CalculateChecksumRequest.new(repository: @gitaly_repo)
response = GitalyClient.call(@storage, :repository_service, :calculate_checksum, request) response = GitalyClient.call(@storage, :repository_service, :calculate_checksum, request)
response.checksum.presence response.checksum.presence
rescue GRPC::DataLoss => e
raise Gitlab::Git::Repository::InvalidRepository.new(e)
end end
def raw_changes_between(from, to) def raw_changes_between(from, to)
......
...@@ -2275,7 +2275,22 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -2275,7 +2275,22 @@ describe Gitlab::Git::Repository, seed_helper: true do
expect(empty_repo.checksum).to eq '0000000000000000000000000000000000000000' expect(empty_repo.checksum).to eq '0000000000000000000000000000000000000000'
end end
it 'raises a no repository exception when there is no repo' do it 'raises Gitlab::Git::Repository::InvalidRepository error for non-valid git repo' do
FileUtils.rm_rf(File.join(storage_path, 'non-valid.git'))
system(git_env, *%W(#{Gitlab.config.git.bin_path} clone --bare #{TEST_REPO_PATH} non-valid.git),
chdir: SEED_STORAGE_PATH,
out: '/dev/null',
err: '/dev/null')
File.truncate(File.join(storage_path, 'non-valid.git/HEAD'), 0)
non_valid = described_class.new('default', 'non-valid.git', '')
expect { non_valid.checksum }.to raise_error(Gitlab::Git::Repository::InvalidRepository)
end
it 'raises Gitlab::Git::Repository::NoRepository error when there is no repo' do
broken_repo = described_class.new('default', 'a/path.git', '') broken_repo = described_class.new('default', 'a/path.git', '')
expect { broken_repo.checksum }.to raise_error(Gitlab::Git::Repository::NoRepository) expect { broken_repo.checksum }.to raise_error(Gitlab::Git::Repository::NoRepository)
......
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