Commit 511d07c4 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge pull request #713 from gitlabhq/fast_automerge

Fast automerge
parents 71819dc3 e5f8397f
......@@ -101,6 +101,9 @@ module Project::HooksTrait
# Execute web hooks
self.execute_web_hooks(oldrev, newrev, ref, user)
# Create satellite
self.satellite.create unless self.satellite.exists?
end
end
end
......@@ -37,6 +37,10 @@ module Project::RepositoryTrait
end
end
def satellite
@satellite ||= Gitlabhq::Satellite.new(self)
end
def write_hook(name, content)
hook_file = File.join(path_to_repo, 'hooks', name)
......
class GitlabMerge
attr_accessor :project, :merge_path, :merge_request, :user
attr_accessor :project, :merge_request, :user
def initialize(merge_request, user)
self.user = user
self.merge_request = merge_request
self.project = merge_request.project
self.merge_path = File.join(Rails.root, "tmp", "merge_repo", project.path, merge_request.id.to_s)
FileUtils.rm_rf(merge_path)
FileUtils.mkdir_p merge_path
end
def can_be_merged?
pull do |repo, output|
process do |repo, output|
!(output =~ /Automatic merge failed/)
end
end
def merge
pull do |repo, output|
process do |repo, output|
if output =~ /Automatic merge failed/
false
else
......@@ -27,19 +24,23 @@ class GitlabMerge
end
end
def pull
def process
Grit::Git.with_timeout(30.seconds) do
File.open(File.join(Rails.root, "tmp", "merge_repo", "#{project.path}.lock"), "w+") do |f|
f.flock(File::LOCK_EX)
self.project.repo.git.clone({:branch => merge_request.target_branch}, project.url_to_repo, merge_path)
unless File.exist?(self.merge_path)
raise "Gitlab user do not have access to repo. You should run: rake gitlab_enable_automerge"
unless project.satellite.exists?
raise "You should run: rake gitlab_enable_automerge"
end
Dir.chdir(merge_path) do
project.satellite.clear
Dir.chdir(project.satellite.path) do
merge_repo = Grit::Repo.new('.')
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
......
module Gitlabhq
class Satellite
PARKING_BRANCH = "__parking_branch"
attr_accessor :project
def initialize project
self.project = project
end
def create
`git clone #{project.url_to_repo} #{path}`
end
def path
File.join(Rails.root, "tmp", "repo_satellites", project.path)
end
def exists?
File.exists? path
end
#will be deleted all branches except PARKING_BRANCH
def clear
Dir.chdir(path) do
heads = Grit::Repo.new(".").heads.map{|head| head.name}
if heads.include? PARKING_BRANCH
`git checkout #{PARKING_BRANCH}`
else
`git checkout -b #{PARKING_BRANCH}`
end
heads.delete(PARKING_BRANCH)
heads.each do |head|
`git branch -D #{head}`
end
end
end
end
end
......@@ -6,6 +6,13 @@ namespace :gitlab do
git.admin_all_repo
end
Project.find_each do |project|
if project.repo_exists? && !project.satellite.exists?
puts "Creating satellite for #{project.name}...".green
project.satellite.create
end
end
puts "Done!".green
end
end
......
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