Skip to content

Commit 3d01252

Browse files
committed
Merge branch 'read-header'
2 parents fbce7bb + 8c9c243 commit 3d01252

File tree

44 files changed

+1208
-336
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1208
-336
lines changed

Diff for: Cargo.lock

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: git-odb/src/cache.rs

+12
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ mod impls {
139139
use git_object::{Data, Kind};
140140
use git_pack::cache::Object;
141141

142+
use crate::find::Header;
142143
use crate::{pack::data::entry::Location, Cache};
143144

144145
impl<S> crate::Write for Cache<S>
@@ -167,6 +168,17 @@ mod impls {
167168
}
168169
}
169170

171+
impl<S> crate::Header for Cache<S>
172+
where
173+
S: crate::Header,
174+
{
175+
type Error = S::Error;
176+
177+
fn try_header(&self, id: impl AsRef<oid>) -> Result<Option<Header>, Self::Error> {
178+
self.inner.try_header(id)
179+
}
180+
}
181+
170182
impl<S> git_pack::Find for Cache<S>
171183
where
172184
S: git_pack::Find,

Diff for: git-odb/src/find.rs

+56-42
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,3 @@
1-
/// A way to indicate if a lookup, despite successful, was ambiguous or yielded exactly
2-
/// one result in the particular index.
3-
// TODO: find better name, ambiguous with git_pack::index::PrefixLookupResult (entry_index inside)
4-
pub type PrefixLookupResult = Result<git_hash::ObjectId, ()>;
5-
6-
/// A potentially ambiguous prefix for use with `Handle::disambiguate_prefix()`.
7-
#[derive(Debug, Copy, Clone)]
8-
pub struct PotentialPrefix {
9-
id: git_hash::ObjectId,
10-
hex_len: usize,
11-
}
12-
13-
impl PotentialPrefix {
14-
/// Create a new potentially ambiguous prefix from an `id` and the desired minimal `hex_len`.
15-
///
16-
/// It is considered ambiguous until it's disambiguated by validating that there is only a single object
17-
/// matching this prefix.
18-
pub fn new(id: impl Into<git_hash::ObjectId>, hex_len: usize) -> Result<Self, git_hash::prefix::Error> {
19-
let id = id.into();
20-
git_hash::Prefix::new(id, hex_len)?;
21-
Ok(PotentialPrefix { id, hex_len })
22-
}
23-
24-
/// Transform ourselves into a `Prefix` with our current hex lengths.
25-
pub fn to_prefix(&self) -> git_hash::Prefix {
26-
git_hash::Prefix::new(self.id, self.hex_len).expect("our hex-len to always be in bounds")
27-
}
28-
29-
pub(crate) fn inc_hex_len(&mut self) {
30-
self.hex_len += 1;
31-
assert!(self.hex_len <= self.id.kind().len_in_hex());
32-
}
33-
34-
pub(crate) fn id(&self) -> &git_hash::oid {
35-
&self.id
36-
}
37-
38-
pub(crate) fn hex_len(&self) -> usize {
39-
self.hex_len
40-
}
41-
}
42-
431
///
442
pub mod existing {
453
use git_hash::ObjectId;
@@ -90,3 +48,59 @@ pub mod existing_iter {
9048
ObjectKind { expected: git_object::Kind },
9149
}
9250
}
51+
52+
/// An object header informing about object properties, without it being fully decoded in the process.
53+
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
54+
pub enum Header {
55+
/// The object was not packed, but is currently located in the loose object portion of the database.
56+
///
57+
/// As packs are searched first, this means that in this very moment, the object whose header we retrieved is unique
58+
/// in the object database.
59+
Loose {
60+
/// The kind of the object.
61+
kind: git_object::Kind,
62+
/// The size of the object's data in bytes.
63+
size: u64,
64+
},
65+
/// The object was present in a pack.
66+
///
67+
/// Note that this does not imply it is unique in the database, as it might be present in more than one pack and even
68+
/// as loose object.
69+
Packed(git_pack::data::decode::header::Outcome),
70+
}
71+
72+
mod header {
73+
use super::Header;
74+
75+
impl Header {
76+
/// Return the object kind of the object we represent.
77+
pub fn kind(&self) -> git_object::Kind {
78+
match self {
79+
Header::Packed(out) => out.kind,
80+
Header::Loose { kind, .. } => *kind,
81+
}
82+
}
83+
/// Return the size of the object in bytes.
84+
pub fn size(&self) -> u64 {
85+
match self {
86+
Header::Packed(out) => out.object_size,
87+
Header::Loose { size, .. } => *size,
88+
}
89+
}
90+
}
91+
92+
impl From<git_pack::data::decode::header::Outcome> for Header {
93+
fn from(packed_header: git_pack::data::decode::header::Outcome) -> Self {
94+
Header::Packed(packed_header)
95+
}
96+
}
97+
98+
impl From<(usize, git_object::Kind)> for Header {
99+
fn from((object_size, kind): (usize, git_object::Kind)) -> Self {
100+
Header::Loose {
101+
kind,
102+
size: object_size as u64,
103+
}
104+
}
105+
}
106+
}

Diff for: git-odb/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub mod find;
7474
/// An object database equivalent to `/dev/null`, dropping all objects stored into it.
7575
mod traits;
7676

77-
pub use traits::{Find, FindExt, Write};
77+
pub use traits::{Find, FindExt, Header, HeaderExt, Write};
7878

7979
/// A thread-local handle to access any object.
8080
pub type Handle = Cache<store::Handle<OwnShared<Store>>>;

0 commit comments

Comments
 (0)