Skip to content

Commit 1eab15d

Browse files
committed
[git-odb] refactor
1 parent 4967c22 commit 1eab15d

File tree

11 files changed

+47
-54
lines changed

11 files changed

+47
-54
lines changed

git-odb/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@
2020
//! * A database containing various [`compound::Backends`][store::compound::Backend] as gathered from `alternates` files.
2121
pub use git_pack as pack;
2222
pub use pack::{data, Find, FindExt};
23-
pub use write::Write;
24-
25-
// TODO: leave it at 'git_odb::pack::data::Object` which is really what it is by now.
2623

2724
///
2825
pub mod store;
2926

3027
pub mod alternate;
3128
///
3229
pub mod write;
30+
pub use write::Write;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use git_pack::data;
99
#[allow(missing_docs)]
1010
pub enum Error {
1111
#[error("An error occurred while obtaining an object from the loose object store")]
12-
Loose(#[from] loose::backend::find::Error),
12+
Loose(#[from] loose::find::Error),
1313
#[error("An error occurred while obtaining an object from the packed object store")]
1414
Pack(#[from] pack::data::decode_entry::Error),
1515
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::store::{compound, loose};
44
use git_object::{mutable, Kind};
55

66
impl crate::write::Write for compound::Backend {
7-
type Error = loose::backend::write::Error;
7+
type Error = loose::write::Error;
88

99
fn write(&self, object: &mutable::Object, hash: git_hash::Kind) -> Result<git_hash::ObjectId, Self::Error> {
1010
self.loose.write(object, hash)

git-odb/src/store/linked/iter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::store::loose;
77
#[allow(clippy::large_enum_variant)]
88
enum DbState {
99
Pack { pack_index: usize, entry_index: u32 },
10-
Loose { iter: loose::backend::iter::Type },
10+
Loose { iter: loose::iter::Type },
1111
}
1212

1313
impl Default for DbState {
@@ -53,7 +53,7 @@ impl<Db> Iterator for AllObjects<Db>
5353
where
5454
Db: Borrow<linked::Db>,
5555
{
56-
type Item = Result<ObjectId, loose::backend::iter::Error>;
56+
type Item = Result<ObjectId, loose::iter::Error>;
5757

5858
fn next(&mut self) -> Option<Self::Item> {
5959
let db = self.db.borrow();

git-odb/src/store/linked/write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::io::Read;
44
use crate::store::{linked, loose};
55

66
impl crate::write::Write for linked::Db {
7-
type Error = loose::backend::write::Error;
7+
type Error = loose::write::Error;
88

99
fn write(&self, object: &mutable::Object, hash: git_hash::Kind) -> Result<git_hash::ObjectId, Self::Error> {
1010
self.dbs[0].loose.write(object, hash)

git-odb/src/store/loose/backend/mod.rs

Lines changed: 0 additions & 39 deletions
This file was deleted.

git-odb/src/store/loose/backend/find.rs renamed to git-odb/src/store/loose/find.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::store::loose::{backend::sha1_path, Backend, HEADER_READ_UNCOMPRESSED_BYTES};
1+
use crate::store::loose::{sha1_path, Backend, HEADER_READ_UNCOMPRESSED_BYTES};
22
use git_features::zlib;
33
use git_pack::{data, loose::object::header};
44
use std::{convert::TryInto, fs, io::Read, path::PathBuf};
File renamed without changes.

git-odb/src/store/loose/mod.rs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,41 @@
11
//! An object database storing each object in a zlib compressed file with its hash in the path
22
const HEADER_READ_UNCOMPRESSED_BYTES: usize = 512;
3+
use std::path::PathBuf;
34

5+
/// A database for reading and writing objects to disk, one file per object.
6+
pub struct Backend {
7+
/// The directory in which objects are stored, containing 256 folders representing the hashes first byte.
8+
pub path: PathBuf,
9+
}
10+
11+
/// Initialization
12+
impl Backend {
13+
/// Initialize the Db with the `objects_directory` containing the hexadecimal first byte subdirectories, which in turn
14+
/// contain all loose objects.
15+
///
16+
/// In a git repository, this would be `.git/objects`.
17+
pub fn at(objects_directory: impl Into<PathBuf>) -> Backend {
18+
Backend {
19+
path: objects_directory.into(),
20+
}
21+
}
22+
}
23+
24+
pub(crate) fn sha1_path(id: &git_hash::oid, mut root: PathBuf) -> PathBuf {
25+
match id.kind() {
26+
git_hash::Kind::Sha1 => {
27+
let hex = id.to_sha1_hex();
28+
let buf = std::str::from_utf8(&hex).expect("ascii only in hex");
29+
root.push(&buf[..2]);
30+
root.push(&buf[2..]);
31+
root
32+
}
33+
}
34+
}
35+
36+
///
37+
pub mod find;
38+
///
39+
pub mod iter;
440
///
5-
pub mod backend;
6-
#[doc(inline)]
7-
pub use backend::Backend;
41+
pub mod write;

git-odb/src/store/loose/backend/write.rs renamed to git-odb/src/store/loose/write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl Backend {
105105
hash::Write { hash, inner: file }: hash::Write<CompressedTempfile>,
106106
) -> Result<git_hash::ObjectId, Error> {
107107
let id = git_hash::ObjectId::from(hash.digest());
108-
let object_path = loose::backend::sha1_path(&id, self.path.clone());
108+
let object_path = loose::sha1_path(&id, self.path.clone());
109109
let object_dir = object_path
110110
.parent()
111111
.expect("each object path has a 1 hex-bytes directory");

0 commit comments

Comments
 (0)