From bdc658095c6bc7e7a2a49447b404156f3f947fe1 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Mon, 5 Dec 2011 09:43:53 +0200 Subject: refcatoring. cleaning after gitosis --- app/controllers/application_controller.rb | 4 +- app/controllers/errors_controller.rb | 4 +- app/controllers/projects_controller.rb | 4 +- app/models/key.rb | 12 ++--- app/models/project.rb | 14 +++--- app/models/repository.rb | 12 ++--- app/models/users_project.rb | 6 +-- config/routes.rb | 2 +- lib/gitlabhq/git_host.rb | 15 +++--- lib/gitlabhq/gitosis.rb | 76 ------------------------------- lib/tasks/gitolite_rebuild.rake | 11 +++++ spec/models/project_spec.rb | 10 ++-- spec/monkeypatch.rb | 12 ++--- 13 files changed, 58 insertions(+), 124 deletions(-) delete mode 100644 lib/gitlabhq/gitosis.rb create mode 100644 lib/tasks/gitolite_rebuild.rake diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 1f971302889..dd0c3c3ec55 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,8 +3,8 @@ class ApplicationController < ActionController::Base protect_from_forgery helper_method :abilities, :can? - rescue_from Gitlabhq::Gitosis::AccessDenied, Gitlabhq::Gitolite::AccessDenied do |exception| - render :file => File.join(Rails.root, "public", "gitosis_error"), :layout => false + rescue_from Gitlabhq::Gitolite::AccessDenied do |exception| + render :file => File.join(Rails.root, "public", "githost_error"), :layout => false end layout :layout_by_resource diff --git a/app/controllers/errors_controller.rb b/app/controllers/errors_controller.rb index c37f10a39f3..3ef7aa46441 100644 --- a/app/controllers/errors_controller.rb +++ b/app/controllers/errors_controller.rb @@ -1,5 +1,5 @@ class ErrorsController < ApplicationController - def gitosis - render :file => File.join(Rails.root, "public", "gitosis_error"), :layout => false + def githost + render :file => File.join(Rails.root, "public", "githost_error"), :layout => false end end diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 4b8c196b375..69a539c85a2 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -42,8 +42,8 @@ class ProjectsController < ApplicationController format.js end end - rescue Gitlabhq::Gitosis::AccessDenied, Gitlabhq::Gitolite::AccessDenied - render :js => "location.href = '#{errors_gitosis_path}'" and return + rescue Gitlabhq::Gitolite::AccessDenied + render :js => "location.href = '#{errors_githost_path}'" and return rescue StandardError => ex @project.errors.add(:base, "Cant save project. Please try again later") respond_to do |format| diff --git a/app/models/key.rb b/app/models/key.rb index 572f002b5bc..e265842caa8 100644 --- a/app/models/key.rb +++ b/app/models/key.rb @@ -11,29 +11,29 @@ class Key < ActiveRecord::Base :length => { :within => 0..5000 } before_save :set_identifier - after_save :update_gitosis - after_destroy :gitosis_delete_key + after_save :update_repository + after_destroy :repository_delete_key def set_identifier self.identifier = "#{user.identifier}_#{Time.now.to_i}" end - def update_gitosis + def update_repository Gitlabhq::GitHost.system.new.configure do |c| c.update_keys(identifier, key) projects.each do |project| - c.update_project(project.path, project.gitosis_writers) + c.update_project(project.path, project.repository_writers) end end end - def gitosis_delete_key + def repository_delete_key Gitlabhq::GitHost.system.new.configure do |c| c.delete_key(identifier) projects.each do |project| - c.update_project(project.path, project.gitosis_writers) + c.update_project(project.path, project.repository_writers) end end end diff --git a/app/models/project.rb b/app/models/project.rb index f4841653996..d78513d42f7 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -40,8 +40,8 @@ class Project < ActiveRecord::Base validate :check_limit validate :repo_name - after_destroy :destroy_gitosis_project - after_save :update_gitosis_project + after_destroy :destroy_repository + after_save :update_repository attr_protected :private_flag, :owner_id @@ -54,8 +54,8 @@ class Project < ActiveRecord::Base delegate :repo, :url_to_repo, :path_to_repo, - :update_gitosis_project, - :destroy_gitosis_project, + :update_repository, + :destroy_repository, :tags, :repo_exists?, :commit, @@ -113,7 +113,7 @@ class Project < ActiveRecord::Base @writers ||= users_projects.includes(:user).where(:write => true).map(&:user) end - def gitosis_writers + def repository_writers keys = Key.joins({:user => :users_projects}).where("users_projects.project_id = ? AND users_projects.write = ?", id, true) keys.map(&:identifier) end @@ -184,8 +184,8 @@ class Project < ActiveRecord::Base end def repo_name - if path == "gitosis-admin" && path == "gitolite-admin" - errors.add(:path, " like 'gitosis-admin' is not allowed") + if path == "gitolite-admin" + errors.add(:path, " like 'gitolite-admin' is not allowed") end end diff --git a/app/models/repository.rb b/app/models/repository.rb index 1a1c90a6b28..7140719556e 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -24,24 +24,20 @@ class Repository end def url_to_repo - if !GIT_HOST["port"] or GIT_HOST["port"] == 22 - "#{GIT_HOST["git_user"]}@#{GIT_HOST["host"]}:#{path}.git" - else - "ssh://#{GIT_HOST["git_user"]}@#{GIT_HOST["host"]}:#{GIT_HOST["port"]}/#{path}.git" - end + Gitlabhq::GitHost.url_to_repo(path) end def path_to_repo GIT_HOST["base_path"] + path + ".git" end - def update_gitosis_project + def update_repository Gitlabhq::GitHost.system.new.configure do |c| - c.update_project(path, project.gitosis_writers) + c.update_project(path, project.repository_writers) end end - def destroy_gitosis_project + def destroy_repository Gitlabhq::GitHost.system.new.configure do |c| c.destroy_project(@project) end diff --git a/app/models/users_project.rb b/app/models/users_project.rb index 96e2d16aff0..9a114087b93 100644 --- a/app/models/users_project.rb +++ b/app/models/users_project.rb @@ -4,7 +4,7 @@ class UsersProject < ActiveRecord::Base attr_protected :project_id, :project - after_commit :update_gitosis_project + after_commit :update_repository validates_uniqueness_of :user_id, :scope => [:project_id] validates_presence_of :user_id @@ -13,9 +13,9 @@ class UsersProject < ActiveRecord::Base delegate :name, :email, :to => :user, :prefix => true - def update_gitosis_project + def update_repository Gitosis.new.configure do |c| - c.update_project(project.path, project.gitosis_writers) + c.update_project(project.path, project.repository) end end diff --git a/config/routes.rb b/config/routes.rb index c74cf2268e0..ad8b0b31eaa 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -14,7 +14,7 @@ Gitlab::Application.routes.draw do root :to => "users#index" end - get "errors/gitosis" + get "errors/githost" get "profile/password", :to => "profile#password" put "profile/password", :to => "profile#password_update" put "profile/reset_private_token", :to => "profile#reset_private_token" diff --git a/lib/gitlabhq/git_host.rb b/lib/gitlabhq/git_host.rb index 714d92f537a..9a6eecb3f0a 100644 --- a/lib/gitlabhq/git_host.rb +++ b/lib/gitlabhq/git_host.rb @@ -1,18 +1,21 @@ require File.join(Rails.root, "lib", "gitlabhq", "gitolite") -require File.join(Rails.root, "lib", "gitlabhq", "gitosis") module Gitlabhq class GitHost def self.system - if GIT_HOST["system"] == "gitosis" - Gitlabhq::Gitosis - else - Gitlabhq::Gitolite - end + Gitlabhq::Gitolite end def self.admin_uri GIT_HOST["admin_uri"] end + + def self.url_to_repo(path) + if !GIT_HOST["port"] or GIT_HOST["port"] == 22 + "#{GIT_HOST["git_user"]}@#{GIT_HOST["host"]}:#{path}.git" + else + "ssh://#{GIT_HOST["git_user"]}@#{GIT_HOST["host"]}:#{GIT_HOST["port"]}/#{path}.git" + end + end end end diff --git a/lib/gitlabhq/gitosis.rb b/lib/gitlabhq/gitosis.rb deleted file mode 100644 index a3dbcc80b22..00000000000 --- a/lib/gitlabhq/gitosis.rb +++ /dev/null @@ -1,76 +0,0 @@ -require 'inifile' -require 'timeout' -require 'fileutils' - -module Gitlabhq - class Gitosis - class AccessDenied < StandardError; end - - def pull - # create tmp dir - @local_dir = File.join(Dir.tmpdir,"gitlabhq-gitosis-#{Time.now.to_i}") - - Dir.mkdir @local_dir - - `git clone #{GitHost.admin_uri} #{@local_dir}/gitosis` - end - - def push - Dir.chdir(File.join(@local_dir, "gitosis")) - `git add -A` - `git commit -am "Gitlab"` - `git push` - Dir.chdir(Rails.root) - - FileUtils.rm_rf(@local_dir) - end - - def configure - status = Timeout::timeout(20) do - File.open(File.join(Dir.tmpdir,"gitlabhq-gitosis.lock"), "w+") do |f| - begin - f.flock(File::LOCK_EX) - pull - yield(self) - push - ensure - f.flock(File::LOCK_UN) - end - end - end - rescue Exception => ex - raise Gitosis::AccessDenied.new("gitosis timeout") - end - - def destroy_project(project) - `sudo -u git rm -rf #{project.path_to_repo}` - - conf = IniFile.new(File.join(@local_dir,'gitosis','gitosis.conf')) - - conf.delete_section("group #{project.path}") - - conf.write - end - - #update or create - def update_keys(user, key) - File.open(File.join(@local_dir, 'gitosis/keydir',"#{user}.pub"), 'w') {|f| f.write(key.gsub(/\n/,'')) } - end - - def delete_key(user) - File.unlink(File.join(@local_dir, 'gitosis/keydir',"#{user}.pub")) - `cd #{File.join(@local_dir,'gitosis')} ; git rm keydir/#{user}.pub` - end - - #update or create - def update_project(repo_name, name_writers) - # write config file - conf = IniFile.new(File.join(@local_dir,'gitosis','gitosis.conf')) - - conf["group #{repo_name}"]['writable'] = repo_name - conf["group #{repo_name}"]['members'] = name_writers.join(' ') - - conf.write - end - end -end diff --git a/lib/tasks/gitolite_rebuild.rake b/lib/tasks/gitolite_rebuild.rake new file mode 100644 index 00000000000..5cf496196eb --- /dev/null +++ b/lib/tasks/gitolite_rebuild.rake @@ -0,0 +1,11 @@ +desc "Rebuild each project at gitolite config" +task :gitolite_rebuild => :environment do + puts "Starting..." + Project.find_each(:batch_size => 100) do |project| + puts + puts "=== #{project.name}" + project.update_repository + puts + end + puts "Done" +end diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index cde1884a987..eda20a0c011 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -18,12 +18,12 @@ describe Project do describe "Respond to" do it { should respond_to(:readers) } it { should respond_to(:writers) } - it { should respond_to(:gitosis_writers) } + it { should respond_to(:repository_writers) } it { should respond_to(:admins) } it { should respond_to(:add_access) } it { should respond_to(:reset_access) } - it { should respond_to(:update_gitosis_project) } - it { should respond_to(:destroy_gitosis_project) } + it { should respond_to(:update_repository) } + it { should respond_to(:destroy_repository) } it { should respond_to(:public?) } it { should respond_to(:private?) } it { should respond_to(:url_to_repo) } @@ -35,9 +35,9 @@ describe Project do it { should respond_to(:commit) } end - it "should not allow 'gitosis-admin' as repo name" do + it "should not allow 'gitolite-admin' as repo name" do should allow_value("blah").for(:path) - should_not allow_value("gitosis-admin").for(:path) + should_not allow_value("gitolite-admin").for(:path) end it "should return valid url to repo" do diff --git a/spec/monkeypatch.rb b/spec/monkeypatch.rb index 2e491496a79..75099e0553a 100644 --- a/spec/monkeypatch.rb +++ b/spec/monkeypatch.rb @@ -1,11 +1,11 @@ -# Stubbing Project <-> gitosis path +# Stubbing Project <-> git host path # create project using Factory only class Project - def update_gitosis_project + def update_repository true end - def update_gitosis + def update_repository true end @@ -15,17 +15,17 @@ class Project end class Key - def update_gitosis + def update_repository true end - def gitosis_delete_key + def repository_delete_key true end end class UsersProject - def update_gitosis_project + def update_repository true end end -- cgit v1.2.1