2018-11-09 13:39:43 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-12-05 14:42:04 -05:00
|
|
|
module Gitlab
|
|
|
|
module GitalyClient
|
|
|
|
class ConflictsService
|
2018-01-03 06:44:57 -05:00
|
|
|
include Gitlab::EncodingHelper
|
|
|
|
|
2017-12-05 17:18:20 -05:00
|
|
|
MAX_MSG_SIZE = 128.kilobytes.freeze
|
|
|
|
|
|
|
|
def initialize(repository, our_commit_oid, their_commit_oid)
|
2017-12-05 14:42:04 -05:00
|
|
|
@gitaly_repo = repository.gitaly_repository
|
|
|
|
@repository = repository
|
2017-12-05 17:18:20 -05:00
|
|
|
@our_commit_oid = our_commit_oid
|
|
|
|
@their_commit_oid = their_commit_oid
|
2017-12-05 14:42:04 -05:00
|
|
|
end
|
|
|
|
|
2017-12-05 17:18:20 -05:00
|
|
|
def list_conflict_files
|
2017-12-05 14:42:04 -05:00
|
|
|
request = Gitaly::ListConflictFilesRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
2017-12-05 17:18:20 -05:00
|
|
|
our_commit_oid: @our_commit_oid,
|
|
|
|
their_commit_oid: @their_commit_oid
|
2017-12-05 14:42:04 -05:00
|
|
|
)
|
|
|
|
response = GitalyClient.call(@repository.storage, :conflicts_service, :list_conflict_files, request)
|
|
|
|
|
2018-01-09 07:53:35 -05:00
|
|
|
GitalyClient::ConflictFilesStitcher.new(response)
|
|
|
|
end
|
|
|
|
|
|
|
|
def conflicts?
|
|
|
|
list_conflict_files.any?
|
2018-07-10 10:00:13 -04:00
|
|
|
rescue GRPC::FailedPrecondition, GRPC::Unknown
|
|
|
|
# The server raises FailedPrecondition when it encounters
|
|
|
|
# ConflictSideMissing, which means a conflict exists but its `theirs` or
|
|
|
|
# `ours` data is nil due to a non-existent file in one of the trees.
|
|
|
|
#
|
|
|
|
# GRPC::Unknown comes from Rugged::ReferenceError and Rugged::OdbError.
|
2018-01-23 12:31:56 -05:00
|
|
|
true
|
2017-12-05 14:42:04 -05:00
|
|
|
end
|
|
|
|
|
2017-12-27 14:49:05 -05:00
|
|
|
def resolve_conflicts(target_repository, resolution, source_branch, target_branch)
|
2019-03-28 15:05:27 -04:00
|
|
|
reader = binary_io(resolution.files.to_json)
|
2017-12-05 17:18:20 -05:00
|
|
|
|
|
|
|
req_enum = Enumerator.new do |y|
|
2017-12-27 14:49:05 -05:00
|
|
|
header = resolve_conflicts_request_header(target_repository, resolution, source_branch, target_branch)
|
2017-12-05 17:18:20 -05:00
|
|
|
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
|
|
|
|
|
2018-07-05 12:05:24 -04:00
|
|
|
response = GitalyClient.call(@repository.storage, :conflicts_service, :resolve_conflicts, req_enum, remote_storage: target_repository.storage, timeout: GitalyClient.medium_timeout)
|
2017-12-05 17:18:20 -05:00
|
|
|
|
|
|
|
if response.resolution_error.present?
|
|
|
|
raise Gitlab::Git::Conflict::Resolver::ResolutionError, response.resolution_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-05 14:42:04 -05:00
|
|
|
private
|
|
|
|
|
2017-12-27 14:49:05 -05:00
|
|
|
def resolve_conflicts_request_header(target_repository, resolution, source_branch, target_branch)
|
|
|
|
Gitaly::ResolveConflictsRequestHeader.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
our_commit_oid: @our_commit_oid,
|
|
|
|
target_repository: target_repository.gitaly_repository,
|
|
|
|
their_commit_oid: @their_commit_oid,
|
2019-06-10 23:50:00 -04:00
|
|
|
source_branch: encode_binary(source_branch),
|
|
|
|
target_branch: encode_binary(target_branch),
|
2019-06-10 18:50:23 -04:00
|
|
|
commit_message: encode_binary(resolution.commit_message),
|
2017-12-27 14:49:05 -05:00
|
|
|
user: Gitlab::Git::User.from_gitlab(resolution.user).to_gitaly
|
|
|
|
)
|
|
|
|
end
|
2017-12-05 14:42:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|