2016-07-25 14:47:09 -04:00
|
|
|
module Gitlab
|
|
|
|
module Conflict
|
|
|
|
class Parser
|
2017-03-01 06:00:37 -05:00
|
|
|
UnresolvableError = Class.new(StandardError)
|
|
|
|
UnmergeableFile = Class.new(UnresolvableError)
|
|
|
|
UnsupportedEncoding = Class.new(UnresolvableError)
|
2016-09-01 08:59:10 -04:00
|
|
|
|
|
|
|
# Recoverable errors - the conflict can be resolved in an editor, but not with
|
|
|
|
# sections.
|
2017-03-01 06:00:37 -05:00
|
|
|
ParserError = Class.new(StandardError)
|
|
|
|
UnexpectedDelimiter = Class.new(ParserError)
|
|
|
|
MissingEndDelimiter = Class.new(ParserError)
|
2016-08-23 11:37:14 -04:00
|
|
|
|
2016-08-02 04:20:22 -04:00
|
|
|
def parse(text, our_path:, their_path:, parent_file: nil)
|
2016-07-29 10:17:06 -04:00
|
|
|
raise UnmergeableFile if text.blank? # Typically a binary file
|
2016-08-26 05:54:19 -04:00
|
|
|
raise UnmergeableFile if text.length > 200.kilobytes
|
2016-07-25 14:47:09 -04:00
|
|
|
|
2017-03-14 10:06:26 -04:00
|
|
|
text.force_encoding('UTF-8')
|
|
|
|
|
|
|
|
raise UnsupportedEncoding unless text.valid_encoding?
|
2016-08-23 11:37:14 -04:00
|
|
|
|
2016-07-25 14:47:09 -04:00
|
|
|
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'
|
2016-08-02 04:20:22 -04:00
|
|
|
lines << Gitlab::Diff::Line.new(full_line, type, line_obj_index, line_old, line_new, parent_file: parent_file)
|
2016-07-25 14:47:09 -04:00
|
|
|
else
|
2016-08-02 04:20:22 -04:00
|
|
|
lines << Gitlab::Diff::Line.new(full_line, type, line_obj_index, line_old, line_new, parent_file: parent_file)
|
2016-07-25 14:47:09 -04:00
|
|
|
line_old += 1 if type != 'new'
|
|
|
|
line_new += 1 if type != 'old'
|
|
|
|
|
|
|
|
line_obj_index += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-27 07:42:18 -04:00
|
|
|
raise MissingEndDelimiter unless type.nil?
|
2016-07-25 14:47:09 -04:00
|
|
|
|
|
|
|
lines
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|