2012-11-24 10:46:19 +01:00
|
|
|
# ProjectMover class
|
|
|
|
#
|
|
|
|
# Used for moving project repositories from one subdir to another
|
|
|
|
module Gitlab
|
|
|
|
class ProjectMover
|
2012-11-24 11:25:04 +01:00
|
|
|
class ProjectMoveError < StandardError; end
|
|
|
|
|
2012-11-24 10:46:19 +01:00
|
|
|
attr_reader :project, :old_dir, :new_dir
|
|
|
|
|
|
|
|
def initialize(project, old_dir, new_dir)
|
|
|
|
@project = project
|
|
|
|
@old_dir = old_dir
|
|
|
|
@new_dir = new_dir
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
# Create new dir if missing
|
2013-02-11 18:16:59 +01:00
|
|
|
new_dir_path = File.join(Gitlab.config.gitlab_shell.repos_path, new_dir)
|
2012-12-07 01:51:49 +01:00
|
|
|
FileUtils.mkdir( new_dir_path, mode: 0770 ) unless File.exists?(new_dir_path)
|
2012-11-24 10:46:19 +01:00
|
|
|
|
2013-02-11 18:16:59 +01:00
|
|
|
old_path = File.join(Gitlab.config.gitlab_shell.repos_path, old_dir, "#{project.path}.git")
|
2012-11-24 10:46:19 +01:00
|
|
|
new_path = File.join(new_dir_path, "#{project.path}.git")
|
|
|
|
|
2012-11-27 07:31:15 +01:00
|
|
|
if File.exists? new_path
|
|
|
|
raise ProjectMoveError.new("Destination #{new_path} already exists")
|
|
|
|
end
|
|
|
|
|
2012-12-07 01:51:49 +01:00
|
|
|
begin
|
|
|
|
FileUtils.mv( old_path, new_path )
|
2012-11-24 10:46:19 +01:00
|
|
|
log_info "Project #{project.name} was moved from #{old_path} to #{new_path}"
|
|
|
|
true
|
2012-12-07 01:51:49 +01:00
|
|
|
rescue Exception => e
|
2012-11-24 11:25:04 +01:00
|
|
|
message = "Project #{project.name} cannot be moved from #{old_path} to #{new_path}"
|
2012-12-07 01:51:49 +01:00
|
|
|
log_info "Error! #{message} (#{e.message})"
|
2012-11-24 11:25:04 +01:00
|
|
|
raise ProjectMoveError.new(message)
|
2012-11-24 10:46:19 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-07 01:51:49 +01:00
|
|
|
protected
|
2012-11-24 10:46:19 +01:00
|
|
|
|
|
|
|
def log_info message
|
|
|
|
Gitlab::AppLogger.info message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|