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
|
|
|
|
|
2021-07-21 17:10:10 -04:00
|
|
|
def list_conflict_files(allow_tree_conflicts: false)
|
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,
|
2021-07-21 17:10:10 -04:00
|
|
|
their_commit_oid: @their_commit_oid,
|
|
|
|
allow_tree_conflicts: allow_tree_conflicts
|
2017-12-05 14:42:04 -05:00
|
|
|
)
|
2020-06-30 20:09:02 -04:00
|
|
|
response = GitalyClient.call(@repository.storage, :conflicts_service, :list_conflict_files, request, timeout: GitalyClient.long_timeout)
|
|
|
|
GitalyClient::ConflictFilesStitcher.new(response, @gitaly_repo)
|
2018-01-09 07:53:35 -05:00
|
|
|
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
|
|
|
|
|
2021-07-07 05:08:35 -04:00
|
|
|
response = GitalyClient.call(@repository.storage, :conflicts_service, :resolve_conflicts, req_enum, remote_storage: target_repository.storage, timeout: GitalyClient.long_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),
|
2021-02-11 07:08:52 -05:00
|
|
|
user: Gitlab::Git::User.from_gitlab(resolution.user).to_gitaly,
|
|
|
|
timestamp: Google::Protobuf::Timestamp.new(seconds: Time.now.utc.to_i)
|
2017-12-27 14:49:05 -05:00
|
|
|
)
|
|
|
|
end
|
2017-12-05 14:42:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|