summaryrefslogtreecommitdiff
path: root/lib/gitlab/gitaly_client/conflict_files_stitcher.rb
blob: 97c13d1fdb0051049b0622c2c846b80c6b91510f (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
module Gitlab
  module GitalyClient
    class ConflictFilesStitcher
      include Enumerable

      def initialize(rpc_response)
        @rpc_response = rpc_response
      end

      def each
        current_file = nil

        @rpc_response.each do |msg|
          msg.files.each do |gitaly_file|
            if gitaly_file.header
              yield current_file if current_file

              current_file = file_from_gitaly_header(gitaly_file.header)
            else
              current_file.content << gitaly_file.content
            end
          end
        end

        yield current_file if current_file
      end

      private

      def file_from_gitaly_header(header)
        Gitlab::Git::Conflict::File.new(
          Gitlab::GitalyClient::Util.git_repository(header.repository),
          header.commit_oid,
          conflict_from_gitaly_file_header(header),
          ''
        )
      end

      def conflict_from_gitaly_file_header(header)
        {
          ours: { path: header.our_path, mode: header.our_mode },
          theirs: { path: header.their_path }
        }
      end
    end
  end
end