2020-03-13 15:44:24 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Repositories::BaseService < BaseService
|
|
|
|
include Gitlab::ShellAdapter
|
|
|
|
|
|
|
|
DELETED_FLAG = '+deleted'
|
|
|
|
|
|
|
|
attr_reader :repository
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
delegate :container, :disk_path, :full_path, to: :repository
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
def initialize(repository)
|
|
|
|
@repository = repository
|
|
|
|
end
|
|
|
|
|
|
|
|
def repo_exists?(path)
|
2020-07-28 23:09:34 +05:30
|
|
|
gitlab_shell.repository_exists?(repository.shard, path + '.git')
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def mv_repository(from_path, to_path)
|
|
|
|
return true unless repo_exists?(from_path)
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
gitlab_shell.mv_repository(repository.shard, from_path, to_path)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# Build a path for removing repositories
|
|
|
|
# We use `+` because its not allowed by GitLab so user can not create
|
|
|
|
# project with name cookies+119+deleted and capture someone stalled repository
|
|
|
|
#
|
|
|
|
# gitlab/cookies.git -> gitlab/cookies+119+deleted.git
|
|
|
|
#
|
|
|
|
def removal_path
|
2020-04-08 14:13:33 +05:30
|
|
|
"#{disk_path}+#{container.id}#{DELETED_FLAG}"
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# If we get a Gitaly error, the repository may be corrupted. We can
|
|
|
|
# ignore these errors since we're going to trash the repositories
|
|
|
|
# anyway.
|
|
|
|
def ignore_git_errors(&block)
|
|
|
|
yield
|
|
|
|
rescue Gitlab::Git::CommandError => e
|
2020-04-08 14:13:33 +05:30
|
|
|
Gitlab::GitLogger.warn(class: self.class.name, container_id: container.id, disk_path: disk_path, message: e.to_s)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def move_error(path)
|
|
|
|
error = %Q{Repository "#{path}" could not be moved}
|
|
|
|
|
|
|
|
log_error(error)
|
|
|
|
error(error)
|
|
|
|
end
|
|
|
|
end
|