1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//! An object database storing each object in a zlib compressed file with its hash in the path
/// The maximum size that an object header can have. `git2` says 64, and `git` says 32 but also mentions it can be larger.
const HEADER_MAX_SIZE: usize = 64;
use std::path::{Path, PathBuf};

use gix_features::fs;

/// A database for reading and writing objects to disk, one file per object.
#[derive(Clone, PartialEq, Eq)]
pub struct Store {
    /// The directory in which objects are stored, containing 256 folders representing the hashes first byte.
    pub(crate) path: PathBuf,
    /// The kind of hash we should assume during iteration and when writing new objects.
    pub(crate) object_hash: gix_hash::Kind,
}

/// Initialization
impl Store {
    /// Initialize the Db with the `objects_directory` containing the hexadecimal first byte subdirectories, which in turn
    /// contain all loose objects.
    ///
    /// In a git repository, this would be `.git/objects`.
    ///
    /// The `object_hash` determines which hash to use when writing, finding or iterating objects.
    pub fn at(objects_directory: impl Into<PathBuf>, object_hash: gix_hash::Kind) -> Store {
        Store {
            path: objects_directory.into(),
            object_hash,
        }
    }

    /// Return the path to our `objects` directory.
    pub fn path(&self) -> &Path {
        &self.path
    }

    /// Return the kind of hash we would iterate and write.
    pub fn object_hash(&self) -> gix_hash::Kind {
        self.object_hash
    }
}

fn hash_path(id: &gix_hash::oid, mut root: PathBuf) -> PathBuf {
    let mut hex = gix_hash::Kind::hex_buf();
    let hex_len = id.hex_to_buf(hex.as_mut());
    let buf = std::str::from_utf8(&hex[..hex_len]).expect("ascii only in hex");
    root.push(&buf[..2]);
    root.push(&buf[2..]);
    root
}

///
#[allow(clippy::empty_docs)]
pub mod find;
///
#[allow(clippy::empty_docs)]
pub mod iter;
///
#[allow(clippy::empty_docs)]
pub mod verify;

/// The type for an iterator over `Result<gix_hash::ObjectId, Error>)`
pub struct Iter {
    inner: fs::walkdir::DirEntryIter,
    hash_hex_len: usize,
}

///
#[allow(clippy::empty_docs)]
pub mod write;