differential-engine 0.13.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
//! Synthetic-repo integration tests: every edge case runs the full pipeline
//! (enumerate → annotate → classify → invariants → document) against a real
//! temporary git repository built with plumbing-adjacent commands.

use differential_engine::config::Config;
use differential_engine::schema::{Disposition, GeneratedBy};
use differential_testutil::{
    TestRepo, assert_all_ok, doc, rename_and_rewrite_repo, verbatim_move_repo,
};

// ---------------------------------------------------------------- newlines

#[test]
fn nonl_base_side_newline_added() {
    let r = TestRepo::new();
    r.write("f.txt", b"a\nlast");
    let base = r.commit_all("base");
    r.write("f.txt", b"a\nlast\n");
    let head = r.commit_all("head");
    let out = r.pipeline(&base, &head);
    assert_all_ok(&out);
    let d = doc(&out);
    assert!(d.hunks[0].nonl_old);
    assert!(!d.hunks[0].nonl_new);
}

#[test]
fn nonl_head_side_newline_removed() {
    let r = TestRepo::new();
    r.write("f.txt", b"a\nlast\n");
    let base = r.commit_all("base");
    r.write("f.txt", b"a\nlast");
    let head = r.commit_all("head");
    let out = r.pipeline(&base, &head);
    assert_all_ok(&out);
    let d = doc(&out);
    assert!(!d.hunks[0].nonl_old);
    assert!(d.hunks[0].nonl_new);
}

#[test]
fn nonl_both_sides() {
    let r = TestRepo::new();
    r.write("f.txt", b"old body");
    let base = r.commit_all("base");
    r.write("f.txt", b"new body");
    let head = r.commit_all("head");
    let out = r.pipeline(&base, &head);
    assert_all_ok(&out);
    let d = doc(&out);
    assert!(d.hunks[0].nonl_old);
    assert!(d.hunks[0].nonl_new);
}

#[test]
fn nonl_created_file_without_final_newline() {
    let r = TestRepo::new();
    let base = r.commit_all("empty base");
    r.write("f.txt", b"no newline here");
    let head = r.commit_all("head");
    let out = r.pipeline(&base, &head);
    assert_all_ok(&out);
    assert!(doc(&out).hunks[0].nonl_new);
}

#[test]
fn nonl_deleted_file_without_final_newline() {
    let r = TestRepo::new();
    r.write("f.txt", b"no newline here");
    let base = r.commit_all("base");
    std::fs::remove_file(r.root.join("f.txt")).unwrap();
    let head = r.commit_all("head");
    let out = r.pipeline(&base, &head);
    assert_all_ok(&out);
    let d = doc(&out);
    assert!(d.hunks[0].nonl_old);
    assert_eq!(d.files[0].disposition, Disposition::D);
}

// ---------------------------------------------------------------- zero-hunk files

#[test]
fn empty_file_add_and_delete() {
    let r = TestRepo::new();
    r.write("keep.txt", b"x\n");
    r.write("doomed.txt", b"");
    let base = r.commit_all("base");
    r.write("fresh.txt", b"");
    std::fs::remove_file(r.root.join("doomed.txt")).unwrap();
    let head = r.commit_all("head");
    let out = r.pipeline(&base, &head);
    assert_all_ok(&out);
    let d = doc(&out);
    assert_eq!(d.stats.hunks, 0);
    assert_eq!(d.stats.files, 2);
    let fresh = d.files.iter().find(|f| f.path == "fresh.txt").unwrap();
    assert_eq!(fresh.disposition, Disposition::A);
    assert!(fresh.hunk_ids.is_empty());
}

#[test]
fn mode_only_change() {
    let r = TestRepo::new();
    r.write("run.sh", b"#!/bin/sh\necho hi\n");
    let base = r.commit_all("base");
    r.git(&["update-index", "--chmod=+x", "run.sh"]);
    r.git(&["commit", "-q", "-m", "chmod"]);
    let head = r.git(&["rev-parse", "HEAD"]);
    let out = r.pipeline(&base, &head);
    assert_all_ok(&out); // fails under the prototype's hardcoded-100644 staging
    let d = doc(&out);
    assert_eq!(d.files[0].mode.as_deref(), Some("100755"));
    assert_eq!(d.files[0].old_mode.as_deref(), Some("100644"));
    assert!(d.files[0].hunk_ids.is_empty());
}

#[cfg(unix)]
#[test]
fn symlink_add_and_retarget() {
    let r = TestRepo::new();
    r.write("real.txt", b"content\n");
    let base = r.commit_all("base");
    std::os::unix::fs::symlink("real.txt", r.root.join("link")).unwrap();
    let mid = r.commit_all("add link");
    let out = r.pipeline(&base, &mid);
    assert_all_ok(&out);
    let d = doc(&out);
    let link = d.files.iter().find(|f| f.path == "link").unwrap();
    assert_eq!(link.mode.as_deref(), Some("120000"));

    std::fs::remove_file(r.root.join("link")).unwrap();
    r.write("other.txt", b"other\n");
    std::os::unix::fs::symlink("other.txt", r.root.join("link")).unwrap();
    let head = r.commit_all("retarget");
    let out = r.pipeline(&mid, &head);
    assert_all_ok(&out);
}

// ---------------------------------------------------------------- binary

#[test]
fn binary_add_and_modify() {
    let r = TestRepo::new();
    let base = r.commit_all("empty");
    r.write("blob.bin", &[0u8, 159, 146, 150, 0, 255, 1, 2]);
    let mid = r.commit_all("add binary");
    let out = r.pipeline(&base, &mid);
    assert_all_ok(&out);
    let d = doc(&out);
    assert!(d.files[0].binary);
    assert!(d.files[0].hunk_ids.is_empty());
    assert_eq!(d.stats.binary_files, 1);

    r.write("blob.bin", &[0u8, 1, 2, 3, 0, 4, 5]);
    let head = r.commit_all("modify binary");
    let out = r.pipeline(&mid, &head);
    assert_all_ok(&out);
    assert_eq!(doc(&out).stats.hunks, 0);
}

// ---------------------------------------------------------------- renames

#[test]
fn rename_verbatim_r100() {
    let (r, base, head) = verbatim_move_repo();
    let out = r.pipeline(&base, &head);
    assert_all_ok(&out);
    let d = doc(&out);
    // Canonical view: D + A.
    assert_eq!(d.stats.files, 2);
    let added = d
        .files
        .iter()
        .find(|f| f.disposition == Disposition::A)
        .unwrap();
    let deleted = d
        .files
        .iter()
        .find(|f| f.disposition == Disposition::D)
        .unwrap();
    assert_eq!(added.rename_similarity, Some(100));
    assert_eq!(added.old_path.as_deref(), Some("src/old_name.rs"));
    assert_eq!(deleted.new_path.as_deref(), Some("src/new_name.rs"));
    assert_eq!(deleted.rename_similarity, Some(100));
}

#[test]
fn rename_with_heavy_edit_carries_low_similarity() {
    let (r, base, head) = rename_and_rewrite_repo();
    let out = r.pipeline(&base, &head);
    assert_all_ok(&out);
    let d = doc(&out);
    let added = d
        .files
        .iter()
        .find(|f| f.disposition == Disposition::A)
        .unwrap();
    let sim = added.rename_similarity.expect("rename must be detected");
    assert!(
        (50..95).contains(&sim),
        "similarity {sim} should be well below the relocation gate"
    );
    assert_eq!(added.old_path.as_deref(), Some("mod/original.txt"));
}

// ---------------------------------------------------------------- CRLF

#[test]
fn crlf_round_trips_and_shares_shape_with_lf_twin() {
    let r = TestRepo::new();
    r.write("dos.txt", b"alpha_value = one\r\nbeta_value = two\r\n");
    r.write("unix.txt", b"alpha_value = one\nbeta_value = two\n");
    let base = r.commit_all("base");
    r.write("dos.txt", b"alpha_value = CHANGED\r\nbeta_value = two\r\n");
    r.write("unix.txt", b"alpha_value = CHANGED\nbeta_value = two\n");
    let head = r.commit_all("head");
    let out = r.pipeline(&base, &head);
    assert_all_ok(&out); // byte-faithful round trip is invariant 1
    let d = doc(&out);
    assert_eq!(d.stats.hunks, 2);
    // The CRLF and LF twins perform the same edit: one shape class.
    assert_eq!(d.stats.classes, 1, "normaliser must be CRLF-agnostic");
}

// ---------------------------------------------------------------- parser regressions

#[test]
fn deleted_lines_starting_with_dashes() {
    let r = TestRepo::new();
    r.write("cli.txt", b"--help\n--version\nplain\n");
    let base = r.commit_all("base");
    r.write("cli.txt", b"plain\n");
    let head = r.commit_all("head");
    let out = r.pipeline(&base, &head);
    assert_all_ok(&out);
    assert_eq!(doc(&out).stats.hunks, 1);
}

#[test]
fn insertion_at_top_and_deletion_to_empty() {
    let r = TestRepo::new();
    r.write("top.txt", b"body\n");
    r.write("empty_me.txt", b"a\nb\n");
    let base = r.commit_all("base");
    r.write("top.txt", b"header\nbody\n");
    r.write("empty_me.txt", b"");
    let head = r.commit_all("head");
    let out = r.pipeline(&base, &head);
    assert_all_ok(&out);
    let d = doc(&out);
    let top_hunk = d.hunks.iter().find(|h| h.file == "top.txt").unwrap();
    assert_eq!((top_hunk.old_start, top_hunk.old_count), (0, 0));
    let emptied = d.files.iter().find(|f| f.path == "empty_me.txt").unwrap();
    assert_eq!(emptied.disposition, Disposition::M);
}

// ---------------------------------------------------------------- submodule

#[test]
fn submodule_bump_is_counted_and_carried() {
    let r = TestRepo::new();
    r.write("README.md", b"top\n");
    let sha_a = "a".repeat(40);
    let sha_b = "b".repeat(40);
    r.git(&[
        "update-index",
        "--add",
        "--cacheinfo",
        &format!("160000,{sha_a},vendor/dep"),
    ]);
    r.git(&["commit", "-q", "-m", "base"]);
    let base = r.git(&["rev-parse", "HEAD"]);
    r.git(&[
        "update-index",
        "--cacheinfo",
        &format!("160000,{sha_b},vendor/dep"),
    ]);
    r.git(&["commit", "-q", "-m", "bump"]);
    let head = r.git(&["rev-parse", "HEAD"]);

    let out = r.pipeline(&base, &head);
    assert_all_ok(&out);
    let d = doc(&out);
    assert_eq!(d.stats.hunks, 1, "pseudo-hunk stays in the canonical count");
    assert_eq!(d.stats.submodules, 1);
    let sub = d.files[0].submodule.as_ref().unwrap();
    assert_eq!(sub.old.as_deref(), Some(sha_a.as_str()));
    assert_eq!(sub.new.as_deref(), Some(sha_b.as_str()));
}

// ---------------------------------------------------------------- typechange

#[cfg(unix)]
#[test]
fn typechange_file_to_symlink() {
    let r = TestRepo::new();
    r.write("thing", b"real content\n");
    r.write("target.txt", b"t\n");
    let base = r.commit_all("base");
    std::fs::remove_file(r.root.join("thing")).unwrap();
    std::os::unix::fs::symlink("target.txt", r.root.join("thing")).unwrap();
    let head = r.commit_all("now a symlink");
    let out = r.pipeline(&base, &head);
    assert_all_ok(&out);
    let d = doc(&out);
    let f = d.files.iter().find(|f| f.path == "thing").unwrap();
    assert_eq!(f.mode.as_deref(), Some("120000"));
    assert_eq!(f.old_mode.as_deref(), Some("100644"));
}

// ---------------------------------------------------------------- paths

#[test]
fn path_with_spaces_survives() {
    let r = TestRepo::new();
    r.write("docs/design notes b.md", b"one\n");
    let base = r.commit_all("base");
    r.write("docs/design notes b.md", b"two\n");
    let head = r.commit_all("head");
    let out = r.pipeline(&base, &head);
    assert_all_ok(&out);
    assert_eq!(doc(&out).files[0].path, "docs/design notes b.md");
}

// ---------------------------------------------------------------- config

#[test]
fn config_glob_marks_generated_with_provenance() {
    let r = TestRepo::new();
    r.write("migrations/0001_init.sql", b"create table t (id int);\n");
    r.write("src/main.rs", b"fn main() {}\n");
    let base = r.commit_all("base");
    r.write("migrations/0001_init.sql", b"create table t (id bigint);\n");
    r.write("src/main.rs", b"fn main() { run() }\n");
    let head = r.commit_all("head");

    let cfg = Config::parse("[classify]\ngenerated = [\"migrations/**\"]", "test").unwrap();
    let out = r.pipeline_with(&base, &head, &cfg);
    assert_all_ok(&out);
    let d = doc(&out);
    let mig = d
        .files
        .iter()
        .find(|f| f.path.starts_with("migrations/"))
        .unwrap();
    assert!(mig.generated);
    assert_eq!(mig.generated_by, Some(GeneratedBy::Config));
    let main = d.files.iter().find(|f| f.path == "src/main.rs").unwrap();
    assert!(!main.generated);
}

#[test]
fn config_not_generated_overrides_builtin() {
    let r = TestRepo::new();
    r.write("important.lock", b"v1\n");
    let base = r.commit_all("base");
    r.write("important.lock", b"v2\n");
    let head = r.commit_all("head");

    let out = r.pipeline(&base, &head);
    assert_eq!(doc(&out).files[0].generated_by, Some(GeneratedBy::Builtin));

    let cfg = Config::parse("[classify]\nnot_generated = [\"important.lock\"]", "test").unwrap();
    let out = r.pipeline_with(&base, &head, &cfg);
    let f = &doc(&out).files[0];
    assert!(!f.generated);
    assert_eq!(f.generated_by, None);
}

#[test]
fn config_can_never_shrink_enumeration() {
    // ADR 0012 regression: a config marking half the tree generated must not
    // remove a single file or hunk from the canonical enumeration.
    let r = TestRepo::new();
    for i in 0..6 {
        r.write(
            &format!("gen/file_{i}.txt"),
            format!("old {i}\n").as_bytes(),
        );
        r.write(
            &format!("src/file_{i}.txt"),
            format!("old {i}\n").as_bytes(),
        );
    }
    let base = r.commit_all("base");
    for i in 0..6 {
        r.write(
            &format!("gen/file_{i}.txt"),
            format!("new {i}\n").as_bytes(),
        );
        r.write(
            &format!("src/file_{i}.txt"),
            format!("new {i}\n").as_bytes(),
        );
    }
    let head = r.commit_all("head");

    let plain = r.pipeline(&base, &head);
    let cfg = Config::parse("[classify]\ngenerated = [\"gen/**\"]", "test").unwrap();
    let hinted = r.pipeline_with(&base, &head, &cfg);
    assert_all_ok(&plain);
    assert_all_ok(&hinted);
    assert_eq!(doc(&plain).stats.files, doc(&hinted).stats.files);
    assert_eq!(doc(&plain).stats.hunks, doc(&hinted).stats.hunks);
    assert_eq!(doc(&hinted).stats.files, 12);
    assert_eq!(doc(&hinted).files.iter().filter(|f| f.generated).count(), 6);
}

#[test]
fn gitattributes_marks_generated_as_attr() {
    let r = TestRepo::new();
    r.write(".gitattributes", b"schema.out linguist-generated\n");
    r.write("schema.out", b"AUTOGENERATED v1\n");
    let base = r.commit_all("base");
    r.write("schema.out", b"AUTOGENERATED v2\n");
    let head = r.commit_all("head");
    let out = r.pipeline(&base, &head);
    assert_all_ok(&out);
    let f = doc(&out)
        .files
        .iter()
        .find(|f| f.path == "schema.out")
        .unwrap();
    assert!(f.generated);
    assert_eq!(f.generated_by, Some(GeneratedBy::Attr));
}

// ---------------------------------------------------------------- document shape

#[test]
fn identical_edits_share_a_class_and_document_is_consistent() {
    let r = TestRepo::new();
    for name in ["a", "b", "c"] {
        r.write(
            &format!("src/{name}.txt"),
            b"use old_helper_name;\nother content\n",
        );
    }
    let base = r.commit_all("base");
    for name in ["a", "b", "c"] {
        r.write(
            &format!("src/{name}.txt"),
            b"use new_helper_name;\nother content\n",
        );
    }
    let head = r.commit_all("head");
    let out = r.pipeline(&base, &head);
    assert_all_ok(&out);
    let d = doc(&out);
    assert_eq!(d.stats.hunks, 3);
    assert_eq!(d.stats.classes, 1, "same edit in three files is one shape");
    let class = &d.classes[0];
    assert_eq!(class.hunk_ids.len(), 3);
    assert!(class.pure_substitution);
    // Round-trip through the frozen schema.
    let json = d.to_json_pretty().unwrap();
    let re = differential_engine::schema::PlanDocument::from_json(&json).unwrap();
    assert_eq!(&re, d);
}

/// A newline inside a path.
///
/// The invariants read every file's blobs back through `ObjectReader::blob`,
/// which passes the `<rev>:<path>` spec to `cat-file --batch` on stdin. Line
/// delimited, such a path would be split into two specs that both come back
/// "missing" — the blob would read as absent, invariant 1 would reconstruct
/// nothing, and nothing would say why. `-z` is what makes this pass (ADR 0021).
#[test]
fn a_newline_inside_a_path_still_reads_its_blobs() {
    let r = TestRepo::new();
    let odd = "we\nird.txt";
    r.write(odd, b"before\n");
    r.write("plain.txt", b"x\n");
    let base = r.commit_all("base");
    r.write(odd, b"after\n");
    let head = r.commit_all("head");

    let out = r.pipeline(&base, &head);
    assert_all_ok(&out);
    let d = doc(&out);
    assert_eq!(d.files.len(), 1);
    assert_eq!(d.files[0].path, odd, "the path survives round-trip");
    assert_eq!(d.hunks.len(), 1);
}

// ----------------------------------------------------------- git's pipes

/// A command that writes while it reads must not deadlock on a large input.
/// `check-attr --stdin` answers each path as it arrives, so an input bigger
/// than a pipe fills the output pipe before the input is written — and a
/// writer that finishes stdin before reading stdout waits forever.
#[test]
fn a_large_stdin_does_not_deadlock_against_git_writing_back() {
    use differential_engine::ports::AttributeSource;
    let r = TestRepo::new();
    r.write("f.txt", b"x\n");
    r.commit_all("base");
    let repo = r.repo();
    // ~1.2 MB of paths in, more than that out: well past any pipe buffer.
    let paths: Vec<Vec<u8>> = (0..20_000)
        .map(|i| format!("some/deep/directory/tree/file-{i:05}.txt").into_bytes())
        .collect();
    let (tx, rx) = std::sync::mpsc::channel();
    std::thread::spawn(move || {
        let refs: Vec<&[u8]> = paths.iter().map(Vec::as_slice).collect();
        let _ = tx.send(
            repo.check_attr("linguist-generated", &refs)
                .map(|v| v.len()),
        );
    });
    let answered = rx
        .recv_timeout(std::time::Duration::from_secs(60))
        .expect("check-attr deadlocked on a large stdin");
    assert_eq!(answered.unwrap(), 20_000);
}