Skip to main content

rustik_diff/
decode.rs

1//! Unified diff decoder.
2//!
3//! Streaming [`Decode`] implementations for [`HunkHeader`], [`DiffLine`],
4//! [`Hunk`], [`FileDiff`], and [`DiffDocument`]. Hunks are read by their
5//! header counts (matching radicle-cli's `unified_diff` shape) so callers
6//! can pull a hunk at a time without arbitrary peek-ahead. File and
7//! document boundaries are detected with [`BufRead::fill_buf`] since
8//! rustik-diff has no libgit2 layer to do it for us.
9//!
10//! [`Decode::decode_str`] is a thin string entry point — it just wraps the
11//! input in [`io::Cursor`] and runs the streaming decoder.
12
13use std::io::{self, BufRead};
14use std::path::PathBuf;
15use std::str::FromStr;
16
17use crate::document::{
18    DecodeError, DiffDocument, DiffLine, DiffLineKind, FileDiff, FileHeader, FileId, FileKind,
19    FileMode, Hunk, HunkHeader, HunkId, Oid, RowId,
20};
21
22/// Maximum number of hexadecimal characters in a Git object id.
23const FULL_OBJECT_ID_HEX_LEN: usize = 64;
24
25/// Decodes a value from unified diff text.
26///
27/// The default `decode_str`/`decode_bytes` adapters route through `decode`
28/// so a single [`BufRead`]-based implementation covers all input shapes.
29pub trait Decode: Sized {
30    /// Reads and decodes a value from a buffered input stream.
31    fn decode(input: &mut impl BufRead) -> Result<Self, DecodeError>;
32
33    /// Reads and decodes a value, returning `Ok(None)` at clean end-of-input.
34    /// Anything else, including a partial value followed by EOF surfaces as
35    /// `Err`.
36    fn try_decode(input: &mut impl BufRead) -> Result<Option<Self>, DecodeError> {
37        match Self::decode(input) {
38            Ok(value) => Ok(Some(value)),
39            Err(error) if error.is_eof() => Ok(None),
40            Err(error) => Err(error),
41        }
42    }
43
44    /// Decodes a value from a UTF-8 string.
45    fn decode_str(input: &str) -> Result<Self, DecodeError> {
46        Self::decode_bytes(input.as_bytes())
47    }
48
49    /// Decodes a value from raw UTF-8 bytes.
50    fn decode_bytes(input: &[u8]) -> Result<Self, DecodeError> {
51        let mut reader = io::BufReader::new(input);
52        Self::decode(&mut reader)
53    }
54}
55
56impl Decode for HunkHeader {
57    /// Reads a hunk header line from the input stream.
58    fn decode(input: &mut impl BufRead) -> Result<Self, DecodeError> {
59        let mut stream = Stream::new(input);
60        let line = stream.read_line()?.ok_or_else(DecodeError::eof)?;
61        HunkHeader::decode_line(&line.text).map_err(|error| error.at_line(line.line_no))
62    }
63}
64
65impl Decode for DiffLine {
66    /// Reads one hunk-body line from the input stream.
67    ///
68    /// The returned [`DiffLine`] uses a zero row id; callers should
69    /// assign a stable ID.
70    fn decode(input: &mut impl BufRead) -> Result<Self, DecodeError> {
71        let mut stream = Stream::new(input);
72        let line = stream.read_line()?.ok_or_else(DecodeError::eof)?;
73        line.decode_diff_line(RowId::new(0))
74    }
75}
76
77impl Decode for Hunk {
78    /// Reads a complete hunk from the input stream.
79    fn decode(input: &mut impl BufRead) -> Result<Self, DecodeError> {
80        Stream::new(input).decode_hunk()
81    }
82}
83
84impl Decode for FileDiff {
85    /// Reads one file diff from the input stream.
86    fn decode(input: &mut impl BufRead) -> Result<Self, DecodeError> {
87        let mut stream = Stream::new(input);
88        if !stream.has_more()? {
89            return Err(DecodeError::eof());
90        }
91        stream.decode_file(FileId::new(0))
92    }
93}
94
95impl Decode for DiffDocument {
96    /// Reads a full diff document from the input stream.
97    fn decode(input: &mut impl BufRead) -> Result<Self, DecodeError> {
98        Stream::new(input).decode_document()
99    }
100
101    /// Decodes a diff document from raw bytes.
102    ///
103    /// Non-UTF-8 bytes are replaced with U+FFFD and ANSI escape sequences are
104    /// stripped before parsing, so output from colored Git pipes round-trips
105    /// without caller-side preprocessing.
106    fn decode_bytes(input: &[u8]) -> Result<Self, DecodeError> {
107        if !input.contains(&b'\x1b')
108            && let Ok(text) = std::str::from_utf8(input)
109        {
110            let mut reader = io::BufReader::new(text.as_bytes());
111            return Self::decode(&mut reader);
112        }
113
114        let text = String::from_utf8_lossy(input);
115        let stripped = rustik_tui::ansi::strip(&text);
116        let mut reader = io::BufReader::new(stripped.as_bytes());
117        Self::decode(&mut reader)
118    }
119}
120
121/// Streaming decoder over a buffered input stream.
122///
123/// Stores the reader, a line counter, and per-document ID counters used while
124/// building a [`DiffDocument`]. Each decode call (`Hunk` or `FileDiff`) starts
125/// from a fresh stream, so IDs begin at zero and callers that combine pieces
126/// must renumber as needed.
127struct Stream<R> {
128    /// Buffered source of unified diff text.
129    reader: R,
130    /// Number of physical input lines already consumed.
131    line_no: usize,
132    /// Hunk ID to assign to the next decoded hunk.
133    next_hunk_id: usize,
134    /// Row ID to assign to the next decoded hunk-body line.
135    next_row_id: usize,
136}
137
138/// One physical line read from the stream, with its 1-based line number.
139struct ReadLine {
140    /// 1-based physical input line number.
141    line_no: usize,
142    /// Line contents without trailing line endings.
143    text: String,
144}
145
146impl<R: BufRead> Stream<R> {
147    /// Creates a new streaming decoder.
148    fn new(reader: R) -> Self {
149        Self {
150            reader,
151            line_no: 0,
152            next_hunk_id: 0,
153            next_row_id: 0,
154        }
155    }
156
157    /// Decodes a full document from the remaining input.
158    fn decode_document(&mut self) -> Result<DiffDocument, DecodeError> {
159        let mut commit = Vec::new();
160        while self.has_more()? && !self.peek_starts_with(b"diff --git ")? {
161            let Some(line) = self.read_line()? else {
162                break;
163            };
164            commit.push(line.text);
165        }
166        let commit = commit.join("\n").parse().ok();
167        let mut files = Vec::new();
168
169        while self.has_more()? {
170            let id = FileId::new(files.len());
171            files.push(self.decode_file(id)?);
172        }
173        Ok(DiffDocument { commit, files })
174    }
175
176    /// Decodes one file diff from the remaining input.
177    fn decode_file(&mut self, id: FileId) -> Result<FileDiff, DecodeError> {
178        let line = self.read_line()?.ok_or_else(DecodeError::eof)?;
179        let rest = line
180            .text
181            .strip_prefix("diff --git ")
182            .ok_or_else(|| DecodeError::new(line.line_no, "missing 'diff --git' prefix"))?;
183        let (old, new) = if rest.starts_with('"') {
184            let mut escaped = false;
185            let end = rest
186                .char_indices()
187                .skip(1)
188                .find_map(|(index, ch)| match (ch, escaped) {
189                    (_, true) => {
190                        escaped = false;
191                        None
192                    }
193                    ('\\', false) => {
194                        escaped = true;
195                        None
196                    }
197                    ('"', false) => Some(index + 1),
198                    _ => None,
199                })
200                .ok_or_else(|| DecodeError::new(line.line_no, "unterminated quoted path"))?;
201            let new = rest[end..]
202                .strip_prefix(' ')
203                .ok_or_else(|| DecodeError::new(line.line_no, "diff header is missing paths"))?;
204
205            (&rest[..end], new)
206        } else if let Some(index) = rest.find(" b/") {
207            (&rest[..index], &rest[index + 1..])
208        } else if let Some(index) = rest.find(" \"") {
209            (&rest[..index], &rest[index + 1..])
210        } else {
211            rest.split_once(' ')
212                .ok_or_else(|| DecodeError::new(line.line_no, "diff header is missing paths"))?
213        };
214        let old_path = Some(decode_git_path(old, "a/"));
215        let new_path = Some(decode_git_path(new, "b/"));
216        let mut header = FileHeader {
217            kind: FileKind::Modified,
218            old_path,
219            new_path,
220            old_mode: None,
221            new_mode: None,
222            old_oid: None,
223            new_oid: None,
224            raw: vec![line.text.clone()],
225        };
226        let mut hunks = Vec::new();
227
228        loop {
229            match self.peek_byte()? {
230                None => break,
231                Some(b'@') => hunks.push(self.decode_hunk()?),
232                Some(b'd') if self.peek_starts_with(b"diff --git ")? => break,
233                Some(_) => {
234                    let Some(line) = self.read_line()? else {
235                        return Err(DecodeError::eof());
236                    };
237                    if let Some(mode) = line.text.strip_prefix("new file mode ") {
238                        header.kind = FileKind::Added;
239                        header.new_mode = FileMode::parse(mode);
240                    } else if let Some(mode) = line.text.strip_prefix("deleted file mode ") {
241                        header.kind = FileKind::Deleted;
242                        header.old_mode = FileMode::parse(mode);
243                    } else if let Some(mode) = line.text.strip_prefix("old mode ") {
244                        header.old_mode = FileMode::parse(mode);
245                    } else if let Some(mode) = line.text.strip_prefix("new mode ") {
246                        header.new_mode = FileMode::parse(mode);
247                        if header.kind == FileKind::Modified {
248                            header.kind = FileKind::ModeChanged;
249                        }
250                    } else if let Some(path) = line.text.strip_prefix("rename from ") {
251                        header.kind = FileKind::Renamed;
252                        header.old_path = Some(decode_git_path(path, ""));
253                    } else if let Some(path) = line.text.strip_prefix("rename to ") {
254                        header.kind = FileKind::Renamed;
255                        header.new_path = Some(decode_git_path(path, ""));
256                    } else if let Some(path) = line.text.strip_prefix("copy from ") {
257                        header.kind = FileKind::Copied;
258                        header.old_path = Some(decode_git_path(path, ""));
259                    } else if let Some(path) = line.text.strip_prefix("copy to ") {
260                        header.kind = FileKind::Copied;
261                        header.new_path = Some(decode_git_path(path, ""));
262                    } else if line.text.starts_with("Binary files ")
263                        || line.text == "GIT binary patch"
264                    {
265                        header.kind = FileKind::Binary;
266                    } else if let Some(rest) = line.text.strip_prefix("index ") {
267                        let mut parts = rest.split_whitespace();
268                        if let Some(oids) = parts.next()
269                            && let Some((old, new)) = oids.split_once("..")
270                        {
271                            header.old_oid = parse_index_oid(old, line.line_no, "old object id")?;
272                            header.new_oid = parse_index_oid(new, line.line_no, "new object id")?;
273                        }
274                        if let Some(mode) = parts.next()
275                            && let Some(parsed) = FileMode::parse(mode)
276                        {
277                            header.old_mode.get_or_insert(parsed);
278                            header.new_mode.get_or_insert(parsed);
279                        }
280                    }
281                    header.raw.push(line.text);
282                }
283            }
284        }
285        Ok(FileDiff { id, header, hunks })
286    }
287
288    /// Decodes one hunk from the remaining input.
289    fn decode_hunk(&mut self) -> Result<Hunk, DecodeError> {
290        let line = self.read_line()?.ok_or_else(DecodeError::eof)?;
291        let header =
292            HunkHeader::decode_line(&line.text).map_err(|error| error.at_line(line.line_no))?;
293        let id = HunkId::new(self.next_hunk_id);
294        self.next_hunk_id += 1;
295
296        let mut lines = Vec::new();
297        let mut old_consumed: u32 = 0;
298        let mut new_consumed: u32 = 0;
299
300        loop {
301            if old_consumed >= header.old_size && new_consumed >= header.new_size {
302                while self.peek_byte()? == Some(b'\\') {
303                    let Some(raw) = self.read_line()? else {
304                        return Err(DecodeError::eof());
305                    };
306                    lines.push(raw.decode_diff_line(self.take_row_id())?);
307                }
308                break;
309            }
310            let Some(raw) = self.read_line()? else {
311                return Err(DecodeError::new(
312                    0,
313                    format!(
314                        "expected '{}' old lines and '{}' new lines, but found '{}' and '{}'",
315                        header.old_size, header.new_size, old_consumed, new_consumed
316                    ),
317                ));
318            };
319            let row_id = self.take_row_id();
320            let parsed = raw.decode_diff_line(row_id)?;
321
322            match parsed.kind {
323                DiffLineKind::Context => {
324                    old_consumed += 1;
325                    new_consumed += 1;
326                }
327                DiffLineKind::Addition => new_consumed += 1,
328                DiffLineKind::Deletion => old_consumed += 1,
329                DiffLineKind::NoNewline => {}
330            }
331            lines.push(parsed);
332        }
333        Ok(Hunk { id, header, lines })
334    }
335
336    /// Returns whether unread input remains in the stream.
337    fn has_more(&mut self) -> Result<bool, DecodeError> {
338        Ok(!self.reader.fill_buf()?.is_empty())
339    }
340
341    /// Returns the next byte without consuming it.
342    fn peek_byte(&mut self) -> Result<Option<u8>, DecodeError> {
343        Ok(self.reader.fill_buf()?.first().copied())
344    }
345
346    /// Checks whether the next bytes match a required prefix.
347    fn peek_starts_with(&mut self, prefix: &[u8]) -> Result<bool, DecodeError> {
348        let buf = self.reader.fill_buf()?;
349        Ok(buf.len() >= prefix.len() && &buf[..prefix.len()] == prefix)
350    }
351
352    /// Reads the next line, stripping trailing line endings.
353    fn read_line(&mut self) -> Result<Option<ReadLine>, DecodeError> {
354        let mut buf = String::new();
355        let n = self.reader.read_line(&mut buf)?;
356        if n == 0 {
357            return Ok(None);
358        }
359        self.line_no += 1;
360
361        if buf.ends_with('\n') {
362            buf.pop();
363            if buf.ends_with('\r') {
364                buf.pop();
365            }
366        }
367        Ok(Some(ReadLine {
368            line_no: self.line_no,
369            text: buf,
370        }))
371    }
372
373    /// Returns the next row identifier.
374    fn take_row_id(&mut self) -> RowId {
375        let id = RowId::new(self.next_row_id);
376        self.next_row_id += 1;
377        id
378    }
379}
380
381impl HunkHeader {
382    /// Decodes one unified diff hunk header line like `@@ -A,B +C,D @@ optional text`.
383    fn decode_line(line: &str) -> Result<Self, DecodeError> {
384        let rest = line
385            .strip_prefix("@@ -")
386            .ok_or_else(|| DecodeError::new(0, "hunk header must start with '@@ -'"))?;
387        let (old, rest) = rest
388            .split_once(" +")
389            .ok_or_else(|| DecodeError::new(0, "hunk header is missing new-file range"))?;
390        let (new, text) = rest
391            .split_once(" @@")
392            .ok_or_else(|| DecodeError::new(0, "hunk header is missing closing '@@'"))?;
393        let (old_line_no, old_size) = Self::decode_range(old)?;
394        let (new_line_no, new_size) = Self::decode_range(new)?;
395
396        Ok(Self {
397            old_line_no,
398            old_size,
399            new_line_no,
400            new_size,
401            text: text.strip_prefix(' ').unwrap_or(text).to_owned(),
402        })
403    }
404
405    /// Decodes a unified diff range into a start and size pair.
406    fn decode_range(input: &str) -> Result<(u32, u32), DecodeError> {
407        let (line, size) = input.split_once(',').unwrap_or((input, "1"));
408        Ok((line.parse()?, size.parse()?))
409    }
410}
411
412impl ReadLine {
413    /// Decodes one hunk-body line into a [`DiffLine`] with the given id.
414    fn decode_diff_line(&self, id: RowId) -> Result<DiffLine, DecodeError> {
415        if self.text.starts_with('\\') {
416            if !self.text.starts_with("\\ No newline at end of file") {
417                return Err(DecodeError::new(
418                    self.line_no,
419                    format!("invalid hunk line: {}", self.text),
420                ));
421            }
422            return Ok(DiffLine {
423                id,
424                kind: DiffLineKind::NoNewline,
425                text: String::new(),
426            });
427        }
428        let mut chars = self.text.chars();
429        let marker = chars.next().ok_or_else(|| {
430            DecodeError::new(self.line_no, format!("invalid hunk line: {}", self.text))
431        })?;
432        let text = chars.as_str();
433        let kind = match marker {
434            ' ' => DiffLineKind::Context,
435            '+' => DiffLineKind::Addition,
436            '-' => DiffLineKind::Deletion,
437            _ => {
438                return Err(DecodeError::new(
439                    self.line_no,
440                    format!("invalid hunk line: {}", self.text),
441                ));
442            }
443        };
444        Ok(DiffLine {
445            id,
446            kind,
447            text: text.to_owned(),
448        })
449    }
450}
451
452/// Decodes Git's C-style path quoting and removes a diff-side prefix.
453fn decode_git_path(input: &str, prefix: &str) -> PathBuf {
454    let Some(input) = input
455        .strip_prefix('"')
456        .and_then(|input| input.strip_suffix('"'))
457    else {
458        return PathBuf::from(input.strip_prefix(prefix).unwrap_or(input));
459    };
460    let input = input.as_bytes();
461    let mut decoded = Vec::with_capacity(input.len());
462    let mut index = 0;
463
464    while index < input.len() {
465        if input[index] != b'\\' {
466            decoded.push(input[index]);
467            index += 1;
468            continue;
469        }
470        let Some(escape) = input.get(index + 1).copied() else {
471            decoded.push(b'\\');
472            break;
473        };
474
475        match escape {
476            b'a' => decoded.push(b'\x07'),
477            b'b' => decoded.push(b'\x08'),
478            b't' => decoded.push(b'\t'),
479            b'n' => decoded.push(b'\n'),
480            b'v' => decoded.push(b'\x0b'),
481            b'f' => decoded.push(b'\x0c'),
482            b'r' => decoded.push(b'\r'),
483            b'\\' | b'"' => decoded.push(escape),
484            b'0'..=b'7' => {
485                let mut value = 0;
486                let mut digits = 0;
487                while digits < 3
488                    && let Some(digit @ b'0'..=b'7') = input.get(index + 1 + digits).copied()
489                {
490                    value = value * 8 + (digit - b'0');
491                    digits += 1;
492                }
493                decoded.push(value);
494                index += digits - 1;
495            }
496            _ => {
497                decoded.push(b'\\');
498                decoded.push(escape);
499            }
500        }
501        index += 2;
502    }
503
504    if decoded.starts_with(prefix.as_bytes()) {
505        drop(decoded.drain(..prefix.len()));
506    }
507
508    #[cfg(unix)]
509    {
510        use std::os::unix::ffi::OsStringExt as _;
511
512        PathBuf::from(std::ffi::OsString::from_vec(decoded))
513    }
514    #[cfg(not(unix))]
515    {
516        PathBuf::from(String::from_utf8_lossy(&decoded).into_owned())
517    }
518}
519
520/// Parses a full object id and accepts abbreviated hex ids as unavailable.
521fn parse_index_oid(
522    input: &str,
523    line_no: usize,
524    description: &'static str,
525) -> Result<Option<Oid>, DecodeError> {
526    match Oid::from_str(input) {
527        Ok(oid) => Ok(Some(oid)),
528        Err(_) if is_abbreviated_oid(input) => Ok(None),
529        Err(_) => Err(DecodeError::new(line_no, format!("invalid {description}"))),
530    }
531}
532
533/// Returns whether `input` is a syntactically valid abbreviated object id.
534fn is_abbreviated_oid(input: &str) -> bool {
535    !input.is_empty()
536        && input.len() < FULL_OBJECT_ID_HEX_LEN
537        && input.bytes().all(|byte| byte.is_ascii_hexdigit())
538}
539
540#[cfg(test)]
541mod tests {
542    use super::*;
543
544    #[test]
545    fn parses_preamble_and_modified_hunk() {
546        let doc = DiffDocument::decode_str(
547            "\
548commit 1234567890abcdef1234567890abcdef12345678
549Author: A
550
551diff --git a/a.rs b/a.rs
552index 1111111111111111111111111111111111111111..2222222222222222222222222222222222222222 100644
553--- a/a.rs
554+++ b/a.rs
555@@ -1 +1 @@
556-old
557+new
558\\ No newline at end of file
559",
560        )
561        .expect("diff parses");
562
563        let commit = doc.commit.as_ref().expect("preamble parsed");
564        assert_eq!(
565            commit.hash.to_string(),
566            "1234567890abcdef1234567890abcdef12345678"
567        );
568        assert_eq!(commit.header("Author"), Some("A"));
569        assert!(commit.message.is_empty());
570        assert_eq!(doc.files.len(), 1);
571        assert_eq!(doc.files[0].header.kind, FileKind::Modified);
572        assert_eq!(
573            doc.files[0].header.old_oid.map(|oid| oid.to_string()),
574            Some("1111111111111111111111111111111111111111".to_owned())
575        );
576        assert_eq!(
577            doc.files[0].header.new_oid.map(|oid| oid.to_string()),
578            Some("2222222222222222222222222222222222222222".to_owned())
579        );
580        assert_eq!(doc.files[0].hunks[0].header.old_line_no, 1);
581        assert_eq!(doc.files[0].hunks[0].lines[0].kind, DiffLineKind::Deletion);
582        assert_eq!(doc.files[0].hunks[0].lines[1].kind, DiffLineKind::Addition);
583        assert_eq!(doc.files[0].hunks[0].lines[2].kind, DiffLineKind::NoNewline);
584    }
585
586    #[test]
587    fn parses_added_deleted_renamed_copied_mode_and_binary_files() {
588        let doc = DiffDocument::decode_str(
589            "\
590diff --git a/new.rs b/new.rs
591new file mode 100644
592index 0000000000000000000000000000000000000000..1111111111111111111111111111111111111111
593--- /dev/null
594+++ b/new.rs
595@@ -0,0 +1 @@
596+new
597diff --git a/old.rs b/old.rs
598deleted file mode 100644
599index 1111111111111111111111111111111111111111..0000000000000000000000000000000000000000
600--- a/old.rs
601+++ /dev/null
602@@ -1 +0,0 @@
603-old
604diff --git a/from.rs b/to.rs
605similarity index 100%
606rename from from.rs
607rename to to.rs
608diff --git a/copy.rs b/copy2.rs
609similarity index 100%
610copy from copy.rs
611copy to copy2.rs
612diff --git a/mode.sh b/mode.sh
613old mode 100644
614new mode 100755
615diff --git a/image.png b/image.png
616index 1111111111111111111111111111111111111111..2222222222222222222222222222222222222222 100644
617Binary files a/image.png and b/image.png differ
618",
619        )
620        .expect("diff parses");
621
622        let kinds = doc
623            .files
624            .iter()
625            .map(|file| file.header.kind)
626            .collect::<Vec<_>>();
627        assert_eq!(
628            kinds,
629            vec![
630                FileKind::Added,
631                FileKind::Deleted,
632                FileKind::Renamed,
633                FileKind::Copied,
634                FileKind::ModeChanged,
635                FileKind::Binary,
636            ]
637        );
638        assert_eq!(doc.files[2].header.old_path, Some(PathBuf::from("from.rs")));
639        assert_eq!(doc.files[2].header.new_path, Some(PathBuf::from("to.rs")));
640        assert_eq!(doc.files[4].header.old_mode, Some(FileMode::Blob));
641        assert_eq!(doc.files[4].header.new_mode, Some(FileMode::BlobExecutable));
642    }
643
644    #[test]
645    fn decodes_git_quoted_paths() {
646        let doc = DiffDocument::decode_str(
647            r#"diff --git "a/modified caf\303\251\t\"slash\\.rs" "b/modified caf\303\251\t\"slash\\.rs"
648index 438999b..4c0acd2 100644
649--- "a/modified caf\303\251\t\"slash\\.rs"	
650+++ "b/modified caf\303\251\t\"slash\\.rs"	
651@@ -1,2 +1,2 @@
652-old
653+new
654 keep
655diff --git a/ordinary.rs b/ordinary.rs
656index 3367afd..3e75765 100644
657--- a/ordinary.rs
658+++ b/ordinary.rs
659@@ -1 +1 @@
660-old
661+new
662diff --git "a/rename from caf\303\251\t\"slash\\.rs" "b/rename to caf\303\251\t\"slash\\.rs"
663similarity index 100%
664rename from "rename from caf\303\251\t\"slash\\.rs"
665rename to "rename to caf\303\251\t\"slash\\.rs"
666"#,
667        )
668        .unwrap();
669
670        assert_eq!(
671            doc.files[0].header.old_path,
672            Some(PathBuf::from("modified café\t\"slash\\.rs"))
673        );
674        assert_eq!(
675            doc.files[0].header.new_path,
676            Some(PathBuf::from("modified café\t\"slash\\.rs"))
677        );
678        assert_eq!(
679            doc.files[1].header.old_path,
680            Some(PathBuf::from("ordinary.rs"))
681        );
682        assert_eq!(
683            doc.files[1].header.new_path,
684            Some(PathBuf::from("ordinary.rs"))
685        );
686        assert_eq!(
687            doc.files[2].header.old_path,
688            Some(PathBuf::from("rename from café\t\"slash\\.rs"))
689        );
690        assert_eq!(
691            doc.files[2].header.new_path,
692            Some(PathBuf::from("rename to café\t\"slash\\.rs"))
693        );
694    }
695
696    #[cfg(unix)]
697    #[test]
698    fn preserves_non_utf8_bytes_in_git_quoted_paths() {
699        use std::os::unix::ffi::OsStrExt as _;
700
701        let doc = DiffDocument::decode_str(
702            r#"diff --git "a/non-utf8-\377.rs" "b/non-utf8-\377.rs"
703"#,
704        )
705        .expect("diff parses");
706
707        assert_eq!(
708            doc.files[0]
709                .header
710                .old_path
711                .as_deref()
712                .expect("old path")
713                .as_os_str()
714                .as_bytes(),
715            b"non-utf8-\xff.rs"
716        );
717        assert_eq!(
718            doc.files[0]
719                .header
720                .new_path
721                .as_deref()
722                .expect("new path")
723                .as_os_str()
724                .as_bytes(),
725            b"non-utf8-\xff.rs"
726        );
727    }
728
729    #[test]
730    fn parses_hunk_header_default_sizes() {
731        let header = HunkHeader::decode_line("@@ -7 +8 @@ fn main").expect("header parses");
732
733        assert_eq!(header.old_line_no, 7);
734        assert_eq!(header.old_size, 1);
735        assert_eq!(header.new_line_no, 8);
736        assert_eq!(header.new_size, 1);
737        assert_eq!(header.text, "fn main");
738    }
739
740    #[test]
741    fn decode_via_bufread_matches_zero_copy_parse() {
742        let input = "\
743diff --git a/a b/a
744@@ -1 +1 @@
745-old
746+new
747";
748        let from_str = DiffDocument::decode_str(input).expect("zero-copy parse");
749        let from_bytes = DiffDocument::decode_bytes(input.as_bytes()).expect("bufread decode");
750
751        assert_eq!(from_str, from_bytes);
752    }
753
754    #[test]
755    fn reports_invalid_hunk_lines() {
756        let error = DiffDocument::decode_str(
757            "\
758diff --git a/a b/a
759@@ -1 +1 @@
760?bad
761",
762        )
763        .expect_err("invalid hunk line is rejected");
764
765        assert_eq!(error.line, 3);
766        assert!(error.message.contains("invalid hunk line"));
767        assert!(!error.is_eof());
768    }
769
770    #[test]
771    fn reports_invalid_index_object_ids() {
772        let error = DiffDocument::decode_str(
773            "\
774diff --git a/a b/a
775index not-hex..2222222222222222222222222222222222222222 100644
776@@ -1 +1 @@
777-old
778+new
779",
780        )
781        .expect_err("invalid object id should be rejected");
782
783        assert_eq!(error.line, 2);
784        assert_eq!(error.message, "invalid old object id");
785    }
786
787    #[test]
788    fn accepts_abbreviated_index_object_ids() {
789        let doc = DiffDocument::decode_str(
790            "\
791diff --git a/a b/a
792index 1234567..89abcde 100644
793@@ -1 +1 @@
794-old
795+new
796",
797        )
798        .unwrap();
799
800        assert_eq!(doc.files.len(), 1);
801        assert_eq!(doc.files[0].header.old_oid, None);
802        assert_eq!(doc.files[0].header.new_oid, None);
803        assert_eq!(doc.files[0].header.old_mode, Some(FileMode::Blob));
804        assert_eq!(doc.files[0].header.new_mode, Some(FileMode::Blob));
805        assert_eq!(doc.stats().insertions, 1);
806        assert_eq!(doc.stats().deletions, 1);
807    }
808
809    #[test]
810    fn try_decode_hunks_one_at_a_time() {
811        let input = "\
812@@ -1 +1 @@
813-old
814+new
815@@ -10,2 +10,2 @@ ctx
816 a
817-b
818+c
819";
820        let mut reader = io::BufReader::new(input.as_bytes());
821
822        let first = Hunk::try_decode(&mut reader)
823            .expect("decodes")
824            .expect("first hunk present");
825        assert_eq!(first.header.old_line_no, 1);
826        assert_eq!(first.lines.len(), 2);
827
828        let second = Hunk::try_decode(&mut reader)
829            .expect("decodes")
830            .expect("second hunk present");
831        assert_eq!(second.header.old_line_no, 10);
832        assert_eq!(second.header.text, "ctx");
833        assert_eq!(second.lines.len(), 3);
834
835        assert!(
836            Hunk::try_decode(&mut reader).expect("decodes").is_none(),
837            "stream should be drained",
838        );
839    }
840
841    #[test]
842    fn try_decode_returns_none_on_empty_input() {
843        let mut empty = io::BufReader::new(&[][..]);
844        assert!(FileDiff::try_decode(&mut empty).expect("eof").is_none());
845        let mut empty = io::BufReader::new(&[][..]);
846        assert!(Hunk::try_decode(&mut empty).expect("eof").is_none());
847        // An empty stream is a valid (empty) document.
848        let mut empty = io::BufReader::new(&[][..]);
849        assert!(
850            DiffDocument::decode(&mut empty)
851                .expect("decodes empty")
852                .is_empty()
853        );
854    }
855
856    #[test]
857    fn try_decode_distinguishes_eof_from_syntax_error() {
858        let mut bad = io::BufReader::new(&b"@@ not a hunk header\n"[..]);
859        let error = Hunk::try_decode(&mut bad).expect_err("syntax error surfaces");
860        assert!(!error.is_eof());
861    }
862
863    #[test]
864    fn streaming_decode_matches_zero_copy_parse_for_full_document() {
865        let input = "\
866commit 1234567890abcdef1234567890abcdef12345678
867
868diff --git a/a.rs b/a.rs
869index 1111111111111111111111111111111111111111..2222222222222222222222222222222222222222 100644
870--- a/a.rs
871+++ b/a.rs
872@@ -1 +1 @@
873-old
874+new
875diff --git a/b.rs b/b.rs
876new file mode 100644
877index 0000000000000000000000000000000000000000..3333333333333333333333333333333333333333
878--- /dev/null
879+++ b/b.rs
880@@ -0,0 +1 @@
881+only
882";
883        let zero_copy = DiffDocument::decode_str(input).expect("zero-copy parses");
884        let streamed = {
885            let mut reader = io::BufReader::new(input.as_bytes());
886            DiffDocument::decode(&mut reader).expect("stream decodes")
887        };
888
889        assert_eq!(zero_copy, streamed);
890    }
891
892    #[test]
893    fn raw_bytes_strip_ansi_before_decoding_document() {
894        let input = b"\
895\x1b[33mdiff --git a/a.rs b/a.rs\x1b[m
896\x1b[33mindex 1111111111111111111111111111111111111111..2222222222222222222222222222222222222222 100644\x1b[m
897\x1b[33m--- a/a.rs\x1b[m
898\x1b[33m+++ b/a.rs\x1b[m
899\x1b[35m@@ -1 +1 @@\x1b[m
900\x1b[31m-old\x1b[m
901\x1b[32m+new\x1b[m
902";
903        let document = DiffDocument::decode_bytes(input).unwrap();
904
905        assert_eq!(document.files.len(), 1);
906        assert_eq!(document.stats().insertions, 1);
907        assert_eq!(document.stats().deletions, 1);
908    }
909
910    #[test]
911    fn hunk_body_uses_header_counts_to_terminate() {
912        // Body is exactly old_size + new_size = 1 + 2 = 3 line-types.
913        // After the body, a `\ No newline...` marker must still be consumed.
914        let input = "\
915@@ -1 +1,2 @@
916-old
917+new
918+extra
919\\ No newline at end of file
920";
921        let mut reader = io::BufReader::new(input.as_bytes());
922        let hunk = Hunk::decode(&mut reader).expect("decodes");
923
924        assert_eq!(hunk.lines.len(), 4);
925        assert_eq!(hunk.lines[3].kind, DiffLineKind::NoNewline);
926        assert!(reader.fill_buf().expect("fills").is_empty());
927    }
928}