Commit 59723625 authored by randx's avatar randx

Merge branch 'lib_refactoring'

parents 8ceb9408 3272620f
...@@ -5,7 +5,7 @@ class ApplicationController < ActionController::Base ...@@ -5,7 +5,7 @@ class ApplicationController < ActionController::Base
protect_from_forgery protect_from_forgery
helper_method :abilities, :can? helper_method :abilities, :can?
rescue_from Gitlabhq::Gitolite::AccessDenied do |exception| rescue_from Gitlab::Gitolite::AccessDenied do |exception|
render "errors/gitolite", :layout => "error" render "errors/gitolite", :layout => "error"
end end
......
...@@ -45,7 +45,7 @@ class ProjectsController < ApplicationController ...@@ -45,7 +45,7 @@ class ProjectsController < ApplicationController
format.js format.js
end end
end end
rescue Gitlabhq::Gitolite::AccessDenied rescue Gitlab::Gitolite::AccessDenied
render :js => "location.href = '#{errors_githost_path}'" and return render :js => "location.href = '#{errors_githost_path}'" and return
rescue StandardError => ex rescue StandardError => ex
@project.errors.add(:base, "Cant save project. Please try again later") @project.errors.add(:base, "Cant save project. Please try again later")
......
...@@ -143,6 +143,6 @@ module ApplicationHelper ...@@ -143,6 +143,6 @@ module ApplicationHelper
end end
def string_to_utf8 str def string_to_utf8 str
Gitlabhq::Encode.utf8 str Gitlab::Encode.utf8 str
end end
end end
class Commit class Commit
include ActiveModel::Conversion include ActiveModel::Conversion
include Gitlabhq::Encode include Gitlab::Encode
extend ActiveModel::Naming extend ActiveModel::Naming
attr_accessor :commit attr_accessor :commit
......
...@@ -38,14 +38,14 @@ class Key < ActiveRecord::Base ...@@ -38,14 +38,14 @@ class Key < ActiveRecord::Base
end end
def update_repository def update_repository
Gitlabhq::GitHost.system.new.configure do |c| Gitlab::GitHost.system.new.configure do |c|
c.update_keys(identifier, key) c.update_keys(identifier, key)
c.update_projects(projects) c.update_projects(projects)
end end
end end
def repository_delete_key def repository_delete_key
Gitlabhq::GitHost.system.new.configure do |c| Gitlab::GitHost.system.new.configure do |c|
#delete key file is there is no identically deploy keys #delete key file is there is no identically deploy keys
if !is_deploy_key || Key.where(:identifier => identifier).count() == 0 if !is_deploy_key || Key.where(:identifier => identifier).count() == 0
c.delete_key(identifier) c.delete_key(identifier)
......
...@@ -82,7 +82,7 @@ class MergeRequest < ActiveRecord::Base ...@@ -82,7 +82,7 @@ class MergeRequest < ActiveRecord::Base
end end
def check_if_can_be_merged def check_if_can_be_merged
self.state = if GitlabMerge.new(self, self.author).can_be_merged? self.state = if Gitlab::Merge.new(self, self.author).can_be_merged?
CAN_BE_MERGED CAN_BE_MERGED
else else
CANNOT_BE_MERGED CANNOT_BE_MERGED
...@@ -184,7 +184,7 @@ class MergeRequest < ActiveRecord::Base ...@@ -184,7 +184,7 @@ class MergeRequest < ActiveRecord::Base
end end
def automerge!(current_user) def automerge!(current_user)
if GitlabMerge.new(self, current_user).merge if Gitlab::Merge.new(self, current_user).merge
self.merge!(current_user.id) self.merge!(current_user.id)
true true
end end
......
...@@ -38,7 +38,7 @@ module Project::RepositoryTrait ...@@ -38,7 +38,7 @@ module Project::RepositoryTrait
end end
def satellite def satellite
@satellite ||= Gitlabhq::Satellite.new(self) @satellite ||= Gitlab::Satellite.new(self)
end end
def write_hook(name, content) def write_hook(name, content)
...@@ -65,7 +65,7 @@ module Project::RepositoryTrait ...@@ -65,7 +65,7 @@ module Project::RepositoryTrait
end end
def url_to_repo def url_to_repo
Gitlabhq::GitHost.url_to_repo(path) Gitlab::GitHost.url_to_repo(path)
end end
def path_to_repo def path_to_repo
...@@ -73,13 +73,13 @@ module Project::RepositoryTrait ...@@ -73,13 +73,13 @@ module Project::RepositoryTrait
end end
def update_repository def update_repository
Gitlabhq::GitHost.system.update_project(path, self) Gitlab::GitHost.system.update_project(path, self)
write_hooks if File.exists?(path_to_repo) write_hooks if File.exists?(path_to_repo)
end end
def destroy_repository def destroy_repository
Gitlabhq::GitHost.system.destroy_project(self) Gitlab::GitHost.system.destroy_project(self)
end end
def repo_exists? def repo_exists?
......
...@@ -7,7 +7,7 @@ class ProtectedBranch < ActiveRecord::Base ...@@ -7,7 +7,7 @@ class ProtectedBranch < ActiveRecord::Base
after_destroy :update_repository after_destroy :update_repository
def update_repository def update_repository
Gitlabhq::GitHost.system.update_project(project.path, project) Gitlab::GitHost.system.update_project(project.path, project)
end end
def commit def commit
......
...@@ -58,7 +58,7 @@ class UsersProject < ActiveRecord::Base ...@@ -58,7 +58,7 @@ class UsersProject < ActiveRecord::Base
end end
def update_repository def update_repository
Gitlabhq::GitHost.system.new.configure do |c| Gitlab::GitHost.system.new.configure do |c|
c.update_project(project.path, project) c.update_project(project.path, project)
end end
end end
......
...@@ -4,4 +4,4 @@ require File.expand_path('../application', __FILE__) ...@@ -4,4 +4,4 @@ require File.expand_path('../application', __FILE__)
# Initialize the rails application # Initialize the rails application
Gitlab::Application.initialize! Gitlab::Application.initialize!
require File.join(Rails.root, "lib", "gitlabhq", "git_host") require File.join(Rails.root, "lib", "gitlab", "git_host")
...@@ -15,11 +15,11 @@ end ...@@ -15,11 +15,11 @@ end
Grit::Diff.class_eval do Grit::Diff.class_eval do
def old_path def old_path
Gitlabhq::Encode.utf8 a_path Gitlab::Encode.utf8 a_path
end end
def new_path def new_path
Gitlabhq::Encode.utf8 b_path Gitlab::Encode.utf8 b_path
end end
end end
......
module Gitlabhq module Gitlab
module Encode module Encode
extend self extend self
......
require File.join(Rails.root, "lib", "gitlabhq", "gitolite") require File.join(Rails.root, "lib", "gitlab", "gitolite")
module Gitlabhq module Gitlab
class GitHost class GitHost
def self.system def self.system
Gitlabhq::Gitolite Gitlab::Gitolite
end end
def self.admin_uri def self.admin_uri
......
...@@ -2,7 +2,7 @@ require 'gitolite' ...@@ -2,7 +2,7 @@ require 'gitolite'
require 'timeout' require 'timeout'
require 'fileutils' require 'fileutils'
module Gitlabhq module Gitlab
class Gitolite class Gitolite
class AccessDenied < StandardError; end class AccessDenied < StandardError; end
...@@ -46,7 +46,7 @@ module Gitlabhq ...@@ -46,7 +46,7 @@ module Gitlabhq
end end
end end
rescue Exception => ex rescue Exception => ex
Gitlabhq::Logger.error(ex.message) Gitlab::Logger.error(ex.message)
raise Gitolite::AccessDenied.new("gitolite timeout") raise Gitolite::AccessDenied.new("gitolite timeout")
end end
......
module Gitlabhq module Gitlab
class Logger class Logger
def self.error(message) def self.error(message)
@@logger ||= ::Logger.new(File.join(Rails.root, "log/githost.log")) @@logger ||= ::Logger.new(File.join(Rails.root, "log/githost.log"))
......
module Gitlab
class Merge
attr_accessor :project, :merge_request, :user
def initialize(merge_request, user)
self.user = user
self.merge_request = merge_request
self.project = merge_request.project
end
def can_be_merged?
result = false
process do |repo, output|
result = !(output =~ /CONFLICT/)
end
result
end
def merge
process do |repo, output|
if output =~ /CONFLICT/
false
else
repo.git.push({}, "origin", merge_request.target_branch)
true
end
end
end
def process
Grit::Git.with_timeout(30.seconds) do
lock_file = File.join(Rails.root, "tmp", "merge_repo_#{project.path}.lock")
File.open(lock_file, "w+") do |f|
f.flock(File::LOCK_EX)
unless project.satellite.exists?
raise "You should run: rake gitlab:app:enable_automerge"
end
project.satellite.clear
Dir.chdir(project.satellite.path) do
merge_repo = Grit::Repo.new('.')
merge_repo.git.sh "git reset --hard"
merge_repo.git.sh "git fetch origin"
merge_repo.git.sh "git config user.name \"#{user.name}\""
merge_repo.git.sh "git config user.email \"#{user.email}\""
merge_repo.git.sh "git checkout -b #{merge_request.target_branch} origin/#{merge_request.target_branch}"
output = merge_repo.git.pull({}, "--no-ff", "origin", merge_request.source_branch)
yield(merge_repo, output)
end
end
end
rescue Grit::Git::GitTimeout
return false
end
end
end
module Gitlabhq module Gitlab
class Satellite class Satellite
PARKING_BRANCH = "__parking_branch" PARKING_BRANCH = "__parking_branch"
......
class GitlabMerge
attr_accessor :project, :merge_request, :user
def initialize(merge_request, user)
self.user = user
self.merge_request = merge_request
self.project = merge_request.project
end
def can_be_merged?
result = false
process do |repo, output|
result = !(output =~ /CONFLICT/)
end
result
end
def merge
process do |repo, output|
if output =~ /CONFLICT/
false
else
repo.git.push({}, "origin", merge_request.target_branch)
true
end
end
end
def process
Grit::Git.with_timeout(30.seconds) do
lock_file = File.join(Rails.root, "tmp", "merge_repo_#{project.path}.lock")
File.open(lock_file, "w+") do |f|
f.flock(File::LOCK_EX)
unless project.satellite.exists?
raise "You should run: rake gitlab:app:enable_automerge"
end
project.satellite.clear
Dir.chdir(project.satellite.path) do
merge_repo = Grit::Repo.new('.')
merge_repo.git.sh "git reset --hard"
merge_repo.git.sh "git fetch origin"
merge_repo.git.sh "git config user.name \"#{user.name}\""
merge_repo.git.sh "git config user.email \"#{user.email}\""
merge_repo.git.sh "git checkout -b #{merge_request.target_branch} origin/#{merge_request.target_branch}"
output = merge_repo.git.pull({}, "--no-ff", "origin", merge_request.source_branch)
yield(merge_repo, output)
end
end
end
rescue Grit::Git::GitTimeout
return false
end
end
...@@ -2,7 +2,7 @@ namespace :gitlab do ...@@ -2,7 +2,7 @@ namespace :gitlab do
namespace :app do namespace :app do
desc "GITLAB | Enable auto merge" desc "GITLAB | Enable auto merge"
task :enable_automerge => :environment do task :enable_automerge => :environment do
Gitlabhq::GitHost.system.new.configure do |git| Gitlab::GitHost.system.new.configure do |git|
git.admin_all_repo git.admin_all_repo
end end
......
...@@ -43,7 +43,7 @@ describe ProtectedBranch do ...@@ -43,7 +43,7 @@ describe ProtectedBranch do
subject { ProtectedBranch.new(:project => project) } subject { ProtectedBranch.new(:project => project) }
it "updates the branch's project repo permissions" do it "updates the branch's project repo permissions" do
Gitlabhq::GitHost.should_receive(:system).and_return(gitolite) Gitlab::GitHost.should_receive(:system).and_return(gitolite)
gitolite.should_receive(:update_project).with(project.path, project) gitolite.should_receive(:update_project).with(project.path, project)
subject.update_repository subject.update_repository
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment