use auths_core::ports::storage::StorageError;
use git2::Repository;
use std::path::{Path, PathBuf};
use std::sync::Mutex;
pub struct GitRepo {
inner: Mutex<Repository>,
path: PathBuf,
}
impl GitRepo {
pub fn open(path: impl AsRef<Path>) -> Result<Self, StorageError> {
let path = path.as_ref().to_path_buf();
let inner = Repository::open(&path).map_err(|e| StorageError::Io(e.to_string()))?;
Ok(Self {
inner: Mutex::new(inner),
path,
})
}
pub fn init(path: impl AsRef<Path>) -> Result<Self, StorageError> {
let path = path.as_ref().to_path_buf();
let inner = Repository::init(&path).map_err(|e| StorageError::Io(e.to_string()))?;
Ok(Self {
inner: Mutex::new(inner),
path,
})
}
pub(crate) fn with_repo<T>(
&self,
f: impl FnOnce(&Repository) -> Result<T, StorageError>,
) -> Result<T, StorageError> {
let repo = self
.inner
.lock()
.map_err(|e| StorageError::Io(format!("mutex poisoned: {}", e)))?;
f(&repo)
}
pub fn path(&self) -> &Path {
&self.path
}
}