diffy-3.4.4/ 0000755 0000041 0000041 00000000000 15024024013 012642 5 ustar www-data www-data diffy-3.4.4/CONTRIBUTORS 0000644 0000041 0000041 00000000361 15024024013 014522 0 ustar www-data www-data * Sam Goldstein
* joelash
* chaffeqa
* Lincoln Ritter
* Bernhard Weichel
* Yuichi Tateno
* Nigel Thorne
* Richard Stiller
* printercu
* Bryan Ricker
* JasonBarnabe
* Skye Shaw
* Abinoam P. Marques Jr.
* evgen
* J Smith @dark-panda
* @P-SiZK
diffy-3.4.4/.gitignore 0000644 0000041 0000041 00000000046 15024024013 014632 0 ustar www-data www-data *.gem
*.swp
Gemfile.lock
tags
.bundle
diffy-3.4.4/lib/ 0000755 0000041 0000041 00000000000 15024024013 013410 5 ustar www-data www-data diffy-3.4.4/lib/diffy.rb 0000644 0000041 0000041 00000000720 15024024013 015035 0 ustar www-data www-data require 'tempfile'
require 'erb'
require 'rbconfig'
require 'open3'
module Diffy
WINDOWS = (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/)
end
require File.join(File.dirname(__FILE__), 'diffy', 'format')
require File.join(File.dirname(__FILE__), 'diffy', 'html_formatter')
require File.join(File.dirname(__FILE__), 'diffy', 'diff')
require File.join(File.dirname(__FILE__), 'diffy', 'split_diff')
require File.join(File.dirname(__FILE__), 'diffy', 'css')
diffy-3.4.4/lib/diffy/ 0000755 0000041 0000041 00000000000 15024024013 014511 5 ustar www-data www-data diffy-3.4.4/lib/diffy/split_diff.rb 0000644 0000041 0000041 00000002316 15024024013 017163 0 ustar www-data www-data module Diffy
class SplitDiff
def initialize(left, right, options = {})
@format = options[:format] || Diffy::Diff.default_format
formats = Format.instance_methods(false).map { |x| x.to_s }
unless formats.include?(@format.to_s)
fail ArgumentError, "Format #{format.inspect} is not a valid format"
end
@diff = Diffy::Diff.new(left, right, options).to_s(@format)
@left_diff, @right_diff = split
end
%w(left right).each do |direction|
define_method direction do
instance_variable_get("@#{direction}_diff")
end
end
private
def split
[split_left, split_right]
end
def split_left
case @format
when :color
@diff.gsub(/\033\[32m\+(.*)\033\[0m\n/, '')
when :html, :html_simple
@diff.gsub(%r{\s+
(.*)}, '')
when :text
@diff.gsub(/^\+(.*)\n/, '')
end
end
def split_right
case @format
when :color
@diff.gsub(/\033\[31m\-(.*)\033\[0m\n/, '')
when :html, :html_simple
@diff.gsub(%r{\s+(.*)}, '')
when :text
@diff.gsub(/^-(.*)\n/, '')
end
end
end
end
diffy-3.4.4/lib/diffy/html_formatter.rb 0000644 0000041 0000041 00000010052 15024024013 020063 0 ustar www-data www-data module Diffy
class HtmlFormatter
def initialize(diff, options = {})
@diff = diff
@options = options
end
def to_s
if @options[:highlight_words]
wrap_lines(highlighted_words)
else
wrap_lines(@diff.map{|line| wrap_line(ERB::Util.h(line))})
end
end
private
def wrap_line(line)
cleaned = clean_line(line)
case line
when /^(---|\+\+\+|\\\\)/
' '
when /^\+/
' ' + cleaned + ''
when /^-/
' ' + cleaned + ''
when /^ /
' ' + cleaned + ''
when /^@@/
' ' + line.chomp + ''
end
end
# remove +/- or wrap in html
def clean_line(line)
if @options[:include_plus_and_minus_in_html]
line.sub(/^(.)/, '\1')
else
line.sub(/^./, '')
end.chomp
end
def wrap_lines(lines)
if lines.empty?
%''
else
%'\n'
end
end
def highlighted_words
chunks = @diff.each_chunk.
reject{|c| c == '\ No newline at end of file'"\n"}
processed = []
lines = chunks.each_with_index.map do |chunk1, index|
next if processed.include? index
processed << index
chunk1 = chunk1
chunk2 = chunks[index + 1]
if not chunk2
next ERB::Util.h(chunk1)
end
dir1 = chunk1.each_char.first
dir2 = chunk2.each_char.first
case [dir1, dir2]
when ['-', '+']
if chunk1.each_char.take(3).join("") =~ /^(---|\+\+\+|\\\\)/ and
chunk2.each_char.take(3).join("") =~ /^(---|\+\+\+|\\\\)/
ERB::Util.h(chunk1)
else
line_diff = Diffy::Diff.new(
split_characters(chunk1),
split_characters(chunk2),
Diffy::Diff::ORIGINAL_DEFAULT_OPTIONS
)
hi1 = reconstruct_characters(line_diff, '-')
hi2 = reconstruct_characters(line_diff, '+')
processed << (index + 1)
[hi1, hi2]
end
else
ERB::Util.h(chunk1)
end
end.flatten
lines.map{|line| line.each_line.map(&:chomp).to_a if line }.flatten.compact.
map{|line|wrap_line(line) }.compact
end
def split_characters(chunk)
chunk.gsub(/^./, '').each_line.map do |line|
if @options[:ignore_crlf]
line.chomp.split('').map{|chr| ERB::Util.h(chr) } + ['']
else
chars = line.sub(/([\r\n]$)/, '').split('')
chars.map{|chr| ERB::Util.h(chr) } + ['']
end
end.flatten.join("\n") + "\n"
end
def reconstruct_characters(line_diff, type)
enum = line_diff.each_chunk.to_a
enum.each_with_index.map do |l, i|
re = /(^|)#{Regexp.escape(type)}/
case l
when re
highlight(l)
when /^ /
if i > 1 and enum[i+1] and l.each_line.to_a.size < 4
highlight(l)
else
l.gsub(/^./, '').gsub("\n", '').
gsub('\r', "\r").gsub('', "\n")
end
end
end.join('').split("\n").map do |l|
type + l.gsub('' , '')
end
end
def highlight(lines)
"" +
lines.
# strip diff tokens (e.g. +,-,etc.)
gsub(/(^|)./, '').
# join characters back by stripping out newlines
gsub("\n", '').
# close and reopen strong tags. we don't want inline elements
# spanning block elements which get added later.
gsub('',"\n") + ""
end
end
end
diffy-3.4.4/lib/diffy/version.rb 0000644 0000041 0000041 00000000045 15024024013 016522 0 ustar www-data www-data module Diffy
VERSION = '3.4.4'
end
diffy-3.4.4/lib/diffy/diff.rb 0000644 0000041 0000041 00000011705 15024024013 015752 0 ustar www-data www-data module Diffy
class Diff
ORIGINAL_DEFAULT_OPTIONS = {
:diff => '-U10000',
:source => 'strings',
:include_diff_info => false,
:include_plus_and_minus_in_html => false,
:context => nil,
:allow_empty_diff => true,
}
class << self
attr_writer :default_format
def default_format
@default_format ||= :text
end
# default options passed to new Diff objects
attr_writer :default_options
def default_options
@default_options ||= ORIGINAL_DEFAULT_OPTIONS.dup
end
end
include Enumerable
attr_reader :string1, :string2, :options
# supported options
# +:diff+:: A cli options string passed to diff
# +:source+:: Either _strings_ or _files_. Determines whether string1
# and string2 should be interpreted as strings or file paths.
# +:include_diff_info+:: Include diff header info
# +:include_plus_and_minus_in_html+:: Show the +, -, ' ' at the
# beginning of lines in html output.
def initialize(string1, string2, options = {})
@options = self.class.default_options.merge(options)
if ! ['strings', 'files'].include?(@options[:source])
raise ArgumentError, "Invalid :source option #{@options[:source].inspect}. Supported options are 'strings' and 'files'."
end
@string1, @string2 = string1, string2
end
def diff
@diff ||= begin
@paths = case options[:source]
when 'strings'
[tempfile(string1), tempfile(string2)]
when 'files'
[string1, string2]
end
diff, _stderr, _process_status = Open3.capture3(diff_bin, *(diff_options + @paths))
diff.force_encoding('ASCII-8BIT') if diff.respond_to?(:valid_encoding?) && !diff.valid_encoding?
if diff =~ /\A\s*\Z/ && !options[:allow_empty_diff]
diff = case options[:source]
when 'strings' then string1
when 'files' then File.read(string1)
end.gsub(/^/, " ")
end
diff
end
ensure
# unlink the tempfiles explicitly now that the diff is generated
if defined? @tempfiles # to avoid Ruby warnings about undefined ins var.
Array(@tempfiles).each do |t|
begin
# check that the path is not nil and file still exists.
# REE seems to be very agressive with when it magically removes
# tempfiles
t.unlink if t.path && File.exist?(t.path)
rescue => e
warn "#{e.class}: #{e}"
warn e.backtrace.join("\n")
end
end
end
end
def each
lines = case @options[:include_diff_info]
when false
# this "primes" the diff and sets up the paths we'll reference below.
diff
# caching this regexp improves the performance of the loop by a
# considerable amount.
regexp = /^(--- "?#{@paths[0]}"?|\+\+\+ "?#{@paths[1]}"?|@@|\\\\)/
diff.split("\n").reject{|x| x =~ regexp }.map {|line| line + "\n" }
when true
diff.split("\n").map {|line| line + "\n" }
end
if block_given?
lines.each{|line| yield line}
else
lines.to_enum
end
end
def each_chunk
old_state = nil
chunks = inject([]) do |cc, line|
state = line.each_char.first
if state == old_state
cc.last << line
else
cc.push line.dup
end
old_state = state
cc
end
if block_given?
chunks.each{|chunk| yield chunk }
else
chunks.to_enum
end
end
def tempfile(string)
t = Tempfile.new('diffy')
# ensure tempfiles aren't unlinked when GC runs by maintaining a
# reference to them.
@tempfiles ||=[]
@tempfiles.push(t)
t.print(string)
t.flush
t.close
t.path
end
def to_s(format = nil)
format ||= self.class.default_format
formats = Format.instance_methods(false).map{|x| x.to_s}
if formats.include? format.to_s
enum = self
enum.extend Format
enum.send format
else
raise ArgumentError,
"Format #{format.inspect} not found in #{formats.inspect}"
end
end
private
@@bin = nil
def diff_bin
return @@bin if @@bin
if @@bin = ENV['DIFFY_DIFF']
# system() trick from Minitest
raise "Can't execute diff program '#@@bin'" unless system(@@bin, __FILE__, __FILE__)
return @@bin
end
diffs = ['diff', 'ldiff']
diffs.first << '.exe' if WINDOWS # ldiff does not have exe extension
@@bin = diffs.find { |name| system(name, __FILE__, __FILE__) }
if @@bin.nil?
raise "Can't find a diff executable in PATH #{ENV['PATH']}"
end
@@bin
end
# options pass to diff program
def diff_options
Array(options[:context] ? "-U#{options[:context]}" : options[:diff])
end
end
end
diffy-3.4.4/lib/diffy/css.rb 0000644 0000041 0000041 00000003002 15024024013 015621 0 ustar www-data www-data module Diffy
CSS = <<-STYLE
.diff{overflow:auto;}
.diff ul{background:#fff;overflow:auto;font-size:13px;list-style:none;margin:0;padding:0;display:table;width:100%;}
.diff del, .diff ins{display:block;text-decoration:none;}
.diff li{padding:0; display:table-row;margin: 0;height:1em;}
.diff li.ins{background:#dfd; color:#080}
.diff li.del{background:#fee; color:#b00}
.diff li:hover{background:#ffc}
/* try 'whitespace:pre;' if you don't want lines to wrap */
.diff del, .diff ins, .diff span{white-space:pre-wrap;font-family:courier;}
.diff del strong{font-weight:normal;background:#fcc;}
.diff ins strong{font-weight:normal;background:#9f9;}
.diff li.diff-comment { display: none; }
.diff li.diff-block-info { background: none repeat scroll 0 0 gray; }
STYLE
CSS_COLORBLIND_1 = <<-STYLE
.diff{overflow:auto;}
.diff ul{background:#fff;overflow:auto;font-size:13px;list-style:none;margin:0;padding:0;display:table;width:100%;}
.diff del, .diff ins{display:block;text-decoration:none;}
.diff li{padding:0; display:table-row;margin: 0;height:1em;}
.diff li.ins{background:#ddf; color:#008}
.diff li.del{background:#fee; color:#b00}
.diff li:hover{background:#ffc}
/* try 'whitespace:pre;' if you don't want lines to wrap */
.diff del, .diff ins, .diff span{white-space:pre-wrap;font-family:courier;}
.diff del strong{font-weight:normal;background:#fcc;}
.diff ins strong{font-weight:normal;background:#99f;}
.diff li.diff-comment { display: none; }
.diff li.diff-block-info { background: none repeat scroll 0 0 gray; }
STYLE
end
diffy-3.4.4/lib/diffy/format.rb 0000644 0000041 0000041 00000001615 15024024013 016331 0 ustar www-data www-data module Diffy
module Format
# ANSI color output suitable for terminal output
def color
map do |line|
case line
when /^(---|\+\+\+|\\\\)/
"\033[90m#{line.chomp}\033[0m"
when /^\+/
"\033[32m#{line.chomp}\033[0m"
when /^-/
"\033[31m#{line.chomp}\033[0m"
when /^@@/
"\033[36m#{line.chomp}\033[0m"
else
line.chomp
end
end.join("\n") + "\n"
end
# Basic text output
def text
to_a.join
end
# Basic html output which does not attempt to highlight the changes
# between lines, and is more performant.
def html_simple
HtmlFormatter.new(self, options).to_s
end
# Html output which does inline highlighting of changes between two lines.
def html
HtmlFormatter.new(self, options.merge(:highlight_words => true)).to_s
end
end
end
diffy-3.4.4/spec/ 0000755 0000041 0000041 00000000000 15024024013 013574 5 ustar www-data www-data diffy-3.4.4/spec/diffy_spec.rb 0000644 0000041 0000041 00000052772 15024024013 016251 0 ustar www-data www-data require 'rspec'
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'diffy'))
describe Diffy::Diff do
describe "diffing two files" do
def tempfile(string, fn = 'diffy-spec')
t = Tempfile.new(fn)
# ensure tempfiles aren't unlinked when GC runs by maintaining a
# reference to them.
@tempfiles ||=[]
@tempfiles.push(t)
t.print(string)
t.flush
t.close
t.path
end
it "should accept file paths as arguments" do
string1 = "foo\nbar\nbang\n"
string2 = "foo\nbang\n"
path1, path2 = tempfile(string1), tempfile(string2)
expect(Diffy::Diff.new(path1, path2, :source => 'files').to_s).to eq <<-DIFF
foo
-bar
bang
DIFF
end
it "should accept file paths with spaces as arguments" do
string1 = "foo\nbar\nbang\n"
string2 = "foo\nbang\n"
path1, path2 = tempfile(string1, 'path with spaces'), tempfile(string2, 'path with spaces')
expect(Diffy::Diff.new(path1, path2, :source => 'files').to_s).to eq <<-DIFF
foo
-bar
bang
DIFF
end
it "should accept file paths with spaces as arguments on windows" do
begin
orig_verbose, $VERBOSE = $VERBOSE, nil #silence redefine constant warnings
orig_windows, Diffy::WINDOWS = Diffy::WINDOWS, true
string1 = "foo\nbar\nbang\n"
string2 = "foo\nbang\n"
path1, path2 = tempfile(string1, 'path with spaces'), tempfile(string2, 'path with spaces')
expect(Diffy::Diff.new(path1, path2, :source => 'files').to_s).to eq <<-DIFF
foo
-bar
bang
DIFF
ensure
Diffy::WINDOWS, $VERBOSE = orig_windows, orig_verbose
end
end
describe "with no line different" do
before do
string1 = "foo\nbar\nbang\n"
string2 = "foo\nbar\nbang\n"
@path1, @path2 = tempfile(string1), tempfile(string2)
end
it "should show everything" do
expect(Diffy::Diff.new(@path1, @path2, :source => 'files', :allow_empty_diff => false).
to_s).to eq <<-DIFF
foo
bar
bang
DIFF
end
it "should not show everything if the :allow_empty_diff option is set" do
expect(Diffy::Diff.new(@path1, @path2, :source => 'files', :allow_empty_diff => true).to_s).to eq('')
end
end
describe "with lines that start with backslashes" do
before do
string1 = "foo\n\\\\bag\nbang\n"
string2 = "foo\n\\\\bar\nbang\n"
@path1, @path2 = tempfile(string1), tempfile(string2)
end
it "should not leave lines out" do
expect(Diffy::Diff.new(@path1, @path2, :source => 'files').to_s).to eq <<-DIFF
foo
-\\\\bag
+\\\\bar
bang
DIFF
end
end
describe "with non valid UTF bytes" do
before do
string1 = "Foo ICS95095010000000000083320000BS01030000004100+\xFF00000000000000000\n"
string2 = "Bar ICS95095010000000000083320000BS01030000004100+\xFF00000000000000000\n"
@path1, @path2 = tempfile(string1), tempfile(string2)
end
it "should not raise invalid encoding issues" do
desired = <<-DIFF
-Foo ICS95095010000000000083320000BS01030000004100+\xFF00000000000000000
+Bar ICS95095010000000000083320000BS01030000004100+\xFF00000000000000000
DIFF
desired.force_encoding("ASCII-8BIT") if desired.respond_to?(:force_encoding)
expect(Diffy::Diff.new(@path1, @path2, :source => 'files').to_s).to eq(desired)
end
end
end
describe "handling temp files" do
it "should unlink tempfiles after generating the diff" do
before_tmpfiles = Dir.entries(Dir.tmpdir)
::Diffy::Diff.new("a", "b").to_s
after_tmpfiles = Dir.entries(Dir.tmpdir)
expect(before_tmpfiles).to match_array(after_tmpfiles)
end
it "should still be able to generate multiple diffs" do
d = ::Diffy::Diff.new("a", "b")
expect(d.to_s).to be_a String
expect(d.to_s(:html)).to be_a String
end
end
describe "options[:context]" do
it "should limit context lines to 1" do
diff = Diffy::Diff.new("foo\nfoo\nBAR\nbang\nbaz", "foo\nfoo\nbar\nbang\nbaz", :context => 1)
expect(diff.to_s).to eq <<-DIFF
foo
-BAR
+bar
bang
DIFF
end
end
describe "options[:include_plus_and_minus_in_html]" do
it "defaults to false" do
@diffy = Diffy::Diff.new(" foo\nbar\n", "foo\nbar\n")
expect(@diffy.options[:include_plus_and_minus_in_html]).to eq(false)
end
it "can be set to true" do
@diffy = Diffy::Diff.new(" foo\nbar\n", "foo\nbar\n", :include_plus_and_minus_in_html=> true )
expect(@diffy.options[:include_plus_and_minus_in_html]).to eq(true)
end
describe "formats" do
it "includes symbols in html_simple" do
output = Diffy::Diff.new("foo\nbar\nbang\n", "foo\nbang\n", :include_plus_and_minus_in_html => true ).
to_s(:html_simple)
expect(output).to eq <<-HTML
HTML
end
it "includes symbols in html" do
output = Diffy::Diff.new("foo\nbar\nbang\n", "foo\naba\nbang\n", :include_plus_and_minus_in_html => true ).
to_s(:html)
expect(output).to eq <<-HTML
HTML
end
end
end
describe "options[:include_diff_info]" do
it "defaults to false" do
@diffy = Diffy::Diff.new(" foo\nbar\n", "foo\nbar\n")
expect(@diffy.options[:include_diff_info]).to eq(false)
end
it "can be set to true" do
@diffy = Diffy::Diff.new(" foo\nbar\n", "foo\nbar\n", :include_diff_info => true )
expect(@diffy.options[:include_diff_info]).to eq(true)
end
it "includes all diff output" do
output = Diffy::Diff.new("foo\nbar\nbang\n", "foo\nbang\n", :include_diff_info => true ).to_s
expect(output.to_s).to match( /@@/)
expect(output).to match( /---/)
expect(output).to match( /\+\+\+/)
end
describe "formats" do
it "works for :color" do
output = Diffy::Diff.new("foo\nbar\nbang\n", "foo\nbang\n", :include_diff_info => true ).to_s(:color)
expect(output).to match( /\e\[0m\n\e\[36m\@\@/ )
expect(output.to_s).to match( /\e\[90m---/)
expect(output.to_s).to match( /\e\[0m\n\e\[90m\+\+\+/)
end
it "works for :html_simple" do
output = Diffy::Diff.new("foo\nbar\nbang\n", "foo\nbang\n", :include_diff_info => true ).to_s(:html_simple)
expect(output.split("\n")).to include( " @@ -1,3 +1,2 @@" )
expect(output).to include( "