Skip to content

Commit

Permalink
Fix ldiff binary files comparison and output
Browse files Browse the repository at this point in the history
Before this change, the comparison was reversed and diff were not
detected.

This commit also change the output when a diff is detected to look
like the GNU diff output.

Also fix the support of all other format than :report, by quickly
halting the diff process if there is a binary diff.
  • Loading branch information
Annih authored and halostatue committed Feb 2, 2025
1 parent 85582d5 commit f40a910
Show file tree
Hide file tree
Showing 14 changed files with 18 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/diff/lcs/ldiff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,24 +116,29 @@ def self.diff?(info_old, info_new, format, output, binary: nil, lines: 0)
file_length_difference = 0

# Test binary status
if @binary.nil?
if binary.nil?
old_bin = info_old.data[0, 4096].include?("\0")
new_bin = info_new.data[0, 4096].include?("\0")
@binary = old_bin || new_bin
binary = old_bin || new_bin
end

# diff yields lots of pieces, each of which is basically a Block object
if binary
diffs = (info_old.data == info_new.data)
has_diffs = (info_old.data != info_new.data)
if format != :report
if has_diffs
output << "Binary files #{info_old.filename} and #{info_new.filename} differ\n"
return true
end
return false
end
else
data_old = info_old.data.lines.to_a
data_new = info_new.data.lines.to_a
diffs = Diff::LCS.diff(data_old, data_new)
diffs = nil if diffs.empty?
return false if diffs.empty?
end

return false unless diffs

case format
when :report
output << "Files #{info_old.filename} and #{info_new.filename} differ\n"
Expand Down
Binary file added spec/fixtures/file1.bin
Binary file not shown.
Binary file added spec/fixtures/file2.bin
Binary file not shown.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
1 change: 1 addition & 0 deletions spec/fixtures/ldiff/output.diff.bin2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Binary files spec/fixtures/file1.bin and spec/fixtures/file2.bin differ
1 change: 1 addition & 0 deletions spec/fixtures/ldiff/output.diff.bin2-c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Binary files spec/fixtures/file1.bin and spec/fixtures/file2.bin differ
1 change: 1 addition & 0 deletions spec/fixtures/ldiff/output.diff.bin2-e
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Binary files spec/fixtures/file1.bin and spec/fixtures/file2.bin differ
1 change: 1 addition & 0 deletions spec/fixtures/ldiff/output.diff.bin2-f
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Binary files spec/fixtures/file1.bin and spec/fixtures/file2.bin differ
1 change: 1 addition & 0 deletions spec/fixtures/ldiff/output.diff.bin2-u
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Binary files spec/fixtures/file1.bin and spec/fixtures/file2.bin differ
2 changes: 2 additions & 0 deletions spec/ldiff_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
# standard:disable Style/HashSyntax
fixtures = [
{:name => "output.diff", :left => "aX", :right => "bXaX", :diff => 1},
{:name => "output.diff.bin1", :left => "file1.bin", :right => "file1.bin", :diff => 0},
{:name => "output.diff.bin2", :left => "file1.bin", :right => "file2.bin", :diff => 1},
{:name => "output.diff.chef", :left => "old-chef", :right => "new-chef", :diff => 1},
{:name => "output.diff.chef2", :left => "old-chef2", :right => "new-chef2", :diff => 1}
].product([nil, "-e", "-f", "-c", "-u"]).map { |(fixture, flag)|
Expand Down

0 comments on commit f40a910

Please sign in to comment.