2012-05-26 12:37:49 +02:00
|
|
|
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
|
2012-08-29 01:03:04 +02:00
|
|
|
!!repo.git.push({}, "origin", merge_request.target_branch)
|
2012-05-26 12:37:49 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def process
|
|
|
|
Grit::Git.with_timeout(30.seconds) do
|
2012-10-12 16:14:52 +02:00
|
|
|
lock_file = Rails.root.join("tmp", "#{project.path}.lock")
|
2012-05-26 12:37:49 +02:00
|
|
|
|
|
|
|
File.open(lock_file, "w+") do |f|
|
|
|
|
f.flock(File::LOCK_EX)
|
|
|
|
|
|
|
|
unless project.satellite.exists?
|
2012-10-12 16:14:52 +02:00
|
|
|
raise "Satellite doesn't exist"
|
2012-05-26 12:37:49 +02:00
|
|
|
end
|
2012-09-26 20:52:01 +02:00
|
|
|
|
2012-05-26 12:37:49 +02:00
|
|
|
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)
|
2012-06-03 20:29:20 +02:00
|
|
|
|
|
|
|
#remove source-branch
|
2012-06-04 23:08:41 +02:00
|
|
|
if merge_request.should_remove_source_branch && !project.root_ref?(merge_request.source_branch)
|
2012-06-03 20:29:20 +02:00
|
|
|
merge_repo.git.sh "git push origin :#{merge_request.source_branch}"
|
|
|
|
end
|
|
|
|
|
2012-05-26 12:37:49 +02:00
|
|
|
yield(merge_repo, output)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
rescue Grit::Git::GitTimeout
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|