Skip to content

Commit e596c52

Browse files
committed
Show file size before/after on binary files
1 parent fd6a9a3 commit e596c52

File tree

4 files changed

+51
-15
lines changed

4 files changed

+51
-15
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ binary files were treated as text.
1111
Added the `--override-binary` option to force files to be treated as
1212
binary rather than text.
1313

14+
### Display
15+
16+
When diffing binary files, the file sizes are now shown to help see
17+
big changes.
18+
1419
## 0.64 (released 16th June 2025)
1520

1621
### Parsing

src/display/json.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl<'f> From<&'f DiffResult> for File<'f> {
156156
File::with_sections(&summary.file_format, &summary.display_path, chunks)
157157
}
158158
(FileContent::Binary, FileContent::Binary) => {
159-
let status = if summary.has_byte_changes {
159+
let status = if summary.has_byte_changes.is_some() {
160160
Status::Changed
161161
} else {
162162
Status::Unchanged

src/main.rs

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,11 @@ fn diff_file(
407407
guess_content(&rhs_bytes, rhs_path, binary_overrides),
408408
) {
409409
(ProbableFileKind::Binary, _) | (_, ProbableFileKind::Binary) => {
410+
let has_byte_changes = if lhs_bytes == rhs_bytes {
411+
None
412+
} else {
413+
Some((lhs_bytes.len(), rhs_bytes.len()))
414+
};
410415
return DiffResult {
411416
extra_info: renamed,
412417
display_path: display_path.to_owned(),
@@ -416,7 +421,7 @@ fn diff_file(
416421
lhs_positions: vec![],
417422
rhs_positions: vec![],
418423
hunks: vec![],
419-
has_byte_changes: lhs_bytes != rhs_bytes,
424+
has_byte_changes,
420425
has_syntactic_changes: false,
421426
};
422427
}
@@ -549,7 +554,11 @@ fn check_only_text(
549554
lhs_src: &str,
550555
rhs_src: &str,
551556
) -> DiffResult {
552-
let has_changes = lhs_src != rhs_src;
557+
let has_byte_changes = if lhs_src == rhs_src {
558+
None
559+
} else {
560+
Some((lhs_src.as_bytes().len(), rhs_src.as_bytes().len()))
561+
};
553562

554563
DiffResult {
555564
display_path: display_path.to_owned(),
@@ -560,8 +569,8 @@ fn check_only_text(
560569
lhs_positions: vec![],
561570
rhs_positions: vec![],
562571
hunks: vec![],
563-
has_byte_changes: has_changes,
564-
has_syntactic_changes: has_changes,
572+
has_byte_changes,
573+
has_syntactic_changes: lhs_src != rhs_src,
565574
}
566575
}
567576

@@ -601,7 +610,7 @@ fn diff_file_content(
601610
lhs_positions: vec![],
602611
rhs_positions: vec![],
603612
hunks: vec![],
604-
has_byte_changes: false,
613+
has_byte_changes: None,
605614
has_syntactic_changes: false,
606615
};
607616
}
@@ -633,6 +642,13 @@ fn diff_file_content(
633642
Ok((lhs, rhs)) => {
634643
if diff_options.check_only {
635644
let has_syntactic_changes = lhs != rhs;
645+
646+
let has_byte_changes = if lhs_src == rhs_src {
647+
None
648+
} else {
649+
Some((lhs_src.as_bytes().len(), rhs_src.as_bytes().len()))
650+
};
651+
636652
return DiffResult {
637653
extra_info,
638654
display_path: display_path.to_owned(),
@@ -642,7 +658,7 @@ fn diff_file_content(
642658
lhs_positions: vec![],
643659
rhs_positions: vec![],
644660
hunks: vec![],
645-
has_byte_changes: true,
661+
has_byte_changes,
646662
has_syntactic_changes,
647663
};
648664
}
@@ -775,6 +791,12 @@ fn diff_file_content(
775791
);
776792
let has_syntactic_changes = !hunks.is_empty();
777793

794+
let has_byte_changes = if lhs_src == rhs_src {
795+
None
796+
} else {
797+
Some((lhs_src.as_bytes().len(), rhs_src.as_bytes().len()))
798+
};
799+
778800
DiffResult {
779801
extra_info,
780802
display_path: display_path.to_owned(),
@@ -784,7 +806,7 @@ fn diff_file_content(
784806
lhs_positions,
785807
rhs_positions,
786808
hunks,
787-
has_byte_changes: true,
809+
has_byte_changes,
788810
has_syntactic_changes,
789811
}
790812
}
@@ -923,7 +945,7 @@ fn print_diff_result(display_options: &DisplayOptions, summary: &DiffResult) {
923945
}
924946
}
925947
(FileContent::Binary, FileContent::Binary) => {
926-
if display_options.print_unchanged || summary.has_byte_changes {
948+
if display_options.print_unchanged || summary.has_byte_changes.is_some() {
927949
println!(
928950
"{}",
929951
display::style::header(
@@ -935,10 +957,17 @@ fn print_diff_result(display_options: &DisplayOptions, summary: &DiffResult) {
935957
display_options
936958
)
937959
);
938-
if summary.has_byte_changes {
939-
println!("Binary contents changed.\n");
940-
} else {
941-
println!("No changes.\n");
960+
961+
match summary.has_byte_changes {
962+
Some((lhs_len, rhs_len)) => {
963+
let format_options = FormatSizeOptions::from(BINARY).decimal_places(1);
964+
println!(
965+
"Binary contents changed (old: {}, new: {}).\n",
966+
&format_size(lhs_len, format_options),
967+
&format_size(rhs_len, format_options),
968+
)
969+
}
970+
None => println!("No changes.\n"),
942971
}
943972
}
944973
}

src/summary.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ pub(crate) struct DiffResult {
5050
pub(crate) lhs_positions: Vec<MatchedPos>,
5151
pub(crate) rhs_positions: Vec<MatchedPos>,
5252

53-
pub(crate) has_byte_changes: bool,
53+
/// If the two files do not have exactly the same bytes, the
54+
/// number of bytes in each file.
55+
pub(crate) has_byte_changes: Option<(usize, usize)>,
5456
pub(crate) has_syntactic_changes: bool,
5557
}
5658

@@ -59,7 +61,7 @@ impl DiffResult {
5961
if matches!(self.lhs_src, FileContent::Binary)
6062
|| matches!(self.rhs_src, FileContent::Binary)
6163
{
62-
return self.has_byte_changes;
64+
return self.has_byte_changes.is_some();
6365
}
6466

6567
self.has_syntactic_changes

0 commit comments

Comments
 (0)