use radicle::cob::object::Storage;
use radicle::cob::ObjectId;
use radicle::storage::git::paths;
use radicle::{issue, Profile};
use snafu::ResultExt;
pub(crate) struct RepositoryLoader<'a> {
profile: &'a Profile,
}
impl<'a> RepositoryLoader<'a> {
pub fn new(profile: &'a Profile) -> Result<Self, snafu::Whatever> {
Ok(Self { profile })
}
pub fn open_repository(
&self,
rid: &radicle::prelude::RepoId,
) -> Result<
(
radicle::storage::git::Repository,
radicle::git::raw::Repository,
),
snafu::Whatever,
> {
let path = paths::repository(&self.profile.storage, rid);
let repo_handle = radicle::storage::git::Repository::open(path.clone(), *rid)
.whatever_context("Failed to open readonly repo")?;
let git2_repo = radicle::git::raw::Repository::open(path)
.whatever_context("Failed to open git2 repo")?;
Ok((repo_handle, git2_repo))
}
pub fn load_patches(
&self,
repo: &radicle::storage::git::Repository,
) -> Result<Vec<ObjectId>, snafu::Whatever> {
let patches =
radicle::patch::Patches::open(repo).whatever_context("Failed opening patches")?;
let patch_ids = patches
.as_ref()
.types(&radicle::patch::TYPENAME)
.whatever_context("Unable to load patch cob ids")?;
Ok(patch_ids.into_keys().collect())
}
pub fn load_issues(
&self,
repo: &radicle::storage::git::Repository,
) -> Result<Vec<ObjectId>, snafu::Whatever> {
let issues =
radicle::issue::Issues::open(repo).whatever_context("Failed opening issues")?;
let issue_ids = issues
.as_ref()
.types(&issue::TYPENAME)
.whatever_context("Unable to load issue cob ids")?;
Ok(issue_ids.into_keys().collect())
}
}