diff: Preserve change kinds for binary files
bf82d8993ab2a8fdf7a148b06a4f1921b1ed1ca1
Keep added, deleted, renamed, and copied binary files classified by their file change kind. Detect binary content independently so file cards stay collapsed and patches round-trip without synthetic hunks. Assisted-by: Codex:gpt-6
1 parent
e46aa3a0
crates/diff/src/decode.rs
+0 -0
{
+0 -0
+0 -0
=> break,
+0 -0
a.rs
+0 -0
a.rs\x1b[m\n\
+0 -0
new.rs
+0 -0
old.rs
+0 -0
to.rs
+0 -0
copy2.rs
+0 -0
mode.sh
+0 -0
image.png
+0 -0
caf\303\251\t\"slash\\.rs" "b/modified caf\303\251\t\"slash\\.rs"
+0 -0
ordinary.rs
+0 -0
from caf\303\251\t\"slash\\.rs" "b/rename to caf\303\251\t\"slash\\.rs"
+0 -0
"b/non-utf8-\377.rs"
+0 -0
a
+0 -0
a
+0 -0
a
+0 -0
a
+0 -0
a.rs
+0 -0
b.rs
+0 -0
a.rs
+0 -0
crates/diff/src/document.rs
+9 -0
| 272 | 272 | stats |
|
| 273 | 273 | } |
|
| 274 | 274 | } |
|
| 275 | 275 | ||
| 276 | 276 | impl FileHeader { |
|
| 277 | + | /// Returns whether the header describes binary content. |
|
| 278 | + | pub(crate) fn is_binary(&self) -> bool { |
|
| 279 | + | self.kind == FileKind::Binary |
|
| 280 | + | || self |
|
| 281 | + | .raw |
|
| 282 | + | .iter() |
|
| 283 | + | .any(|line| line.starts_with("Binary files ") || line == "GIT binary patch") |
|
| 284 | + | } |
|
| 285 | + | ||
| 277 | 286 | /// Returns the path callers should usually show for this file. |
|
| 278 | 287 | pub fn display_path(&self) -> Option<&PathBuf> { |
|
| 279 | 288 | self.new_path.as_ref().or(self.old_path.as_ref()) |
|
| 280 | 289 | } |
|
| 281 | 290 | } |
a.txt
+0 -0
a
+0 -0
crates/diff/src/encode.rs
+1 -1
| 37 | 37 | impl Encode for FileDiff { |
|
| 38 | 38 | fn encode(&self, out: &mut impl Write) -> io::Result<()> { |
|
| 39 | 39 | for line in &self.header.raw { |
|
| 40 | 40 | writeln!(out, "{line}")?; |
|
| 41 | 41 | } |
|
| 42 | - | if !matches!(self.header.kind, FileKind::Binary | FileKind::ModeChanged) { |
|
| 42 | + | if !self.header.is_binary() && self.header.kind != FileKind::ModeChanged { |
|
| 43 | 43 | for hunk in &self.hunks { |
|
| 44 | 44 | hunk.encode(out)?; |
|
| 45 | 45 | } |
|
| 46 | 46 | } |
|
| 47 | 47 | Ok(()) |
a.txt
+0 -0
b.txt
+0 -0
image.png
+0 -0
crates/diff/src/viewer.rs
+6 -5
| 439 | 439 | ||
| 440 | 440 | impl From<&FileDiff> for FileSummary { |
|
| 441 | 441 | fn from(file: &FileDiff) -> Self { |
|
| 442 | 442 | let stats = file.stats(); |
|
| 443 | 443 | Self { |
|
| 444 | - | loc: (file.header.kind != FileKind::Binary).then(|| file.loc()), |
|
| 444 | + | loc: (!file.header.is_binary()).then(|| file.loc()), |
|
| 445 | 445 | stats, |
|
| 446 | 446 | } |
|
| 447 | 447 | } |
|
| 448 | 448 | } |
|
| 449 | 449 |
| 775 | 775 | mut read_blob: impl FnMut(Oid) -> Result<Vec<u8>, E>, |
|
| 776 | 776 | ) -> Result<(), E> { |
|
| 777 | 777 | let mut blobs: BTreeMap<Oid, usize> = BTreeMap::new(); |
|
| 778 | 778 | ||
| 779 | 779 | for file in &self.document.files { |
|
| 780 | - | if file.header.kind == FileKind::Binary |
|
| 780 | + | if file.header.is_binary() |
|
| 781 | 781 | || matches!( |
|
| 782 | 782 | (file.header.old_mode, file.header.new_mode), |
|
| 783 | 783 | (Some(FileMode::Commit | FileMode::Tree), _) |
|
| 784 | 784 | | (_, Some(FileMode::Commit | FileMode::Tree)) |
|
| 785 | 785 | ) |
| 827 | 827 | pub fn load_worktree_sources(&mut self, root: &Path) { |
|
| 828 | 828 | let Ok(root) = root.canonicalize() else { |
|
| 829 | 829 | return; |
|
| 830 | 830 | }; |
|
| 831 | 831 | for file in &self.document.files { |
|
| 832 | - | if file.header.kind == FileKind::Binary |
|
| 832 | + | if file.header.is_binary() |
|
| 833 | 833 | || file.header.kind == FileKind::Deleted |
|
| 834 | 834 | || file.header.new_oid.is_some() |
|
| 835 | 835 | { |
|
| 836 | 836 | continue; |
|
| 837 | 837 | } |
| 2720 | 2720 | } |
|
| 2721 | 2721 | ||
| 2722 | 2722 | impl FileDiff { |
|
| 2723 | 2723 | /// Returns whether this file card should render hunk rows. |
|
| 2724 | 2724 | fn has_body(&self) -> bool { |
|
| 2725 | - | !matches!(self.header.kind, FileKind::Binary | FileKind::ModeChanged) |
|
| 2725 | + | !self.header.is_binary() |
|
| 2726 | + | && self.header.kind != FileKind::ModeChanged |
|
| 2726 | 2727 | && !self.hunks.is_empty() |
|
| 2727 | 2728 | } |
|
| 2728 | 2729 | ||
| 2729 | 2730 | /// Returns the rendered row count for this file's card. |
|
| 2730 | 2731 | fn rendered_rows(&self, gaps: HunkGaps) -> usize { |
| 2979 | 2980 | use std::path::PathBuf; |
|
| 2980 | 2981 | use std::time::{SystemTime, UNIX_EPOCH}; |
|
| 2981 | 2982 | ||
| 2982 | 2983 | use super::*; |
|
| 2983 | 2984 | ||
| 2984 | - | use crate::Decode; |
|
| 2985 | 2985 | use crate::DiffDocument; |
|
| 2986 | + | use crate::{Decode, Encode}; |
|
| 2986 | 2987 | use rustik_repo::{DiffRepository, RepoError}; |
|
| 2987 | 2988 | use rustik_tui::{Modifiers, Point}; |
|
| 2988 | 2989 | ||
| 2989 | 2990 | /// Returns a left-click event for tests. |
|
| 2990 | 2991 | fn left_click(x: usize, y: usize, modifiers: Modifiers) -> Event<()> { |
a.rs
+0 -0
image.png
+0 -0
image.png\n{metadata}\nindex {old_oid:040x}..{new_oid:040x}\n{marker}\n"
+0 -0
image.png
+0 -0
a.rs
+0 -0
a.rs
+0 -0
b.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
b.rs
+0 -0
crates/diff/tests/golden/sticky_header_before_next_file_64x5.txt
+0 -0
src/a.rs
+0 -0
src/b.rs
+0 -0
README.md
+0 -0
src/app/a.rs
+0 -0
src/lib/b.rs
+0 -0
README.md
+0 -0
a.rs
+0 -0
b.rs
+0 -0
first.rs
+0 -0
second.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
b.rs
+0 -0
a.rs\n",
+0 -0
new.rs\n",
+0 -0