Class: Gitlab::Diff::ParallelDiff

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/diff/parallel_diff.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(diff_file) ⇒ ParallelDiff

Returns a new instance of ParallelDiff.


8
9
10
# File 'lib/gitlab/diff/parallel_diff.rb', line 8

def initialize(diff_file)
  @diff_file = diff_file
end

Instance Attribute Details

#diff_fileObject

Returns the value of attribute diff_file


6
7
8
# File 'lib/gitlab/diff/parallel_diff.rb', line 6

def diff_file
  @diff_file
end

Instance Method Details

#parallelizeObject


12
13
14
15
16
17
18
19
20
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
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gitlab/diff/parallel_diff.rb', line 12

def parallelize
  i = 0
  free_right_index = nil

  lines = []
  highlighted_diff_lines = diff_file.highlighted_diff_lines
  highlighted_diff_lines.each do |line|
    if line.removed?
      lines << {
        left: line,
        right: nil
      }

      # Once we come upon a new line it can be put on the right of this old line
      free_right_index ||= i
      i += 1
    elsif line.added?
      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.
        lines[free_right_index][:right] = line

        # 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
      else
        lines << {
          left: nil,
          right: line
        }

        free_right_index = nil
        i += 1
      end
    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
    end
  end

  lines
end