Codebase list ruby-diffy / 08f3340
Merge pull request #99 from dark-panda/fix-diffs-with-dashes-and-pluses Fix diff lines that begin with -- or ++ Sam Goldstein authored 5 years ago GitHub committed 5 years ago
2 changed file(s) with 40 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
4141
4242 def diff
4343 @diff ||= begin
44 paths = case options[:source]
44 @paths = case options[:source]
4545 when 'strings'
4646 [tempfile(string1), tempfile(string2)]
4747 when 'files'
5050
5151 if WINDOWS
5252 # 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(' ')
5454 diff = `#{cmd}`
5555 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 }
5757 end
5858 diff.force_encoding('ASCII-8BIT') if diff.respond_to?(:valid_encoding?) && !diff.valid_encoding?
5959 if diff =~ /\A\s*\Z/ && !options[:allow_empty_diff]
8383
8484 def each
8585 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" }
8898 end
99
89100 if block_given?
90101 lines.each{|line| yield line}
91102 else
584584 line
585585 end).to eq([" foo\n", " bar\n", "+baz\n"])
586586 end
587
588 it "should handle lines that begin with --" do
589 string1 = "a a\n-- b\nc c\n"
590 string2 = "a a\nb b\nc c\n"
591
592 expect(Diffy::Diff.new(string1, string2).to_s).to eq <<-DIFF
593 a a
594 --- b
595 +b b
596 c c
597 DIFF
598 end
599
600 it "should handle lines that begin with ++" do
601 string1 = "a a\nb b\nc c\n"
602 string2 = "a a\n++ b\nc c\n"
603
604 expect(Diffy::Diff.new(string1, string2).to_s).to eq <<-DIFF
605 a a
606 -b b
607 +++ b
608 c c
609 DIFF
610 end
587611 end
588612 end
589613