gix_ref/store/packed/mod.rs
1use std::path::PathBuf;
2
3use gix_hash::ObjectId;
4use gix_object::bstr::{BStr, BString};
5use memmap2::Mmap;
6
7use crate::{file, transaction::RefEdit, FullNameRef, Namespace};
8
9#[derive(Debug)]
10enum Backing {
11 /// The buffer is loaded entirely in memory, along with the `offset` to the first record past the header.
12 InMemory(Vec<u8>),
13 /// The buffer is mapping the file on disk, along with the offset to the first record past the header
14 Mapped(Mmap),
15}
16
17/// A buffer containing a packed-ref file that is either memory mapped or fully in-memory depending on a cutoff.
18///
19/// The buffer is guaranteed to be sorted as per the packed-ref rules which allows some operations to be more efficient.
20#[derive(Debug)]
21pub struct Buffer {
22 data: Backing,
23 /// The offset to the first record, how many bytes to skip past the header
24 offset: usize,
25 /// The path from which we were loaded
26 path: PathBuf,
27}
28
29struct Edit {
30 inner: RefEdit,
31 peeled: Option<ObjectId>,
32}
33
34/// A transaction for editing packed references
35pub(crate) struct Transaction {
36 buffer: Option<file::packed::SharedBufferSnapshot>,
37 edits: Option<Vec<Edit>>,
38 lock: Option<gix_lock::File>,
39 #[allow(dead_code)] // It just has to be kept alive, hence no reads
40 closed_lock: Option<gix_lock::Marker>,
41 precompose_unicode: bool,
42 /// The namespace to use when preparing or writing refs
43 namespace: Option<Namespace>,
44}
45
46/// A reference as parsed from the `packed-refs` file
47#[derive(Debug, PartialEq, Eq)]
48pub struct Reference<'a> {
49 /// The validated full name of the reference.
50 pub name: &'a FullNameRef,
51 /// The target object id of the reference, hex encoded.
52 pub target: &'a BStr,
53 /// The fully peeled object id, hex encoded, that the ref is ultimately pointing to
54 /// i.e. when all indirections are removed.
55 pub object: Option<&'a BStr>,
56}
57
58impl Reference<'_> {
59 /// Decode the target as object
60 pub fn target(&self) -> ObjectId {
61 gix_hash::ObjectId::from_hex(self.target).expect("parser validation")
62 }
63
64 /// Decode the object this reference is ultimately pointing to. Note that this is
65 /// the [`target()`][Reference::target()] if this is not a fully peeled reference like a tag.
66 pub fn object(&self) -> ObjectId {
67 self.object.map_or_else(
68 || self.target(),
69 |id| ObjectId::from_hex(id).expect("parser validation"),
70 )
71 }
72}
73
74/// An iterator over references in a packed refs file
75pub struct Iter<'a> {
76 /// The position at which to parse the next reference
77 cursor: &'a [u8],
78 /// The next line, starting at 1
79 current_line: usize,
80 /// If set, references returned will match the prefix, the first failed match will stop all iteration.
81 prefix: Option<BString>,
82}
83
84mod decode;
85
86///
87pub mod iter;
88
89///
90pub mod buffer;
91
92///
93pub mod find;
94
95///
96pub mod transaction;