girt 0.18.0

An incremental, idiomatic Rust implementation of Git
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
//! Original Git-generated acceptance fixtures for additive incremental batch publication.
use std::fs;
use std::path::PathBuf;
use std::sync::atomic::AtomicBool;

use girt::multi_pack_index::{BatchPublication, BatchRepackLimits};
use girt::retention::MaintenanceIsolation;
use girt::{InitKind, ObjectFormat, ObjectId, Repository};

#[path = "support/maintenance_git.rs"]
mod oracle;
use oracle::git;

struct PrivateFixture;
impl MaintenanceIsolation for PrivateFixture {
    type Guard = ();
    fn acquire(&mut self, _: &Repository) -> std::io::Result<()> {
        Ok(())
    }
}

struct Fixture {
    _root: tempfile::TempDir,
    repo: Repository,
    packs: Vec<PathBuf>,
    ids: Vec<ObjectId>,
}

impl Fixture {
    fn new(format: ObjectFormat) -> Self {
        let root = tempfile::tempdir().unwrap();
        let repo = Repository::init(format, root.path().join("r"), InitKind::Bare).unwrap();
        let mut packs = Vec::new();
        let mut ids = Vec::new();
        for (number, size) in [20, 30, 40].into_iter().enumerate() {
            let id = repo
                .loose_objects()
                .write_blob(&vec![b'A' + number as u8; size])
                .unwrap();
            let name = git(
                repo.git_dir(),
                &["pack-objects", "--compression=0", "objects/pack/pack"],
                format!("{id}\n").as_bytes(),
            );
            let path = repo.object_dir().join(format!(
                "pack/pack-{}.pack",
                std::str::from_utf8(&name).unwrap().trim()
            ));
            packs.push(path);
            ids.push(id);
        }
        git(repo.git_dir(), &["multi-pack-index", "write"], b"");
        Self {
            _root: root,
            repo,
            packs,
            ids,
        }
    }

    fn assert_old_storage(&self, before: &[Vec<u8>]) {
        for (path, bytes) in self.packs.iter().zip(before) {
            assert_eq!(fs::read(path).unwrap(), *bytes);
        }
        let objects = self.repo.objects().unwrap();
        for id in &self.ids {
            assert!(objects.read(*id).unwrap().is_some());
        }
    }
}

#[rstest::rstest]
#[case(ObjectFormat::Sha1)]
#[case(ObjectFormat::Sha256)]
fn publishes_one_batch_and_midx_without_retiring_any_pack(#[case] format: ObjectFormat) {
    let fixture = Fixture::new(format);
    let before: Vec<_> = fixture
        .packs
        .iter()
        .map(|path| fs::read(path).unwrap())
        .collect();
    let report = fixture
        .repo
        .repack_incremental_batch(
            &mut PrivateFixture,
            0,
            BatchRepackLimits::default(),
            &AtomicBool::new(false),
        )
        .unwrap();
    assert_eq!(report.publication, BatchPublication::Pair);
    assert!(report.index_published);
    assert_eq!(report.written.unwrap().objects, 3);
    fixture.assert_old_storage(&before);
    git(fixture.repo.git_dir(), &["multi-pack-index", "verify"], b"");
    git(fixture.repo.git_dir(), &["fsck", "--full"], b"");
    let again = fixture
        .repo
        .repack_incremental_batch(
            &mut PrivateFixture,
            0,
            BatchRepackLimits::default(),
            &AtomicBool::new(false),
        )
        .unwrap();
    assert_eq!(again.publication, BatchPublication::None);
    assert!(!again.index_published);
}

#[test]
fn write_limit_and_precancellation_preserve_prior_storage() {
    let fixture = Fixture::new(ObjectFormat::Sha1);
    let before: Vec<_> = fixture
        .packs
        .iter()
        .map(|path| fs::read(path).unwrap())
        .collect();
    let mut limits = BatchRepackLimits::default();
    limits.write.max_input_bytes = 1;
    let failure = fixture
        .repo
        .repack_incremental_batch(&mut PrivateFixture, 0, limits, &AtomicBool::new(false))
        .unwrap_err();
    assert_eq!(failure.report.publication, BatchPublication::None);
    assert!(!failure.report.index_published);
    let failure = fixture
        .repo
        .repack_incremental_batch(
            &mut PrivateFixture,
            0,
            BatchRepackLimits::default(),
            &AtomicBool::new(true),
        )
        .unwrap_err();
    assert_eq!(failure.report.publication, BatchPublication::None);
    fixture.assert_old_storage(&before);
}

#[test]
fn isolation_refusal_cannot_publish() {
    struct Refuse;
    impl MaintenanceIsolation for Refuse {
        type Guard = ();
        fn acquire(&mut self, _: &Repository) -> std::io::Result<()> {
            Err(std::io::Error::other("fixture isolation refused"))
        }
    }
    let fixture = Fixture::new(ObjectFormat::Sha1);
    let before = fs::read(fixture.repo.object_dir().join("pack/multi-pack-index")).unwrap();
    let failure = fixture
        .repo
        .repack_incremental_batch(
            &mut Refuse,
            0,
            BatchRepackLimits::default(),
            &AtomicBool::new(false),
        )
        .unwrap_err();
    assert_eq!(failure.report.publication, BatchPublication::None);
    assert_eq!(
        before,
        fs::read(fixture.repo.object_dir().join("pack/multi-pack-index")).unwrap()
    );
}

#[test]
fn duplicate_promisor_object_cannot_enter_an_ordinary_batch_pack() {
    let fixture = Fixture::new(ObjectFormat::Sha1);
    let name = git(
        fixture.repo.git_dir(),
        &[
            "pack-objects",
            "--compression=9",
            "--no-reuse-object",
            "objects/pack/pack",
        ],
        format!("{}\n", fixture.ids[0]).as_bytes(),
    );
    let path = fixture.repo.object_dir().join(format!(
        "pack/pack-{}.pack",
        std::str::from_utf8(&name).unwrap().trim()
    ));
    fs::write(path.with_extension("promisor"), b"promisor fixture\n").unwrap();
    // The ordinary pack owns this identity in the MIDX; classification must still notice
    // the duplicate promisor copy, rather than relying only on the selected owner.
    let preferred = format!(
        "--preferred-pack={}",
        fixture.packs[0].file_name().unwrap().to_str().unwrap()
    );
    git(
        fixture.repo.git_dir(),
        &["multi-pack-index", "write", &preferred],
        b"",
    );
    let report = fixture
        .repo
        .repack_incremental_batch(
            &mut PrivateFixture,
            0,
            BatchRepackLimits::default(),
            &AtomicBool::new(false),
        )
        .unwrap();
    assert_eq!(report.protected_objects, 1);
    let written = report.written.unwrap();
    assert_eq!(written.objects, 2);
    let index = fixture
        .repo
        .object_dir()
        .join(format!("pack/pack-{}.idx", written.checksum));
    let names = git(
        fixture.repo.git_dir(),
        &["show-index"],
        &fs::read(index).unwrap(),
    );
    assert!(
        !std::str::from_utf8(&names)
            .unwrap()
            .contains(&fixture.ids[0].to_string())
    );
    assert_eq!(
        fs::read(path.with_extension("promisor")).unwrap(),
        b"promisor fixture\n"
    );
    git(fixture.repo.git_dir(), &["fsck", "--full"], b"");
}

#[test]
fn admission_limits_leave_midx_and_pack_inventory_unchanged() {
    for format in [ObjectFormat::Sha1, ObjectFormat::Sha256] {
        for open in [
            girt::OpenLimits::default().with_metadata_bytes(0),
            girt::OpenLimits::default()
                .with_configuration(girt::config::ResolveLimits::default().with_bytes(0)),
        ] {
            let fixture = Fixture::new(format);
            let directory = fixture.repo.object_dir().join("pack");
            let mut before: Vec<_> = fs::read_dir(&directory)
                .unwrap()
                .map(|entry| entry.unwrap().file_name())
                .collect();
            before.sort();
            let index = fs::read(directory.join("multi-pack-index")).unwrap();
            let failure = fixture
                .repo
                .repack_incremental_batch(
                    &mut PrivateFixture,
                    0,
                    BatchRepackLimits::default().with_open(open),
                    &AtomicBool::new(false),
                )
                .unwrap_err();
            assert!(matches!(
                failure.cause,
                girt::multi_pack_index::BatchRepackCause::Open(_)
            ));
            assert_eq!(failure.report.publication, BatchPublication::None);
            assert!(!failure.report.index_published);
            let mut after: Vec<_> = fs::read_dir(&directory)
                .unwrap()
                .map(|entry| entry.unwrap().file_name())
                .collect();
            after.sort();
            assert_eq!(before, after);
            assert_eq!(fs::read(directory.join("multi-pack-index")).unwrap(), index);
        }
    }
}

#[test]
fn cancellation_acquired_with_isolation_reaches_batch_opener() {
    struct CancelOnAcquire<'a>(&'a AtomicBool);
    impl MaintenanceIsolation for CancelOnAcquire<'_> {
        type Guard = ();
        fn acquire(&mut self, _: &Repository) -> std::io::Result<()> {
            self.0.store(true, std::sync::atomic::Ordering::Relaxed);
            Ok(())
        }
    }
    let fixture = Fixture::new(ObjectFormat::Sha1);
    let cancel = AtomicBool::new(false);
    let failure = fixture
        .repo
        .repack_incremental_batch(
            &mut CancelOnAcquire(&cancel),
            0,
            BatchRepackLimits::default(),
            &cancel,
        )
        .unwrap_err();
    let girt::multi_pack_index::BatchRepackCause::Open(source) = failure.cause else {
        panic!("expected controlled opener cancellation");
    };
    assert!(matches!(*source, girt::repository::OpenError::Cancelled));
    assert_eq!(failure.report.publication, BatchPublication::None);
    assert!(!failure.report.index_published);
}

#[test]
fn subset_batch_copies_only_owners_in_selected_pack_ordinals() {
    for format in [ObjectFormat::Sha1, ObjectFormat::Sha256] {
        let fixture = Fixture::new(format);
        let target = fs::metadata(&fixture.packs[1]).unwrap().len() + 1;
        let before: Vec<_> = fixture
            .packs
            .iter()
            .map(|path| fs::read(path).unwrap())
            .collect();
        let report = fixture
            .repo
            .repack_incremental_batch(
                &mut PrivateFixture,
                target,
                BatchRepackLimits::default(),
                &AtomicBool::new(false),
            )
            .unwrap();
        assert_eq!(report.batch.packs.len(), 2);
        assert!(
            !report
                .batch
                .packs
                .iter()
                .any(|pack| pack.path == fixture.packs[2])
        );
        let written = report.written.unwrap();
        assert_eq!(written.objects, 2);
        let index = fixture
            .repo
            .object_dir()
            .join(format!("pack/pack-{}.idx", written.checksum));
        let names = git(
            fixture.repo.git_dir(),
            &["show-index"],
            &fs::read(index).unwrap(),
        );
        let names = std::str::from_utf8(&names).unwrap();
        assert!(names.contains(&fixture.ids[0].to_string()));
        assert!(names.contains(&fixture.ids[1].to_string()));
        assert!(!names.contains(&fixture.ids[2].to_string()));
        fixture.assert_old_storage(&before);
        git(fixture.repo.git_dir(), &["multi-pack-index", "verify"], b"");
        git(
            fixture.repo.git_dir(),
            &["fsck", "--strict", "--no-dangling"],
            b"",
        );
    }
}

#[test]
fn checksum_valid_corrupt_claim_in_unselected_pack_still_refuses_before_publication() {
    use sha1::{Digest, Sha1};
    let fixture = Fixture::new(ObjectFormat::Sha1);
    let path = fixture.repo.object_dir().join("pack/multi-pack-index");
    let mut bytes = fs::read(&path).unwrap();
    let parsed = girt::multi_pack_index::MultiPackIndex::parse(&bytes, ObjectFormat::Sha1).unwrap();
    let ordinal = parsed
        .pack_names()
        .iter()
        .position(|name| {
            *name
                == fixture.packs[2]
                    .with_extension("idx")
                    .file_name()
                    .unwrap()
                    .to_str()
                    .unwrap()
        })
        .unwrap();
    let chunks = bytes[6] as usize;
    let row = bytes[12..12 + chunks * 12]
        .as_chunks::<12>()
        .0
        .iter()
        .find(|row| &row[..4] == b"OOFF")
        .unwrap();
    let start = u64::from_be_bytes(row[4..12].try_into().unwrap()) as usize;
    let entry = bytes[start..]
        .as_chunks::<8>()
        .0
        .iter()
        .take(3)
        .position(|entry| u32::from_be_bytes(entry[..4].try_into().unwrap()) as usize == ordinal)
        .unwrap();
    let offset = start + entry * 8 + 4;
    let old = u32::from_be_bytes(bytes[offset..offset + 4].try_into().unwrap());
    bytes[offset..offset + 4].copy_from_slice(&(old + 1).to_be_bytes());
    let end = bytes.len() - 20;
    let checksum = Sha1::digest(&bytes[..end]);
    bytes[end..].copy_from_slice(&checksum);
    fs::rename(&path, fixture._root.path().join("original-midx")).unwrap();
    fs::write(&path, &bytes).unwrap();
    let before: Vec<_> = fixture
        .packs
        .iter()
        .map(|path| fs::read(path).unwrap())
        .collect();
    let target = fs::metadata(&fixture.packs[1]).unwrap().len() + 1;
    let failure = fixture
        .repo
        .repack_incremental_batch(
            &mut PrivateFixture,
            target,
            BatchRepackLimits::default(),
            &AtomicBool::new(false),
        )
        .unwrap_err();
    assert_eq!(failure.report.publication, BatchPublication::None);
    assert!(matches!(
        failure.cause,
        girt::multi_pack_index::BatchRepackCause::Plan(_)
    ));
    assert_eq!(fs::read(path).unwrap(), bytes);
    for (path, expected) in fixture.packs.iter().zip(before) {
        assert_eq!(fs::read(path).unwrap(), expected);
    }
}