Skip to main content

oak_core/
diff.rs

1use similar::{ChangeTag, TextDiff};
2
3/// Default number of context lines to show around changes
4pub const DEFAULT_CONTEXT_LINES: usize = 3;
5
6/// Maximum byte length for either side of a text diff. Past this, content is
7/// treated as non-text: the line differ's cost grows superlinearly and the
8/// result is unreadable, so callers emit a notice instead of diffing. Mirrors
9/// the spirit of git's `core.bigFileThreshold`.
10pub const MAX_TEXT_DIFF_BYTES: usize = 16 * 1024 * 1024;
11
12/// A file is treated as binary if it contains a NUL byte — the same heuristic
13/// git uses to decide whether to print a textual diff or "Binary files differ".
14pub fn is_binary(bytes: &[u8]) -> bool {
15    bytes.contains(&0)
16}
17
18/// Returns a stand-in notice when two blobs should not be rendered as a text
19/// diff — either side is binary, or either exceeds [`MAX_TEXT_DIFF_BYTES`].
20/// `None` means it's safe (and worthwhile) to run [`FileDiff::new`].
21///
22/// Every diff-rendering path should gate on this *before* reading blobs into
23/// strings and calling the line differ: a NUL-laden or multi-megabyte blob (an
24/// asset, a packfile, a binary in an import commit) makes `TextDiff` both
25/// useless and pathologically slow, which is what otherwise freezes `oak log`
26/// on large repos.
27pub fn binary_or_large_notice(path: &str, old_bytes: &[u8], new_bytes: &[u8]) -> Option<String> {
28    binary_or_oversize_notice(path, old_bytes, new_bytes, Some(MAX_TEXT_DIFF_BYTES))
29}
30
31/// Like [`binary_or_large_notice`], but callers may pass `None` to force a
32/// text diff for oversized blobs while preserving the NUL-byte binary guard.
33pub fn binary_or_oversize_notice(
34    path: &str,
35    old_bytes: &[u8],
36    new_bytes: &[u8],
37    max_text_bytes: Option<usize>,
38) -> Option<String> {
39    if is_binary(old_bytes) || is_binary(new_bytes) {
40        return Some(format!("Binary files a/{path} and b/{path} differ"));
41    }
42    if max_text_bytes.is_some_and(|limit| old_bytes.len() > limit || new_bytes.len() > limit) {
43        return Some(format!(
44            "Binary files a/{path} and b/{path} differ ({} -> {} bytes; text diff omitted, use --text to force)",
45            old_bytes.len(),
46            new_bytes.len()
47        ));
48    }
49    None
50}
51
52/// Byte ranges that differ between a paired removed/added line, computed
53/// with a word-level diff: `(ranges_in_old, ranges_in_new)`.
54///
55/// Powers word-level emphasis — the diff browser's intra-line highlights
56/// and `oak diff --word-diff`. Returns `None` when the pair is mostly
57/// different (similarity below 0.4): highlighting nearly everything reads
58/// worse than a plain replaced line.
59/// Byte ranges of intra-line changes, one list per side of a paired line.
60pub type InlineRanges = Vec<std::ops::Range<usize>>;
61
62pub fn inline_diff_ranges(old: &str, new: &str) -> Option<(InlineRanges, InlineRanges)> {
63    // Word tokens (whitespace runs are their own tokens, so values
64    // concatenate back to the exact input and byte offsets stay honest).
65    let diff = TextDiff::from_words(old, new);
66    if diff.ratio() < 0.4 {
67        return None;
68    }
69    let mut old_ranges: InlineRanges = Vec::new();
70    let mut new_ranges: InlineRanges = Vec::new();
71    let (mut old_pos, mut new_pos) = (0usize, 0usize);
72    for change in diff.iter_all_changes() {
73        let len = change.value().len();
74        match change.tag() {
75            ChangeTag::Equal => {
76                old_pos += len;
77                new_pos += len;
78            }
79            ChangeTag::Delete => {
80                match old_ranges.last_mut() {
81                    // Coalesce adjacent changed chars into one range.
82                    Some(last) if last.end == old_pos => last.end = old_pos + len,
83                    _ => old_ranges.push(old_pos..old_pos + len),
84                }
85                old_pos += len;
86            }
87            ChangeTag::Insert => {
88                match new_ranges.last_mut() {
89                    Some(last) if last.end == new_pos => last.end = new_pos + len,
90                    _ => new_ranges.push(new_pos..new_pos + len),
91                }
92                new_pos += len;
93            }
94        }
95    }
96    Some((old_ranges, new_ranges))
97}
98
99/// Represents a line change in a diff
100#[derive(Debug, Clone, PartialEq, Eq)]
101pub enum DiffLine {
102    /// Context line (unchanged)
103    Context(String),
104    /// Added line
105    Added(String),
106    /// Removed line
107    Removed(String),
108}
109
110/// The unified-diff marker that follows a line whose side of the file ended
111/// without a newline terminator. Applicators (`git apply`, `patch`) need it
112/// to reproduce the exact final bytes; without it they either reject the
113/// hunk or silently add a newline the intended content never had.
114pub const NO_NEWLINE_MARKER: &str = "\\ No newline at end of file";
115
116/// Split text into lines that keep their `\n` terminator, splitting on `\n`
117/// only. `similar`'s own line tokenizer also ends a line at a lone `\r`,
118/// which git does not: a file `a\rb` is one line to `git apply`, and a side
119/// ending in `c\r` has no newline. Only the final line can lack a `\n`.
120fn split_lines_keep_newline(text: &str) -> Vec<&str> {
121    let mut lines = Vec::new();
122    let mut rest = text;
123    while let Some(idx) = rest.find('\n') {
124        lines.push(&rest[..=idx]);
125        rest = &rest[idx + 1..];
126    }
127    if !rest.is_empty() {
128        lines.push(rest);
129    }
130    lines
131}
132
133/// The `--- `/`+++ ` file-header pair of a unified diff for `path`.
134///
135/// A side that does not exist is `/dev/null` — `--- /dev/null` for an added
136/// file, `+++ /dev/null` for a deleted one — which is how `git apply` and
137/// `patch` tell a creation or deletion from an edit that empties a file.
138/// Without it, applying a deletion leaves a 0-byte file behind.
139pub fn unified_side_headers(path: &str, old_exists: bool, new_exists: bool) -> [String; 2] {
140    [
141        if old_exists {
142            format!("--- a/{path}")
143        } else {
144            "--- /dev/null".to_string()
145        },
146        if new_exists {
147            format!("+++ b/{path}")
148        } else {
149            "+++ /dev/null".to_string()
150        },
151    ]
152}
153
154/// Result of diffing two files
155///
156/// Construct with [`FileDiff::new`]; the struct is `#[non_exhaustive]` so
157/// fields can be added without breaking downstream crates again.
158#[derive(Debug, Clone)]
159#[non_exhaustive]
160pub struct FileDiff {
161    pub path: String,
162    pub lines: Vec<DiffLine>,
163    pub has_changes: bool,
164    /// Indices into `lines` whose content was the final line of its side and
165    /// carried no newline terminator (at most one per side, so at most two).
166    /// The line strings themselves are always terminator-free, so this is the
167    /// only record of that information; a faithful renderer emits
168    /// [`NO_NEWLINE_MARKER`] right after each of these lines.
169    pub missing_newline_at: Vec<usize>,
170}
171
172impl FileDiff {
173    /// Create a diff between two text contents
174    pub fn new(path: &str, old: &str, new: &str) -> Self {
175        let old_lines = split_lines_keep_newline(old);
176        let new_lines = split_lines_keep_newline(new);
177        let diff = TextDiff::configure().diff_slices(&old_lines, &new_lines);
178        let mut lines = Vec::new();
179        let mut has_changes = false;
180        let mut missing_newline_at = Vec::new();
181
182        for change in diff.iter_all_changes() {
183            // Line tokens keep their terminator, so a final `c` and a final
184            // `c\n` are different tokens and never pair up as one Equal.
185            // Record the missing terminator before stripping it.
186            let value: &str = change.value();
187            if !value.ends_with('\n') {
188                missing_newline_at.push(lines.len());
189            }
190            let line = value.strip_suffix('\n').unwrap_or(value).to_string();
191            match change.tag() {
192                ChangeTag::Equal => {
193                    lines.push(DiffLine::Context(line));
194                }
195                ChangeTag::Delete => {
196                    lines.push(DiffLine::Removed(line));
197                    has_changes = true;
198                }
199                ChangeTag::Insert => {
200                    lines.push(DiffLine::Added(line));
201                    has_changes = true;
202                }
203            }
204        }
205
206        FileDiff {
207            path: path.to_string(),
208            lines,
209            has_changes,
210            missing_newline_at,
211        }
212    }
213
214    /// Format the diff as a unified diff string with context lines (like git diff)
215    pub fn to_unified(&self) -> String {
216        self.to_unified_with_context(DEFAULT_CONTEXT_LINES)
217    }
218
219    /// Format the diff as a unified diff string with a custom number of context lines
220    pub fn to_unified_with_context(&self, context: usize) -> String {
221        self.to_unified_with_sides(context, true, true)
222    }
223
224    /// Like [`FileDiff::to_unified_with_context`], but naming a side that
225    /// does not exist (an added file's old side, a deleted file's new side)
226    /// as `/dev/null`, so applicators create or delete the file instead of
227    /// leaving an empty one behind. See [`unified_side_headers`].
228    pub fn to_unified_with_sides(
229        &self,
230        context: usize,
231        old_exists: bool,
232        new_exists: bool,
233    ) -> String {
234        let mut output = String::new();
235
236        if !self.has_changes {
237            return output;
238        }
239
240        output.push_str(&format!("diff --oak a/{} b/{}\n", self.path, self.path));
241        for header in unified_side_headers(&self.path, old_exists, new_exists) {
242            output.push_str(&header);
243            output.push('\n');
244        }
245
246        // Find hunks: groups of changes with their surrounding context
247        let hunks = self.compute_hunks(context);
248
249        for hunk in hunks {
250            for line in hunk.unified_lines() {
251                output.push_str(&line);
252                output.push('\n');
253            }
254        }
255
256        output
257    }
258
259    /// Get hunks for rendering diffs with the given number of context lines
260    pub fn hunks(&self, context: usize) -> Vec<Hunk> {
261        self.compute_hunks(context)
262    }
263
264    /// Compute hunks for unified diff output
265    fn compute_hunks(&self, context: usize) -> Vec<Hunk> {
266        if self.lines.is_empty() {
267            return vec![];
268        }
269
270        // First, find all indices where there are changes
271        let change_indices: Vec<usize> = self
272            .lines
273            .iter()
274            .enumerate()
275            .filter(|(_, line)| !matches!(line, DiffLine::Context(_)))
276            .map(|(i, _)| i)
277            .collect();
278
279        if change_indices.is_empty() {
280            return vec![];
281        }
282
283        // Group changes into hunks - changes are in the same hunk if they're
284        // within 2*context lines of each other
285        let mut hunks = Vec::new();
286        let mut hunk_start = change_indices[0];
287        let mut hunk_end = change_indices[0];
288
289        for &idx in &change_indices[1..] {
290            // If this change is close enough to the previous one, extend the hunk
291            if idx <= hunk_end + 2 * context + 1 {
292                hunk_end = idx;
293            } else {
294                // Start a new hunk
295                hunks.push(self.create_hunk(hunk_start, hunk_end, context));
296                hunk_start = idx;
297                hunk_end = idx;
298            }
299        }
300        // Don't forget the last hunk
301        hunks.push(self.create_hunk(hunk_start, hunk_end, context));
302
303        hunks
304    }
305
306    /// Create a hunk from a range of change indices
307    fn create_hunk(&self, first_change: usize, last_change: usize, context: usize) -> Hunk {
308        // Calculate the actual range including context
309        let start = first_change.saturating_sub(context);
310        let end = (last_change + context + 1).min(self.lines.len());
311
312        // Collect the lines for this hunk
313        let lines: Vec<DiffLine> = self.lines[start..end].to_vec();
314        // Re-base the missing-terminator record onto the hunk's own indices;
315        // a final line that falls outside this hunk's context needs no marker
316        // here (the hunk never mentions it), exactly as git renders it.
317        let missing_newline_at: Vec<usize> = self
318            .missing_newline_at
319            .iter()
320            .filter(|&&idx| idx >= start && idx < end)
321            .map(|&idx| idx - start)
322            .collect();
323
324        // Calculate line numbers for the hunk header
325        // We need to track old line number and new line number
326        let mut old_line = 1;
327        let mut new_line = 1;
328
329        // Count lines up to the start of the hunk
330        for line in &self.lines[..start] {
331            match line {
332                DiffLine::Context(_) => {
333                    old_line += 1;
334                    new_line += 1;
335                }
336                DiffLine::Removed(_) => {
337                    old_line += 1;
338                }
339                DiffLine::Added(_) => {
340                    new_line += 1;
341                }
342            }
343        }
344
345        let old_start = old_line;
346        let new_start = new_line;
347
348        // Count lines in the hunk
349        let mut old_count = 0;
350        let mut new_count = 0;
351        for line in &lines {
352            match line {
353                DiffLine::Context(_) => {
354                    old_count += 1;
355                    new_count += 1;
356                }
357                DiffLine::Removed(_) => {
358                    old_count += 1;
359                }
360                DiffLine::Added(_) => {
361                    new_count += 1;
362                }
363            }
364        }
365
366        Hunk::new(
367            old_start,
368            old_count,
369            new_start,
370            new_count,
371            lines,
372            missing_newline_at,
373        )
374    }
375}
376
377/// A hunk in a unified diff
378///
379/// Produced by [`FileDiff::hunks`]; build one by hand with [`Hunk::new`].
380/// `#[non_exhaustive]` so fields can be added without breaking downstream
381/// crates again.
382#[non_exhaustive]
383pub struct Hunk {
384    pub old_start: usize,
385    pub old_count: usize,
386    pub new_start: usize,
387    pub new_count: usize,
388    pub lines: Vec<DiffLine>,
389    /// Indices into `lines` that must be followed by [`NO_NEWLINE_MARKER`];
390    /// see [`FileDiff::missing_newline_at`].
391    pub missing_newline_at: Vec<usize>,
392}
393
394impl Hunk {
395    /// Assemble a hunk. `missing_newline_at` indexes `lines`.
396    pub fn new(
397        old_start: usize,
398        old_count: usize,
399        new_start: usize,
400        new_count: usize,
401        lines: Vec<DiffLine>,
402        missing_newline_at: Vec<usize>,
403    ) -> Self {
404        Hunk {
405            old_start,
406            old_count,
407            new_start,
408            new_count,
409            lines,
410            missing_newline_at,
411        }
412    }
413
414    /// The `@@ -a,b +c,d @@` header line (no trailing newline).
415    ///
416    /// An empty range is numbered the way `diff -u` and `git diff` number
417    /// it: by the line *after which* it sits, so an added file's old side
418    /// is `-0,0` and a zero-context insertion after line 3 is `-3,0`. The
419    /// struct fields keep the 1-based position of the next line.
420    pub fn header(&self) -> String {
421        let range = |start: usize, count: usize| {
422            let start = if count == 0 {
423                start.saturating_sub(1)
424            } else {
425                start
426            };
427            format!("{start},{count}")
428        };
429        format!(
430            "@@ -{} +{} @@",
431            range(self.old_start, self.old_count),
432            range(self.new_start, self.new_count)
433        )
434    }
435
436    /// The hunk as unified-diff lines without trailing newlines: the header,
437    /// then each ` `/`+`/`-` prefixed line, with [`NO_NEWLINE_MARKER`] after
438    /// any line whose side of the file ended there without a terminator.
439    ///
440    /// Every renderer that prints hunk bodies should go through this so the
441    /// applicator-facing details (prefixes, markers) cannot drift apart.
442    pub fn unified_lines(&self) -> Vec<String> {
443        let mut out = Vec::with_capacity(self.lines.len() + 1 + self.missing_newline_at.len());
444        out.push(self.header());
445        for (idx, line) in self.lines.iter().enumerate() {
446            out.push(match line {
447                DiffLine::Context(s) => format!(" {s}"),
448                DiffLine::Added(s) => format!("+{s}"),
449                DiffLine::Removed(s) => format!("-{s}"),
450            });
451            if self.missing_newline_at.contains(&idx) {
452                out.push(NO_NEWLINE_MARKER.to_string());
453            }
454        }
455        out
456    }
457}
458
459#[cfg(test)]
460mod tests {
461    use super::*;
462
463    #[test]
464    fn test_diff_no_changes() {
465        let content = "line1\nline2\nline3\n";
466        let diff = FileDiff::new("test.txt", content, content);
467        assert!(!diff.has_changes);
468    }
469
470    #[test]
471    fn test_diff_added_lines() {
472        let old = "line1\nline2\n";
473        let new = "line1\nline2\nline3\n";
474        let diff = FileDiff::new("test.txt", old, new);
475        assert!(diff.has_changes);
476        assert!(diff.lines.iter().any(|l| matches!(l, DiffLine::Added(_))));
477    }
478
479    #[test]
480    fn test_diff_removed_lines() {
481        let old = "line1\nline2\nline3\n";
482        let new = "line1\nline2\n";
483        let diff = FileDiff::new("test.txt", old, new);
484        assert!(diff.has_changes);
485        assert!(diff.lines.iter().any(|l| matches!(l, DiffLine::Removed(_))));
486    }
487
488    #[test]
489    fn test_diff_modified_lines() {
490        let old = "line1\nline2\nline3\n";
491        let new = "line1\nmodified\nline3\n";
492        let diff = FileDiff::new("test.txt", old, new);
493        assert!(diff.has_changes);
494    }
495
496    #[test]
497    fn test_unified_output() {
498        let old = "line1\nline2\n";
499        let new = "line1\nline2\nline3\n";
500        let diff = FileDiff::new("test.txt", old, new);
501        let output = diff.to_unified();
502        assert!(output.contains("diff --oak"));
503        assert!(output.contains("@@"));
504        assert!(output.contains("+line3"));
505    }
506
507    #[test]
508    fn test_unified_shows_limited_context() {
509        // Create a file with many lines, change one in the middle
510        let old = (1..=20)
511            .map(|i| format!("line{i}"))
512            .collect::<Vec<_>>()
513            .join("\n")
514            + "\n";
515        let new = (1..=20)
516            .map(|i| {
517                if i == 10 {
518                    "modified".to_string()
519                } else {
520                    format!("line{i}")
521                }
522            })
523            .collect::<Vec<_>>()
524            .join("\n")
525            + "\n";
526
527        let diff = FileDiff::new("test.txt", &old, &new);
528        let output = diff.to_unified();
529
530        // Should have hunk header
531        assert!(output.contains("@@"));
532
533        // Should NOT contain line1 (too far from change at line 10)
534        assert!(
535            !output.contains(" line1\n"),
536            "Should not show line1 (too far from change)"
537        );
538        assert!(
539            !output.contains(" line2\n"),
540            "Should not show line2 (too far from change)"
541        );
542        assert!(
543            !output.contains(" line3\n"),
544            "Should not show line3 (too far from change)"
545        );
546        assert!(
547            !output.contains(" line4\n"),
548            "Should not show line4 (too far from change)"
549        );
550        assert!(
551            !output.contains(" line5\n"),
552            "Should not show line5 (too far from change)"
553        );
554        assert!(
555            !output.contains(" line6\n"),
556            "Should not show line6 (too far from change)"
557        );
558
559        // Should contain lines near the change (line 7, 8, 9 as context before)
560        assert!(
561            output.contains(" line7\n"),
562            "Should show line7 (context before)"
563        );
564        assert!(
565            output.contains(" line8\n"),
566            "Should show line8 (context before)"
567        );
568        assert!(
569            output.contains(" line9\n"),
570            "Should show line9 (context before)"
571        );
572
573        // Should contain the change
574        assert!(output.contains("-line10\n"), "Should show removed line10");
575        assert!(output.contains("+modified\n"), "Should show added modified");
576
577        // Should contain lines after (line 11, 12, 13 as context after)
578        assert!(
579            output.contains(" line11\n"),
580            "Should show line11 (context after)"
581        );
582        assert!(
583            output.contains(" line12\n"),
584            "Should show line12 (context after)"
585        );
586        assert!(
587            output.contains(" line13\n"),
588            "Should show line13 (context after)"
589        );
590
591        // Should NOT contain line20 (too far from change)
592        assert!(
593            !output.contains(" line20\n"),
594            "Should not show line20 (too far from change)"
595        );
596    }
597
598    #[test]
599    fn test_multiple_hunks() {
600        // Changes at line 2 and line 15 should create two separate hunks
601        let old = (1..=20)
602            .map(|i| format!("line{i}"))
603            .collect::<Vec<_>>()
604            .join("\n")
605            + "\n";
606        let new = (1..=20)
607            .map(|i| {
608                if i == 2 {
609                    "change1".to_string()
610                } else if i == 15 {
611                    "change2".to_string()
612                } else {
613                    format!("line{i}")
614                }
615            })
616            .collect::<Vec<_>>()
617            .join("\n")
618            + "\n";
619
620        let diff = FileDiff::new("test.txt", &old, &new);
621        let output = diff.to_unified();
622
623        // Should have two hunk headers (changes are far enough apart)
624        let hunk_count = output.matches("@@").count() / 2; // Each @@ appears twice per hunk header
625        assert!(
626            hunk_count >= 2,
627            "Should have at least 2 hunks, got {hunk_count}"
628        );
629    }
630
631    #[test]
632    fn test_diff_empty_files() {
633        let diff = FileDiff::new("empty.txt", "", "");
634        assert!(!diff.has_changes);
635        let output = diff.to_unified();
636        assert!(output.is_empty());
637    }
638
639    #[test]
640    fn test_diff_new_file_from_empty() {
641        let diff = FileDiff::new("new.txt", "", "hello\nworld\n");
642        assert!(diff.has_changes);
643        let output = diff.to_unified();
644        assert!(output.contains("+hello"));
645        assert!(output.contains("+world"));
646    }
647
648    #[test]
649    fn test_diff_delete_entire_file() {
650        let diff = FileDiff::new("gone.txt", "hello\nworld\n", "");
651        assert!(diff.has_changes);
652        let output = diff.to_unified();
653        assert!(output.contains("-hello"));
654        assert!(output.contains("-world"));
655    }
656
657    #[test]
658    fn test_diff_single_line_change() {
659        let diff = FileDiff::new("f.txt", "one line\n", "different line\n");
660        assert!(diff.has_changes);
661        let output = diff.to_unified();
662        assert!(output.contains("-one line"));
663        assert!(output.contains("+different line"));
664    }
665
666    #[test]
667    fn test_diff_no_trailing_newline() {
668        let diff = FileDiff::new("f.txt", "no newline", "no newline");
669        assert!(!diff.has_changes);
670    }
671
672    #[test]
673    fn test_diff_crlf_vs_lf() {
674        // CRLF and LF should be treated as different content
675        let diff = FileDiff::new("f.txt", "line\r\n", "line\n");
676        assert!(diff.has_changes);
677    }
678
679    // --- terminal-newline fidelity (fb521) -------------------------------
680    //
681    // Unified output must carry `\ No newline at end of file` exactly where
682    // git puts it, so `git apply`/`patch` reproduce the intended bytes.
683    // These compare whole strings: substring checks cannot catch a marker
684    // that is missing, duplicated, or attached to the wrong line.
685
686    fn hunk_body(path: &str, old: &str, new: &str, context: usize) -> String {
687        let unified = FileDiff::new(path, old, new).to_unified_with_context(context);
688        let header = format!("diff --oak a/{path} b/{path}\n--- a/{path}\n+++ b/{path}\n");
689        unified
690            .strip_prefix(&header)
691            .unwrap_or_else(|| panic!("unexpected file header in {unified:?}"))
692            .to_string()
693    }
694
695    #[test]
696    fn missing_newline_on_both_sides_marks_removed_and_added_final_lines() {
697        // The retained fb521 reproduction: `a\nb\nc` -> `a\nB\nc`.
698        let diff = FileDiff::new("f.txt", "a\nb\nc", "a\nB\nc");
699        // `c` is one Context line shared by both sides, so one record.
700        assert_eq!(diff.missing_newline_at, vec![3]);
701        assert_eq!(
702            hunk_body("f.txt", "a\nb\nc", "a\nB\nc", 3),
703            "@@ -1,3 +1,3 @@\n a\n-b\n+B\n c\n\\ No newline at end of file\n"
704        );
705    }
706
707    #[test]
708    fn newline_only_transitions_render_as_a_marked_replace() {
709        // Old side lacks the terminator, new side gains it.
710        assert_eq!(
711            hunk_body("f.txt", "a\nb\nc", "a\nb\nc\n", 3),
712            "@@ -1,3 +1,3 @@\n a\n b\n-c\n\\ No newline at end of file\n+c\n"
713        );
714        // And the reverse: the marker moves to the added line.
715        assert_eq!(
716            hunk_body("f.txt", "a\nb\nc\n", "a\nb\nc", 3),
717            "@@ -1,3 +1,3 @@\n a\n b\n-c\n+c\n\\ No newline at end of file\n"
718        );
719    }
720
721    #[test]
722    fn unterminated_final_line_that_changes_gets_a_marker_on_each_side() {
723        let diff = FileDiff::new("f.txt", "a\nb\nc", "a\nb\nC");
724        assert_eq!(diff.missing_newline_at, vec![2, 3]);
725        assert_eq!(
726            hunk_body("f.txt", "a\nb\nc", "a\nb\nC", 3),
727            "@@ -1,3 +1,3 @@\n a\n b\n-c\n\\ No newline at end of file\n+C\n\\ No newline at end of file\n"
728        );
729        // Growing past an unterminated line: the old `c` is replaced by a
730        // terminated `c` and a new unterminated `d`.
731        assert_eq!(
732            hunk_body("f.txt", "a\nb\nc", "a\nb\nc\nd", 3),
733            "@@ -1,3 +1,4 @@\n a\n b\n-c\n\\ No newline at end of file\n+c\n+d\n\\ No newline at end of file\n"
734        );
735    }
736
737    #[test]
738    fn marker_follows_the_final_line_only_when_the_hunk_reaches_it() {
739        let old = "a\nb\nc\nd\ne\nf\ng\nh";
740        let new = "a\nX\nc\nd\ne\nf\ng\nh";
741        // Default context stops at `e`; the unterminated `h` is not in the
742        // hunk, so no marker (git does the same).
743        assert_eq!(
744            hunk_body("f.txt", old, new, 3),
745            "@@ -1,5 +1,5 @@\n a\n-b\n+X\n c\n d\n e\n"
746        );
747        // Wide context reaches `h`: marker after that context line, once.
748        assert_eq!(
749            hunk_body("f.txt", old, new, 10),
750            "@@ -1,8 +1,8 @@\n a\n-b\n+X\n c\n d\n e\n f\n g\n h\n\\ No newline at end of file\n"
751        );
752        // Hunk indices are re-based: the marker index refers to the hunk's
753        // own `lines`, not the whole diff.
754        let diff = FileDiff::new("f.txt", old, new);
755        let hunks = diff.hunks(10);
756        assert_eq!(hunks.len(), 1);
757        assert_eq!(hunks[0].missing_newline_at, vec![hunks[0].lines.len() - 1]);
758        assert!(diff.hunks(3)[0].missing_newline_at.is_empty());
759    }
760
761    #[test]
762    fn multiple_hunks_keep_their_count_and_only_the_last_can_carry_a_marker() {
763        let old = (1..=20)
764            .map(|i| format!("line{i}"))
765            .collect::<Vec<_>>()
766            .join("\n");
767        let new = old
768            .replace("line2\n", "change1\n")
769            .replace("line20", "change2");
770        let diff = FileDiff::new("f.txt", &old, &new);
771        let hunks = diff.hunks(3);
772        assert_eq!(hunks.len(), 2, "two far-apart edits stay two hunks");
773        assert!(hunks[0].missing_newline_at.is_empty());
774        assert_eq!(hunks[1].missing_newline_at.len(), 2);
775        let rendered = diff.to_unified();
776        assert_eq!(rendered.matches("\n@@ ").count(), 2);
777        assert!(rendered.ends_with(
778            "-line20\n\\ No newline at end of file\n+change2\n\\ No newline at end of file\n"
779        ));
780    }
781
782    #[test]
783    fn added_and_deleted_unterminated_files_are_marked() {
784        // Empty ranges are numbered `0,0`, as git numbers them.
785        assert_eq!(
786            hunk_body("new.txt", "", "hello\nworld", 3),
787            "@@ -0,0 +1,2 @@\n+hello\n+world\n\\ No newline at end of file\n"
788        );
789        assert_eq!(
790            hunk_body("gone.txt", "hello\nworld", "", 3),
791            "@@ -1,2 +0,0 @@\n-hello\n-world\n\\ No newline at end of file\n"
792        );
793        // Terminated content stays marker-free.
794        assert_eq!(
795            hunk_body("new.txt", "", "hello\nworld\n", 3),
796            "@@ -0,0 +1,2 @@\n+hello\n+world\n"
797        );
798    }
799
800    #[test]
801    fn crlf_content_keeps_its_carriage_returns_and_marks_the_missing_lf() {
802        assert_eq!(
803            hunk_body("f.txt", "a\r\nb\r\nc", "a\r\nB\r\nc", 3),
804            "@@ -1,3 +1,3 @@\n a\r\n-b\r\n+B\r\n c\n\\ No newline at end of file\n"
805        );
806        assert_eq!(
807            hunk_body("f.txt", "a\r\nb", "a\r\nb\r\n", 3),
808            "@@ -1,2 +1,2 @@\n a\r\n-b\n\\ No newline at end of file\n+b\r\n"
809        );
810    }
811
812    #[test]
813    fn lone_carriage_return_is_content_not_a_line_terminator() {
814        // git sees `a\rb` as one line; so must the patch, or it cannot apply.
815        let diff = FileDiff::new("f.txt", "a\rb", "a\rB");
816        assert_eq!(
817            diff.lines,
818            vec![
819                DiffLine::Removed("a\rb".to_string()),
820                DiffLine::Added("a\rB".to_string())
821            ]
822        );
823        assert_eq!(
824            hunk_body("f.txt", "a\rb", "a\rB", 3),
825            "@@ -1,1 +1,1 @@\n-a\rb\n\\ No newline at end of file\n+a\rB\n\\ No newline at end of file\n"
826        );
827        // A side ending in `c\r` has no LF: it must be marked, not silently
828        // given one.
829        assert_eq!(
830            hunk_body("f.txt", "a\nc\r", "a\nc\r\n", 3),
831            "@@ -1,2 +1,2 @@\n a\n-c\r\n\\ No newline at end of file\n+c\r\n"
832        );
833        // Mid-file lone `\r` stays inside its line on both sides.
834        assert!(!FileDiff::new("f.txt", "x\ry\nz\n", "x\ry\nz\n").has_changes);
835    }
836
837    #[test]
838    fn split_lines_keep_newline_splits_on_lf_only() {
839        assert_eq!(split_lines_keep_newline(""), Vec::<&str>::new());
840        assert_eq!(split_lines_keep_newline("\n"), vec!["\n"]);
841        assert_eq!(split_lines_keep_newline("a"), vec!["a"]);
842        assert_eq!(split_lines_keep_newline("a\nb"), vec!["a\n", "b"]);
843        assert_eq!(
844            split_lines_keep_newline("a\r\nb\r\n"),
845            vec!["a\r\n", "b\r\n"]
846        );
847        assert_eq!(split_lines_keep_newline("a\rb\n"), vec!["a\rb\n"]);
848        assert_eq!(split_lines_keep_newline("a\n\n"), vec!["a\n", "\n"]);
849    }
850
851    #[test]
852    fn terminated_content_never_carries_a_marker() {
853        for (old, new) in [
854            ("a\nb\nc\n", "a\nB\nc\n"),
855            ("", "\n"),
856            ("\n", ""),
857            ("a\n\n", "a\n"),
858        ] {
859            let diff = FileDiff::new("f.txt", old, new);
860            assert!(diff.missing_newline_at.is_empty(), "{old:?} -> {new:?}");
861            assert!(!diff.to_unified().contains(NO_NEWLINE_MARKER));
862        }
863    }
864
865    #[test]
866    fn hunk_unified_lines_match_the_unified_string() {
867        let diff = FileDiff::new("f.txt", "a\nb\nc", "a\nB\nc\nd");
868        let mut from_hunks = String::from("diff --oak a/f.txt b/f.txt\n--- a/f.txt\n+++ b/f.txt\n");
869        for hunk in diff.hunks(3) {
870            for line in hunk.unified_lines() {
871                from_hunks.push_str(&line);
872                from_hunks.push('\n');
873            }
874        }
875        assert_eq!(from_hunks, diff.to_unified());
876    }
877
878    #[test]
879    fn test_is_binary_detects_nul() {
880        assert!(is_binary(b"abc\0def"));
881        assert!(!is_binary(b"plain text\n"));
882        assert!(!is_binary(b""));
883    }
884
885    #[test]
886    fn test_binary_notice_for_nul_content() {
887        let notice = binary_or_large_notice("a.bin", b"\0\0", b"\0\x01");
888        assert_eq!(
889            notice.as_deref(),
890            Some("Binary files a/a.bin and b/a.bin differ")
891        );
892    }
893
894    #[test]
895    fn test_large_text_gets_notice() {
896        let big = vec![b'a'; MAX_TEXT_DIFF_BYTES + 1];
897        let notice = binary_or_large_notice("big.txt", b"small", &big);
898        assert!(notice.is_some());
899        assert!(notice.unwrap().contains("text diff omitted"));
900    }
901
902    #[test]
903    fn test_force_text_keeps_binary_guard_but_overrides_size_guard() {
904        let big = vec![b'a'; MAX_TEXT_DIFF_BYTES + 1];
905        assert!(binary_or_oversize_notice("big.txt", b"small", &big, None).is_none());
906        assert!(binary_or_oversize_notice("bin.dat", b"\0", &big, None).is_some());
907    }
908
909    #[test]
910    fn test_normal_text_no_notice() {
911        assert!(binary_or_large_notice("f.txt", b"old\n", b"new\n").is_none());
912    }
913
914    #[test]
915    fn test_diff_with_custom_context() {
916        let old = "line1\nline2\nline3\nline4\nline5\n";
917        let new = "line1\nline2\nchanged\nline4\nline5\n";
918        let diff = FileDiff::new("f.txt", old, new);
919        let output = diff.to_unified_with_context(1);
920        // With context of 1, should show line2 and line4 as context
921        assert!(output.contains(" line2"));
922        assert!(output.contains("-line3"));
923        assert!(output.contains("+changed"));
924        assert!(output.contains(" line4"));
925        // Should NOT show line1 with context=1
926        assert!(!output.contains(" line1"));
927    }
928
929    #[test]
930    fn inline_diff_ranges_marks_changed_words_with_exact_byte_offsets() {
931        let old = "the quick brown fox jumps";
932        let new = "the quick red fox leaps";
933        let (old_ranges, new_ranges) = inline_diff_ranges(old, new).expect("similar enough");
934        let old_words: Vec<&str> = old_ranges.iter().map(|r| &old[r.clone()]).collect();
935        let new_words: Vec<&str> = new_ranges.iter().map(|r| &new[r.clone()]).collect();
936        assert_eq!(old_words, vec!["brown", "jumps"]);
937        assert_eq!(new_words, vec!["red", "leaps"]);
938    }
939
940    #[test]
941    fn inline_diff_ranges_declines_mostly_different_lines() {
942        assert!(inline_diff_ranges("AAA", "BBB").is_none());
943    }
944
945    #[test]
946    fn inline_diff_ranges_handles_multibyte_content() {
947        let old = "prefix héllo suffix";
948        let new = "prefix wörld suffix";
949        let (old_ranges, new_ranges) = inline_diff_ranges(old, new).expect("similar enough");
950        // Ranges must be valid char boundaries — slicing panics otherwise.
951        for r in &old_ranges {
952            let _ = &old[r.clone()];
953        }
954        for r in &new_ranges {
955            let _ = &new[r.clone()];
956        }
957    }
958
959    #[test]
960    fn empty_ranges_are_numbered_like_git() {
961        // Zero-context insertion after line 2: git prints `-2,0 +3,1`.
962        let diff = FileDiff::new("f.txt", "a\nb\nc\n", "a\nb\nX\nc\n");
963        let hunks = diff.hunks(0);
964        assert_eq!(hunks.len(), 1);
965        assert_eq!(hunks[0].header(), "@@ -2,0 +3,1 @@");
966        // Zero-context deletion of line 2: `-2,1 +1,0`.
967        let diff = FileDiff::new("f.txt", "a\nb\nc\n", "a\nc\n");
968        assert_eq!(diff.hunks(0)[0].header(), "@@ -2,1 +1,0 @@");
969    }
970
971    #[test]
972    fn side_headers_name_dev_null_for_absent_sides() {
973        assert_eq!(
974            unified_side_headers("p", true, true),
975            ["--- a/p".to_string(), "+++ b/p".to_string()]
976        );
977        assert_eq!(
978            unified_side_headers("p", false, true),
979            ["--- /dev/null".to_string(), "+++ b/p".to_string()]
980        );
981        assert_eq!(
982            unified_side_headers("p", true, false),
983            ["--- a/p".to_string(), "+++ /dev/null".to_string()]
984        );
985        let added = FileDiff::new("n.txt", "", "hi\n").to_unified_with_sides(3, false, true);
986        assert_eq!(
987            added,
988            "diff --oak a/n.txt b/n.txt\n--- /dev/null\n+++ b/n.txt\n@@ -0,0 +1,1 @@\n+hi\n"
989        );
990    }
991}