Skip to main content

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 hash kind to expect when parsing packed references.
24    hash_kind: gix_hash::Kind,
25    /// The offset to the first record, how many bytes to skip past the header
26    offset: usize,
27    /// The path from which we were loaded
28    path: PathBuf,
29}
30
31struct Edit {
32    inner: RefEdit,
33    peeled: Option<ObjectId>,
34}
35
36/// A transaction for editing packed references
37pub(crate) struct Transaction {
38    buffer: Option<file::packed::SharedBufferSnapshot>,
39    edits: Option<Vec<Edit>>,
40    lock: Option<gix_lock::File>,
41    #[allow(dead_code)] // It just has to be kept alive, hence no reads
42    closed_lock: Option<gix_lock::Marker>,
43    precompose_unicode: bool,
44    /// The namespace to use when preparing or writing refs
45    namespace: Option<Namespace>,
46}
47
48/// A reference as parsed from the `packed-refs` file
49#[derive(Debug, PartialEq, Eq)]
50pub struct Reference<'a> {
51    /// The validated full name of the reference.
52    pub name: &'a FullNameRef,
53    /// The target object id of the reference, hex encoded.
54    pub target: &'a BStr,
55    /// The fully peeled object id, hex encoded, that the ref is ultimately pointing to
56    /// i.e. when all indirections are removed.
57    pub object: Option<&'a BStr>,
58}
59
60impl Reference<'_> {
61    /// Decode the target as object
62    pub fn target(&self) -> ObjectId {
63        gix_hash::ObjectId::from_hex(self.target).expect("parser validation")
64    }
65
66    /// Decode the object this reference is ultimately pointing to. Note that this is
67    /// the [`target()`][Reference::target()] if this is not a fully peeled reference like a tag.
68    pub fn object(&self) -> ObjectId {
69        self.object.map_or_else(
70            || self.target(),
71            |id| ObjectId::from_hex(id).expect("parser validation"),
72        )
73    }
74}
75
76/// An iterator over references in a packed refs file
77pub struct Iter<'a> {
78    /// The position at which to parse the next reference
79    cursor: &'a [u8],
80    /// The hash kind to expect when parsing packed references.
81    hash_kind: gix_hash::Kind,
82    /// The next line, starting at 1
83    current_line: usize,
84    /// If set, references returned will match the prefix, the first failed match will stop all iteration.
85    prefix: Option<BString>,
86}
87
88mod decode;
89
90///
91pub mod iter;
92
93///
94pub mod buffer;
95
96///
97pub mod find;
98
99///
100pub mod transaction;