2012-05-26 12:37:49 +02:00
|
|
|
module Gitlab
|
|
|
|
class Merge
|
2012-10-20 15:25:48 +02:00
|
|
|
attr_accessor :merge_request, :project, :user
|
2012-05-26 12:37:49 +02:00
|
|
|
|
|
|
|
def initialize(merge_request, user)
|
2012-10-20 15:25:48 +02:00
|
|
|
@merge_request = merge_request
|
|
|
|
@project = merge_request.project
|
|
|
|
@user = user
|
2012-05-26 12:37:49 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_be_merged?
|
2012-10-20 15:25:48 +02:00
|
|
|
in_locked_and_timed_satellite do |merge_repo|
|
|
|
|
merge_in_satellite!(merge_repo)
|
2012-05-26 12:37:49 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-10-20 15:25:48 +02:00
|
|
|
# Merges the source branch into the target branch in the satellite and
|
|
|
|
# pushes it back to Gitolite.
|
|
|
|
# It also removes the source branch if requested in the merge request.
|
|
|
|
#
|
|
|
|
# Returns false if the merge produced conflicts
|
|
|
|
# Returns false if pushing from the satallite to Gitolite failed or was rejected
|
|
|
|
# Returns true otherwise
|
|
|
|
def merge!
|
|
|
|
in_locked_and_timed_satellite do |merge_repo|
|
|
|
|
if merge_in_satellite!(merge_repo)
|
|
|
|
# push merge back to Gitolite
|
|
|
|
# will raise CommandFailed when push fails
|
|
|
|
merge_repo.git.push({raise: true}, :origin, merge_request.target_branch)
|
|
|
|
|
|
|
|
# remove source branch
|
|
|
|
if merge_request.should_remove_source_branch && !project.root_ref?(merge_request.source_branch)
|
|
|
|
# will raise CommandFailed when push fails
|
|
|
|
merge_repo.git.push({raise: true}, :origin, ":#{merge_request.source_branch}")
|
|
|
|
end
|
|
|
|
|
|
|
|
# merge, push and branch removal successful
|
|
|
|
true
|
2012-05-26 12:37:49 +02:00
|
|
|
end
|
|
|
|
end
|
2012-10-20 15:25:48 +02:00
|
|
|
rescue Grit::Git::CommandFailed
|
|
|
|
false
|
2012-05-26 12:37:49 +02:00
|
|
|
end
|
|
|
|
|
2012-10-20 15:25:48 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
# * Sets a 30s timeout for Git
|
|
|
|
# * Locks the satellite repo
|
|
|
|
# * Yields the prepared satallite repo
|
|
|
|
def in_locked_and_timed_satellite
|
2012-05-26 12:37:49 +02:00
|
|
|
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
|
|
|
Dir.chdir(project.satellite.path) do
|
2012-10-20 15:25:48 +02:00
|
|
|
repo = Grit::Repo.new('.')
|
|
|
|
|
|
|
|
return yield repo
|
2012-05-26 12:37:49 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue Grit::Git::GitTimeout
|
|
|
|
return false
|
|
|
|
end
|
2012-10-20 15:25:48 +02:00
|
|
|
|
|
|
|
# Merges the source_branch into the target_branch in the satellite.
|
|
|
|
#
|
|
|
|
# Note: it will clear out the satellite before doing anything
|
|
|
|
#
|
|
|
|
# Returns false if the merge produced conflicts
|
|
|
|
# Returns true otherwise
|
|
|
|
def merge_in_satellite!(repo)
|
|
|
|
prepare_satelite!(repo)
|
|
|
|
|
|
|
|
# create target branch in satellite at the corresponding commit from Gitolite
|
|
|
|
repo.git.checkout({b: true}, merge_request.target_branch, "origin/#{merge_request.target_branch}")
|
|
|
|
|
|
|
|
# merge the source branch from Gitolite into the satellite
|
|
|
|
# will raise CommandFailed when merge fails
|
|
|
|
repo.git.pull({no_ff: true, raise: true}, :origin, merge_request.source_branch)
|
|
|
|
rescue Grit::Git::CommandFailed
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
# * Clears the satellite
|
|
|
|
# * Updates the satellite from Gitolite
|
|
|
|
# * Sets up Git variables for the user
|
|
|
|
def prepare_satelite!(repo)
|
|
|
|
project.satellite.clear
|
|
|
|
|
|
|
|
repo.git.reset(hard: true)
|
|
|
|
repo.git.fetch({}, :origin)
|
|
|
|
|
|
|
|
repo.git.config({}, "user.name", user.name)
|
|
|
|
repo.git.config({}, "user.email", user.email)
|
|
|
|
end
|
2012-05-26 12:37:49 +02:00
|
|
|
end
|
|
|
|
end
|