Skip to content

Commit 420dca2

Browse files
committed
[pack #179] refactor bundle
1 parent 7ad7a44 commit 420dca2

File tree

9 files changed

+97
-93
lines changed

9 files changed

+97
-93
lines changed

etc/check-package-size.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ indent cargo diet -n --package-size-limit 25KB
3030
(enter git-validate && indent cargo diet -n --package-size-limit 5KB)
3131
(enter git-object && indent cargo diet -n --package-size-limit 20KB)
3232
(enter git-commitgraph && indent cargo diet -n --package-size-limit 15KB)
33-
(enter git-pack && indent cargo diet -n --package-size-limit 70KB)
33+
(enter git-pack && indent cargo diet -n --package-size-limit 75KB)
3434
(enter git-odb && indent cargo diet -n --package-size-limit 15KB)
3535
(enter git-protocol && indent cargo diet -n --package-size-limit 25KB)
3636
(enter git-packetline && indent cargo diet -n --package-size-limit 15KB)

git-odb/src/store/compound/init.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub enum Error {
1212
#[error("The objects directory at '{0}' is not an accessible directory")]
1313
Inaccessible(PathBuf),
1414
#[error(transparent)]
15-
Pack(#[from] pack::bundle::Error),
15+
Pack(#[from] pack::bundle::init::Error),
1616
#[error(transparent)]
1717
Alternate(#[from] Box<crate::alternate::Error>),
1818
}

git-pack/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@
77
- `Find::find_existing_*` -> `Find::find_*`
88
- `Find::find_existing_*` -> `Find::find_*`
99
- `Find::find()-> `Find::try_find()`
10+
- `bundle::Bundle` -> `Bundle`
11+
- `bundle::Error` -> `bundle::init::Error`
12+
13+
* **new methods**
14+
- `Find::find_tag_iter()`

git-pack/src/bundle/init.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use crate::Bundle;
2+
use std::{
3+
convert::TryFrom,
4+
path::{Path, PathBuf},
5+
};
6+
7+
/// Returned by [`Bundle::at()`]
8+
#[derive(thiserror::Error, Debug)]
9+
#[allow(missing_docs)]
10+
pub enum Error {
11+
#[error("An 'idx' extension is expected of an index file: '{0}'")]
12+
InvalidPath(PathBuf),
13+
#[error(transparent)]
14+
Pack(#[from] crate::data::header::decode::Error),
15+
#[error(transparent)]
16+
Index(#[from] crate::index::init::Error),
17+
}
18+
19+
/// Initialization
20+
impl Bundle {
21+
/// Create a `Bundle` from `path`, which is either a pack file _(*.pack)_ or an index file _(*.idx)_.
22+
///
23+
/// The corresponding complementary file is expected to be present.
24+
/// Also available via [`Bundle::try_from()`].
25+
pub fn at(path: impl AsRef<Path>) -> Result<Self, Error> {
26+
Self::try_from(path.as_ref())
27+
}
28+
}
29+
30+
impl TryFrom<&Path> for Bundle {
31+
type Error = Error;
32+
33+
fn try_from(path: &Path) -> Result<Self, Self::Error> {
34+
let ext = path
35+
.extension()
36+
.and_then(|e| e.to_str())
37+
.ok_or_else(|| Error::InvalidPath(path.to_owned()))?;
38+
Ok(match ext {
39+
"idx" => Self {
40+
index: crate::index::File::at(path)?,
41+
pack: crate::data::File::at(path.with_extension("pack"))?,
42+
},
43+
"pack" => Self {
44+
pack: crate::data::File::at(path)?,
45+
index: crate::index::File::at(path.with_extension("idx"))?,
46+
},
47+
_ => return Err(Error::InvalidPath(path.to_owned())),
48+
})
49+
}
50+
}

git-pack/src/bundle/mod.rs

Lines changed: 25 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
1-
use std::{
2-
convert::TryFrom,
3-
path::{Path, PathBuf},
4-
};
1+
/// A way to uniquely identify the location of an object within a pack bundle
2+
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
3+
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
4+
pub struct Location {
5+
/// The id of the pack containing the object //TODO: this should probably at least by a typedef or even an opaque type
6+
pub pack_id: u32,
7+
/// The index at which the object can be found in the index file corresponding to the `pack_id`.
8+
pub index_file_id: u32,
9+
/// The size of the entry of disk so that the range of bytes of the entry is `pack_offset..pack_offset + entry_size`.
10+
pub entry_size: usize,
11+
/// The start of the entry in the pack identified by `pack_id`.
12+
pub pack_offset: u64,
13+
}
14+
15+
impl Location {
16+
/// Compute a range suitable for lookup in pack data using the [`entry_slice()`][crate::data::File::entry_slice()] method.
17+
pub fn entry_range(&self, pack_offset: u64) -> crate::data::EntryRange {
18+
pack_offset..pack_offset + self.entry_size as u64
19+
}
20+
}
21+
22+
///
23+
pub mod init;
524

625
mod find;
726
///
@@ -10,9 +29,10 @@ pub mod write;
1029
mod verify {
1130
use std::sync::{atomic::AtomicBool, Arc};
1231

32+
use crate::Bundle;
1333
use git_features::progress::Progress;
1434

15-
impl super::Bundle {
35+
impl Bundle {
1636
/// Similar to [`crate::index::File::verify_integrity()`] but more convenient to call as the presence of the
1737
/// pack file is a given.
1838
pub fn verify_integrity<C, P>(
@@ -40,77 +60,3 @@ mod verify {
4060
}
4161
}
4262
}
43-
44-
/// Returned by [`Bundle::at()`]
45-
#[derive(thiserror::Error, Debug)]
46-
#[allow(missing_docs)]
47-
pub enum Error {
48-
#[error("An 'idx' extension is expected of an index file: '{0}'")]
49-
InvalidPath(PathBuf),
50-
#[error(transparent)]
51-
Pack(#[from] crate::data::header::decode::Error),
52-
#[error(transparent)]
53-
Index(#[from] crate::index::init::Error),
54-
}
55-
56-
/// A way to uniquely identify the location of an object within a pack bundle
57-
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
58-
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
59-
pub struct Location {
60-
/// The id of the pack containing the object //TODO: this should probably at least by a typedef or even an opaque type
61-
pub pack_id: u32,
62-
/// The index at which the object can be found in the index file corresponding to the `pack_id`.
63-
pub index_file_id: u32,
64-
/// The size of the entry of disk so that the range of bytes of the entry is `pack_offset..pack_offset + entry_size`.
65-
pub entry_size: usize,
66-
/// The start of the entry in the pack identified by `pack_id`.
67-
pub pack_offset: u64,
68-
}
69-
70-
impl Location {
71-
/// Compute a range suitable for lookup in pack data using the [`entry_slice()`][crate::data::File::entry_slice()] method.
72-
pub fn entry_range(&self, pack_offset: u64) -> crate::data::EntryRange {
73-
pack_offset..pack_offset + self.entry_size as u64
74-
}
75-
}
76-
77-
/// A bundle of pack data and the corresponding pack index
78-
pub struct Bundle {
79-
/// The pack file corresponding to `index`
80-
pub pack: crate::data::File,
81-
/// The index file corresponding to `pack`
82-
pub index: crate::index::File,
83-
}
84-
85-
/// Initialization
86-
impl Bundle {
87-
/// Create a `Bundle` from `path`, which is either a pack file _(*.pack)_ or an index file _(*.idx)_.
88-
///
89-
/// The corresponding complementary file is expected to be present.
90-
/// Also available via [`Bundle::try_from()`].
91-
pub fn at(path: impl AsRef<Path>) -> Result<Self, Error> {
92-
Self::try_from(path.as_ref())
93-
}
94-
}
95-
96-
impl TryFrom<&Path> for Bundle {
97-
type Error = Error;
98-
99-
fn try_from(path: &Path) -> Result<Self, Self::Error> {
100-
let ext = path
101-
.extension()
102-
.and_then(|e| e.to_str())
103-
.ok_or_else(|| Error::InvalidPath(path.to_owned()))?;
104-
Ok(match ext {
105-
"idx" => Self {
106-
index: crate::index::File::at(path)?,
107-
pack: crate::data::File::at(path.with_extension("pack"))?,
108-
},
109-
"pack" => Self {
110-
pack: crate::data::File::at(path)?,
111-
index: crate::index::File::at(path.with_extension("idx"))?,
112-
},
113-
_ => return Err(Error::InvalidPath(path.to_owned())),
114-
})
115-
}
116-
}

git-pack/src/bundle/write/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub struct Outcome {
4343

4444
impl Outcome {
4545
/// Instantiate a bundle from the newly written index and data file that are represented by this `Outcome`
46-
pub fn to_bundle(&self) -> Option<Result<crate::Bundle, crate::bundle::Error>> {
46+
pub fn to_bundle(&self) -> Option<Result<crate::Bundle, crate::bundle::init::Error>> {
4747
self.index_path.as_ref().map(crate::Bundle::at)
4848
}
4949
}

git-pack/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@
1515
1616
///
1717
pub mod bundle;
18-
pub use crate::bundle::Bundle;
18+
/// A bundle of pack data and the corresponding pack index
19+
pub struct Bundle {
20+
/// The pack file corresponding to `index`
21+
pub pack: crate::data::File,
22+
/// The index file corresponding to `pack`
23+
pub index: crate::index::File,
24+
}
1925

2026
///
2127
pub mod find;

git-pack/src/loose.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
pub mod object {
33
///
44
pub mod header {
5-
//! loose object header encoding and decoding
5+
//! loose object header encoding and decoding.
6+
//!
7+
//! Note that these are still relevant for packs as they are part of the computed hash of any git object, packed or not.
8+
//! It just so happened that loose objects where the first ones used for implementing an object database.
69
use byteorder::WriteBytesExt;
710
use git_object as object;
811

gitoxide-core/src/pack/receive.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -301,15 +301,9 @@ fn receive_pack_blocking<W: io::Write>(
301301
index_kind: pack::index::Version::V2,
302302
iteration_mode: pack::data::input::Mode::Verify,
303303
};
304-
let outcome = pack::bundle::Bundle::write_to_directory(
305-
input,
306-
directory.take(),
307-
progress,
308-
&ctx.should_interrupt,
309-
None,
310-
options,
311-
)
312-
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
304+
let outcome =
305+
pack::Bundle::write_to_directory(input, directory.take(), progress, &ctx.should_interrupt, None, options)
306+
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
313307

314308
if let Some(directory) = refs_directory.take() {
315309
write_raw_refs(refs, directory)?;

0 commit comments

Comments
 (0)