Module: Brute::Diff

Defined in:
lib/brute/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.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/brute/diff.rb', line 9

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)
  return '' if diffs.empty?

  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