Commit 0aa87bbe authored by Alejandro Rodríguez's avatar Alejandro Rodríguez

Incorporate ConflictsService.ResolveConflicts Gitaly RPC

parent 351f205c
...@@ -15,7 +15,7 @@ module Gitlab ...@@ -15,7 +15,7 @@ module Gitlab
@conflicts ||= begin @conflicts ||= begin
@target_repository.gitaly_migrate(:conflicts_list_conflict_files) do |is_enabled| @target_repository.gitaly_migrate(:conflicts_list_conflict_files) do |is_enabled|
if is_enabled if is_enabled
@target_repository.gitaly_conflicts_client.list_conflict_files(@our_commit_oid, @their_commit_oid) gitaly_conflicts_client(@target_repository).list_conflict_files
else else
rugged_list_conflict_files rugged_list_conflict_files
end end
...@@ -28,28 +28,12 @@ module Gitlab ...@@ -28,28 +28,12 @@ module Gitlab
end end
def resolve_conflicts(source_repository, user, files, source_branch:, target_branch:, commit_message:) def resolve_conflicts(source_repository, user, files, source_branch:, target_branch:, commit_message:)
source_repository.with_repo_branch_commit(@target_repository, target_branch) do source_repository.gitaly_migrate(:conflicts_resolve_conflicts) do |is_enabled|
index = source_repository.rugged.merge_commits(@our_commit_oid, @their_commit_oid) if is_enabled
conflicts = conflict_files(source_repository, index) gitaly_conflicts_client(source_repository).resolve_conflicts(@target_repository, user, files, source_branch, target_branch, commit_message)
else
files.each do |file_params| rugged_resolve_conflicts(source_repository, user, files, source_branch, target_branch, commit_message)
conflict_file = conflict_for_path(conflicts, file_params[:old_path], file_params[:new_path])
write_resolved_file_to_index(source_repository, index, conflict_file, file_params)
end
unless index.conflicts.empty?
missing_files = index.conflicts.map { |file| file[:ours][:path] }
raise ResolutionError, "Missing resolutions for the following files: #{missing_files.join(', ')}"
end end
commit_params = {
message: commit_message,
parents: [@our_commit_oid, @their_commit_oid]
}
source_repository.commit_index(user, source_branch, index, commit_params)
end end
end end
...@@ -74,6 +58,10 @@ module Gitlab ...@@ -74,6 +58,10 @@ module Gitlab
end end
end end
def gitaly_conflicts_client(repository)
repository.gitaly_conflicts_client(@our_commit_oid, @their_commit_oid)
end
def write_resolved_file_to_index(repository, index, file, params) def write_resolved_file_to_index(repository, index, file, params)
if params[:sections] if params[:sections]
resolved_lines = file.resolve_lines(params[:sections]) resolved_lines = file.resolve_lines(params[:sections])
...@@ -98,6 +86,32 @@ module Gitlab ...@@ -98,6 +86,32 @@ module Gitlab
# project always fetches source refs when creating merge request diffs. # project always fetches source refs when creating merge request diffs.
conflict_files(@target_repository, target_index) conflict_files(@target_repository, target_index)
end end
def rugged_resolve_conflicts(source_repository, user, files, source_branch, target_branch, commit_message)
source_repository.with_repo_branch_commit(@target_repository, target_branch) do
index = source_repository.rugged.merge_commits(@our_commit_oid, @their_commit_oid)
conflicts = conflict_files(source_repository, index)
files.each do |file_params|
conflict_file = conflict_for_path(conflicts, file_params[:old_path], file_params[:new_path])
write_resolved_file_to_index(source_repository, index, conflict_file, file_params)
end
unless index.conflicts.empty?
missing_files = index.conflicts.map { |file| file[:ours][:path] }
raise ResolutionError, "Missing resolutions for the following files: #{missing_files.join(', ')}"
end
commit_params = {
message: commit_message,
parents: [@our_commit_oid, @their_commit_oid]
}
source_repository.commit_index(user, source_branch, index, commit_params)
end
end
end end
end end
end end
......
...@@ -1294,8 +1294,8 @@ module Gitlab ...@@ -1294,8 +1294,8 @@ module Gitlab
@gitaly_remote_client ||= Gitlab::GitalyClient::RemoteService.new(self) @gitaly_remote_client ||= Gitlab::GitalyClient::RemoteService.new(self)
end end
def gitaly_conflicts_client def gitaly_conflicts_client(our_commit_oid, their_commit_oid)
@gitaly_conflicts_client ||= Gitlab::GitalyClient::ConflictsService.new(self) Gitlab::GitalyClient::ConflictsService.new(self, our_commit_oid, their_commit_oid)
end end
def gitaly_migrate(method, status: Gitlab::GitalyClient::MigrationStatus::OPT_IN, &block) def gitaly_migrate(method, status: Gitlab::GitalyClient::MigrationStatus::OPT_IN, &block)
......
module Gitlab module Gitlab
module GitalyClient module GitalyClient
class ConflictsService class ConflictsService
def initialize(repository) MAX_MSG_SIZE = 128.kilobytes.freeze
def initialize(repository, our_commit_oid, their_commit_oid)
@gitaly_repo = repository.gitaly_repository @gitaly_repo = repository.gitaly_repository
@repository = repository @repository = repository
@our_commit_oid = our_commit_oid
@their_commit_oid = their_commit_oid
end end
def list_conflict_files(our_commit_oid, their_commit_oid) def list_conflict_files
request = Gitaly::ListConflictFilesRequest.new( request = Gitaly::ListConflictFilesRequest.new(
repository: @gitaly_repo, repository: @gitaly_repo,
our_commit_oid: our_commit_oid, our_commit_oid: @our_commit_oid,
their_commit_oid: their_commit_oid their_commit_oid: @their_commit_oid
) )
response = GitalyClient.call(@repository.storage, :conflicts_service, :list_conflict_files, request) response = GitalyClient.call(@repository.storage, :conflicts_service, :list_conflict_files, request)
files = [] files = []
...@@ -38,6 +42,27 @@ module Gitlab ...@@ -38,6 +42,27 @@ module Gitlab
files files
end end
def resolve_conflicts(target_repository, user, files, source_branch, target_branch, commit_message)
reader = GitalyClient.binary_stringio(files.to_json)
req_enum = Enumerator.new do |y|
header = resolve_conflicts_request_header(target_repository, user, source_branch, target_branch, commit_message)
y.yield Gitaly::ResolveConflictsRequest.new(header: header)
until reader.eof?
chunk = reader.read(MAX_MSG_SIZE)
y.yield Gitaly::ResolveConflictsRequest.new(files_json: chunk)
end
end
response = GitalyClient.call(@repository.storage, :conflicts_service, :resolve_conflicts, req_enum, remote_storage: target_repository.storage)
if response.resolution_error.present?
raise Gitlab::Git::Conflict::Resolver::ResolutionError, response.resolution_error
end
end
private private
def conflict_file_from_gitaly(header, content) def conflict_file_from_gitaly(header, content)
...@@ -55,6 +80,19 @@ module Gitlab ...@@ -55,6 +80,19 @@ module Gitlab
theirs: { path: header.their_path } theirs: { path: header.their_path }
} }
end end
def resolve_conflicts_request_header(target_repository, user, source_branch, target_branch, commit_message)
Gitaly::ResolveConflictsRequestHeader.new(
repository: @gitaly_repo,
our_commit_oid: @our_commit_oid,
target_repository: target_repository.gitaly_repository,
their_commit_oid: @their_commit_oid,
source_branch: source_branch,
target_branch: target_branch,
commit_message: commit_message,
user: Gitlab::Git::User.from_gitlab(user).to_gitaly
)
end
end end
end end
end end
...@@ -3,10 +3,14 @@ require 'spec_helper' ...@@ -3,10 +3,14 @@ require 'spec_helper'
describe Gitlab::GitalyClient::ConflictsService do describe Gitlab::GitalyClient::ConflictsService do
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
let(:target_project) { create(:project, :repository) } let(:target_project) { create(:project, :repository) }
let(:repository) { project.repository } let(:source_repository) { project.repository.raw }
let(:gitaly_repositoy) { repository.gitaly_repository } let(:target_repository) { target_project.repository.raw }
let(:target_repository) { target_project.repository }
let(:target_gitaly_repository) { target_repository.gitaly_repository } let(:target_gitaly_repository) { target_repository.gitaly_repository }
let(:our_commit_oid) { 'f00' }
let(:their_commit_oid) { 'f44' }
let(:client) do
described_class.new(target_repository, our_commit_oid, their_commit_oid)
end
describe '#list_conflict_files' do describe '#list_conflict_files' do
let(:request) do let(:request) do
...@@ -15,8 +19,6 @@ describe Gitlab::GitalyClient::ConflictsService do ...@@ -15,8 +19,6 @@ describe Gitlab::GitalyClient::ConflictsService do
their_commit_oid: their_commit_oid their_commit_oid: their_commit_oid
) )
end end
let(:our_commit_oid) { 'f00' }
let(:their_commit_oid) { 'f44' }
let(:our_path) { 'our/path' } let(:our_path) { 'our/path' }
let(:their_path) { 'their/path' } let(:their_path) { 'their/path' }
let(:our_mode) { 0744 } let(:our_mode) { 0744 }
...@@ -31,9 +33,8 @@ describe Gitlab::GitalyClient::ConflictsService do ...@@ -31,9 +33,8 @@ describe Gitlab::GitalyClient::ConflictsService do
] ]
end end
let(:file) { subject[0] } let(:file) { subject[0] }
let(:client) { described_class.new(target_repository) }
subject { client.list_conflict_files(our_commit_oid, their_commit_oid) } subject { client.list_conflict_files }
it 'sends an RPC request' do it 'sends an RPC request' do
expect_any_instance_of(Gitaly::ConflictsService::Stub).to receive(:list_conflict_files) expect_any_instance_of(Gitaly::ConflictsService::Stub).to receive(:list_conflict_files)
...@@ -55,4 +56,32 @@ describe Gitlab::GitalyClient::ConflictsService do ...@@ -55,4 +56,32 @@ describe Gitlab::GitalyClient::ConflictsService do
expect(file.commit_oid).to eq(our_commit_oid) expect(file.commit_oid).to eq(our_commit_oid)
end end
end end
describe '#resolve_conflicts' do
let(:user) { create(:user) }
let(:files) do
[{ old_path: 'some/path', new_path: 'some/path', content: '' }]
end
let(:source_branch) { 'master' }
let(:target_branch) { 'feature' }
let(:commit_message) { 'Solving conflicts' }
subject do
client.resolve_conflicts(source_repository, user, files, source_branch, target_branch, commit_message)
end
it 'sends an RPC request' do
expect_any_instance_of(Gitaly::ConflictsService::Stub).to receive(:resolve_conflicts)
.with(kind_of(Enumerator), kind_of(Hash)).and_return(double(resolution_error: ""))
subject
end
it 'raises a relevant exception if resolution_error is present' do
expect_any_instance_of(Gitaly::ConflictsService::Stub).to receive(:resolve_conflicts)
.with(kind_of(Enumerator), kind_of(Hash)).and_return(double(resolution_error: "something happened"))
expect { subject }.to raise_error(Gitlab::Git::Conflict::Resolver::ResolutionError)
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