class Clash::Diff

Attributes

test_failures[RW]

Public Class Methods

new(a, b, options={}) click to toggle source
# File lib/clash/diff.rb, line 7
def initialize(a, b, options={})
  @diffs   = {}
  @a       = a
  @b       = b
  @context = options[:context] || 2
  @test_failures = []
end

Public Instance Methods

diff() click to toggle source
# File lib/clash/diff.rb, line 15
def diff
  if File.directory?(@a)
    diff_dirs(@a, @b)
  else
    diff_files(@a, @b)
  end

  @diffs
end
diff_dirs(dir1, dir2) click to toggle source

Recursively diff common files between dir1 and dir2

# File lib/clash/diff.rb, line 39
def diff_dirs(dir1, dir2)
  mattching_dir_files(dir1, dir2).each do |file|
    a = File.join(dir1, file)
    b = File.join(dir2, file)
    diff_files(a,b)
  end
end
diff_files(a, b) click to toggle source
# File lib/clash/diff.rb, line 25
def diff_files(a, b)
  if exists(a) && exists(b)
    diffy = Diffy::Diff.new(a,b, :source => 'files', :context => @context)
    file_diff = diffy.to_a

    if !file_diff.empty?
      file_diff = format_diff(file_diff)
      @diffs[yellowit("\nCompared #{a} to #{b}:\n")] = file_diff 
    end
  end
end
dir_files(dir) click to toggle source

Find all files in a given directory

# File lib/clash/diff.rb, line 63
def dir_files(dir)
  Find.find(dir).to_a.reject!{|f| File.directory?(f) }
end
exists(f) click to toggle source
# File lib/clash/diff.rb, line 81
def exists(f)
  file_exists = File.exists?(f)

  if !file_exists
    @test_failures << "#{redit('File not found:')} #{f}\n"
  end

  file_exists
end
format_diff(diff) click to toggle source
# File lib/clash/diff.rb, line 92
def format_diff(diff)
  count = 0

  diff = diff.map { |line|
    case line
    when /^\+/ then 
      count = 0
      "  #{greenit(line)}"
    when /^-/ then 
      count = 0
      "  #{redit(line)}"
    else 
      if count == @context
        count = 0
        "...\n  #{line}"
      else
        count += 1
        "  #{line}"
      end
    end
  }
  diff.join('')
end
mattching_dir_files(dir1, dir2) click to toggle source

Return files that exist in both directories (without dir names)

# File lib/clash/diff.rb, line 49
def mattching_dir_files(dir1, dir2)
  dir1_files = dir_files(dir1).map {|f| f.sub("#{dir1}/",'') }
  dir2_files = dir_files(dir2).map {|f| f.sub("#{dir2}/",'') }

  matches = dir1_files & dir2_files

  unique_files(dir1, dir2_files, matches)
  unique_files(dir2, dir1_files, matches)

  matches
end
unique_files(dir, dir_files, common_files) click to toggle source

Find files which aren't common to both directories

# File lib/clash/diff.rb, line 69
def unique_files(dir, dir_files, common_files)
  unique = dir_files - common_files
  if !unique.empty?
    @test_failures << yellowit("\nMissing from directory #{dir}:\n")
    unique.each do |f| 
      failure = "  - #{f}"
      failure << "\n" if unique.last == f
      @test_failures << failure
    end
  end
end