brain: Accept binary files as one hunk
afe4450fbaff988b9a545ca3f679ff921b0b6ecb
Binary files can now be selected and accepted from the collapsed file card. Represent each binary file as one non-rendered review hunk, and apply its complete preserved patch when accepted. Keep binary diff encoding byte-for-byte stable by excluding the review hunk from serialized output. Assisted-by: Codex:gpt-5.6-sol
1 parent
ec3a646a
crates/brain/src/backend.rs
+36 -17
| 229 | 229 | } |
|
| 230 | 230 | ||
| 231 | 231 | impl FilePatches { |
|
| 232 | 232 | /// Builds combined and per-hunk patches for `hunks` of `file`. |
|
| 233 | 233 | pub fn new(file: &FileDiff, hunks: Vec<Hunk>) -> Result<Self, Error> { |
|
| 234 | + | if file.header.kind == FileKind::Binary { |
|
| 235 | + | let patch = DiffDocument::for_hunks(file, []).to_unified_string()?; |
|
| 236 | + | return Ok(Self { |
|
| 237 | + | combined: patch.clone(), |
|
| 238 | + | hunks: vec![patch], |
|
| 239 | + | }); |
|
| 240 | + | } |
|
| 234 | 241 | let individual = hunks |
|
| 235 | 242 | .iter() |
|
| 236 | 243 | .map(|hunk| DiffDocument::for_hunks(file, [hunk.clone()]).to_unified_string()) |
|
| 237 | 244 | .collect::<Result<Vec<_>, _>>()?; |
|
| 238 | 245 | let combined = if file.header.kind == FileKind::Added && hunks.len() > 1 { |
| 1082 | 1089 | pub fn hunk_identities( |
|
| 1083 | 1090 | &self, |
|
| 1084 | 1091 | document: &DiffDocument, |
|
| 1085 | 1092 | commit: Oid, |
|
| 1086 | 1093 | ) -> Result<BTreeMap<HunkId, AcceptedHunk>, Error> { |
|
| 1087 | - | let hunks = document |
|
| 1088 | - | .files |
|
| 1089 | - | .iter() |
|
| 1090 | - | .flat_map(|file| &file.hunks) |
|
| 1091 | - | .map(|hunk| (hunk.id, hunk.stats())) |
|
| 1092 | - | .collect::<BTreeMap<_, _>>(); |
|
| 1093 | - | let commit_lines = hunks.values().try_fold(0u32, |total, stats| { |
|
| 1094 | - | let lines = u32::try_from(stats.additions + stats.deletions) |
|
| 1095 | - | .map_err(|_| Error::Git("hunk contains too many changed lines".to_owned()))?; |
|
| 1096 | - | total |
|
| 1097 | - | .checked_add(lines) |
|
| 1098 | - | .ok_or_else(|| Error::Git("commit contains too many changed lines".to_owned())) |
|
| 1099 | - | })?; |
|
| 1094 | + | let mut hunks = BTreeMap::new(); |
|
| 1095 | + | let mut commit_lines = 0u32; |
|
| 1096 | + | for file in &document.files { |
|
| 1097 | + | for hunk in &file.hunks { |
|
| 1098 | + | let stats = hunk.stats(); |
|
| 1099 | + | let changed = stats.additions + stats.deletions; |
|
| 1100 | + | let changed = usize::from(file.header.kind == FileKind::Binary).max(changed); |
|
| 1101 | + | let lines = u32::try_from(changed) |
|
| 1102 | + | .map_err(|_| Error::Git("hunk contains too many changed lines".to_owned()))?; |
|
| 1103 | + | commit_lines = commit_lines.checked_add(lines).ok_or_else(|| { |
|
| 1104 | + | Error::Git("commit contains too many changed lines".to_owned()) |
|
| 1105 | + | })?; |
|
| 1106 | + | hunks.insert(hunk.id, lines); |
|
| 1107 | + | } |
|
| 1108 | + | } |
|
| 1100 | 1109 | let (ids, patches): (Vec<_>, Vec<_>) = hunk_identity_patches(document)?.into_iter().unzip(); |
|
| 1101 | 1110 | let patch_ids = self.repository.patch_ids(&patches)?; |
|
| 1102 | 1111 | let mut seen = BTreeMap::<PatchId, u32>::new(); |
|
| 1103 | 1112 | let mut identities = BTreeMap::new(); |
|
| 1104 | 1113 | ||
| 1105 | 1114 | for (id, patch_id) in ids.into_iter().zip(patch_ids) { |
|
| 1106 | 1115 | let ordinal = seen.entry(patch_id.clone()).or_default(); |
|
| 1107 | - | let stats = hunks |
|
| 1116 | + | let lines = hunks |
|
| 1108 | 1117 | .get(&id) |
|
| 1118 | + | .copied() |
|
| 1109 | 1119 | .ok_or_else(|| Error::Git(format!("hunk {id:?} has no diff statistics")))?; |
|
| 1110 | - | let lines = u32::try_from(stats.additions + stats.deletions) |
|
| 1111 | - | .map_err(|_| Error::Git("hunk contains too many changed lines".to_owned()))?; |
|
| 1112 | 1120 | let identity = AcceptedHunk { |
|
| 1113 | 1121 | commit, |
|
| 1114 | 1122 | patch_id, |
|
| 1115 | 1123 | ordinal: *ordinal, |
|
| 1116 | 1124 | lines, |
| 1486 | 1494 | stats |
|
| 1487 | 1495 | } |
|
| 1488 | 1496 | ||
| 1489 | 1497 | /// Returns the patch text used to identify `hunk` across base movement. |
|
| 1490 | 1498 | fn hunk_identity_patch(file: &FileDiff, hunk: &Hunk) -> Result<String, Error> { |
|
| 1491 | - | Ok(DiffDocument::for_hunks(file, [hunk.identity()]).to_unified_string()?) |
|
| 1499 | + | let hunks = (file.header.kind != FileKind::Binary).then(|| hunk.identity()); |
|
| 1500 | + | Ok(DiffDocument::for_hunks(file, hunks).to_unified_string()?) |
|
| 1492 | 1501 | } |
|
| 1493 | 1502 | ||
| 1494 | 1503 | /// Returns each hunk's id and identity patch text, in document order. |
|
| 1495 | 1504 | fn hunk_identity_patches(document: &DiffDocument) -> Result<Vec<(HunkId, String)>, Error> { |
|
| 1496 | 1505 | let mut patches = Vec::new(); |
| 1753 | 1762 | .current_dir(&self.path) |
|
| 1754 | 1763 | .status() |
|
| 1755 | 1764 | .unwrap(); |
|
| 1756 | 1765 | assert!(status.success()); |
|
| 1757 | 1766 | } |
|
| 1767 | + | /// Runs a Git command and returns its standard output. |
|
| 1768 | + | fn git_output(&self, args: &[&str]) -> Vec<u8> { |
|
| 1769 | + | let output = Command::new("git") |
|
| 1770 | + | .args(args) |
|
| 1771 | + | .current_dir(&self.path) |
|
| 1772 | + | .output() |
|
| 1773 | + | .unwrap(); |
|
| 1774 | + | assert!(output.status.success()); |
|
| 1775 | + | output.stdout |
|
| 1776 | + | } |
|
| 1758 | 1777 | /// Runs a Jujutsu command and requires success. |
|
| 1759 | 1778 | fn jj(&self, args: &[&str]) { |
|
| 1760 | 1779 | let status = Command::new("jj") |
|
| 1761 | 1780 | .args(args) |
|
| 1762 | 1781 | .current_dir(&self.path) |
f
+0 -0
new.rs
+0 -0
image.png
+0 -0
new.txt
+0 -0
crates/diff/src/decode.rs
+0 -0
{
+0 -0
+0 -0
=> break,
+0 -0
a.rs
+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\x1b[m
+0 -0
crates/diff/src/encode.rs
+5 -3
| 5 | 5 | //! byte-for-byte for any document the decoder accepted, including the |
|
| 6 | 6 | //! preamble and the raw header lines preserved on each [`crate::FileHeader`]. |
|
| 7 | 7 | ||
| 8 | 8 | use std::io::{self, Write}; |
|
| 9 | 9 | ||
| 10 | - | use crate::document::{DiffDocument, DiffLine, DiffLineKind, FileDiff, Hunk, HunkHeader}; |
|
| 10 | + | use crate::document::{DiffDocument, DiffLine, DiffLineKind, FileDiff, FileKind, Hunk, HunkHeader}; |
|
| 11 | 11 | ||
| 12 | 12 | /// Encodes a value as unified-diff text. |
|
| 13 | 13 | pub trait Encode { |
|
| 14 | 14 | /// Writes the unified-diff representation to `out`. |
|
| 15 | 15 | fn encode(&self, out: &mut impl Write) -> io::Result<()>; |
| 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 | - | for hunk in &self.hunks { |
|
| 43 | - | hunk.encode(out)?; |
|
| 42 | + | if self.header.kind != FileKind::Binary { |
|
| 43 | + | for hunk in &self.hunks { |
|
| 44 | + | hunk.encode(out)?; |
|
| 45 | + | } |
|
| 44 | 46 | } |
|
| 45 | 47 | Ok(()) |
|
| 46 | 48 | } |
|
| 47 | 49 | } |
|
| 48 | 50 |
a.txt
+0 -0
b.txt
+0 -0
image.png
+0 -0
crates/diff/src/viewer.rs
+8 -4
| 2150 | 2150 | row: indexed.start, |
|
| 2151 | 2151 | height: 1, |
|
| 2152 | 2152 | file_start: indexed.start, |
|
| 2153 | 2153 | }); |
|
| 2154 | 2154 | } |
|
| 2155 | - | if filter == FocusFilter::HunkNavigation && file.hunks.is_empty() { |
|
| 2155 | + | if filter == FocusFilter::HunkNavigation && !file.has_body() { |
|
| 2156 | + | let focus = file |
|
| 2157 | + | .hunks |
|
| 2158 | + | .first() |
|
| 2159 | + | .map_or(DiffFocus::File(file.id), |hunk| DiffFocus::Hunk(hunk.id)); |
|
| 2156 | 2160 | targets.push(FocusTarget { |
|
| 2157 | - | focus: DiffFocus::File(file.id), |
|
| 2161 | + | focus, |
|
| 2158 | 2162 | row: indexed.start + FILE_HEADER_ROW, |
|
| 2159 | 2163 | height: 1, |
|
| 2160 | 2164 | file_start: indexed.start, |
|
| 2161 | 2165 | }); |
|
| 2162 | 2166 | } |
|
| 2163 | - | if filter == FocusFilter::HunkNavigation { |
|
| 2167 | + | if filter == FocusFilter::HunkNavigation && file.has_body() { |
|
| 2164 | 2168 | for (hunk, local) in file.hunk_body_rows(self.hunk_gaps(file.id)) { |
|
| 2165 | 2169 | let leading = hunk |
|
| 2166 | 2170 | .lines |
|
| 2167 | 2171 | .iter() |
|
| 2168 | 2172 | .take_while(|line| self.expanded_context.contains(&line.id)) |
| 2584 | 2588 | } |
|
| 2585 | 2589 | ||
| 2586 | 2590 | impl FileDiff { |
|
| 2587 | 2591 | /// Returns whether this file card should render hunk rows. |
|
| 2588 | 2592 | fn has_body(&self) -> bool { |
|
| 2589 | - | !self.hunks.is_empty() |
|
| 2593 | + | self.header.kind != FileKind::Binary && !self.hunks.is_empty() |
|
| 2590 | 2594 | } |
|
| 2591 | 2595 | ||
| 2592 | 2596 | /// Returns the rendered row count for this file's card. |
|
| 2593 | 2597 | fn rendered_rows(&self, gaps: HunkGaps) -> usize { |
|
| 2594 | 2598 | if !self.has_body() { |
a.rs
+0 -0
image.png
+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