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 ...@@ -101,6 +101,9 @@ module Project::HooksTrait
# Execute web hooks # Execute web hooks
self.execute_web_hooks(oldrev, newrev, ref, user) self.execute_web_hooks(oldrev, newrev, ref, user)
# Create satellite
self.satellite.create unless self.satellite.exists?
end end
end end
end end
...@@ -37,6 +37,10 @@ module Project::RepositoryTrait ...@@ -37,6 +37,10 @@ module Project::RepositoryTrait
end end
end end
def satellite
@satellite ||= Gitlabhq::Satellite.new(self)
end
def write_hook(name, content) def write_hook(name, content)
hook_file = File.join(path_to_repo, 'hooks', name) hook_file = File.join(path_to_repo, 'hooks', name)
......
class GitlabMerge class GitlabMerge
attr_accessor :project, :merge_path, :merge_request, :user attr_accessor :project, :merge_request, :user
def initialize(merge_request, user) def initialize(merge_request, user)
self.user = user self.user = user
self.merge_request = merge_request self.merge_request = merge_request
self.project = merge_request.project 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 end
def can_be_merged? def can_be_merged?
pull do |repo, output| process do |repo, output|
!(output =~ /Automatic merge failed/) !(output =~ /Automatic merge failed/)
end end
end end
def merge def merge
pull do |repo, output| process do |repo, output|
if output =~ /Automatic merge failed/ if output =~ /Automatic merge failed/
false false
else else
...@@ -27,19 +24,23 @@ class GitlabMerge ...@@ -27,19 +24,23 @@ class GitlabMerge
end end
end end
def pull def process
Grit::Git.with_timeout(30.seconds) do Grit::Git.with_timeout(30.seconds) do
File.open(File.join(Rails.root, "tmp", "merge_repo", "#{project.path}.lock"), "w+") do |f| File.open(File.join(Rails.root, "tmp", "merge_repo", "#{project.path}.lock"), "w+") do |f|
f.flock(File::LOCK_EX) f.flock(File::LOCK_EX)
self.project.repo.git.clone({:branch => merge_request.target_branch}, project.url_to_repo, merge_path) unless project.satellite.exists?
unless File.exist?(self.merge_path) raise "You should run: rake gitlab_enable_automerge"
raise "Gitlab user do not have access to repo. You should run: rake gitlab_enable_automerge"
end end
Dir.chdir(merge_path) do
project.satellite.clear
Dir.chdir(project.satellite.path) do
merge_repo = Grit::Repo.new('.') 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.name \"#{user.name}\""
merge_repo.git.sh "git config user.email \"#{user.email}\"" 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) output = merge_repo.git.pull({}, "--no-ff", "origin", merge_request.source_branch)
yield(merge_repo, output) yield(merge_repo, output)
end 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 ...@@ -6,6 +6,13 @@ namespace :gitlab do
git.admin_all_repo git.admin_all_repo
end 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 puts "Done!".green
end end
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