From 4023d9f85290faea47a2b2bcf3ce879ed0f07e9f Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Sat, 24 Nov 2012 11:46:19 +0200 Subject: [PATCH] class for moving project --- app/observers/project_observer.rb | 21 ++++------------- features/support/env.rb | 2 +- lib/gitlab/project_mover.rb | 37 ++++++++++++++++++++++++++++++ spec/support/namespaces_stub.rb | 14 +++++++++++ spec/support/stubbed_repository.rb | 6 ----- 5 files changed, 56 insertions(+), 24 deletions(-) create mode 100644 lib/gitlab/project_mover.rb create mode 100644 spec/support/namespaces_stub.rb diff --git a/app/observers/project_observer.rb b/app/observers/project_observer.rb index 6d92562a275..2d59daf1ec3 100644 --- a/app/observers/project_observer.rb +++ b/app/observers/project_observer.rb @@ -4,7 +4,10 @@ class ProjectObserver < ActiveRecord::Observer # Move repository if namespace changed if project.namespace_id_changed? and not project.new_record? - move_project(project) + old_dir = Namespace.find_by_id(project.namespace_id_was).try(:path) || '' + new_dir = Namespace.find_by_id(project.namespace_id).try(:path) || '' + + Gitlab::ProjectMover.new(project, old_dir, new_dir).execute end end @@ -23,20 +26,4 @@ class ProjectObserver < ActiveRecord::Observer def log_info message Gitlab::AppLogger.info message end - - def move_project(project) - old_dir = Namespace.find_by_id(project.namespace_id_was).try(:path) || '' - new_dir = Namespace.find_by_id(project.namespace_id).try(:path) || '' - - # Create new dir if missing - new_dir_path = File.join(Gitlab.config.git_base_path, new_dir) - Dir.mkdir(new_dir_path) unless File.exists?(new_dir_path) - - old_path = File.join(Gitlab.config.git_base_path, old_dir, "#{project.path}.git") - new_path = File.join(new_dir_path, "#{project.path}.git") - - `mv #{old_path} #{new_path}` - - log_info "Project #{project.name} was moved from #{old_path} to #{new_path}" - end end diff --git a/features/support/env.rb b/features/support/env.rb index 1a72d765197..42aba5a6d68 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -5,7 +5,7 @@ require 'rspec' require 'database_cleaner' require 'spinach/capybara' -%w(gitolite_stub stubbed_repository valid_commit).each do |f| +%w(namespaces_stub gitolite_stub stubbed_repository valid_commit).each do |f| require Rails.root.join('spec', 'support', f) end diff --git a/lib/gitlab/project_mover.rb b/lib/gitlab/project_mover.rb new file mode 100644 index 00000000000..9dc3e019faa --- /dev/null +++ b/lib/gitlab/project_mover.rb @@ -0,0 +1,37 @@ +# ProjectMover class +# +# Used for moving project repositories from one subdir to another +module Gitlab + class ProjectMover + 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 + new_dir_path = File.join(Gitlab.config.git_base_path, new_dir) + Dir.mkdir(new_dir_path) unless File.exists?(new_dir_path) + + old_path = File.join(Gitlab.config.git_base_path, old_dir, "#{project.path}.git") + new_path = File.join(new_dir_path, "#{project.path}.git") + + if system("mv #{old_path} #{new_path}") + log_info "Project #{project.name} was moved from #{old_path} to #{new_path}" + true + else + log_info "Error! Project #{project.name} cannot be moved from #{old_path} to #{new_path}" + false + end + end + + protected + + def log_info message + Gitlab::AppLogger.info message + end + end +end diff --git a/spec/support/namespaces_stub.rb b/spec/support/namespaces_stub.rb new file mode 100644 index 00000000000..35754f56e83 --- /dev/null +++ b/spec/support/namespaces_stub.rb @@ -0,0 +1,14 @@ +require 'namespace' +require 'gitlab/project_mover' + +class Namespace + def ensure_dir_exist + true + end +end + +class Gitlab::ProjectMover + def execute + true + end +end diff --git a/spec/support/stubbed_repository.rb b/spec/support/stubbed_repository.rb index 871319fa1b8..5bf3ea46099 100644 --- a/spec/support/stubbed_repository.rb +++ b/spec/support/stubbed_repository.rb @@ -28,10 +28,4 @@ module StubbedRepository end end -class Namespace - def ensure_dir_exist - true - end -end - Project.send(:include, StubbedRepository)