Skip to content

git-pack refactoring #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion etc/check-package-size.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ indent cargo diet -n --package-size-limit 25KB
(enter git-validate && indent cargo diet -n --package-size-limit 5KB)
(enter git-object && indent cargo diet -n --package-size-limit 20KB)
(enter git-commitgraph && indent cargo diet -n --package-size-limit 15KB)
(enter git-pack && indent cargo diet -n --package-size-limit 70KB)
(enter git-pack && indent cargo diet -n --package-size-limit 75KB)
(enter git-odb && indent cargo diet -n --package-size-limit 15KB)
(enter git-protocol && indent cargo diet -n --package-size-limit 25KB)
(enter git-packetline && indent cargo diet -n --package-size-limit 15KB)
Expand Down
7 changes: 2 additions & 5 deletions experiments/diffing/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ fn main() -> anyhow::Result<()> {

let start = Instant::now();
let all_commits = commit_id
.ancestors_iter(|oid, buf| {
db.find_existing_commit_iter(oid, buf, &mut odb::pack::cache::Never)
.ok()
})
.ancestors_iter(|oid, buf| db.find_commit_iter(oid, buf, &mut odb::pack::cache::Never).ok())
.collect::<Result<Vec<_>, _>>()?;
let num_diffs = all_commits.len();
let elapsed = start.elapsed();
Expand Down Expand Up @@ -77,7 +74,7 @@ fn main() -> anyhow::Result<()> {
Some(odb::data::Object::new(*kind, buf))
}
None => {
let obj = db.find_existing(oid, buf, pack_cache).ok();
let obj = db.find(oid, buf, pack_cache).ok();
if let Some(ref obj) = obj {
obj_cache.insert(
oid,
Expand Down
4 changes: 2 additions & 2 deletions experiments/object-access/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ where
let mut bytes = 0u64;
let mut cache = new_cache();
for hash in hashes {
let obj = repo.odb.find_existing(hash, &mut buf, &mut cache)?;
let obj = repo.odb.find(hash, &mut buf, &mut cache)?;
bytes += obj.data.len() as u64;
}
Ok(bytes)
Expand All @@ -200,7 +200,7 @@ where
|(buf, cache), hash| {
match mode {
AccessMode::ObjectData => {
let obj = repo.odb.find_existing(hash, buf, cache)?;
let obj = repo.odb.find(hash, buf, cache)?;
bytes.fetch_add(obj.data.len() as u64, std::sync::atomic::Ordering::Relaxed);
}
AccessMode::ObjectExists => {
Expand Down
23 changes: 8 additions & 15 deletions experiments/traversal/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ fn main() -> anyhow::Result<()> {

let start = Instant::now();
let all_commits = commit_id
.ancestors_iter(|oid, buf| {
db.find_existing_commit_iter(oid, buf, &mut odb::pack::cache::Never)
.ok()
})
.ancestors_iter(|oid, buf| db.find_commit_iter(oid, buf, &mut odb::pack::cache::Never).ok())
.collect::<Result<Vec<_>, _>>()?;
let elapsed = start.elapsed();
println!(
Expand Down Expand Up @@ -149,7 +146,7 @@ where
C: odb::pack::cache::DecodeEntry,
{
let mut cache = new_cache();
let ancestors = tip.ancestors_iter(|oid, buf| db.find_existing_commit_iter(oid, buf, &mut cache).ok());
let ancestors = tip.ancestors_iter(|oid, buf| db.find_commit_iter(oid, buf, &mut cache).ok());
let mut commits = 0;
for commit_id in ancestors {
let _ = commit_id?;
Expand Down Expand Up @@ -211,18 +208,14 @@ where

for commit in commits {
let tree_id = db
.find(commit, &mut buf, &mut cache)?
.try_find(commit, &mut buf, &mut cache)?
.and_then(|o| o.into_commit_iter().and_then(|mut c| c.tree_id()))
.expect("commit as starting point");

let mut count = Count { entries: 0, seen };
db.find_existing_tree_iter(tree_id, &mut buf2, &mut cache)?.traverse(
db.find_tree_iter(tree_id, &mut buf2, &mut cache)?.traverse(
&mut state,
|oid, buf| {
db.find_existing(oid, buf, &mut cache)
.ok()
.and_then(|o| o.into_tree_iter())
},
|oid, buf| db.find(oid, buf, &mut cache).ok().and_then(|o| o.into_tree_iter()),
&mut count,
)?;
entries += count.entries as u64;
Expand Down Expand Up @@ -278,13 +271,13 @@ where
},
|(count, buf, buf2, cache, state), commit| {
let tid = db
.find_existing_commit_iter(commit, buf, cache)?
.find_commit_iter(commit, buf, cache)?
.tree_id()
.expect("commit as starting point");
count.entries = 0;
db.find_existing_tree_iter(tid, buf2, cache)?.traverse(
db.find_tree_iter(tid, buf2, cache)?.traverse(
state,
|oid, buf| db.find_existing_tree_iter(oid, buf, cache).ok(),
|oid, buf| db.find_tree_iter(oid, buf, cache).ok(),
count,
)?;
entries.fetch_add(count.entries as u64, std::sync::atomic::Ordering::Relaxed);
Expand Down
18 changes: 9 additions & 9 deletions git-diff/tests/visit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ mod changes {
buf: &'a mut Vec<u8>,
) -> crate::Result<TreeRefIter<'a>> {
let tree_id = db
.find(commit, buf, &mut pack::cache::Never)?
.try_find(commit, buf, &mut pack::cache::Never)?
.ok_or_else(|| format!("start commit {:?} to be present", commit))?
.decode()?
.into_commit()
.expect("id is actually a commit")
.tree();

Ok(db
.find(tree_id, buf, &mut pack::cache::Never)?
.try_find(tree_id, buf, &mut pack::cache::Never)?
.expect("main tree present")
.into_tree_iter()
.expect("id to be a tree"))
Expand All @@ -50,7 +50,7 @@ mod changes {
rhs_tree,
git_diff::tree::State::default(),
|oid, buf| {
db.find(oid, buf, &mut pack::cache::Never)
db.try_find(oid, buf, &mut pack::cache::Never)
.ok()
.flatten()
.and_then(|obj| obj.into_tree_iter())
Expand All @@ -64,7 +64,7 @@ mod changes {
let mut buf = Vec::new();
let (main_tree_id, parent_commit_id) = {
let commit = db
.find(commit_id, &mut buf, &mut pack::cache::Never)?
.try_find(commit_id, &mut buf, &mut pack::cache::Never)?
.ok_or_else(|| format!("start commit {:?} to be present", commit_id))?
.decode()?
.into_commit()
Expand All @@ -76,18 +76,18 @@ mod changes {
})
};
let current_tree = db
.find(main_tree_id, &mut buf, &mut pack::cache::Never)?
.try_find(main_tree_id, &mut buf, &mut pack::cache::Never)?
.expect("main tree present")
.into_tree_iter()
.expect("id to be a tree");
let mut buf2 = Vec::new();
let previous_tree: Option<_> = {
parent_commit_id
.and_then(|id| db.find(id, &mut buf2, &mut pack::cache::Never).ok().flatten())
.and_then(|id| db.try_find(id, &mut buf2, &mut pack::cache::Never).ok().flatten())
.and_then(|c| c.decode().ok())
.and_then(|c| c.into_commit())
.map(|c| c.tree())
.and_then(|tree| db.find(tree, &mut buf2, &mut pack::cache::Never).ok().flatten())
.and_then(|tree| db.try_find(tree, &mut buf2, &mut pack::cache::Never).ok().flatten())
.and_then(|tree| tree.into_tree_iter())
};

Expand All @@ -96,7 +96,7 @@ mod changes {
current_tree,
&mut git_diff::tree::State::default(),
|oid, buf| {
db.find(oid, buf, &mut pack::cache::Never)
db.try_find(oid, buf, &mut pack::cache::Never)
.ok()
.flatten()
.and_then(|obj| obj.into_tree_iter())
Expand Down Expand Up @@ -130,7 +130,7 @@ mod changes {

let head = head_of(db);
commit::Ancestors::new(Some(head), commit::ancestors::State::default(), |oid, buf| {
db.find(oid, buf, &mut pack::cache::Never)
db.try_find(oid, buf, &mut pack::cache::Never)
.ok()
.flatten()
.and_then(|o| o.into_commit_iter())
Expand Down
2 changes: 1 addition & 1 deletion git-odb/src/store/compound/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub enum Error {
#[error("The objects directory at '{0}' is not an accessible directory")]
Inaccessible(PathBuf),
#[error(transparent)]
Pack(#[from] pack::bundle::Error),
Pack(#[from] pack::bundle::init::Error),
#[error(transparent)]
Alternate(#[from] Box<crate::alternate::Error>),
}
Expand Down
6 changes: 3 additions & 3 deletions git-odb/src/store/linked/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl linked::Store {
impl crate::Find for linked::Store {
type Error = compound::find::Error;

fn find<'a>(
fn try_find<'a>(
&self,
id: impl AsRef<oid>,
buffer: &'a mut Vec<u8>,
Expand Down Expand Up @@ -107,13 +107,13 @@ impl crate::Find for linked::Store {
impl crate::Find for &linked::Store {
type Error = compound::find::Error;

fn find<'a>(
fn try_find<'a>(
&self,
id: impl AsRef<oid>,
buffer: &'a mut Vec<u8>,
pack_cache: &mut impl pack::cache::DecodeEntry,
) -> Result<Option<Object<'a>>, Self::Error> {
(*self).find(id, buffer, pack_cache)
(*self).try_find(id, buffer, pack_cache)
}

fn location_by_oid(&self, id: impl AsRef<oid>, buf: &mut Vec<u8>) -> Option<Location> {
Expand Down
2 changes: 1 addition & 1 deletion git-odb/tests/odb/store/linked/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ mod locate {
fn can_locate(db: &Store, hex_id: &str) {
let mut buf = vec![];
assert!(db
.find(hex_to_id(hex_id), &mut buf, &mut pack::cache::Never)
.try_find(hex_to_id(hex_id), &mut buf, &mut pack::cache::Never)
.expect("no read error")
.is_some());
}
Expand Down
14 changes: 14 additions & 0 deletions git-pack/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
### 0.9.0 (2021-08-??)

- **renames / moves**
- `find::Find` and `find::FindExt` only in `Find` and `FindExt` (not in `find` anymore)
- `data::output::count::Count` -> `data::output::Count`
- `data::output::entry::Entry` -> `data::output::Entry`
- `Find::find_existing_*` -> `Find::find_*`
- `Find::find_existing_*` -> `Find::find_*`
- `Find::find()-> `Find::try_find()`
- `bundle::Bundle` -> `Bundle`
- `bundle::Error` -> `bundle::init::Error`

* **new methods**
- `Find::find_tag_iter()`
2 changes: 1 addition & 1 deletion git-pack/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ authors = ["Sebastian Thiel <sebastian.thiel@icloud.com>"]
license = "MIT/Apache-2.0"
description = "Implements git packs and related data structures"
edition = "2018"
include = ["src/**/*"]
include = ["src/**/*", "CHANGELOG.md"]

[lib]
doctest = false
Expand Down
50 changes: 50 additions & 0 deletions git-pack/src/bundle/init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use crate::Bundle;
use std::{
convert::TryFrom,
path::{Path, PathBuf},
};

/// Returned by [`Bundle::at()`]
#[derive(thiserror::Error, Debug)]
#[allow(missing_docs)]
pub enum Error {
#[error("An 'idx' extension is expected of an index file: '{0}'")]
InvalidPath(PathBuf),
#[error(transparent)]
Pack(#[from] crate::data::header::decode::Error),
#[error(transparent)]
Index(#[from] crate::index::init::Error),
}

/// Initialization
impl Bundle {
/// Create a `Bundle` from `path`, which is either a pack file _(*.pack)_ or an index file _(*.idx)_.
///
/// The corresponding complementary file is expected to be present.
/// Also available via [`Bundle::try_from()`].
pub fn at(path: impl AsRef<Path>) -> Result<Self, Error> {
Self::try_from(path.as_ref())
}
}

impl TryFrom<&Path> for Bundle {
type Error = Error;

fn try_from(path: &Path) -> Result<Self, Self::Error> {
let ext = path
.extension()
.and_then(|e| e.to_str())
.ok_or_else(|| Error::InvalidPath(path.to_owned()))?;
Ok(match ext {
"idx" => Self {
index: crate::index::File::at(path)?,
pack: crate::data::File::at(path.with_extension("pack"))?,
},
"pack" => Self {
pack: crate::data::File::at(path)?,
index: crate::index::File::at(path.with_extension("idx"))?,
},
_ => return Err(Error::InvalidPath(path.to_owned())),
})
}
}
Loading