Commit 4023d9f85290faea47a2b2bcf3ce879ed0f07e9f
1 parent
d71a68af
Exists in
master
and in
4 other branches
class for moving project
Showing
5 changed files
with
56 additions
and
24 deletions
Show diff stats
app/observers/project_observer.rb
@@ -4,7 +4,10 @@ class ProjectObserver < ActiveRecord::Observer | @@ -4,7 +4,10 @@ class ProjectObserver < ActiveRecord::Observer | ||
4 | 4 | ||
5 | # Move repository if namespace changed | 5 | # Move repository if namespace changed |
6 | if project.namespace_id_changed? and not project.new_record? | 6 | if project.namespace_id_changed? and not project.new_record? |
7 | - move_project(project) | 7 | + old_dir = Namespace.find_by_id(project.namespace_id_was).try(:path) || '' |
8 | + new_dir = Namespace.find_by_id(project.namespace_id).try(:path) || '' | ||
9 | + | ||
10 | + Gitlab::ProjectMover.new(project, old_dir, new_dir).execute | ||
8 | end | 11 | end |
9 | end | 12 | end |
10 | 13 | ||
@@ -23,20 +26,4 @@ class ProjectObserver < ActiveRecord::Observer | @@ -23,20 +26,4 @@ class ProjectObserver < ActiveRecord::Observer | ||
23 | def log_info message | 26 | def log_info message |
24 | Gitlab::AppLogger.info message | 27 | Gitlab::AppLogger.info message |
25 | end | 28 | end |
26 | - | ||
27 | - def move_project(project) | ||
28 | - old_dir = Namespace.find_by_id(project.namespace_id_was).try(:path) || '' | ||
29 | - new_dir = Namespace.find_by_id(project.namespace_id).try(:path) || '' | ||
30 | - | ||
31 | - # Create new dir if missing | ||
32 | - new_dir_path = File.join(Gitlab.config.git_base_path, new_dir) | ||
33 | - Dir.mkdir(new_dir_path) unless File.exists?(new_dir_path) | ||
34 | - | ||
35 | - old_path = File.join(Gitlab.config.git_base_path, old_dir, "#{project.path}.git") | ||
36 | - new_path = File.join(new_dir_path, "#{project.path}.git") | ||
37 | - | ||
38 | - `mv #{old_path} #{new_path}` | ||
39 | - | ||
40 | - log_info "Project #{project.name} was moved from #{old_path} to #{new_path}" | ||
41 | - end | ||
42 | end | 29 | end |
features/support/env.rb
@@ -5,7 +5,7 @@ require 'rspec' | @@ -5,7 +5,7 @@ require 'rspec' | ||
5 | require 'database_cleaner' | 5 | require 'database_cleaner' |
6 | require 'spinach/capybara' | 6 | require 'spinach/capybara' |
7 | 7 | ||
8 | -%w(gitolite_stub stubbed_repository valid_commit).each do |f| | 8 | +%w(namespaces_stub gitolite_stub stubbed_repository valid_commit).each do |f| |
9 | require Rails.root.join('spec', 'support', f) | 9 | require Rails.root.join('spec', 'support', f) |
10 | end | 10 | end |
11 | 11 |
@@ -0,0 +1,37 @@ | @@ -0,0 +1,37 @@ | ||
1 | +# ProjectMover class | ||
2 | +# | ||
3 | +# Used for moving project repositories from one subdir to another | ||
4 | +module Gitlab | ||
5 | + class ProjectMover | ||
6 | + attr_reader :project, :old_dir, :new_dir | ||
7 | + | ||
8 | + def initialize(project, old_dir, new_dir) | ||
9 | + @project = project | ||
10 | + @old_dir = old_dir | ||
11 | + @new_dir = new_dir | ||
12 | + end | ||
13 | + | ||
14 | + def execute | ||
15 | + # Create new dir if missing | ||
16 | + new_dir_path = File.join(Gitlab.config.git_base_path, new_dir) | ||
17 | + Dir.mkdir(new_dir_path) unless File.exists?(new_dir_path) | ||
18 | + | ||
19 | + old_path = File.join(Gitlab.config.git_base_path, old_dir, "#{project.path}.git") | ||
20 | + new_path = File.join(new_dir_path, "#{project.path}.git") | ||
21 | + | ||
22 | + if system("mv #{old_path} #{new_path}") | ||
23 | + log_info "Project #{project.name} was moved from #{old_path} to #{new_path}" | ||
24 | + true | ||
25 | + else | ||
26 | + log_info "Error! Project #{project.name} cannot be moved from #{old_path} to #{new_path}" | ||
27 | + false | ||
28 | + end | ||
29 | + end | ||
30 | + | ||
31 | + protected | ||
32 | + | ||
33 | + def log_info message | ||
34 | + Gitlab::AppLogger.info message | ||
35 | + end | ||
36 | + end | ||
37 | +end |
spec/support/stubbed_repository.rb