From dc289f6985a1cb9bdc24e5bc0df1a8be4010ff2c Mon Sep 17 00:00:00 2001 From: Alexandre Magno Date: Thu, 18 Sep 2014 11:06:04 -0300 Subject: [PATCH 1/4] Inverted diff: lists the keys with same values in the two files. --- bin/yamldiff | 12 +++++++---- lib/yamldiff/version.rb | 2 +- lib/yamldiff/yamldiff.rb | 43 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/bin/yamldiff b/bin/yamldiff index 5ac0745..09ec3b4 100755 --- a/bin/yamldiff +++ b/bin/yamldiff @@ -3,10 +3,14 @@ $LOAD_PATH.unshift(File.expand_path("../../lib", __FILE__)) require 'yamldiff' -if ARGV.size != 2 - $stderr.puts "USAGE: yamldiff file1 file2" +if ARGV.size != 2 && !(ARGV.size == 3 && ARGV[0] == "-i") + $stderr.puts "USAGE: yamldiff [-i] file1 file2" exit 1 end -errors = Yamldiff.diff_yaml(ARGV[0], ARGV[1]) -puts errors[ARGV[1]] +if ARGV.size == 3 + puts Yamlcomm.comm_yaml(ARGV[1], ARGV[2]) +else + errors = Yamldiff.diff_yaml(ARGV[0], ARGV[1]) + puts errors[ARGV[1]] +end diff --git a/lib/yamldiff/version.rb b/lib/yamldiff/version.rb index 8b285b4..16be204 100644 --- a/lib/yamldiff/version.rb +++ b/lib/yamldiff/version.rb @@ -1,3 +1,3 @@ class Yamldiff - VERSION = "0.0.9" + VERSION = "0.0.10" end diff --git a/lib/yamldiff/yamldiff.rb b/lib/yamldiff/yamldiff.rb index 6979f7e..370d6a1 100644 --- a/lib/yamldiff/yamldiff.rb +++ b/lib/yamldiff/yamldiff.rb @@ -44,3 +44,46 @@ def compare_hashes(first, second, context = []) end end end + +class Yamlcomm + class << self + # Compare the two yaml files + def comm_yaml(first, second, common = []) + primary = YAML.load(ERB.new(File.read(first)).result) + secondary = YAML.load(ERB.new(File.read(second)).result) + common = compare_hashes(primary, secondary) + common + end + + # Adapted from yamldiff.rb + def compare_hashes(first, second, context = []) + common = [] + + first.each do |key, value| + + unless second + next + end + + if value.is_a?(Hash) + common << compare_hashes(value, second[key], context + [key]) + next + end + + if second.key?(key) + value2 = second[key] + if value.class == value2.class + if value == value2 + s = "" + context.each { |p| s += p + "." } + common << s + key + end + end + end + + end + + common.flatten + end + end +end From 966ee607ca76abc82fb8a19bd80f249bcdc51b9a Mon Sep 17 00:00:00 2001 From: Alexandre Magno Date: Thu, 18 Sep 2014 11:20:44 -0300 Subject: [PATCH 2/4] README more easy --- README.md | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index c919d3d..a248ae8 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,37 @@ # Yamldiff [![Build Status](https://travis-ci.org/wallace/yamldiff.png)](https://travis-ci.org/wallace/yamldiff) -Given two yaml files, Yamldiff tells you which keys present in the first file -are not present in the second. +Given two yaml files, Yamldiff tells you which keys present in the first file are not present in the second. ## Installation -Add this line to your application's Gemfile: +From release: +```bash +$ sudo gem install yamldiff +``` - gem 'yamldiff' - -And then execute: - - $ bundle - -Or install it yourself as: - - $ gem install yamldiff +From source code: +```bash +$ bundle +$ gem build yamldiff.gemspec +$ sudo gem install yamldiff-VERSION.gem +``` ## Usage - require 'yamldiff' - result = Yamldiff.diff_yaml('path_to_file_1', 'path_to_file_2') - result.inspect - # { 'path_to_file_2' => [] } +Of the program: +```console +$ yamldiff +USAGE: yamldiff [-i] file1 file2 +``` + +Of the lib: +```ruby +require 'yamldiff' +result = Yamldiff.diff_yaml('path_to_file_1', 'path_to_file_2') +result.inspect +# { 'path_to_file_2' => [] } +``` ## Contributing From eb100b3d0264e61efd4f5e1d9a664f8bebc6d4b5 Mon Sep 17 00:00:00 2001 From: Alexandre Magno Date: Thu, 18 Sep 2014 17:49:02 -0300 Subject: [PATCH 3/4] Capacity of ignoring keys when diff is inverted. Debug comments. --- bin/yamldiff | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/bin/yamldiff b/bin/yamldiff index 09ec3b4..f743003 100755 --- a/bin/yamldiff +++ b/bin/yamldiff @@ -2,6 +2,8 @@ $LOAD_PATH.unshift(File.expand_path("../../lib", __FILE__)) require 'yamldiff' +require 'set' +#require 'hex_string' if ARGV.size != 2 && !(ARGV.size == 3 && ARGV[0] == "-i") $stderr.puts "USAGE: yamldiff [-i] file1 file2" @@ -9,7 +11,17 @@ if ARGV.size != 2 && !(ARGV.size == 3 && ARGV[0] == "-i") end if ARGV.size == 3 - puts Yamlcomm.comm_yaml(ARGV[1], ARGV[2]) + begin + #ignore = File.readlines(ARGV[2] + ".ignore").each{|line| line.chomp!} + ignore = Set.new File.readlines(ARGV[2] + ".ignore").map(&:chomp) + #ignore = File.open(ARGV[2] + ".ignore", "r"){|file| file.readlines.collect{|line| line.chomp}} + common = Set.new Yamlcomm.comm_yaml(ARGV[1], ARGV[2]) + #print ignore[0].to_hex_string + "\n" + #print common[0].to_hex_string + "\n" + puts (common - ignore).to_a() + rescue Errno::ENOENT + puts Yamlcomm.comm_yaml(ARGV[1], ARGV[2]) + end else errors = Yamldiff.diff_yaml(ARGV[0], ARGV[1]) puts errors[ARGV[1]] From 19715a1893e1b6db06e5131d94f4eff41a0d915e Mon Sep 17 00:00:00 2001 From: Alexandre Magno Date: Thu, 18 Sep 2014 17:52:05 -0300 Subject: [PATCH 4/4] Now without debug comments. --- bin/yamldiff | 5 ----- 1 file changed, 5 deletions(-) diff --git a/bin/yamldiff b/bin/yamldiff index f743003..b2ce833 100755 --- a/bin/yamldiff +++ b/bin/yamldiff @@ -3,7 +3,6 @@ $LOAD_PATH.unshift(File.expand_path("../../lib", __FILE__)) require 'yamldiff' require 'set' -#require 'hex_string' if ARGV.size != 2 && !(ARGV.size == 3 && ARGV[0] == "-i") $stderr.puts "USAGE: yamldiff [-i] file1 file2" @@ -12,12 +11,8 @@ end if ARGV.size == 3 begin - #ignore = File.readlines(ARGV[2] + ".ignore").each{|line| line.chomp!} ignore = Set.new File.readlines(ARGV[2] + ".ignore").map(&:chomp) - #ignore = File.open(ARGV[2] + ".ignore", "r"){|file| file.readlines.collect{|line| line.chomp}} common = Set.new Yamlcomm.comm_yaml(ARGV[1], ARGV[2]) - #print ignore[0].to_hex_string + "\n" - #print common[0].to_hex_string + "\n" puts (common - ignore).to_a() rescue Errno::ENOENT puts Yamlcomm.comm_yaml(ARGV[1], ARGV[2])