git-storage: port storage from librad

This provides a port of the librad::git::storage[0] API, with a focus
on being more generic and decomposing writing and reading
operations. It also takes heavy inspiration from
link_replication::io::refdb[1] for its implementation of the
transactional writes.

[0]: https://github.com/radicle-dev/radicle-link/blob/master/librad/src/git/storage.rs
[1]: https://github.com/radicle-dev/radicle-link/blob/master/link-replication/src/io/refdb.rs

Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com>
This commit is contained in:
Fintan Halpenny
2022-09-27 12:32:20 +01:00
parent 69e815c124
commit e6f36318cd
19 changed files with 2131 additions and 0 deletions
+49
View File
@@ -0,0 +1,49 @@
// Copyright © 2022 The Radicle Link Contributors
// SPDX-License-Identifier: GPL-3.0-or-later
use super::*;
/// Read-only access to a git refdb.
///
/// See [`crate::Read`] for an implementation of this trait.
pub trait Read {
/// The error type for finding an object in the refdb.
type FindObj: Error + Send + Sync + 'static;
/// The error type for finding a blob in the refdb.
type FindBlob: Error + Send + Sync + 'static;
/// The error type for finding a commit in the refdb.
type FindCommit: Error + Send + Sync + 'static;
/// The error type for finding a tag in the refdb.
type FindTag: Error + Send + Sync + 'static;
/// The error type for finding a tree in the refdb.
type FindTree: Error + Send + Sync + 'static;
/// Find the [`Object`] corresponding to the given `oid`.
///
/// Returns `None` if the [`Object`] did not exist.
fn find_object(&self, oid: Oid) -> Result<Option<Object>, Self::FindObj>;
/// Find the [`Blob`] corresponding to the given `oid`.
///
/// Returns `None` if the [`Blob`] did not exist.
fn find_blob(&self, oid: Oid) -> Result<Option<Blob>, Self::FindBlob>;
/// Find the [`Commit`] corresponding to the given `oid`.
///
/// Returns `None` if the [`Commit`] did not exist.
fn find_commit(&self, oid: Oid) -> Result<Option<Commit>, Self::FindCommit>;
/// Find the [`Tag`] corresponding to the given `oid`.
///
/// Returns `None` if the [`Tag`] did not exist.
fn find_tag(&self, oid: Oid) -> Result<Option<Tag>, Self::FindTag>;
/// Find the [`Object`] corresponding to the given `oid`.
///
/// Returns `None` if the [`Tag`] did not exist.
fn find_tree(&self, oid: Oid) -> Result<Option<Tree>, Self::FindTree>;
}
+66
View File
@@ -0,0 +1,66 @@
// Copyright © 2022 The Radicle Link Contributors
// SPDX-License-Identifier: GPL-3.0-or-later
use std::error::Error;
use git_ext::Oid;
use git_ref_format::RefStr;
use super::{Commit, Object, Read, Tree};
/// Read-write access to a git odb.
///
/// See [`crate::Write`] for an implementation of this trait.
pub trait Write: Read {
/// The error type for writing a blob to the odb.
type WriteBlob: Error + Send + Sync + 'static;
/// The error type for writing a commit to the odb.
type WriteCommit: Error + Send + Sync + 'static;
/// The error type for writing a tag to the odb.
type WriteTag: Error + Send + Sync + 'static;
/// The error type for writing a tree to the odb.
type WriteTree: Error + Send + Sync + 'static;
/// Write a [`super::Blob`] containing the `data` provided.
fn write_blob(&self, data: &[u8]) -> Result<Oid, Self::WriteBlob>;
/// Write a [`Commit`] that points to the given `tree` and has the provided
/// `parents`.
///
/// The signature of the [`Commit`] is expected to be provided by the
/// implementor of the trait.
///
/// The commit will not be associated with any reference. If this is
/// required then you can use the [`Oid`] as the target for a
/// [`crate::refdb::Update`].
fn write_commit<'a>(
&self,
tree: &Tree,
parents: &[&Commit<'a>],
message: &str,
) -> Result<Oid, Self::WriteCommit>;
/// Write a [`super::Tag`] that points to the given `target`.
///
/// The signature of the [`super::Tag`] is expected to be provided by the
/// implementor of the trait.
///
/// No reference is created, however, the `name` is used for naming the
/// [`super::Tag`] object.
///
/// If a reference is required then you can use the [`Oid`] as the target
/// for a [`crate::refdb::Update`].
fn write_tag<R>(&self, name: R, target: &Object, message: &str) -> Result<Oid, Self::WriteTag>
where
R: AsRef<RefStr>;
/// Write a [`super::Tree`] using the provided `builder`.
// XXX: It's annoying that this exposes git2 but the effort of abstracting the
// tree builder is too high
// TODO: instead of passing the builder, we could use an enum of operations to
// modify a builder
fn write_tree(&self, builder: git2::TreeBuilder) -> Result<Oid, Self::WriteTree>;
}