git_ref/store/packed/mod.rs
1use std::path::PathBuf;
2
3use git_hash::ObjectId;
4use git_object::bstr::{BStr, BString};
5use memmap2::Mmap;
6
7use crate::{file, transaction::RefEdit, FullNameRef};
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<git_lock::File>,
39 #[allow(dead_code)] // It just has to be kept alive, hence no reads
40 closed_lock: Option<git_lock::Marker>,
41}
42
43/// A reference as parsed from the `packed-refs` file
44#[derive(Debug, PartialEq, Eq)]
45pub struct Reference<'a> {
46 /// The validated full name of the reference.
47 pub name: &'a FullNameRef,
48 /// The target object id of the reference, hex encoded.
49 pub target: &'a BStr,
50 /// The fully peeled object id, hex encoded, that the ref is ultimately pointing to
51 /// i.e. when all indirections are removed.
52 pub object: Option<&'a BStr>,
53}
54
55impl<'a> Reference<'a> {
56 /// Decode the target as object
57 pub fn target(&self) -> ObjectId {
58 git_hash::ObjectId::from_hex(self.target).expect("parser validation")
59 }
60
61 /// Decode the object this reference is ultimately pointing to. Note that this is
62 /// the [`target()`][Reference::target()] if this is not a fully peeled reference like a tag.
63 pub fn object(&self) -> ObjectId {
64 self.object.map_or_else(
65 || self.target(),
66 |id| ObjectId::from_hex(id).expect("parser validation"),
67 )
68 }
69}
70
71/// An iterator over references in a packed refs file
72pub struct Iter<'a> {
73 /// The position at which to parse the next reference
74 cursor: &'a [u8],
75 /// The next line, starting at 1
76 current_line: usize,
77 /// If set, references returned will match the prefix, the first failed match will stop all iteration.
78 prefix: Option<BString>,
79}
80
81mod decode;
82
83///
84pub mod iter;
85
86///
87pub mod buffer;
88
89///
90pub mod find;
91
92///
93pub mod transaction;