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