// Copyright © 2019-2020 The Radicle Foundation // // This file is part of radicle-link, distributed under the GPLv3 with Radicle // Linking Exception. For full terms see the included LICENSE file. use std::path::Path; use git_ext::{error::is_not_found_err, Oid}; use std_ext::result::ResultExt as _; use crate::{ glob, odb::{self, Blob, Commit, Object, Tag, Tree}, refdb::{self, Reference, References, ReferencesGlob}, }; /// A read-only storage backend for accessing git's odb and refdb. /// /// For access to the odb see [`odb::Read`]. /// For access to the refdb see [`refdb::Read`]. /// /// To construct the `Read` storage use [`Read::open`]. pub struct Read { pub(super) raw: git2::Repository, } impl Read { /// Open the [`Read`] storage using the `path` provided. /// /// The storage **must** exist already. To initialise the storage for the /// first time see [`crate::Write::open`]. /// /// # Concurrency /// /// [`Read`] can be sent between threads, but it can't be shared between /// threads. _Some_ operations are safe to perform concurrently in much /// the same way two `git` processes can access the same repository. /// However, if you need multiple [`Read`]s to be shared between /// threads, use a [`crate::Pool`] instead. pub fn open>(path: P) -> Result { crate::init(); let raw = git2::Repository::open(path)?; Ok(Self { raw }) } } pub mod error { use thiserror::Error; use crate::refdb::error::ParseReference; #[derive(Debug, Error)] pub enum Init { #[error(transparent)] Git(#[from] git2::Error), } #[derive(Debug, Error)] pub enum Identifier { #[error(transparent)] Git(#[from] git2::Error), } #[derive(Debug, Error)] pub enum FindRef { #[error(transparent)] Git(#[from] git2::Error), #[error(transparent)] Parse(#[from] ParseReference), } } // impls impl<'a> refdb::Read for &'a Read { type FindRef = error::FindRef; type FindRefs = refdb::error::Iter; type FindRefOid = git2::Error; type References = References<'a>; fn find_reference(&self, reference: Ref) -> Result, Self::FindRef> where Ref: AsRef, { let reference = self .raw .find_reference(reference.as_ref().as_str()) .map(Some) .or_matches::(is_not_found_err, || Ok(None))?; Ok(reference.map(Reference::try_from).transpose()?) } fn find_references(&self, reference: Pat) -> Result where Pat: AsRef, { Ok(References { inner: ReferencesGlob { iter: self.raw.references()?, glob: glob::RefspecMatcher::from(reference.as_ref().to_owned()), }, }) } fn find_reference_oid(&self, reference: Ref) -> Result, Self::FindRefOid> where Ref: AsRef, { self.raw .refname_to_id(reference.as_ref().as_str()) .map(|oid| Some(Oid::from(oid))) .or_matches(is_not_found_err, || Ok(None)) } } impl odb::Read for Read { type FindObj = git2::Error; type FindBlob = git2::Error; type FindCommit = git2::Error; type FindTag = git2::Error; type FindTree = git2::Error; fn find_object(&self, oid: Oid) -> Result, Self::FindObj> { self.raw .find_object(oid.into(), None) .map(Some) .or_matches::(is_not_found_err, || Ok(None)) } fn find_blob(&self, oid: Oid) -> Result, Self::FindBlob> { self.raw .find_blob(oid.into()) .map(Some) .or_matches(is_not_found_err, || Ok(None)) } fn find_commit(&self, oid: Oid) -> Result, Self::FindCommit> { self.raw .find_commit(oid.into()) .map(Some) .or_matches(is_not_found_err, || Ok(None)) } fn find_tag(&self, oid: Oid) -> Result, Self::FindTag> { self.raw .find_tag(oid.into()) .map(Some) .or_matches(is_not_found_err, || Ok(None)) } fn find_tree(&self, oid: Oid) -> Result, Self::FindTree> { self.raw .find_tree(oid.into()) .map(Some) .or_matches(is_not_found_err, || Ok(None)) } }