BigW Consortium Gitlab

parallel_diff.rb 1.64 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
module Gitlab
  module Diff
    class ParallelDiff
      attr_accessor :diff_file

      def initialize(diff_file)
        @diff_file = diff_file
      end

      def parallelize
11 12 13 14
        i = 0
        free_right_index = nil

        lines = []
15 16
        highlighted_diff_lines = diff_file.highlighted_diff_lines
        highlighted_diff_lines.each do |line|
17
          if line.removed?
18
            lines << {
19 20
              left: line,
              right: nil
21 22 23 24 25
            }

            # Once we come upon a new line it can be put on the right of this old line
            free_right_index ||= i
            i += 1
26
          elsif line.added?
27 28 29
            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.
30
              lines[free_right_index][:right] = line
31 32 33 34 35

              # 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
36 37
            else
              lines << {
38 39
                left: nil,
                right: line
40
              }
41 42 43

              free_right_index = nil
              i += 1
44
            end
45 46 47 48 49 50 51 52 53
          elsif line.meta? || line.unchanged?
            # line in the right panel is the same as in the left one
            lines << {
              left: line,
              right: line
            }

            free_right_index = nil
            i += 1
54 55
          end
        end
56

57 58 59 60 61
        lines
      end
    end
  end
end