#![deny(missing_docs)]
use std::path::PathBuf;
use git2::Repository;
use thread_local::ThreadLocal;
pub struct ThreadLocalRepo {
tl: ThreadLocal<Repository>,
path: PathBuf,
}
impl ThreadLocalRepo {
pub fn new(path: PathBuf) -> Self {
Self {
path,
tl: ThreadLocal::new(),
}
}
pub fn get(&self) -> Result<&Repository, git2::Error> {
self.tl.get_or_try(|| Repository::open(&self.path))
}
pub fn get_uncached(&self) -> Result<Repository, git2::Error> {
Repository::open(&self.path)
}
}
pub trait RepositoryExt {
fn thread_local(&self) -> ThreadLocalRepo;
}
impl RepositoryExt for Repository {
fn thread_local(&self) -> ThreadLocalRepo {
ThreadLocalRepo::new(self.path().to_path_buf())
}
}