Skip to content

Commit

Permalink
Fix initializing pack bundles in compound db (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Apr 3, 2021
1 parent d629339 commit 5a48e08
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
28 changes: 16 additions & 12 deletions git-odb/src/compound/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,22 @@ impl compound::Db {
if !loose_objects.is_dir() {
return Err(Error::Inaccessible(loose_objects));
}
let packs = if let Ok(entries) = std::fs::read_dir(loose_objects.join("packs")) {
let mut packs_and_sizes = entries
.filter_map(Result::ok)
.filter_map(|e| e.metadata().map(|md| (e.path(), md)).ok())
.filter(|(_, md)| md.file_type().is_file())
.filter(|(p, _)| p.extension().unwrap_or_default() == "idx" && p.starts_with("pack-"))
.map(|(p, md)| pack::Bundle::at(p).map(|b| (b, md.len())))
.collect::<Result<Vec<_>, _>>()?;
packs_and_sizes.sort_by_key(|e| e.1);
packs_and_sizes.into_iter().rev().map(|(b, _)| b).collect()
} else {
Vec::new()
let packs = match std::fs::read_dir(loose_objects.join("pack")) {
Ok(entries) => {
let mut packs_and_sizes = entries
.filter_map(Result::ok)
.filter_map(|e| e.metadata().map(|md| (e.path(), md)).ok())
.filter(|(_, md)| md.file_type().is_file())
.filter(|(p, _)| {
p.extension().unwrap_or_default() == "idx"
&& p.file_name().unwrap_or_default().to_string_lossy().starts_with("pack-")
})
.map(|(p, md)| pack::Bundle::at(p).map(|b| (b, md.len())))
.collect::<Result<Vec<_>, _>>()?;
packs_and_sizes.sort_by_key(|e| e.1);
packs_and_sizes.into_iter().rev().map(|(b, _)| b).collect()
}
Err(_) => Vec::new(),
};

Ok(compound::Db {
Expand Down
39 changes: 27 additions & 12 deletions git-odb/tests/compound/mod.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
use crate::{fixture_path, hex_to_id};
use crate::fixture_path;
use git_odb::compound::Db;

fn db() -> Db {
Db::at(fixture_path("objects")).expect("valid object path")
}

fn can_locate(db: &Db, hex_id: &str) {
let mut buf = vec![];
assert!(db.locate(hex_to_id(hex_id).to_borrowed(), &mut buf).is_some());
}
mod init {
use crate::compound::db;

#[test]
fn loose_object_lookup() {
can_locate(&db(), "37d4e6c5c48ba0d245164c4e10d5f41140cab980");
#[test]
fn has_packs() {
assert_eq!(db().packs.len(), 3)
}
}

#[test]
#[should_panic]
fn pack_object_lookup() {
can_locate(&db(), "501b297447a8255d3533c6858bb692575cdefaa0");
mod locate {
use crate::{compound::db, hex_to_id};
use git_odb::compound::Db;

fn can_locate(db: &Db, hex_id: &str) {
let mut buf = vec![];
assert!(db.locate(hex_to_id(hex_id).to_borrowed(), &mut buf).is_some());
}

#[test]
fn loose_object() {
can_locate(&db(), "37d4e6c5c48ba0d245164c4e10d5f41140cab980");
}

#[test]
fn pack_object() {
can_locate(&db(), "501b297447a8255d3533c6858bb692575cdefaa0"); // pack 11fd
can_locate(&db(), "4dac9989f96bc5b5b1263b582c08f0c5f0b58542"); // pack a2bf
can_locate(&db(), "dd25c539efbb0ab018caa4cda2d133285634e9b5"); // pack c043
}
}

0 comments on commit 5a48e08

Please sign in to comment.