Module: Brute::Diff

Defined in:
lib/brute/utils/diff.rb

Class Method Summary collapse

Class Method Details

.unified(old_text, new_text, context: 3) ⇒ Object

Generate a unified diff string from two texts.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/brute/utils/diff.rb', line 11

def self.unified(old_text, new_text, context: 3)
  old_lines = old_text.lines
  new_lines = new_text.lines
  diffs = ::Diff::LCS.diff(old_lines, new_lines)
  if diffs.empty?
    ''
  else
    output = +''
    file_length_difference = 0
    diffs.each do |piece|
      hunk = ::Diff::LCS::Hunk.new(
        old_lines,
        new_lines,
        piece,
        context,
        file_length_difference,
      )
      file_length_difference = hunk.file_length_difference
      output << hunk.diff(:unified)
      output << "\n"
    end
    output
  end
end