parser.rb 2.0 KB
Newer Older
1 2 3
module Gitlab
  module Conflict
    class Parser
4 5 6
      UnresolvableError = Class.new(StandardError)
      UnmergeableFile = Class.new(UnresolvableError)
      UnsupportedEncoding = Class.new(UnresolvableError)
7 8 9

      # Recoverable errors - the conflict can be resolved in an editor, but not with
      # sections.
10 11 12
      ParserError = Class.new(StandardError)
      UnexpectedDelimiter = Class.new(ParserError)
      MissingEndDelimiter = Class.new(ParserError)
13

S
Sean McGivern 已提交
14
      def parse(text, our_path:, their_path:, parent_file: nil)
15
        raise UnmergeableFile if text.blank? # Typically a binary file
S
Sean McGivern 已提交
16
        raise UnmergeableFile if text.length > 200.kilobytes
17

18 19 20
        text.force_encoding('UTF-8')

        raise UnsupportedEncoding unless text.valid_encoding?
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
        line_obj_index = 0
        line_old = 1
        line_new = 1
        type = nil
        lines = []
        conflict_start = "<<<<<<< #{our_path}"
        conflict_middle = '======='
        conflict_end = ">>>>>>> #{their_path}"

        text.each_line.map do |line|
          full_line = line.delete("\n")

          if full_line == conflict_start
            raise UnexpectedDelimiter unless type.nil?

            type = 'new'
          elsif full_line == conflict_middle
            raise UnexpectedDelimiter unless type == 'new'

            type = 'old'
          elsif full_line == conflict_end
            raise UnexpectedDelimiter unless type == 'old'

            type = nil
          elsif line[0] == '\\'
            type = 'nonewline'
S
Sean McGivern 已提交
48
            lines << Gitlab::Diff::Line.new(full_line, type, line_obj_index, line_old, line_new, parent_file: parent_file)
49
          else
S
Sean McGivern 已提交
50
            lines << Gitlab::Diff::Line.new(full_line, type, line_obj_index, line_old, line_new, parent_file: parent_file)
51 52 53 54 55 56 57
            line_old += 1 if type != 'new'
            line_new += 1 if type != 'old'

            line_obj_index += 1
          end
        end

58
        raise MissingEndDelimiter unless type.nil?
59 60 61 62 63 64

        lines
      end
    end
  end
end