2016-07-25 18:47:09 +00:00
|
|
|
module Gitlab
|
|
|
|
module Conflict
|
|
|
|
class Parser
|
2016-07-29 13:51:11 +00:00
|
|
|
class ParserError < StandardError
|
2016-07-25 18:47:09 +00:00
|
|
|
end
|
|
|
|
|
2016-07-29 13:51:11 +00:00
|
|
|
class UnexpectedDelimiter < ParserError
|
|
|
|
end
|
|
|
|
|
|
|
|
class MissingEndDelimiter < ParserError
|
2016-07-25 18:47:09 +00:00
|
|
|
end
|
|
|
|
|
2016-07-29 14:17:06 +00:00
|
|
|
class UnmergeableFile < ParserError
|
|
|
|
end
|
|
|
|
|
2016-07-29 12:29:14 +00:00
|
|
|
def parse(text, our_path:, their_path:, parent: nil)
|
2016-07-29 14:17:06 +00:00
|
|
|
raise UnmergeableFile if text.blank? # Typically a binary file
|
|
|
|
raise UnmergeableFile if text.length > 102400
|
2016-07-25 18:47:09 +00: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-07-29 12:29:14 +00:00
|
|
|
lines << Gitlab::Diff::Line.new(full_line, type, line_obj_index, line_old, line_new, parent: parent)
|
2016-07-25 18:47:09 +00:00
|
|
|
else
|
2016-07-29 12:29:14 +00:00
|
|
|
lines << Gitlab::Diff::Line.new(full_line, type, line_obj_index, line_old, line_new, parent: parent)
|
2016-07-25 18:47:09 +00:00
|
|
|
line_old += 1 if type != 'new'
|
|
|
|
line_new += 1 if type != 'old'
|
|
|
|
|
|
|
|
line_obj_index += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-27 11:42:18 +00:00
|
|
|
raise MissingEndDelimiter unless type.nil?
|
2016-07-25 18:47:09 +00:00
|
|
|
|
|
|
|
lines
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|