girt 0.10.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
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
//! Native local advertisement and reachable-pack construction.

use std::collections::VecDeque;
use std::num::NonZeroU32;
use std::ops::ControlFlow;

use super::{
    AdvertisedRef, Advertisement, FetchError, FetchLimits, KnownHistory, LocalFetchProgress,
    NativeContents, ReceivedFetch,
};
use crate::refs::{RefName, Target};
use crate::transport::TransportControl;
use crate::{IdMap, IdSet, ObjectId, ObjectKind, PackWriteLimits, Repository, UnverifiedObject};

#[cfg(test)]
pub(super) fn receive(
    source: &Repository,
    select: impl FnOnce(&Advertisement) -> Vec<ObjectId>,
    known: &KnownHistory,
    limits: FetchLimits,
    control: TransportControl<'_>,
    progress: impl FnMut(&[u8]) -> ControlFlow<()>,
    observe: impl FnMut(LocalFetchProgress),
) -> Result<ReceivedFetch, FetchError> {
    receive_depth(
        source, select, known, None, limits, control, progress, observe,
    )
}

#[cfg(test)]
#[expect(
    clippy::too_many_arguments,
    reason = "local transfer keeps explicit selection and progress policy"
)]
pub(super) fn receive_depth(
    source: &Repository,
    select: impl FnOnce(&Advertisement) -> Vec<ObjectId>,
    known: &KnownHistory,
    depth: Option<NonZeroU32>,
    limits: FetchLimits,
    control: TransportControl<'_>,
    progress: impl FnMut(&[u8]) -> ControlFlow<()>,
    observe: impl FnMut(LocalFetchProgress),
) -> Result<ReceivedFetch, FetchError> {
    receive_depth_observed(
        source,
        select,
        known,
        depth,
        limits,
        control,
        progress,
        observe,
        |_| {},
    )
}

#[expect(
    clippy::too_many_arguments,
    reason = "local transfer keeps explicit progress policy"
)]
pub(super) fn receive_depth_observed(
    source: &Repository,
    select: impl FnOnce(&Advertisement) -> Vec<ObjectId>,
    known: &KnownHistory,
    depth: Option<NonZeroU32>,
    limits: FetchLimits,
    control: TransportControl<'_>,
    mut progress: impl FnMut(&[u8]) -> ControlFlow<()>,
    mut observe: impl FnMut(LocalFetchProgress),
    mut validation: impl FnMut(super::ValidationProgress),
) -> Result<ReceivedFetch, FetchError> {
    check(control)?;
    if depth.is_some_and(|depth| depth.get() != 1) {
        return Err(FetchError::Unsupported("native local depth beyond one"));
    }
    if depth.is_some() && !known.shallow.is_empty() {
        return Err(FetchError::Unsupported("native local shallow refresh"));
    }
    if !source.shallow_roots().is_empty() {
        return Err(FetchError::Unsupported("shallow local source"));
    }
    let store = source
        .objects(Default::default())
        .map_err(FetchError::Destination)?;
    let advertisement = advertise_store(source, &store, limits, control)?;
    let wants = select(&advertisement);
    if depth.is_some() && wants.iter().copied().collect::<IdSet>().len() != 1 {
        return Err(FetchError::Unsupported("native local depth selection"));
    }
    let empty = KnownHistory::default();
    let known = if known.applies_to(&wants) {
        known
    } else {
        &empty
    };
    if wants.len() > limits.max_wants {
        return Err(FetchError::Limit("wants"));
    }
    let offered: IdSet = advertisement
        .refs
        .iter()
        .map(|reference| reference.id)
        .collect();
    for id in &wants {
        if !offered.contains(id) {
            return Err(FetchError::Unadvertised(*id));
        }
        if id.format() != source.object_format() {
            return Err(FetchError::Unsupported("local object format"));
        }
    }
    observe(LocalFetchProgress::Reading { objects: 0 });
    check(control)?;
    if wants.is_empty() {
        observe(LocalFetchProgress::Complete);
        return Ok(ReceivedFetch::native(
            advertisement,
            wants,
            NativeContents {
                format: source.object_format(),
                pack: Vec::new().into(),
                index: Vec::new(),
                checksum: None,
                objects: 0,
                dependencies: Vec::new(),
                complete: IdMap::default(),
                shallow: Vec::new(),
                limits,
            },
        ));
    }
    let mut pending: VecDeque<_> = wants.iter().copied().map(|id| (id, None)).collect();
    // Each object's actual kind once read. Every edge naming it must require that kind.
    let mut observed = IdMap::<ObjectKind>::default();
    let mut selected = Vec::<ObjectId>::new();
    let mut dependencies = Vec::new();
    let mut boundaries = super::boundaries::TreeBoundaries::new(known, limits);
    let mut shallow = Vec::new();
    let mut bytes = 0_usize;
    let mut edges = 0_usize;
    let mut read = limits.known_read;
    read.max_object_bytes = read.max_object_bytes.min(limits.max_object_bytes);
    prefetch::with_prefetch(
        &store,
        read,
        control.cancel,
        limits.max_threads,
        |prefetch| {
            while let Some((id, kind)) = pending.pop_front() {
                prefetch.popped();
                check(control)?;
                if let Some(&actual) = observed.get(&id) {
                    if kind.is_some_and(|kind| kind != actual) {
                        return Err(FetchError::Kind(id));
                    }
                    continue;
                }
                if observed.len() >= limits.max_objects {
                    return Err(FetchError::Limit("local objects"));
                }
                let prefetch::Fetched {
                    kind: object_kind,
                    length,
                    payload,
                } = prefetch.take(id, kind, &pending, &observed)?;
                if kind.is_some_and(|kind| kind != object_kind) {
                    return Err(FetchError::Kind(id));
                }
                observed.insert(id, object_kind);
                // Readers synthesize the empty tree even without a Git object file. Materialize it
                // whenever this walk reaches it, rather than treating it as stored destination
                // data.
                let available = depth.is_none()
                    && limits.max_haves > 0
                    && known.kind(id) == Some(object_kind)
                    && !(object_kind == ObjectKind::Tree && length == 0);
                if available
                    && known.complete_kind(id) == Some(object_kind)
                    && !known.shallow.contains(&id)
                {
                    dependencies.push(id);
                    continue;
                }
                if depth.is_some() && id == wants[0] && object_kind != ObjectKind::Commit {
                    return Err(FetchError::Unsupported(
                        "native local depth requires commit tip",
                    ));
                }
                bytes = bytes
                    .checked_add(length)
                    .ok_or(FetchError::Limit("local bytes"))?;
                if bytes > limits.max_decode_bytes {
                    return Err(FetchError::Limit("local bytes"));
                }
                // Blobs have no edges. Their framing is checked now and their identity during
                // import. Selection only follows edges: every selected object's
                // identity is checked when the destination imports it, so this walk
                // does not hash payloads it never returns.
                let object = if object_kind == ObjectKind::Blob {
                    UnverifiedObject {
                        kind: object_kind,
                        data: Default::default(),
                    }
                } else if let Some(object) = payload {
                    object
                } else {
                    store
                        .read_unverified(id, read, control.cancel)
                        .map_err(|source| FetchError::LocalRead { id, source })?
                        .ok_or(FetchError::Missing(id))?
                };
                if depth.is_none() {
                    match object_kind {
                        ObjectKind::Commit => {
                            if let Ok(commit) =
                                crate::Commit::parse(source.object_format(), object.data())
                            {
                                boundaries.pair_commit(known, commit.tree(), commit.parents());
                            }
                        }
                        ObjectKind::Tree => {
                            boundaries.pair_tree(known, id, object.data());
                        }
                        _ => {}
                    }
                }
                crate::edges::visit(id, object.kind(), object.data(), |target, kind| {
                    check(control)?;
                    if depth.is_some() && id == wants[0] && kind == ObjectKind::Commit {
                        // Depth one cuts only the selected commit's parent edges. Its tree and all
                        // reachable content stay in the self-contained pack.
                        if shallow.is_empty() {
                            shallow.push(id);
                        }
                        return Ok(());
                    }
                    edges += 1;
                    if edges > limits.max_connectivity_edges {
                        return Err(FetchError::Limit("local edges"));
                    }
                    // Most edges name an object already read. Check it now rather than queueing it,
                    // as trees repeat their unchanged entries across a history.
                    if let Some(&actual) = observed.get(&target) {
                        return if actual == kind {
                            Ok(())
                        } else {
                            Err(FetchError::Kind(target))
                        };
                    }
                    // A complete parent's tree names this object. Send it again only if the
                    // destination lost it.
                    if boundaries.kind(target) == Some(kind)
                        && known.stored_kind(target) == Some(kind)
                    {
                        observed.insert(target, kind);
                        dependencies.push(target);
                        return Ok(());
                    }
                    pending.push_back((target, Some(kind)));
                    Ok(())
                })?;
                if available {
                    // An interrupted unpack can leave this object without its descendants. Walking
                    // its source edges above proves those descendants are
                    // either selected or complete.
                    dependencies.push(id);
                } else {
                    selected.push(id);
                    observe(LocalFetchProgress::Reading {
                        objects: selected.len() as u64,
                    });
                }
                check(control)?;
            }
            Ok::<_, FetchError>(())
        },
    )?;
    // The walk's tables are proportional to the whole history; release them before writing.
    drop((pending, observed));
    check(control)?;
    if progress(&[]) == ControlFlow::Break(()) {
        return Err(FetchError::Cancelled);
    }
    check(control)?;
    if selected.is_empty() {
        observe(LocalFetchProgress::Complete);
        return Ok(ReceivedFetch::native(
            advertisement,
            wants,
            NativeContents {
                format: source.object_format(),
                pack: Vec::new().into(),
                index: Vec::new(),
                checksum: None,
                objects: 0,
                complete: dependencies
                    .iter()
                    .filter_map(|&id| {
                        Some((
                            id,
                            super::boundaries::complete_kind(known, &boundaries, id)?,
                        ))
                    })
                    .collect(),
                dependencies,
                shallow,
                limits,
            },
        ));
    }
    let mut temporary = tempfile::NamedTempFile::new()?;
    let mut index = Vec::new();
    let written = {
        let mut sink = std::io::BufWriter::with_capacity(64 * 1024, temporary.as_file_mut());
        let (written, _) = crate::pack::write_transfer(
            &store,
            &selected,
            &[],
            (&mut sink, &mut index),
            PackWriteLimits {
                max_objects: limits.max_objects.try_into().unwrap_or(u32::MAX),
                max_object_bytes: limits.max_object_bytes as u64,
                max_input_bytes: limits.max_decode_bytes as u64,
                max_pack_bytes: limits.max_pack_bytes as u64,
                ..Default::default()
            },
            crate::PackCompression::Ordinary,
            (
                crate::ReadLimits {
                    max_object_bytes: limits.max_object_bytes,
                    max_delta_bytes: limits.max_delta_bytes,
                    ..limits.known_read
                },
                control.cancel,
            ),
            limits.max_delta_depth,
            // Like Git's fetch, and so import finds bases by position rather than identity.
            crate::pack::DeltaFraming::Offset,
            &mut || control.check().map_err(crate::PackWriteError::Io),
            &mut |done, total| {
                observe(LocalFetchProgress::Packing {
                    objects: (done, total),
                })
            },
        )
        .map_err(|error| match error {
            crate::pack::ReuseError::Read { id, source } => FetchError::LocalRead { id, source },
            crate::pack::ReuseError::Write(crate::PackWriteError::Io(error))
                if crate::transport::interruption(&error).is_some() =>
            {
                FetchError::from(error)
            }
            crate::pack::ReuseError::Write(error) => FetchError::PackWrite(error),
        })?;
        std::io::Write::flush(&mut sink)?;
        written
    };
    // Release the source packs and their caches before validation builds its own.
    drop(store);
    check(control)?;
    // Never trust source index identities for reused bytes. Import derives a new index and checks
    // delta depth, identity, framing and connectivity before any destination mutation.
    let received = ReceivedFetch::validate_known(
        advertisement,
        wants,
        super::bytes::Bytes::File(temporary, written.pack_bytes as usize),
        known,
        shallow,
        super::FetchOptions { limits, depth },
        &mut super::progress::ValidationObserver::new(control.cancel, &mut validation),
    )?;
    check(control)?;
    observe(LocalFetchProgress::Complete);
    Ok(received)
}

pub(super) fn advertise(
    source: &Repository,
    limits: FetchLimits,
    control: TransportControl<'_>,
) -> Result<Advertisement, FetchError> {
    check(control)?;
    let store = source
        .objects(Default::default())
        .map_err(FetchError::Destination)?;
    advertise_store(source, &store, limits, control)
}

fn advertise_store(
    source: &Repository,
    store: &crate::Objects,
    limits: FetchLimits,
    control: TransportControl<'_>,
) -> Result<Advertisement, FetchError> {
    let refs = source
        .references()
        .map_err(|_| FetchError::Protocol("local references"))?;
    let mut advertised = Vec::new();
    let mut remaining = limits.max_advertisement_bytes;
    let head = RefName::new(b"HEAD").expect("valid HEAD");
    let head_target = refs
        .read(&head)
        .map_err(|_| FetchError::Protocol("local HEAD"))?;
    let head_id = refs
        .resolve(&head, 32)
        .map_err(|_| FetchError::Protocol("local HEAD"))?
        .id;
    if let Some(id) = head_id {
        add_ref(
            &mut advertised,
            &mut remaining,
            limits.max_refs,
            head,
            id,
            false,
        )?;
    }
    for reference in refs
        .list()
        .map_err(|_| FetchError::Protocol("local references"))?
    {
        check(control)?;
        let id = match reference.target {
            Target::Direct(id) => Some(id),
            Target::Symbolic(_) => {
                refs.resolve(&reference.name, 32)
                    .map_err(|_| FetchError::Protocol("local symbolic reference"))?
                    .id
            }
        };
        if let Some(id) = id {
            let tag_name = reference.name.as_bytes().starts_with(b"refs/tags/");
            add_ref(
                &mut advertised,
                &mut remaining,
                limits.max_refs,
                reference.name.clone(),
                id,
                false,
            )?;
            if tag_name {
                let peeled = store
                    .peel(id, Default::default(), control.cancel)
                    .map_err(|error| FetchError::Peel(Box::new(error)))?;
                if !peeled.tags.is_empty() {
                    add_ref(
                        &mut advertised,
                        &mut remaining,
                        limits.max_refs,
                        reference.name,
                        peeled.target,
                        true,
                    )?;
                }
            }
        }
    }
    let mut capabilities = match head_target {
        Some(Target::Symbolic(name)) if name.as_bytes().starts_with(b"refs/heads/") => {
            let mut hint = b"symref=HEAD:".to_vec();
            hint.extend_from_slice(name.as_bytes());
            vec![hint]
        }
        _ => Vec::new(),
    };
    if source.object_format() == crate::ObjectFormat::Sha256 {
        capabilities.push(b"object-format=sha256".to_vec());
    }
    for capability in &capabilities {
        remaining = remaining
            .checked_sub(capability.len() + 1)
            .ok_or(FetchError::Limit("local advertisement bytes"))?;
    }
    Ok(Advertisement {
        refs: advertised,
        capabilities,
    })
}

fn add_ref(
    advertised: &mut Vec<AdvertisedRef>,
    remaining: &mut usize,
    max_refs: usize,
    name: RefName,
    id: ObjectId,
    peeled: bool,
) -> Result<(), FetchError> {
    if advertised.len() == max_refs {
        return Err(FetchError::Limit("local references"));
    }
    let bytes = name.as_bytes().len() + id.as_bytes().len() * 2 + 8;
    *remaining = remaining
        .checked_sub(bytes)
        .ok_or(FetchError::Limit("local advertisement bytes"))?;
    advertised.push(AdvertisedRef {
        name,
        id,
        kind: if peeled {
            crate::fetch::AdvertisementKind::PeeledHint
        } else {
            crate::fetch::AdvertisementKind::Tip
        },
    });
    Ok(())
}

fn check(control: TransportControl<'_>) -> Result<(), FetchError> {
    control.check().map_err(FetchError::from)
}

mod prefetch;

#[cfg(test)]
mod tests;