brygge 0.8.1

Carry version-control history from one system to another: decode Git, Mercurial, Subversion and CVS into a portable, verifiable IR artifact, with every inference marked and every loss recorded, and encode it into a target.
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
//! Level 1: unit tests for the pure trailer/identity helpers (no process, no artifact).

#![allow(clippy::unwrap_used, clippy::expect_used, clippy::indexing_slicing)]

use super::*;
use brygge_ir::Text;

#[test]
fn a_present_identity_needs_nothing_supplied_or_altered() {
    let identity = Identity {
        name: Text::utf8("A U Thor"),
        email: Some(Text::utf8("a@example.com")),
    };
    let mut supplied = Supplied::default();
    let mut altered = Altered::default();
    let line = identity_line(Some(&identity), &mut supplied, &mut altered, "author");
    assert_eq!(line, "A U Thor <a@example.com>");
    assert!(supplied.trailer().is_none());
    assert!(altered.trailer().is_none());
}

#[test]
fn a_missing_email_is_supplied_as_empty_and_marked() {
    // Review 136 F-1: only the email is missing here, so the mark must name `author-email`, not the
    // bare `author` — that would claim brygge invented the whole identity, which it did not (the name
    // is the IR's own).
    let identity = Identity {
        name: Text::utf8("A U Thor"),
        email: None,
    };
    let mut supplied = Supplied::default();
    let mut altered = Altered::default();
    let line = identity_line(Some(&identity), &mut supplied, &mut altered, "author");
    assert_eq!(line, "A U Thor <>");
    assert_eq!(supplied.trailer().unwrap(), "Brygge-Supplied: author-email");
    assert!(altered.trailer().is_none());
}

#[test]
fn a_missing_identity_is_supplied_under_the_bare_field_not_email() {
    // The other half of F-1: the whole identity absent (distinct from only its email) is marked under
    // the bare field name.
    let mut supplied = Supplied::default();
    let mut altered = Altered::default();
    let line = identity_line(None, &mut supplied, &mut altered, "tagger");
    assert_eq!(line, "unknown <>");
    assert_eq!(supplied.trailer().unwrap(), "Brygge-Supplied: tagger");
    assert!(altered.trailer().is_none());
}

#[test]
fn a_missing_identity_entirely_is_supplied_and_marked() {
    let mut supplied = Supplied::default();
    let mut altered = Altered::default();
    let line = identity_line(None, &mut supplied, &mut altered, "committer");
    assert_eq!(line, "unknown <>");
    assert_eq!(supplied.trailer().unwrap(), "Brygge-Supplied: committer");
    assert!(altered.trailer().is_none());
}

#[test]
fn a_newline_in_a_name_is_folded_to_a_space_and_marked_altered() {
    let identity = Identity {
        name: Text::utf8("A U\nThor"),
        email: Some(Text::utf8("a@example.com")),
    };
    let mut supplied = Supplied::default();
    let mut altered = Altered::default();
    let line = identity_line(Some(&identity), &mut supplied, &mut altered, "author");
    assert_eq!(line, "A U Thor <a@example.com>");
    assert!(supplied.trailer().is_none());
    assert_eq!(
        altered.trailer().unwrap(),
        "Brygge-Altered: author-name (newline folded)"
    );
}

#[test]
fn a_non_utf8_name_is_replaced_with_unknown_and_marked_altered() {
    let identity = Identity {
        name: Text {
            bytes: vec![0xff, 0xfe],
            encoding: None,
        },
        email: Some(Text::utf8("a@example.com")),
    };
    let mut supplied = Supplied::default();
    let mut altered = Altered::default();
    let line = identity_line(Some(&identity), &mut supplied, &mut altered, "author");
    assert_eq!(line, "unknown <a@example.com>");
    assert!(supplied.trailer().is_none());
    assert_eq!(
        altered.trailer().unwrap(),
        "Brygge-Altered: author-name (not UTF-8, replaced)"
    );
}

#[test]
fn a_non_utf8_email_is_replaced_with_empty_and_marked_altered() {
    let identity = Identity {
        name: Text::utf8("A U Thor"),
        email: Some(Text {
            bytes: vec![0xff, 0xfe],
            encoding: None,
        }),
    };
    let mut supplied = Supplied::default();
    let mut altered = Altered::default();
    let line = identity_line(Some(&identity), &mut supplied, &mut altered, "committer");
    assert_eq!(line, "A U Thor <>");
    assert!(supplied.trailer().is_none());
    assert_eq!(
        altered.trailer().unwrap(),
        "Brygge-Altered: committer-email (not UTF-8, replaced)"
    );
}

#[test]
fn angle_brackets_in_an_email_are_removed_and_marked_altered() {
    let identity = Identity {
        name: Text::utf8("A U Thor"),
        email: Some(Text::utf8("a<b>@example.com")),
    };
    let mut supplied = Supplied::default();
    let mut altered = Altered::default();
    let line = identity_line(Some(&identity), &mut supplied, &mut altered, "author");
    assert_eq!(line, "A U Thor <ab@example.com>");
    assert_eq!(
        altered.trailer().unwrap(),
        "Brygge-Altered: author-email (angle bracket removed)"
    );
}

#[test]
fn a_nul_in_a_name_is_removed_and_marked_altered() {
    let identity = Identity {
        name: Text::utf8("A U\0Thor"),
        email: Some(Text::utf8("a@example.com")),
    };
    let mut supplied = Supplied::default();
    let mut altered = Altered::default();
    let line = identity_line(Some(&identity), &mut supplied, &mut altered, "author");
    assert_eq!(line, "A UThor <a@example.com>");
    assert_eq!(
        altered.trailer().unwrap(),
        "Brygge-Altered: author-name (NUL removed)"
    );
}

#[test]
fn multiple_alterations_on_one_field_are_combined_in_one_reason() {
    let identity = Identity {
        name: Text::utf8("A\nU <Thor>"),
        email: Some(Text::utf8("a@example.com")),
    };
    let mut supplied = Supplied::default();
    let mut altered = Altered::default();
    let _ = identity_line(Some(&identity), &mut supplied, &mut altered, "author");
    assert_eq!(
        altered.trailer().unwrap(),
        "Brygge-Altered: author-name (newline folded, angle bracket removed)"
    );
}

#[test]
fn a_present_time_with_a_negative_offset_formats_correctly() {
    let time = Time {
        seconds: 1_700_000_000,
        offset_minutes: Some(-330),
    };
    let mut supplied = Supplied::default();
    let w = when(Some(time), &mut supplied, "author-time", "author-time-zone");
    assert_eq!(w, "1700000000 -0530");
    assert!(supplied.trailer().is_none());
}

#[test]
fn a_missing_time_zone_is_supplied_as_plus_0000_and_marked() {
    let time = Time {
        seconds: 1_700_000_000,
        offset_minutes: None,
    };
    let mut supplied = Supplied::default();
    let w = when(Some(time), &mut supplied, "author-time", "author-time-zone");
    assert_eq!(w, "1700000000 +0000");
    assert_eq!(
        supplied.trailer().unwrap(),
        "Brygge-Supplied: author-time-zone"
    );
}

#[test]
fn a_missing_time_entirely_is_supplied_as_epoch_zero_and_both_marked() {
    let mut supplied = Supplied::default();
    let w = when(None, &mut supplied, "author-time", "author-time-zone");
    assert_eq!(w, "0 +0000");
    assert_eq!(
        supplied.trailer().unwrap(),
        "Brygge-Supplied: author-time, author-time-zone"
    );
}

#[test]
fn an_ordinary_path_is_unquoted() {
    assert_eq!(path_token("src/main.rs"), "src/main.rs");
    assert_eq!(
        path_token("a path with spaces.txt"),
        "a path with spaces.txt"
    );
}

#[test]
fn a_path_starting_with_a_quote_is_quoted_and_escaped() {
    assert_eq!(path_token("\"quoted\".txt"), "\"\\\"quoted\\\".txt\"");
}

#[test]
fn a_path_with_a_newline_is_quoted_and_escaped() {
    assert_eq!(path_token("a\nb"), "\"a\\nb\"");
}

#[test]
fn a_stated_status_has_no_derived_trailer() {
    assert!(derived_trailer("atom", &EpistemicStatus::Stated).is_none());
}

#[test]
fn a_derived_status_renders_its_kind_and_params() {
    use brygge_ir::{Derivation, DerivationKind};
    use std::collections::BTreeMap;
    let mut params = BTreeMap::new();
    params.insert("window_secs".to_string(), "180".to_string());
    let status = EpistemicStatus::Derived(Derivation {
        kind: DerivationKind::ReconstructedChangeset,
        by: "brygge-decode-cvs".to_string(),
        decoder_version: "0.5.0".to_string(),
        params,
        confidence: None,
    });
    let line = derived_trailer("atom", &status).unwrap();
    assert_eq!(
        line,
        "Brygge-Derived: atom reconstructed-changeset window_secs=180"
    );
}

// ---- review 137 F-7: one readable source-id form per source kind, with a hex: fallback -------------

#[test]
fn a_git_commit_id_is_forty_lowercase_hex_no_prefix() {
    let id = [0x01u8; 20];
    assert_eq!(
        source_id_text(&SourceKind::Git, &id),
        "0101010101010101010101010101010101010101"
    );
}

#[test]
fn an_hg_node_is_forty_lowercase_hex_no_prefix() {
    let id = [0xabu8; 20];
    assert_eq!(
        source_id_text(&SourceKind::Hg, &id),
        "abababababababababababababababababababab"
    );
}

#[test]
fn a_git_id_of_the_wrong_length_falls_back_to_hex_prefixed() {
    assert_eq!(source_id_text(&SourceKind::Git, &[0x01, 0x02]), "hex:0102");
}

#[test]
fn an_svn_revision_is_r_decimal() {
    let id = 1234u64.to_be_bytes();
    assert_eq!(source_id_text(&SourceKind::Svn, &id), "r1234");
}

#[test]
fn an_svn_id_of_the_wrong_length_falls_back_to_hex_prefixed() {
    assert_eq!(source_id_text(&SourceKind::Svn, &[0x00, 0x01]), "hex:0001");
}

/// Review 147 F-4: `--branch-lines`' extended SVN form, `r<N> <root>` — round trips through
/// `parse_source_id`, the exact inverse `source_id_text` is specified against.
#[test]
fn an_svn_branch_lines_id_round_trips_with_its_root() {
    let mut id = 1234u64.to_be_bytes().to_vec();
    id.extend_from_slice(b"branches/1.5");
    let text = source_id_text(&SourceKind::Svn, &id);
    assert_eq!(text, "r1234 branches/1.5");
    assert_eq!(parse_source_id(&SourceKind::Svn, &text), Some(id));
}

/// The one fallback the handoff names explicitly: a root containing a space (which the `r<N> <root>`
/// form's own separator could not be told apart from) falls back to `hex:`, and still round trips.
#[test]
fn an_svn_branch_lines_root_with_a_space_falls_back_to_hex() {
    let mut id = 5u64.to_be_bytes().to_vec();
    id.extend_from_slice(b"branches/with space");
    let text = source_id_text(&SourceKind::Svn, &id);
    assert!(text.starts_with("hex:"), "{text}");
    assert_eq!(parse_source_id(&SourceKind::Svn, &text), Some(id));
}

#[test]
fn a_cvs_branch_point_id_is_written_as_is() {
    let id = b"branch-point:OPENBSD_2_0";
    assert_eq!(
        source_id_text(&SourceKind::Cvs, id),
        "branch-point:OPENBSD_2_0"
    );
}

#[test]
fn a_cvs_changeset_id_is_one_pair_per_line_as_a_continuation() {
    let id = b"a.c@1.1\nb.c@1.2";
    assert_eq!(source_id_text(&SourceKind::Cvs, id), "a.c@1.1\n b.c@1.2");
}

#[test]
fn a_cvs_changeset_id_with_many_pairs_continues_each_on_its_own_line() {
    let id = b"a.c@1.1\nb.c@1.2\nc.c@1.3";
    assert_eq!(
        source_id_text(&SourceKind::Cvs, id),
        "a.c@1.1\n b.c@1.2\n c.c@1.3"
    );
}

#[test]
fn a_non_utf8_cvs_id_falls_back_to_hex() {
    let id = [0xffu8, 0xfeu8];
    assert_eq!(source_id_text(&SourceKind::Cvs, &id), "hex:fffe");
}

#[test]
fn a_cvs_pair_missing_an_at_sign_falls_back_to_hex() {
    let id = b"not-a-pair";
    let expected_hex: String = id.iter().map(|b| format!("{b:02x}")).collect();
    assert_eq!(
        source_id_text(&SourceKind::Cvs, id),
        format!("hex:{expected_hex}")
    );
}

#[test]
fn a_cvs_pair_with_a_leading_space_falls_back_to_hex() {
    let id = b"a.c@1.1\n b.c@1.2"; // the second "pair" already looks like a continuation line
    let expected_hex: String = id.iter().map(|b| format!("{b:02x}")).collect();
    assert_eq!(
        source_id_text(&SourceKind::Cvs, id),
        format!("hex:{expected_hex}")
    );
}

#[test]
fn an_empty_cvs_pair_falls_back_to_hex() {
    let id = b"a.c@1.1\n\nb.c@1.2"; // two adjacent LFs: an empty "pair"
    let expected_hex: String = id.iter().map(|b| format!("{b:02x}")).collect();
    assert_eq!(
        source_id_text(&SourceKind::Cvs, id),
        format!("hex:{expected_hex}")
    );
}

#[test]
fn an_other_source_kind_uses_the_printable_ascii_or_hex_fallback() {
    assert_eq!(
        source_id_text(&SourceKind::Other("bzr".to_string()), b"abcDEF-0123"),
        "abcDEF-0123"
    );
    let id = [0x01u8, 0xffu8, 0x00u8, 0x41u8];
    assert_eq!(
        source_id_text(&SourceKind::Other("bzr".to_string()), &id),
        "hex:01ff0041"
    );
}

#[test]
fn an_empty_id_falls_back_to_hex_not_an_empty_string() {
    // An empty value is not "every byte printable ASCII" (vacuous truth) in a way that should ever print
    // as an empty, invisible trailer value — write it as hex (the empty string) explicitly instead.
    assert_eq!(
        source_id_text(&SourceKind::Other("bzr".to_string()), &[]),
        "hex:"
    );
}

#[test]
fn truncate_for_refusal_leaves_a_short_id_unchanged() {
    assert_eq!(truncate_for_refusal("short"), "short");
}

#[test]
fn truncate_for_refusal_cuts_a_long_cvs_id_to_eighty_chars_plus_ellipsis() {
    let long_id: String = (0..200).map(|i| format!("f{i}.c@1.1\n")).collect();
    let truncated = truncate_for_refusal(&long_id);
    assert_eq!(truncated.chars().count(), 81);
    assert!(truncated.ends_with('…'));
    assert!(long_id.starts_with(&truncated[..truncated.len() - '…'.len_utf8()]));
}

#[test]
fn source_kind_labels_match_the_trailer_vocabulary() {
    assert_eq!(source_kind_label(&SourceKind::Git), "git");
    assert_eq!(source_kind_label(&SourceKind::Hg), "hg");
    assert_eq!(source_kind_label(&SourceKind::Svn), "svn");
    assert_eq!(source_kind_label(&SourceKind::Cvs), "cvs");
}

#[test]
fn supplied_and_altered_fold_into_per_field_totals() {
    let mut supplied = Supplied::default();
    supplied.mark("author");
    supplied.mark("author");
    supplied.mark("committer");
    let mut totals: std::collections::BTreeMap<String, u64> = std::collections::BTreeMap::new();
    supplied.fold_into(&mut totals);
    assert_eq!(totals.get("author"), Some(&2));
    assert_eq!(totals.get("committer"), Some(&1));

    let mut altered = Altered::default();
    altered.mark("author-name", "not UTF-8, replaced");
    let mut altered_totals: std::collections::BTreeMap<String, u64> =
        std::collections::BTreeMap::new();
    altered.fold_into(&mut altered_totals);
    assert_eq!(altered_totals.get("author-name"), Some(&1));
}