41 | 41 |
|
42 | 42 |
def diff
|
43 | 43 |
@diff ||= begin
|
44 | |
paths = case options[:source]
|
|
44 |
@paths = case options[:source]
|
45 | 45 |
when 'strings'
|
46 | 46 |
[tempfile(string1), tempfile(string2)]
|
47 | 47 |
when 'files'
|
|
50 | 50 |
|
51 | 51 |
if WINDOWS
|
52 | 52 |
# don't use open3 on windows
|
53 | |
cmd = sprintf '"%s" %s %s', diff_bin, diff_options.join(' '), paths.map { |s| %("#{s}") }.join(' ')
|
|
53 |
cmd = sprintf '"%s" %s %s', diff_bin, diff_options.join(' '), @paths.map { |s| %("#{s}") }.join(' ')
|
54 | 54 |
diff = `#{cmd}`
|
55 | 55 |
else
|
56 | |
diff = Open3.popen3(diff_bin, *(diff_options + paths)) { |i, o, e| o.read }
|
|
56 |
diff = Open3.popen3(diff_bin, *(diff_options + @paths)) { |i, o, e| o.read }
|
57 | 57 |
end
|
58 | 58 |
diff.force_encoding('ASCII-8BIT') if diff.respond_to?(:valid_encoding?) && !diff.valid_encoding?
|
59 | 59 |
if diff =~ /\A\s*\Z/ && !options[:allow_empty_diff]
|
|
83 | 83 |
|
84 | 84 |
def each
|
85 | 85 |
lines = case @options[:include_diff_info]
|
86 | |
when false then diff.split("\n").reject{|x| x =~ /^(---|\+\+\+|@@|\\\\)/ }.map {|line| line + "\n" }
|
87 | |
when true then diff.split("\n").map {|line| line + "\n" }
|
|
86 |
when false
|
|
87 |
# this "primes" the diff and sets up the paths we'll reference below.
|
|
88 |
diff
|
|
89 |
|
|
90 |
# caching this regexp improves the performance of the loop by a
|
|
91 |
# considerable amount.
|
|
92 |
regexp = /^(--- "?#{@paths[0]}"?|\+\+\+ "?#{@paths[1]}"?|@@|\\\\)/
|
|
93 |
|
|
94 |
diff.split("\n").reject{|x| x =~ regexp }.map {|line| line + "\n" }
|
|
95 |
|
|
96 |
when true
|
|
97 |
diff.split("\n").map {|line| line + "\n" }
|
88 | 98 |
end
|
|
99 |
|
89 | 100 |
if block_given?
|
90 | 101 |
lines.each{|line| yield line}
|
91 | 102 |
else
|