Created
August 16, 2009 14:56
-
-
Save indrekj/168641 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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