Skip to content

Instantly share code, notes, and snippets.

@indrekj
Created August 16, 2009 14:56
Show Gist options
  • Save indrekj/168641 to your computer and use it in GitHub Desktop.
Save indrekj/168641 to your computer and use it in GitHub Desktop.
require 'tempfile'
class Diff
KDIFF3 = '/usr/bin/kdiff3 --display NONE' unless defined?(KDIFF3)
DIFF3 = '/usr/bin/diff3' unless defined?(DIFF3)
class << self
def diff(content1, content2, content3)
file1_path = make_tmpfile('file1', content1)
file2_path = make_tmpfile('file2', content2)
file3_path = make_tmpfile('file3', content3)
output = kdiff3(file1_path, file2_path, file3_path)
output ||= diff3(file1_path, file2_path, file3_path)
File.delete(file1_path, file2_path, file3_path)
output
end
def kdiff3(file1_path, file2_path, file3_path)
output = make_tmpfile('output')
`#{KDIFF3} #{file1_path} #{file2_path} #{file3_path} --auto -o #{output} 2>&1`
content = File.size?(output) ? File.read(output) : nil
File.delete(output)
content
end
def diff3(file1_path, file2_path, file3_path)
`#{DIFF3} #{file2_path} #{file1_path} #{file3_path} -m`
end
# Returns path to the file
def make_tmpfile(name, content = nil)
tempfile = Tempfile.new(name)
begin
tempfile << content if content
ensure
tempfile.close
end
tempfile.path
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment