module Puppet::Util::Diff
Provide a diff between two strings.
Public Class Methods
diff(old, new)
click to toggle source
# File lib/puppet/util/diff.rb 9 def diff(old, new) 10 diff_cmd = Puppet[:diff] 11 return '' unless diff_cmd && diff_cmd != "" 12 13 command = [diff_cmd] 14 args = Puppet[:diff_args] 15 if args && args != "" 16 args.split(' ').each do|arg| 17 command << arg 18 end 19 end 20 command << old << new 21 Puppet::Util::Execution.execute(command, :failonfail => false, :combine => false) 22 end
Public Instance Methods
lcs_diff(data_old, data_new, format=:unified, context_lines=3)
click to toggle source
return diff string of two input strings format defaults to unified context defaults to 3 lines
# File lib/puppet/util/diff.rb 29 def lcs_diff(data_old, data_new, format=:unified, context_lines=3) 30 unless Puppet.features.diff? 31 Puppet.warning _("Cannot provide diff without the diff/lcs Ruby library") 32 return "" 33 end 34 data_old = data_old.split(/\n/).map! { |e| e.chomp } 35 data_new = data_new.split(/\n/).map! { |e| e.chomp } 36 37 output = String.new 38 39 diffs = ::Diff::LCS.diff(data_old, data_new) 40 return output if diffs.empty? 41 42 oldhunk = hunk = nil 43 file_length_difference = 0 44 45 diffs.each do |piece| 46 begin 47 48 hunk = ::Diff::LCS::Hunk.new( 49 data_old, data_new, piece, 50 context_lines, 51 52 file_length_difference) 53 file_length_difference = hunk.file_length_difference 54 next unless oldhunk 55 # Hunks may overlap, which is why we need to be careful when our 56 # diff includes lines of context. Otherwise, we might print 57 # redundant lines. 58 if (context_lines > 0) and hunk.overlaps?(oldhunk) 59 hunk.unshift(oldhunk) 60 else 61 output << oldhunk.diff(format) 62 end 63 ensure 64 oldhunk = hunk 65 output << "\n" 66 end 67 end 68 69 # Handle the last remaining hunk 70 output << oldhunk.diff(format) << "\n" 71 end
string_file_diff(path, string)
click to toggle source
# File lib/puppet/util/diff.rb 73 def string_file_diff(path, string) 74 tempfile = Tempfile.new("puppet-diffing") 75 tempfile.open 76 tempfile.print string 77 tempfile.close 78 notice "\n" + diff(path, tempfile.path) 79 tempfile.delete 80 end
Private Instance Methods
diff(old, new)
click to toggle source
# File lib/puppet/util/diff.rb 9 def diff(old, new) 10 diff_cmd = Puppet[:diff] 11 return '' unless diff_cmd && diff_cmd != "" 12 13 command = [diff_cmd] 14 args = Puppet[:diff_args] 15 if args && args != "" 16 args.split(' ').each do|arg| 17 command << arg 18 end 19 end 20 command << old << new 21 Puppet::Util::Execution.execute(command, :failonfail => false, :combine => false) 22 end