2016-01-20 08:51:56 -05:00
|
|
|
module Gitlab
|
|
|
|
module Diff
|
|
|
|
class ParallelDiff
|
|
|
|
attr_accessor :diff_file
|
|
|
|
|
|
|
|
def initialize(diff_file)
|
|
|
|
@diff_file = diff_file
|
|
|
|
end
|
|
|
|
|
|
|
|
def parallelize
|
2016-07-11 00:48:44 -04:00
|
|
|
i = 0
|
|
|
|
free_right_index = nil
|
|
|
|
|
|
|
|
lines = []
|
2016-01-21 20:12:43 -05:00
|
|
|
highlighted_diff_lines = diff_file.highlighted_diff_lines
|
|
|
|
highlighted_diff_lines.each do |line|
|
2016-07-20 18:18:18 -04:00
|
|
|
if line.meta? || line.unchanged?
|
2016-01-20 08:51:56 -05:00
|
|
|
# line in the right panel is the same as in the left one
|
|
|
|
lines << {
|
2016-07-20 18:18:18 -04:00
|
|
|
left: line,
|
|
|
|
right: line
|
2016-01-20 08:51:56 -05:00
|
|
|
}
|
2016-07-11 00:48:44 -04:00
|
|
|
|
|
|
|
free_right_index = nil
|
|
|
|
i += 1
|
2016-07-20 18:18:18 -04:00
|
|
|
elsif line.removed?
|
2016-07-11 00:48:44 -04:00
|
|
|
lines << {
|
2016-07-20 18:18:18 -04:00
|
|
|
left: line,
|
|
|
|
right: nil
|
2016-07-11 00:48:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
# Once we come upon a new line it can be put on the right of this old line
|
|
|
|
free_right_index ||= i
|
|
|
|
i += 1
|
2016-07-20 18:18:18 -04:00
|
|
|
elsif line.added?
|
2016-07-11 00:48:44 -04:00
|
|
|
if free_right_index
|
|
|
|
# If an old line came before this without a line on the right, this
|
|
|
|
# line can be put to the right of it.
|
2016-07-20 18:18:18 -04:00
|
|
|
lines[free_right_index][:right] = line
|
2016-07-11 00:48:44 -04:00
|
|
|
|
|
|
|
# If there are any other old lines on the left that don't yet have
|
|
|
|
# a new counterpart on the right, update the free_right_index
|
|
|
|
next_free_right_index = free_right_index + 1
|
|
|
|
free_right_index = next_free_right_index < i ? next_free_right_index : nil
|
2016-01-20 08:51:56 -05:00
|
|
|
else
|
|
|
|
lines << {
|
2016-07-20 18:18:18 -04:00
|
|
|
left: nil,
|
|
|
|
right: line
|
2016-01-20 08:51:56 -05:00
|
|
|
}
|
2016-07-11 00:48:44 -04:00
|
|
|
|
|
|
|
free_right_index = nil
|
|
|
|
i += 1
|
2016-01-20 08:51:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-07-11 00:48:44 -04:00
|
|
|
|
2016-01-20 08:51:56 -05:00
|
|
|
lines
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|