20: def lcs_diff(data_old, data_new, format=:unified, context_lines=3)
21: unless Puppet.features.diff?
22: Puppet.warning "Cannot provide diff without the diff/lcs Ruby library"
23: return ""
24: end
25: data_old = data_old.split(/\n/).map! { |e| e.chomp }
26: data_new = data_new.split(/\n/).map! { |e| e.chomp }
27:
28: output = ""
29:
30: diffs = ::Diff::LCS.diff(data_old, data_new)
31: return output if diffs.empty?
32:
33: oldhunk = hunk = nil
34: file_length_difference = 0
35:
36: diffs.each do |piece|
37: begin
38:
39: hunk = ::Diff::LCS::Hunk.new(
40: data_old, data_new, piece,
41: context_lines,
42:
43: file_length_difference)
44: file_length_difference = hunk.file_length_difference
45: next unless oldhunk
46:
47:
48:
49: if (context_lines > 0) and hunk.overlaps?(oldhunk)
50: hunk.unshift(oldhunk)
51: else
52: output << oldhunk.diff(format)
53: end
54: ensure
55: oldhunk = hunk
56: output << "\n"
57: end
58: end
59:
60:
61: output << oldhunk.diff(format) << "\n"
62: end