Commit ba327e69 authored by Sean McGivern's avatar Sean McGivern Committed by Fatih Acet

Move resolving code to ResolveService

parent ac9229a3
...@@ -150,7 +150,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController ...@@ -150,7 +150,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController
def resolve_conflicts def resolve_conflicts
begin begin
Gitlab::Conflict::FileCollection.new(@merge_request).resolve_conflicts!(params, user: current_user) MergeRequests::ResolveService.new(project, current_user, params).execute(@merge_request)
head :ok head :ok
rescue Gitlab::Conflict::File::MissingResolution => e rescue Gitlab::Conflict::File::MissingResolution => e
......
...@@ -869,6 +869,14 @@ class Repository ...@@ -869,6 +869,14 @@ class Repository
end end
end end
def resolve_conflicts(user, branch, params)
commit_with_hooks(user, branch) do
committer = user_to_committer(user)
Rugged::Commit.create(rugged, params.merge(author: committer, committer: committer))
end
end
def check_revert_content(commit, base_branch) def check_revert_content(commit, base_branch)
source_sha = find_branch(base_branch).target.sha source_sha = find_branch(base_branch).target.sha
args = [commit.id, source_sha] args = [commit.id, source_sha]
......
module MergeRequests
class ResolveService < MergeRequests::BaseService
attr_accessor :conflicts, :rugged, :merge_index
def execute(merge_request)
@conflicts = merge_request.conflicts
@rugged = project.repository.rugged
@merge_index = conflicts.merge_index
conflicts.files.each do |file|
write_resolved_file_to_index(file, params[:sections])
end
commit_params = {
message: params[:commit_message] || conflicts.default_commit_message,
parents: [conflicts.our_commit, conflicts.their_commit].map(&:oid),
tree: merge_index.write_tree(rugged)
}
project.repository.resolve_conflicts(current_user, merge_request.source_branch, commit_params)
end
def write_resolved_file_to_index(file, resolutions)
new_file = file.resolve_lines(resolutions).map(&:text).join("\n")
our_path = file.our_path
merge_index.add(path: our_path, oid: rugged.write(new_file, :blob), mode: file.our_mode)
merge_index.conflict_remove(our_path)
end
end
end
...@@ -28,14 +28,6 @@ module Gitlab ...@@ -28,14 +28,6 @@ module Gitlab
parent_file: self) parent_file: self)
end end
def resolve!(resolution, index:, rugged:)
new_file = resolve_lines(resolution).map(&:text).join("\n")
oid = rugged.write(new_file, :blob)
index.add(path: our_path, oid: oid, mode: our_mode)
index.conflict_remove(our_path)
end
def resolve_lines(resolution) def resolve_lines(resolution)
section_id = nil section_id = nil
......
...@@ -20,27 +20,6 @@ module Gitlab ...@@ -20,27 +20,6 @@ module Gitlab
@merge_index ||= repository.rugged.merge_commits(our_commit, their_commit) @merge_index ||= repository.rugged.merge_commits(our_commit, their_commit)
end end
def resolve_conflicts!(params, user:)
resolutions = params[:sections]
commit_message = params[:commit_message] || default_commit_message
rugged = repository.rugged
committer = repository.user_to_committer(user)
files.each do |file|
file.resolve!(resolutions, index: merge_index, rugged: rugged)
end
new_tree = merge_index.write_tree(rugged)
Rugged::Commit.create(rugged,
author: committer,
committer: committer,
tree: new_tree,
message: commit_message,
parents: [our_commit, their_commit].map(&:oid),
update_ref: Gitlab::Git::BRANCH_REF_PREFIX + merge_request.source_branch)
end
def files def files
@files ||= merge_index.conflicts.map do |conflict| @files ||= merge_index.conflicts.map do |conflict|
raise ConflictSideMissing unless conflict[:theirs] && conflict[:ours] raise ConflictSideMissing unless conflict[:theirs] && conflict[:ours]
......
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