diff options
author | ameyavarade <ameya.varade@clogeny.com> | 2013-10-08 13:21:36 +0530 |
---|---|---|
committer | adamedx <adamed@opscode.com> | 2013-10-10 12:45:14 -0700 |
commit | 76947bf7275084b41aa2286fb4606a03ef1f9ae0 (patch) | |
tree | fbd71a18f171241cf6b9469f4ac88536509491a7 /lib/chef/util | |
parent | f6c97951e30b0744079ac6e13cc7fff80238b97f (diff) | |
download | chef-76947bf7275084b41aa2286fb4606a03ef1f9ae0.tar.gz |
Changes to remove redundant code and use chef/util/diff for knife diff
Diffstat (limited to 'lib/chef/util')
-rw-r--r-- | lib/chef/util/diff.rb | 72 |
1 files changed, 36 insertions, 36 deletions
diff --git a/lib/chef/util/diff.rb b/lib/chef/util/diff.rb index 41b1f8b076..59edb59c4b 100644 --- a/lib/chef/util/diff.rb +++ b/lib/chef/util/diff.rb @@ -82,6 +82,42 @@ class Chef end end end + + # produces a unified-output-format diff with 3 lines of context + def udiff(old_file, new_file) + diff_str = "" + file_length_difference = 0 + + old_data = IO.readlines(old_file).map { |e| e.chomp } + new_data = IO.readlines(new_file).map { |e| e.chomp } + diff_data = ::Diff::LCS.diff(old_data, new_data) + + return diff_str if old_data.empty? && new_data.empty? + return "No differences encountered\n" if diff_data.empty? + + # write diff header (standard unified format) + ft = File.stat(old_file).mtime.localtime.strftime('%Y-%m-%d %H:%M:%S.%N %z') + diff_str << "--- #{old_file}\t#{ft}\n" + ft = File.stat(new_file).mtime.localtime.strftime('%Y-%m-%d %H:%M:%S.%N %z') + diff_str << "+++ #{new_file}\t#{ft}\n" + + # loop over diff hunks. if a hunk overlaps with the last hunk, + # join them. otherwise, print out the old one. + old_hunk = hunk = nil + diff_data.each do |piece| + begin + hunk = ::Diff::LCS::Hunk.new(old_data, new_data, piece, 3, file_length_difference) + file_length_difference = hunk.file_length_difference + next unless old_hunk + next if hunk.merge(old_hunk) + diff_str << old_hunk.diff(:unified) << "\n" + ensure + old_hunk = hunk + end + end + diff_str << old_hunk.diff(:unified) << "\n" + return diff_str + end private @@ -138,42 +174,6 @@ class Chef end end - # produces a unified-output-format diff with 3 lines of context - def udiff(old_file, new_file) - diff_str = "" - file_length_difference = 0 - - old_data = IO.readlines(old_file).map { |e| e.chomp } - new_data = IO.readlines(new_file).map { |e| e.chomp } - diff_data = ::Diff::LCS.diff(old_data, new_data) - - return diff_str if old_data.empty? && new_data.empty? - return "No differences encountered\n" if diff_data.empty? - - # write diff header (standard unified format) - ft = File.stat(old_file).mtime.localtime.strftime('%Y-%m-%d %H:%M:%S.%N %z') - diff_str << "--- #{old_file}\t#{ft}\n" - ft = File.stat(new_file).mtime.localtime.strftime('%Y-%m-%d %H:%M:%S.%N %z') - diff_str << "+++ #{new_file}\t#{ft}\n" - - # loop over diff hunks. if a hunk overlaps with the last hunk, - # join them. otherwise, print out the old one. - old_hunk = hunk = nil - diff_data.each do |piece| - begin - hunk = ::Diff::LCS::Hunk.new(old_data, new_data, piece, 3, file_length_difference) - file_length_difference = hunk.file_length_difference - next unless old_hunk - next if hunk.merge(old_hunk) - diff_str << old_hunk.diff(:unified) << "\n" - ensure - old_hunk = hunk - end - end - diff_str << old_hunk.diff(:unified) << "\n" - return diff_str - end - def encode_diff_for_json(diff_str) if Object.const_defined? :Encoding diff_str.encode!('UTF-8', :invalid => :replace, :undef => :replace, :replace => '?') |