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
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
//! Explicitly budgeted mapping; trusted entry points retain their original algorithm.
use std::sync::atomic::{AtomicBool, Ordering};

use super::{Direction, ForceIntent, Mapping, MappingError, Plan, RefSource, Refspec, Refspecs};
use crate::fetch::{Advertisement, AdvertisementKind};

/// Admission and work budgets for pure reference mapping.
///
/// Defaults admit one million sources, 4,096 specs, 128 MiB of input names/specs,
/// one million output attempts, 256 MiB of output names and 1 GiB of comparison work.
/// Names and specs are already caller-owned. These limits bound additional mapping work,
/// not their construction or exact allocator overhead.
// New limit fields must remain Copy.
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct MappingLimits {
    /// Input sources or advertisement entries, including peeled hints.
    pub max_sources: usize,
    /// Parsed refspec occurrences, including exclusions and duplicates.
    pub max_specs: usize,
    /// Aggregate raw refspec and input name bytes; capabilities are not inspected.
    pub max_input_bytes: usize,
    /// Attempted mappings plus missing-source reports, including duplicate attempts.
    pub max_mappings: usize,
    /// Logical source/destination name bytes per attempt, or missing-source report bytes.
    ///
    /// Deduplication does not refund these charges. Internal sets retain a constant number
    /// of copies of admitted names; this is not exact heap accounting.
    pub max_output_bytes: usize,
    /// Loop steps plus source/pattern bytes examined by comparisons, including exclusions.
    pub max_work: usize,
}

impl Default for MappingLimits {
    fn default() -> Self {
        Self {
            max_sources: 1_000_000,
            max_specs: 4096,
            max_input_bytes: 128 * 1024 * 1024,
            max_mappings: 1_000_000,
            max_output_bytes: 256 * 1024 * 1024,
            max_work: 1024 * 1024 * 1024,
        }
    }
}

impl MappingLimits {
    /// Sets [`Self::max_sources`].
    #[must_use]
    pub fn with_max_sources(mut self, value: usize) -> Self {
        self.max_sources = value;
        self
    }
    /// Sets [`Self::max_specs`].
    #[must_use]
    pub fn with_max_specs(mut self, value: usize) -> Self {
        self.max_specs = value;
        self
    }
    /// Sets [`Self::max_input_bytes`].
    #[must_use]
    pub fn with_max_input_bytes(mut self, value: usize) -> Self {
        self.max_input_bytes = value;
        self
    }
    /// Sets [`Self::max_mappings`].
    #[must_use]
    pub fn with_max_mappings(mut self, value: usize) -> Self {
        self.max_mappings = value;
        self
    }
    /// Sets [`Self::max_output_bytes`].
    #[must_use]
    pub fn with_max_output_bytes(mut self, value: usize) -> Self {
        self.max_output_bytes = value;
        self
    }
    /// Sets [`Self::max_work`].
    #[must_use]
    pub fn with_max_work(mut self, value: usize) -> Self {
        self.max_work = value;
        self
    }
}

/// Explicit controlled mapping of an already-parsed refspec list.
///
/// No I/O or mutation occurs. Limits precede name clones, and cancellation is observed
/// on entry and at most every 64 loop steps. One bounded name comparison, hashing or
/// validation may finish before the next poll. Failure returns no partial plan.
/// Existing [`Refspecs`] mapping methods remain unbounded trusted alternatives.
///
/// # Examples
///
/// ```
/// use std::sync::atomic::AtomicBool;
///
/// use girt::remote::{Direction, MappingLimits, MappingOptions, Refspecs};
///
/// let specs = Refspecs::parse(Direction::Fetch, ["refs/heads/*:refs/remotes/origin/*"])?;
/// let mappings = MappingOptions::new(&specs)
///     .with_limits(MappingLimits::default().with_max_sources(100))
///     .map(&[], &AtomicBool::new(false))?;
/// assert!(mappings.is_empty());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct MappingOptions<'a> {
    specs: &'a Refspecs,
    limits: MappingLimits,
}

impl<'a> MappingOptions<'a> {
    /// Selects refspecs and default budgets, without performing work.
    pub fn new(specs: &'a Refspecs) -> Self {
        Self {
            specs,
            limits: MappingLimits::default(),
        }
    }

    /// Replaces the mapping budgets.
    #[must_use]
    pub fn with_limits(mut self, limits: MappingLimits) -> Self {
        self.limits = limits;
        self
    }

    /// Maps explicit sources, refusing an absent exact selector.
    ///
    /// # Errors
    ///
    /// Returns ordinary mapping errors, [`MappingError::Limit`] or [`MappingError::Cancelled`].
    pub fn map(
        self,
        sources: &[RefSource],
        cancel: &AtomicBool,
    ) -> Result<Vec<Mapping>, MappingError> {
        complete(self.map_with_missing(sources, cancel)?)
    }

    /// Maps explicit sources and separately reports absent exact selectors.
    ///
    /// # Errors
    ///
    /// Returns ordinary validation/collision errors, limits or cancellation without a partial plan.
    pub fn map_with_missing(
        self,
        sources: &[RefSource],
        cancel: &AtomicBool,
    ) -> Result<(Vec<Mapping>, Vec<Vec<u8>>), MappingError> {
        let mut budget = Budget::new(self.limits, cancel)?;
        self.admit(
            sources.len(),
            sources.iter().map(|source| source.name.as_bytes()),
            &mut budget,
        )?;
        self.map_admitted(sources, &mut budget)
    }

    /// Maps advertisement tips; peeled hints count toward input admission but cannot be selected.
    ///
    /// # Errors
    ///
    /// Returns direction, mapping, limit or cancellation errors without a partial plan.
    pub fn map_advertisement(
        self,
        advertisement: &Advertisement,
        cancel: &AtomicBool,
    ) -> Result<Vec<Mapping>, MappingError> {
        complete(self.map_advertisement_with_missing(advertisement, cancel)?)
    }

    /// Maps advertisement tips and separately reports absent exact selectors.
    ///
    /// Capabilities are not inspected or copied. Input budgets cover all entries before any
    /// tip names are cloned. The caller bounds advertisement acquisition separately.
    ///
    /// # Errors
    ///
    /// Returns direction, validation/collision, limit or cancellation errors without a partial
    /// plan.
    pub fn map_advertisement_with_missing(
        self,
        advertisement: &Advertisement,
        cancel: &AtomicBool,
    ) -> Result<(Vec<Mapping>, Vec<Vec<u8>>), MappingError> {
        let mut budget = Budget::new(self.limits, cancel)?;
        if self.specs.direction != Direction::Fetch {
            return Err(MappingError::Direction);
        }
        self.admit(
            advertisement.refs.len(),
            advertisement
                .refs
                .iter()
                .map(|reference| reference.name.as_bytes()),
            &mut budget,
        )?;
        let mut sources = Vec::new();
        for reference in &advertisement.refs {
            budget.step()?;
            if reference.kind == AdvertisementKind::Tip {
                sources.push(RefSource {
                    name: reference.name.clone(),
                    id: reference.id,
                });
            }
        }
        self.map_admitted(&sources, &mut budget)
    }

    /// Reverse matching for maintenance pruning, under one budget for the complete phase.
    /// Borrow output names; the planner admits its own result clones separately.
    pub(crate) fn prune_candidates<'n>(
        self,
        destinations: &[&'n crate::refs::RefName],
        advertisement: &Advertisement,
        retained: &std::collections::BTreeSet<Vec<u8>>,
        cancel: &AtomicBool,
    ) -> Result<Vec<&'n crate::refs::RefName>, MappingError> {
        let mut budget = Budget::new(self.limits, cancel)?;
        if self.specs.direction != Direction::Fetch {
            return Err(MappingError::Direction);
        }
        let count = advertisement
            .refs
            .len()
            .checked_add(destinations.len())
            .and_then(|count| count.checked_add(retained.len()))
            .ok_or(MappingError::Limit("sources"))?;
        self.admit(
            count,
            advertisement
                .refs
                .iter()
                .map(|reference| reference.name.as_bytes())
                .chain(destinations.iter().map(|name| name.as_bytes()))
                .chain(retained.iter().map(Vec::as_slice)),
            &mut budget,
        )?;
        let mut advertised = std::collections::HashSet::new();
        for reference in &advertisement.refs {
            budget.step()?;
            if reference.kind == AdvertisementKind::Tip {
                budget.comparison(b"", reference.name.as_bytes())?;
                advertised.insert(reference.name.as_bytes());
            }
        }
        let mut pruned = Vec::new();
        for &destination in destinations {
            budget.step()?;
            let mut owned = false;
            let mut protected = false;
            for spec in &self.specs.specs {
                budget.step()?;
                let Some(source) = reverse_source(spec, destination, &mut budget)? else {
                    continue;
                };
                let mut excluded = false;
                for negative in &self.specs.specs {
                    budget.step()?;
                    if !negative.negative {
                        continue;
                    }
                    budget.comparison(&negative.source, &source)?;
                    if negative.capture(&source).is_some() {
                        excluded = true;
                        break;
                    }
                }
                if excluded {
                    continue;
                }
                owned = true;
                budget.comparison(b"", &source)?;
                if advertised.contains(source.as_slice()) || retained.contains(source.as_slice()) {
                    protected = true;
                    break;
                }
            }
            if owned && !protected {
                pruned.push(destination);
            }
        }
        if cancel.load(Ordering::Relaxed) {
            return Err(MappingError::Cancelled);
        }
        Ok(pruned)
    }

    fn admit<'b>(
        self,
        count: usize,
        names: impl Iterator<Item = &'b [u8]>,
        budget: &mut Budget<'_>,
    ) -> Result<(), MappingError> {
        if count > self.limits.max_sources {
            return Err(MappingError::Limit("sources"));
        }
        if self.specs.specs.len() > self.limits.max_specs {
            return Err(MappingError::Limit("refspecs"));
        }
        let mut bytes = self.limits.max_input_bytes;
        for spec in &self.specs.specs {
            budget.step()?;
            debit(&mut bytes, spec.raw.len(), "input bytes")?;
        }
        for name in names {
            budget.step()?;
            debit(&mut bytes, name.len(), "input bytes")?;
        }
        Ok(())
    }

    fn map_admitted(
        self,
        sources: &[RefSource],
        budget: &mut Budget<'_>,
    ) -> Result<(Vec<Mapping>, Vec<Vec<u8>>), MappingError> {
        let mut names = std::collections::HashSet::new();
        for source in sources {
            budget.step()?;
            if !names.insert(&source.name) {
                return Err(MappingError::DuplicateSource(source.name.clone()));
            }
            if source.id.is_null() {
                return Err(MappingError::ZeroId(source.name.clone()));
            }
        }
        let mut plan = Plan::default();
        let mut missing = Vec::new();
        for spec in &self.specs.specs {
            budget.step()?;
            if spec.negative {
                continue;
            }
            if spec.source.is_empty() {
                budget.output(spec, b"", None)?;
                plan.add(mapping(spec, b"", None)?)?;
                continue;
            }
            let mut matched = false;
            for source in sources {
                budget.comparison(&spec.source, source.name.as_bytes())?;
                let Some(capture) = spec.capture(source.name.as_bytes()) else {
                    continue;
                };
                matched = true;
                let mut excluded = false;
                for negative in &self.specs.specs {
                    budget.step()?;
                    if negative.negative {
                        budget.comparison(&negative.source, source.name.as_bytes())?;
                        if negative.capture(source.name.as_bytes()).is_some() {
                            excluded = true;
                            break;
                        }
                    }
                }
                if !excluded {
                    budget.output(spec, capture, Some(source))?;
                    plan.add(mapping(spec, capture, Some(source))?)?;
                }
            }
            if !matched && !spec.wildcard {
                budget.report(spec.source.len())?;
                missing.push(spec.source.clone());
            }
        }
        Ok((plan.mappings, missing))
    }
}

fn complete(
    (mappings, missing): (Vec<Mapping>, Vec<Vec<u8>>),
) -> Result<Vec<Mapping>, MappingError> {
    match missing.into_iter().next() {
        Some(source) => Err(MappingError::UnmatchedSource(source)),
        None => Ok(mappings),
    }
}

fn mapping(
    spec: &Refspec,
    capture: &[u8],
    source: Option<&RefSource>,
) -> Result<Mapping, MappingError> {
    Ok(Mapping {
        source: source.cloned(),
        destination: spec.destination(capture)?,
        force: if spec.force {
            ForceIntent::Requested
        } else {
            ForceIntent::Normal
        },
    })
}

/// The controlled counterpart of `source_for_destination`: charge the constructed source
/// before allocating it. Keep the trusted implementation unchanged.
fn reverse_source(
    spec: &Refspec,
    destination: &crate::refs::RefName,
    budget: &mut Budget<'_>,
) -> Result<Option<Vec<u8>>, MappingError> {
    if spec.negative {
        return Ok(None);
    }
    let Some(pattern) = &spec.destination else {
        return Ok(None);
    };
    budget.comparison(pattern, destination.as_bytes())?;
    let capture = if let Some(star) = pattern.iter().position(|byte| *byte == b'*') {
        let Some(capture) = destination
            .as_bytes()
            .strip_prefix(&pattern[..star])
            .and_then(|capture| capture.strip_suffix(&pattern[star + 1..]))
        else {
            return Ok(None);
        };
        capture
    } else if pattern == destination.as_bytes() {
        b""
    } else {
        return Ok(None);
    };
    budget.comparison(&spec.source, capture)?;
    if let Some(star) = spec.source.iter().position(|byte| *byte == b'*') {
        let bytes = spec
            .source
            .len()
            .checked_sub(1)
            .and_then(|bytes| bytes.checked_add(capture.len()))
            .ok_or(MappingError::Limit("output bytes"))?;
        budget.report(bytes)?;
        Ok(Some(
            [&spec.source[..star], capture, &spec.source[star + 1..]].concat(),
        ))
    } else {
        budget.report(spec.source.len())?;
        Ok(Some(spec.source.clone()))
    }
}

struct Budget<'a> {
    cancel: &'a AtomicBool,
    remaining: usize,
    mappings: usize,
    bytes: usize,
    work: usize,
}

impl<'a> Budget<'a> {
    fn new(limits: MappingLimits, cancel: &'a AtomicBool) -> Result<Self, MappingError> {
        if cancel.load(Ordering::Relaxed) {
            return Err(MappingError::Cancelled);
        }
        Ok(Self {
            cancel,
            remaining: 64,
            mappings: limits.max_mappings,
            bytes: limits.max_output_bytes,
            work: limits.max_work,
        })
    }

    fn step(&mut self) -> Result<(), MappingError> {
        debit(&mut self.work, 1, "comparison work")?;
        self.remaining -= 1;
        if self.remaining == 0 {
            if self.cancel.load(Ordering::Relaxed) {
                return Err(MappingError::Cancelled);
            }
            self.remaining = 64;
        }
        Ok(())
    }

    fn comparison(&mut self, pattern: &[u8], name: &[u8]) -> Result<(), MappingError> {
        self.step()?;
        debit(&mut self.work, pattern.len(), "comparison work")?;
        debit(&mut self.work, name.len(), "comparison work")
    }

    fn report(&mut self, bytes: usize) -> Result<(), MappingError> {
        debit(&mut self.mappings, 1, "mapping attempts")?;
        debit(&mut self.bytes, bytes, "output bytes")
    }

    fn output(
        &mut self,
        spec: &Refspec,
        capture: &[u8],
        source: Option<&RefSource>,
    ) -> Result<(), MappingError> {
        self.report(source.map_or(0, |source| source.name.as_bytes().len()))?;
        if let Some(pattern) = &spec.destination {
            let bytes = if spec.wildcard {
                pattern.len() - 1
            } else {
                pattern.len()
            };
            debit(&mut self.bytes, bytes, "output bytes")?;
            if spec.wildcard {
                debit(&mut self.bytes, capture.len(), "output bytes")?;
            }
        }
        Ok(())
    }
}

fn debit(remaining: &mut usize, bytes: usize, name: &'static str) -> Result<(), MappingError> {
    *remaining = remaining
        .checked_sub(bytes)
        .ok_or(MappingError::Limit(name))?;
    Ok(())
}

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

    #[test]
    fn cancellation_is_polled_at_the_batch_boundary() {
        let cancel = AtomicBool::new(false);
        let mut budget = Budget::new(MappingLimits::default(), &cancel).unwrap();
        for _ in 0..63 {
            budget.step().unwrap();
        }
        cancel.store(true, Ordering::Relaxed);
        assert!(matches!(budget.step(), Err(MappingError::Cancelled)));
    }

    #[test]
    fn skipped_exclusion_entries_still_charge_work() {
        let cancel = AtomicBool::new(false);
        let mut budget = Budget::new(MappingLimits::default().with_max_work(1), &cancel).unwrap();
        budget.step().unwrap();
        assert!(matches!(
            budget.step(),
            Err(MappingError::Limit("comparison work"))
        ));
    }
}