2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
module Gitlab
|
|
|
|
module Git
|
|
|
|
module Conflict
|
|
|
|
class Resolver
|
2018-12-13 13:39:08 +05:30
|
|
|
include Gitlab::Git::WrapsGitalyErrors
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
ConflictSideMissing = Class.new(StandardError)
|
|
|
|
ResolutionError = Class.new(StandardError)
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
def initialize(target_repository, our_commit_oid, their_commit_oid, allow_tree_conflicts: false)
|
2018-03-17 18:26:18 +05:30
|
|
|
@target_repository = target_repository
|
|
|
|
@our_commit_oid = our_commit_oid
|
|
|
|
@their_commit_oid = their_commit_oid
|
2021-10-27 15:23:28 +05:30
|
|
|
@allow_tree_conflicts = allow_tree_conflicts
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def conflicts
|
2018-12-13 13:39:08 +05:30
|
|
|
@conflicts ||= wrapped_gitaly_errors do
|
2021-10-27 15:23:28 +05:30
|
|
|
gitaly_conflicts_client(@target_repository).list_conflict_files(allow_tree_conflicts: @allow_tree_conflicts).to_a
|
2021-09-04 01:27:46 +05:30
|
|
|
rescue GRPC::FailedPrecondition => e
|
|
|
|
raise Gitlab::Git::Conflict::Resolver::ConflictSideMissing, e.message
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2018-11-18 11:00:15 +05:30
|
|
|
rescue GRPC::BadStatus => e
|
2021-06-08 01:23:25 +05:30
|
|
|
raise Gitlab::Git::CommandError, e
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def resolve_conflicts(source_repository, resolution, source_branch:, target_branch:)
|
2018-12-13 13:39:08 +05:30
|
|
|
wrapped_gitaly_errors do
|
2018-11-08 19:23:39 +05:30
|
|
|
gitaly_conflicts_client(source_repository).resolve_conflicts(@target_repository, resolution, source_branch, target_branch)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def conflict_for_path(conflicts, old_path, new_path)
|
|
|
|
conflicts.find do |conflict|
|
|
|
|
conflict.their_path == old_path && conflict.our_path == new_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def conflict_files(repository, index)
|
|
|
|
index.conflicts.map do |conflict|
|
|
|
|
raise ConflictSideMissing unless conflict[:theirs] && conflict[:ours]
|
|
|
|
|
|
|
|
Gitlab::Git::Conflict::File.new(
|
|
|
|
repository,
|
|
|
|
@our_commit_oid,
|
|
|
|
conflict,
|
|
|
|
index.merge_file(conflict[:ours][:path])[:data]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def gitaly_conflicts_client(repository)
|
|
|
|
repository.gitaly_conflicts_client(@our_commit_oid, @their_commit_oid)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|