Commit 2a406d15 authored by Nick Thomas's avatar Nick Thomas

Merge branch 'jc-refactor-mv-repository' into 'master'

Call RenameRepository and RemoveRepository

See merge request gitlab-org/gitlab!17292
parents 4d3aac3c 1e1ca339
......@@ -234,8 +234,6 @@ module Geo
# Move the current canonical repository to the deleted path for reference
if repository.exists?
ensure_repository_namespace(deleted_disk_path_temp)
unless gitlab_shell.mv_repository(project.repository_storage, repository.disk_path, deleted_disk_path_temp)
raise Gitlab::Shell::Error, 'Can not move original repository out of the way'
end
......@@ -243,8 +241,6 @@ module Geo
# Move the temporary repository to the canonical path
ensure_repository_namespace(repository.disk_path)
unless gitlab_shell.mv_repository(project.repository_storage, disk_path_temp, repository.disk_path)
raise Gitlab::Shell::Error, 'Can not move temporary repository to canonical location'
end
......@@ -255,13 +251,6 @@ module Geo
end
end
def ensure_repository_namespace(disk_path)
gitlab_shell.add_namespace(
project.repository_storage,
File.dirname(disk_path)
)
end
def new_repository?
@new_repository
end
......
......@@ -347,16 +347,6 @@ describe Geo::RepositorySyncService do
expect(subject).to receive(:sync_repository).and_call_original
expect(subject.gitlab_shell).to receive(:mv_repository).exactly(2).times.and_call_original
expect(subject.gitlab_shell).to receive(:add_namespace).with(
project.repository_storage,
"@failed-geo-sync/#{File.dirname(repository.disk_path)}"
).and_call_original
expect(subject.gitlab_shell).to receive(:add_namespace).with(
project.repository_storage,
File.dirname(repository.disk_path)
).and_call_original
expect(subject.gitlab_shell).to receive(:remove_repository).exactly(2).times.and_call_original
subject.execute
......
......@@ -131,6 +131,18 @@ module Gitlab
end
end
def rename(new_relative_path)
wrapped_gitaly_errors do
gitaly_repository_client.rename(new_relative_path)
end
end
def remove
wrapped_gitaly_errors do
gitaly_repository_client.remove
end
end
def expire_has_local_branches_cache
clear_memoization(:has_local_branches)
end
......
......@@ -347,6 +347,18 @@ module Gitlab
GitalyClient.call(@storage, :object_pool_service, :disconnect_git_alternates, request, timeout: GitalyClient.long_timeout)
end
def rename(relative_path)
request = Gitaly::RenameRepositoryRequest.new(repository: @gitaly_repo, relative_path: relative_path)
GitalyClient.call(@storage, :repository_service, :rename_repository, request, timeout: GitalyClient.fast_timeout)
end
def remove
request = Gitaly::RemoveRepositoryRequest.new(repository: @gitaly_repo)
GitalyClient.call(@storage, :repository_service, :remove_repository, request, timeout: GitalyClient.long_timeout)
end
private
def search_results_from_response(gitaly_response)
......
......@@ -126,7 +126,13 @@ module Gitlab
def mv_repository(storage, path, new_path)
return false if path.empty? || new_path.empty?
!!mv_directory(storage, "#{path}.git", "#{new_path}.git")
Gitlab::Git::Repository.new(storage, "#{path}.git", nil, nil).rename("#{new_path}.git")
true
rescue => e
Gitlab::Sentry.track_acceptable_exception(e, extra: { path: path, new_path: new_path, storage: storage })
false
end
# Fork repository to new path
......@@ -151,9 +157,13 @@ module Gitlab
def remove_repository(storage, name)
return false if name.empty?
!!rm_directory(storage, "#{name}.git")
rescue ArgumentError => e
Gitlab::Git::Repository.new(storage, "#{name}.git", nil, nil).remove
true
rescue => e
Rails.logger.warn("Repository does not exist: #{e} at: #{name}.git") # rubocop:disable Gitlab/RailsLogger
Gitlab::Sentry.track_acceptable_exception(e, extra: { path: name, storage: storage })
false
end
......
......@@ -2236,4 +2236,33 @@ describe Gitlab::Git::Repository, :seed_helper do
expect(repository.commit(new_commit.oid).id).to eq(new_commit.oid)
end
end
describe '#rename' do
let(:project) { create(:project, :repository)}
let(:repository) { project.repository }
it 'moves the repository' do
checksum = repository.checksum
new_relative_path = "rename_test/relative/path"
renamed_repository = Gitlab::Git::Repository.new(repository.storage, new_relative_path, nil, nil)
repository.rename(new_relative_path)
expect(renamed_repository.checksum).to eq(checksum)
expect(repository.exists?).to be false
end
end
describe '#remove' do
let(:project) { create(:project, :repository)}
let(:repository) { project.repository }
it 'removes the repository' do
expect(repository.exists?).to be true
repository.remove
expect(repository.raw_repository.exists?).to be false
end
end
end
......@@ -272,4 +272,26 @@ describe Gitlab::GitalyClient::RepositoryService do
end
end
end
describe 'remove' do
it 'sends a remove_repository message' do
expect_any_instance_of(Gitaly::RepositoryService::Stub)
.to receive(:remove_repository)
.with(gitaly_request_with_path(storage_name, relative_path), kind_of(Hash))
.and_return(double(value: true))
client.remove
end
end
describe 'rename' do
it 'sends a rename_repository message' do
expect_any_instance_of(Gitaly::RepositoryService::Stub)
.to receive(:rename_repository)
.with(gitaly_request_with_path(storage_name, relative_path), kind_of(Hash))
.and_return(double(value: true))
client.rename('some/new/path')
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