Commit 6f393877 authored by Yorick Peterse's avatar Yorick Peterse

Use File.exist? to check if a repository exists

Initializing Rugged objects is way too expensive just to check if a
repository exists. Even though we cache this data once in a while we
have to refresh this. On GitLab.com we have seen Repository#exists?
taking up to _1 minute_ to complete in the absolute worst case, though
usually it sits around a second or so.

Using File.exist? to instead check if $GIT_DIR/refs exists is a much
faster way of checking if a repository was initialized properly.
parent df5548e1
...@@ -41,11 +41,7 @@ class Repository ...@@ -41,11 +41,7 @@ class Repository
return @exists unless @exists.nil? return @exists unless @exists.nil?
@exists = cache.fetch(:exists?) do @exists = cache.fetch(:exists?) do
begin refs_directory_exists?
raw_repository && raw_repository.rugged ? true : false
rescue Gitlab::Git::Repository::NoRepository
false
end
end end
end end
...@@ -1148,6 +1144,12 @@ class Repository ...@@ -1148,6 +1144,12 @@ class Repository
private private
def refs_directory_exists?
return false unless path_with_namespace
File.exist?(File.join(path_to_repo, 'refs'))
end
def cache def cache
@cache ||= RepositoryCache.new(path_with_namespace, @project.id) @cache ||= RepositoryCache.new(path_with_namespace, @project.id)
end end
......
...@@ -811,8 +811,7 @@ describe Repository, models: true do ...@@ -811,8 +811,7 @@ describe Repository, models: true do
end end
it 'returns false when a repository does not exist' do it 'returns false when a repository does not exist' do
expect(repository.raw_repository).to receive(:rugged). allow(repository).to receive(:refs_directory_exists?).and_return(false)
and_raise(Gitlab::Git::Repository::NoRepository)
expect(repository.exists?).to eq(false) expect(repository.exists?).to eq(false)
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