differential-engine 0.10.0

Core engine: git io, diff parsing, byte-exact apply, shape classes, invariants
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//! Parser for `git diff-tree -r -U0 --no-renames` patch output.
//!
//! Count-driven: the `@@ -a,b +c,d @@` header states exactly how many removed and
//! added lines follow, so hunk bodies are consumed by count, never by prefix
//! sniffing. (Prefix sniffing silently drops deleted lines that start with `--`:
//! deleting a line `--help` emits `---help`, which a `startswith("---")` header
//! guard eats. That was a real latent bug in the validated prototype.)
//!
//! Unknown lines are hard errors, not skips — a parser that guesses is how hunks
//! get lost silently.

use std::collections::HashMap;

use crate::EngineError;
use crate::model::{DiffView, Disposition, FileChange, Hunk};
use crate::paths::parse_diff_git_path;

/// Parse the canonical `-U0 --no-renames` patch into a [`DiffView`].
///
/// `dispositions` comes from a separate `--name-status -z` call; header hints
/// (`new file mode` / `deleted file mode`) are only a fallback.
pub fn parse_canonical(
    raw: &[u8],
    dispositions: &HashMap<Vec<u8>, Disposition>,
) -> Result<DiffView, EngineError> {
    let lines: Vec<&[u8]> = split_lines(raw);
    let mut files: Vec<FileChange> = Vec::new();
    let mut hunks: Vec<Hunk> = Vec::new();
    // Extra per-file parse state, parallel to `files`.
    let mut header_disp: Vec<Option<Disposition>> = Vec::new();

    let mut i = 0usize;
    while i < lines.len() {
        let line = lines[i];

        if let Some(rest) = line.strip_prefix(b"diff --git ".as_slice()) {
            let path = parse_diff_git_path(rest).ok_or_else(|| EngineError::Parse {
                line: i + 1,
                msg: format!("unparseable diff --git header: {}", lossy(line)),
            })?;
            files.push(FileChange {
                path,
                disposition: Disposition::Modified, // resolved in finalise
                new_mode: None,
                old_mode: None,
                binary: false,
                submodule: None,
                hunks: Vec::new(),
                rename_similarity: None,
                rename_from: None,
                rename_to: None,
                generated: None,
                old_oid: None,
                new_oid: None,
            });
            header_disp.push(None);
            i += 1;
            continue;
        }

        let Some(f) = files.last_mut() else {
            // diff-tree between two trees may emit nothing at all; anything else
            // before the first file header is unexpected.
            if line.is_empty() {
                i += 1;
                continue;
            }
            return Err(EngineError::Parse {
                line: i + 1,
                msg: format!("content before first diff header: {}", lossy(line)),
            });
        };

        if let Some(rest) = line.strip_prefix(b"old mode ".as_slice()) {
            f.old_mode = Some(lossy(rest));
        } else if let Some(rest) = line.strip_prefix(b"new mode ".as_slice()) {
            f.new_mode = Some(lossy(rest));
        } else if let Some(rest) = line.strip_prefix(b"new file mode ".as_slice()) {
            f.new_mode = Some(lossy(rest));
            *header_disp.last_mut().unwrap() = Some(Disposition::Added);
        } else if let Some(rest) = line.strip_prefix(b"deleted file mode ".as_slice()) {
            f.old_mode = Some(lossy(rest));
            *header_disp.last_mut().unwrap() = Some(Disposition::Deleted);
        } else if let Some(rest) = line.strip_prefix(b"index ".as_slice()) {
            // `index <old>..<new>[ <mode>]` — mode present when unchanged.
            if let Some(pos) = rest.iter().rposition(|&b| b == b' ') {
                let mode = &rest[pos + 1..];
                if mode.len() == 6 && mode.iter().all(u8::is_ascii_digit) && f.new_mode.is_none() {
                    f.new_mode = Some(lossy(mode));
                }
            }
        } else if line.starts_with(b"Binary files ") || line == b"GIT binary patch" {
            f.binary = true;
        } else if line.starts_with(b"--- ") || line.starts_with(b"+++ ") {
            // Paths already known from the diff --git header.
        } else if line.starts_with(b"@@ ") {
            let (old_start, old_count, new_start, new_count) =
                parse_hunk_header(line).ok_or_else(|| EngineError::Parse {
                    line: i + 1,
                    msg: format!("unparseable hunk header: {}", lossy(line)),
                })?;

            let mut removed = Vec::with_capacity(old_count as usize);
            let mut added = Vec::with_capacity(new_count as usize);
            let mut nonl_old = false;
            let mut nonl_new = false;
            i += 1;

            for _ in 0..old_count {
                let body = *lines.get(i).ok_or_else(|| truncated(i))?;
                let content =
                    body.strip_prefix(b"-".as_slice())
                        .ok_or_else(|| EngineError::Parse {
                            line: i + 1,
                            msg: format!("expected removed line, got: {}", lossy(body)),
                        })?;
                removed.push(content.to_vec());
                i += 1;
            }
            if old_count > 0 && lines.get(i).is_some_and(|l| l.starts_with(b"\\")) {
                nonl_old = true;
                i += 1;
            }
            for _ in 0..new_count {
                let body = *lines.get(i).ok_or_else(|| truncated(i))?;
                let content =
                    body.strip_prefix(b"+".as_slice())
                        .ok_or_else(|| EngineError::Parse {
                            line: i + 1,
                            msg: format!("expected added line, got: {}", lossy(body)),
                        })?;
                added.push(content.to_vec());
                i += 1;
            }
            if new_count > 0 && lines.get(i).is_some_and(|l| l.starts_with(b"\\")) {
                nonl_new = true;
                i += 1;
            }

            let file_idx = files.len() - 1;
            files[file_idx].hunks.push(hunks.len());
            hunks.push(Hunk {
                file: file_idx,
                old_start,
                old_count,
                new_start,
                new_count,
                removed,
                added,
                nonl_old,
                nonl_new,
            });
            continue; // i already advanced past the body
        } else if line.is_empty() {
            // Blank separator lines do not occur in -U0 patch output; tolerate
            // them only as trailing end-of-input noise.
            if lines[i + 1..].iter().any(|l| !l.is_empty()) {
                return Err(EngineError::Parse {
                    line: i + 1,
                    msg: "unexpected blank line inside patch".into(),
                });
            }
        } else {
            return Err(EngineError::Parse {
                line: i + 1,
                msg: format!("unrecognised patch line: {}", lossy(line)),
            });
        }
        i += 1;
    }

    finalise(files, hunks, header_disp, dispositions)
}

/// Resolve dispositions, extract submodule ids, and merge duplicate path entries
/// (a typechange emits two file headers for the same path).
fn finalise(
    mut files: Vec<FileChange>,
    mut hunks: Vec<Hunk>,
    header_disp: Vec<Option<Disposition>>,
    dispositions: &HashMap<Vec<u8>, Disposition>,
) -> Result<DiffView, EngineError> {
    for (f, hd) in files.iter_mut().zip(&header_disp) {
        f.disposition = dispositions
            .get(&f.path)
            .copied()
            .or(*hd)
            .unwrap_or(Disposition::Modified);
    }

    // Merge consecutive duplicate-path entries (typechange): hunks concatenate,
    // old side from the first, new side from the second.
    let mut merged: Vec<FileChange> = Vec::with_capacity(files.len());
    for f in files.into_iter() {
        let continues_previous = merged.last().is_some_and(|prev| prev.path == f.path);
        if continues_previous {
            let idx = merged.len() - 1;
            let prev = &mut merged[idx];
            if f.new_mode.is_some() {
                prev.new_mode = f.new_mode;
            }
            if prev.old_mode.is_none() {
                prev.old_mode = f.old_mode;
            }
            prev.binary |= f.binary;
            prev.disposition = dispositions
                .get(&prev.path)
                .copied()
                .unwrap_or(Disposition::Modified);
            for h in &f.hunks {
                hunks[*h].file = idx;
                prev.hunks.push(*h);
            }
        } else {
            let idx = merged.len();
            for h in &f.hunks {
                hunks[*h].file = idx;
            }
            merged.push(f);
        }
    }

    // Submodules: gitlink mode; commit ids live in the pseudo-hunk body.
    for f in merged.iter_mut() {
        let is_gitlink =
            f.new_mode.as_deref() == Some("160000") || f.old_mode.as_deref() == Some("160000");
        if !is_gitlink {
            continue;
        }
        let mut old = None;
        let mut new = None;
        for &hi in &f.hunks {
            for l in &hunks[hi].removed {
                if let Some(rest) = l.strip_prefix(b"Subproject commit ".as_slice()) {
                    old = Some(lossy(rest));
                }
            }
            for l in &hunks[hi].added {
                if let Some(rest) = l.strip_prefix(b"Subproject commit ".as_slice()) {
                    new = Some(lossy(rest));
                }
            }
        }
        f.submodule = Some((old, new));
    }

    Ok(DiffView {
        files: merged,
        hunks,
    })
}

/// `@@ -a[,b] +c[,d] @@…` — counts default to 1 when elided.
fn parse_hunk_header(line: &[u8]) -> Option<(u32, u32, u32, u32)> {
    let rest = line.strip_prefix(b"@@ -".as_slice())?;
    let (old_start, rest) = take_num(rest)?;
    let (old_count, rest) = take_opt_count(rest)?;
    let rest = rest.strip_prefix(b" +".as_slice())?;
    let (new_start, rest) = take_num(rest)?;
    let (new_count, rest) = take_opt_count(rest)?;
    rest.starts_with(b" @@").then_some(())?;
    Some((old_start, old_count, new_start, new_count))
}

fn take_num(s: &[u8]) -> Option<(u32, &[u8])> {
    let end = s
        .iter()
        .position(|b| !b.is_ascii_digit())
        .unwrap_or(s.len());
    if end == 0 {
        return None;
    }
    let n: u32 = std::str::from_utf8(&s[..end]).ok()?.parse().ok()?;
    Some((n, &s[end..]))
}

fn take_opt_count(s: &[u8]) -> Option<(u32, &[u8])> {
    match s.strip_prefix(b",".as_slice()) {
        Some(rest) => take_num(rest),
        None => Some((1, s)),
    }
}

fn split_lines(raw: &[u8]) -> Vec<&[u8]> {
    let mut v: Vec<&[u8]> = raw.split(|&b| b == b'\n').collect();
    if v.last().is_some_and(|l| l.is_empty()) {
        v.pop();
    }
    v
}

fn truncated(i: usize) -> EngineError {
    EngineError::Parse {
        line: i + 1,
        msg: "patch truncated inside hunk body".into(),
    }
}

fn lossy(b: &[u8]) -> String {
    String::from_utf8_lossy(&b[..b.len().min(160)]).into_owned()
}

#[cfg(test)]
mod tests {
    use super::*;

    fn disp_map(entries: &[(&[u8], Disposition)]) -> HashMap<Vec<u8>, Disposition> {
        entries.iter().map(|(p, d)| (p.to_vec(), *d)).collect()
    }

    #[test]
    fn simple_modification() {
        let raw = b"diff --git a/f.txt b/f.txt\n\
index 000..111 100644\n\
--- a/f.txt\n\
+++ b/f.txt\n\
@@ -2,1 +2,2 @@\n\
-old line\n\
+new line\n\
+extra\n";
        let v = parse_canonical(raw, &disp_map(&[(b"f.txt", Disposition::Modified)])).unwrap();
        assert_eq!(v.files.len(), 1);
        assert_eq!(v.hunks.len(), 1);
        let h = &v.hunks[0];
        assert_eq!(
            (h.old_start, h.old_count, h.new_start, h.new_count),
            (2, 1, 2, 2)
        );
        assert_eq!(h.removed, vec![b"old line".to_vec()]);
        assert_eq!(h.added, vec![b"new line".to_vec(), b"extra".to_vec()]);
        assert_eq!(v.files[0].new_mode.as_deref(), Some("100644"));
    }

    #[test]
    fn deleted_line_starting_with_dashes_is_kept() {
        // Deleting the line `--help` emits `---help`. Prefix sniffing eats it;
        // count-driven parsing must not.
        let raw = b"diff --git a/cli.txt b/cli.txt\n\
--- a/cli.txt\n\
+++ b/cli.txt\n\
@@ -1,2 +1,1 @@\n\
---help\n\
-+++weird\n\
+usage\n";
        let v = parse_canonical(raw, &Default::default()).unwrap();
        let h = &v.hunks[0];
        assert_eq!(h.removed, vec![b"--help".to_vec(), b"+++weird".to_vec()]);
        assert_eq!(h.added, vec![b"usage".to_vec()]);
    }

    #[test]
    fn count_elision_defaults_to_one() {
        let raw = b"diff --git a/f b/f\n\
--- a/f\n\
+++ b/f\n\
@@ -5 +5,2 @@ fn context()\n\
-x\n\
+y\n\
+z\n";
        let v = parse_canonical(raw, &Default::default()).unwrap();
        let h = &v.hunks[0];
        assert_eq!(
            (h.old_start, h.old_count, h.new_start, h.new_count),
            (5, 1, 5, 2)
        );
    }

    #[test]
    fn no_newline_markers_per_side() {
        let raw = b"diff --git a/f b/f\n\
--- a/f\n\
+++ b/f\n\
@@ -1,1 +1,1 @@\n\
-old\n\
\\ No newline at end of file\n\
+new\n\
\\ No newline at end of file\n";
        let v = parse_canonical(raw, &Default::default()).unwrap();
        let h = &v.hunks[0];
        assert!(h.nonl_old);
        assert!(h.nonl_new);
    }

    #[test]
    fn nonl_old_only() {
        // Newline added to the final line: old side lacks it, new side has it.
        let raw = b"diff --git a/f b/f\n\
--- a/f\n\
+++ b/f\n\
@@ -1,1 +1,1 @@\n\
-old\n\
\\ No newline at end of file\n\
+old\n";
        let v = parse_canonical(raw, &Default::default()).unwrap();
        assert!(v.hunks[0].nonl_old);
        assert!(!v.hunks[0].nonl_new);
    }

    #[test]
    fn binary_file_has_no_hunks() {
        let raw = b"diff --git a/img.png b/img.png\n\
new file mode 100644\n\
index 000..111\n\
Binary files /dev/null and b/img.png differ\n";
        let v = parse_canonical(raw, &disp_map(&[(b"img.png", Disposition::Added)])).unwrap();
        assert!(v.files[0].binary);
        assert!(v.files[0].hunks.is_empty());
        assert_eq!(v.files[0].disposition, Disposition::Added);
    }

    #[test]
    fn mode_only_change_has_no_hunks() {
        let raw = b"diff --git a/run.sh b/run.sh\n\
old mode 100644\n\
new mode 100755\n";
        let v = parse_canonical(raw, &Default::default()).unwrap();
        assert_eq!(v.files[0].old_mode.as_deref(), Some("100644"));
        assert_eq!(v.files[0].new_mode.as_deref(), Some("100755"));
        assert!(v.files[0].hunks.is_empty());
    }

    #[test]
    fn submodule_pseudo_hunk_is_kept_and_ids_extracted() {
        let raw = b"diff --git a/dep b/dep\n\
index aaa..bbb 160000\n\
--- a/dep\n\
+++ b/dep\n\
@@ -1 +1 @@\n\
-Subproject commit aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n\
+Subproject commit bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n";
        let v = parse_canonical(raw, &Default::default()).unwrap();
        assert_eq!(v.hunks.len(), 1, "pseudo-hunk stays in the canonical count");
        let (old, new) = v.files[0].submodule.clone().unwrap();
        assert_eq!(old.unwrap(), "a".repeat(40));
        assert_eq!(new.unwrap(), "b".repeat(40));
    }

    #[test]
    fn unrecognised_line_is_a_hard_error() {
        let raw = b"diff --git a/f b/f\n\
some garbage git will never emit\n";
        assert!(parse_canonical(raw, &Default::default()).is_err());
    }

    #[test]
    fn typechange_double_entry_merges() {
        let raw = b"diff --git a/link b/link\n\
deleted file mode 100644\n\
--- a/link\n\
+++ /dev/null\n\
@@ -1,1 +0,0 @@\n\
-real content\n\
diff --git a/link b/link\n\
new file mode 120000\n\
--- /dev/null\n\
+++ b/link\n\
@@ -0,0 +1,1 @@\n\
+target\n\
\\ No newline at end of file\n";
        let v = parse_canonical(raw, &disp_map(&[(b"link", Disposition::Modified)])).unwrap();
        assert_eq!(v.files.len(), 1);
        assert_eq!(v.files[0].hunks.len(), 2);
        assert_eq!(v.files[0].old_mode.as_deref(), Some("100644"));
        assert_eq!(v.files[0].new_mode.as_deref(), Some("120000"));
        assert_eq!(v.hunks[1].file, 0);
    }
}