summaryrefslogtreecommitdiff
path: root/lib/gitlab/git/conflict/resolver.rb
blob: aa5d50d1fb10c1089b64dcf80725c85ca15f7483 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# frozen_string_literal: true

module Gitlab
  module Git
    module Conflict
      class Resolver
        include Gitlab::Git::WrapsGitalyErrors

        ConflictSideMissing = Class.new(StandardError)
        ResolutionError = Class.new(StandardError)

        def initialize(target_repository, our_commit_oid, their_commit_oid)
          @target_repository = target_repository
          @our_commit_oid = our_commit_oid
          @their_commit_oid = their_commit_oid
        end

        def conflicts
          @conflicts ||= wrapped_gitaly_errors do
            gitaly_conflicts_client(@target_repository).list_conflict_files.to_a
          rescue GRPC::FailedPrecondition => e
            raise Gitlab::Git::Conflict::Resolver::ConflictSideMissing, e.message
          end
        rescue GRPC::BadStatus => e
          raise Gitlab::Git::CommandError, e
        end

        def resolve_conflicts(source_repository, resolution, source_branch:, target_branch:)
          wrapped_gitaly_errors do
            gitaly_conflicts_client(source_repository).resolve_conflicts(@target_repository, resolution, source_branch, target_branch)
          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