From 70bf7f6e191c0e99ad03e1eec2ecbe2aae53fa09 Mon Sep 17 00:00:00 2001
From: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Date: Wed, 21 Nov 2012 08:54:05 +0300
Subject: [PATCH] Project -> update repo only on create and destroy. Fixtures
 Updated with namespaces. Fixed moving repo

---
 app/controllers/projects_controller.rb     |  2 +-
 app/models/namespace.rb                    |  6 +++++
 app/models/project.rb                      |  4 ++++
 app/observers/project_observer.rb          |  5 +++-
 app/views/layouts/notify.html.haml         |  4 ++--
 app/views/projects/_form.html.haml         |  6 ++---
 db/fixtures/development/001_admin.rb       | 27 +++++++---------------
 db/fixtures/development/002_project.rb     |  2 +-
 db/fixtures/development/009_source_code.rb |  7 +++---
 lib/gitlab/backend/gitolite.rb             |  7 ++++++
 lib/gitlab/backend/gitolite_config.rb      |  6 ++++-
 11 files changed, 45 insertions(+), 31 deletions(-)

diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb
index 1a402efa7c4..37b47adeba9 100644
--- a/app/controllers/projects_controller.rb
+++ b/app/controllers/projects_controller.rb
@@ -36,7 +36,7 @@ class ProjectsController < ProjectResourceController
   def update
     namespace_id = params[:project].delete(:namespace_id)
 
-    if namespace_id
+    if namespace_id.present? and namespace_id.to_i != project.namespace_id
       namespace = Namespace.find(namespace_id)
       project.transfer(namespace)
     end
diff --git a/app/models/namespace.rb b/app/models/namespace.rb
index 013660406af..8eae36db8e2 100644
--- a/app/models/namespace.rb
+++ b/app/models/namespace.rb
@@ -27,6 +27,7 @@ class Namespace < ActiveRecord::Base
 
   after_create :ensure_dir_exist
   after_update :move_dir
+  after_destroy :rm_dir
 
   scope :root, where('type IS NULL')
 
@@ -52,4 +53,9 @@ class Namespace < ActiveRecord::Base
     new_path = File.join(Gitlab.config.git_base_path, path)
     system("mv #{old_path} #{new_path}")
   end
+
+  def rm_dir
+    dir_path = File.join(Gitlab.config.git_base_path, path)
+    system("rm -rf #{dir_path}")
+  end
 end
diff --git a/app/models/project.rb b/app/models/project.rb
index 4874dd14d8e..707f8286ef8 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -254,6 +254,10 @@ class Project < ActiveRecord::Base
       old_dir = old_namespace.try(:path) || ''
       new_dir = new_namespace.try(:path) || ''
 
+      old_repo = File.join(old_dir, self.path)
+
+      git_host.move_repository(old_repo, self.path_with_namespace, self)
+
       Gitlab::ProjectMover.new(self, old_dir, new_dir).execute
 
       save!
diff --git a/app/observers/project_observer.rb b/app/observers/project_observer.rb
index 03a61709829..bd41e51e8e4 100644
--- a/app/observers/project_observer.rb
+++ b/app/observers/project_observer.rb
@@ -1,8 +1,11 @@
 class ProjectObserver < ActiveRecord::Observer
-  def after_save(project)
+  def after_create(project)
     project.update_repository
   end
 
+  def after_save(project)
+  end
+
   def after_destroy(project)
     log_info("Project \"#{project.name}\" was removed")
 
diff --git a/app/views/layouts/notify.html.haml b/app/views/layouts/notify.html.haml
index 7b79897b653..8cfe1982316 100644
--- a/app/views/layouts/notify.html.haml
+++ b/app/views/layouts/notify.html.haml
@@ -4,7 +4,7 @@
       %title
         GitLab
       :css
-        .header h1 {color: #BBBBBB !important; font: bold 32px Helvetica, Arial, sans-serif; margin: 0; padding: 0; line-height: 40px;}
+        .header h1 {color: #BBBBBB !important; font: bold 22px Helvetica, Arial, sans-serif; margin: 0; padding: 0; line-height: 32px;}
         .header p {color: #c6c6c6; font: normal 12px Helvetica, Arial, sans-serif; margin: 0; padding: 0; line-height: 18px;}
         .content h2 {color:#646464 !important; font-weight: bold; margin: 0; padding: 0; line-height: 26px; font-size: 18px; font-family: Helvetica, Arial, sans-serif;  }
         .content p {color:#767676; font-weight: normal; margin: 0; padding: 0; line-height: 20px; font-size: 12px;font-family: Helvetica, Arial, sans-serif;}
@@ -35,5 +35,5 @@
                   %p{style: "font-size: 11px; color:#7d7a7a; margin: 0; padding: 0; font-family: Helvetica, Arial, sans-serif;"}
                     You're receiving this notification because you are a member of the
                     - if @project
-                      #{@project.name} 
+                      #{@project.name}
                     project team.
diff --git a/app/views/projects/_form.html.haml b/app/views/projects/_form.html.haml
index fe926adbceb..5c60132cc82 100644
--- a/app/views/projects/_form.html.haml
+++ b/app/views/projects/_form.html.haml
@@ -13,15 +13,15 @@
     %legend  Advanced settings:
     .control-group
       = f.label :path do
-        Path
+        Repository
       .controls
-        = text_field_tag :ppath, @project.path_to_repo, class: "xlarge", disabled: true
+        = text_field_tag :ppath, @project.path_to_repo, class: "xxlarge", readonly: true
 
     .control-group
       = f.label :namespace_id do
         %span Namespace
       .controls
-        = f.select :namespace_id, namespaces_options(@project.namespace_id), {}, {class: 'chosen'}
+        = f.select :namespace_id, namespaces_options(@project.namespace_id), {prompt: 'Choose a project namespace'}, {class: 'chosen'}
         &nbsp;
         %span.cred Be careful. Changing project namespace can have unintended side effects
 
diff --git a/db/fixtures/development/001_admin.rb b/db/fixtures/development/001_admin.rb
index 51939e8ee9c..fbe41e4d22d 100644
--- a/db/fixtures/development/001_admin.rb
+++ b/db/fixtures/development/001_admin.rb
@@ -1,22 +1,11 @@
-unless User.count > 0
-  admin = User.create(
-    email: "admin@local.host",
+User.seed(:id, [
+  {
+    id: 1,
     name: "Administrator",
+    email: "admin@local.host",
     username: 'root',
     password: "5iveL!fe",
-    password_confirmation: "5iveL!fe"
-  )
-
-  admin.projects_limit = 10000
-  admin.admin = true
-  admin.save!
-
-  if admin.valid?
-    puts %q[
-    Administrator account created:
-
-    login.........admin@local.host
-    password......5iveL!fe
-    ]
-  end
-end
+    password_confirmation: "5iveL!fe",
+    admin: true,
+  }
+])
diff --git a/db/fixtures/development/002_project.rb b/db/fixtures/development/002_project.rb
index b6c0b54764f..91d42a14201 100644
--- a/db/fixtures/development/002_project.rb
+++ b/db/fixtures/development/002_project.rb
@@ -1,5 +1,5 @@
 Project.seed(:id, [
-  { id: 1, name: "Underscore.js", path: "underscore", owner_id: 1 },
+  { id: 1, name: "Underscore.js", path: "underscore", owner_id: 1, namespace_id: 1 },
   { id: 2, name: "Diaspora", path: "diaspora", owner_id: 1 },
   { id: 3, name: "Ruby on Rails", path: "rails", owner_id: 1 }
 ])
diff --git a/db/fixtures/development/009_source_code.rb b/db/fixtures/development/009_source_code.rb
index 489bd02ea32..849d1aab8ae 100644
--- a/db/fixtures/development/009_source_code.rb
+++ b/db/fixtures/development/009_source_code.rb
@@ -1,7 +1,7 @@
 root = Gitlab.config.git_base_path
 
 projects = [
-  { path: 'underscore.git', git: 'https://github.com/documentcloud/underscore.git' },
+  { path: 'root/underscore.git', git: 'https://github.com/documentcloud/underscore.git' },
   { path: 'diaspora.git', git: 'https://github.com/diaspora/diaspora.git' },
   { path: 'rails.git', git: 'https://github.com/rails/rails.git' },
 ]
@@ -13,9 +13,10 @@ projects.each do |project|
   next if File.exists?(project_path)
 
   cmds = [
-    "cd #{root} && sudo -u git -H git clone --bare #{project[:git]}",
+    "cd #{root} && sudo -u git -H git clone --bare #{project[:git]} ./#{project[:path]}",
     "sudo cp ./lib/hooks/post-receive #{project_path}/hooks/post-receive",
-    "sudo chown git:git #{project_path}/hooks/post-receive"
+    "sudo chown git:git -R #{project_path}",
+    "sudo chmod 770 -R #{project_path}",
   ]
 
   cmds.each do |cmd|
diff --git a/lib/gitlab/backend/gitolite.rb b/lib/gitlab/backend/gitolite.rb
index 6d88b92bdd2..79fe150c19c 100644
--- a/lib/gitlab/backend/gitolite.rb
+++ b/lib/gitlab/backend/gitolite.rb
@@ -26,6 +26,13 @@ module Gitlab
       config.update_project!(project.path, project)
     end
 
+    def move_repository(old_repo, new_repo, project)
+      config.apply do |config|
+        config.clean_repo(old_repo)
+        config.update_project(new_repo, project)
+      end
+    end
+
     def remove_repository project
       config.destroy_project!(project)
     end
diff --git a/lib/gitlab/backend/gitolite_config.rb b/lib/gitlab/backend/gitolite_config.rb
index 396d4329505..c000aab0aa9 100644
--- a/lib/gitlab/backend/gitolite_config.rb
+++ b/lib/gitlab/backend/gitolite_config.rb
@@ -83,7 +83,11 @@ module Gitlab
 
     def destroy_project(project)
       FileUtils.rm_rf(project.path_to_repo)
-      conf.rm_repo(project.path)
+      conf.rm_repo(project.path_with_namespace)
+    end
+
+    def clean_repo repo_name
+      conf.rm_repo(repo_name)
     end
 
     def destroy_project!(project)
-- 
2.30.9