WIP, re-doing fs and fake git repos

This commit is contained in:
Mikayla Maki 2022-09-28 11:42:22 -07:00 committed by Julia
parent d5fd531743
commit 71b2126eca
7 changed files with 145 additions and 50 deletions

View file

@ -17,6 +17,8 @@ util = { path = "../util" }
log = { version = "0.4.16", features = ["kv_unstable_serde"] }
smol = "1.2"
parking_lot = "0.11.1"
async-trait = "0.1"
[dev-dependencies]
unindent = "0.1.7"

View file

@ -4,8 +4,29 @@ use parking_lot::Mutex;
use std::{path::Path, sync::Arc};
use util::ResultExt;
#[async_trait::async_trait]
pub trait GitRepository: Send + Sync + std::fmt::Debug {
fn manages(&self, path: &Path) -> bool;
fn in_dot_git(&self, path: &Path) -> bool;
fn content_path(&self) -> &Path;
fn git_dir_path(&self) -> &Path;
fn scan_id(&self) -> usize;
fn set_scan_id(&mut self, scan_id: usize);
fn git_repo(&self) -> Arc<Mutex<LibGitRepository>>;
fn boxed_clone(&self) -> Box<dyn GitRepository>;
async fn load_head_text(&self, relative_file_path: &Path) -> Option<String>;
}
#[derive(Clone)]
pub struct GitRepository {
pub struct RealGitRepository {
// Path to folder containing the .git file or directory
content_path: Arc<Path>,
// Path to the actual .git folder.
@ -15,50 +36,48 @@ pub struct GitRepository {
libgit_repository: Arc<Mutex<LibGitRepository>>,
}
impl GitRepository {
pub fn open(dotgit_path: &Path) -> Option<GitRepository> {
impl RealGitRepository {
pub fn open(dotgit_path: &Path) -> Option<Box<dyn GitRepository>> {
LibGitRepository::open(&dotgit_path)
.log_err()
.and_then(|libgit_repository| {
Some(Self {
.and_then::<Box<dyn GitRepository>, _>(|libgit_repository| {
Some(Box::new(Self {
content_path: libgit_repository.workdir()?.into(),
git_dir_path: dotgit_path.canonicalize().log_err()?.into(),
scan_id: 0,
libgit_repository: Arc::new(parking_lot::Mutex::new(libgit_repository)),
})
}))
})
}
}
pub fn manages(&self, path: &Path) -> bool {
#[async_trait::async_trait]
impl GitRepository for RealGitRepository {
fn manages(&self, path: &Path) -> bool {
path.canonicalize()
.map(|path| path.starts_with(&self.content_path))
.unwrap_or(false)
}
pub fn in_dot_git(&self, path: &Path) -> bool {
fn in_dot_git(&self, path: &Path) -> bool {
path.canonicalize()
.map(|path| path.starts_with(&self.git_dir_path))
.unwrap_or(false)
}
pub fn content_path(&self) -> &Path {
fn content_path(&self) -> &Path {
self.content_path.as_ref()
}
pub fn git_dir_path(&self) -> &Path {
fn git_dir_path(&self) -> &Path {
self.git_dir_path.as_ref()
}
pub fn scan_id(&self) -> usize {
fn scan_id(&self) -> usize {
self.scan_id
}
pub fn set_scan_id(&mut self, scan_id: usize) {
println!("setting scan id");
self.scan_id = scan_id;
}
pub async fn load_head_text(&self, relative_file_path: &Path) -> Option<String> {
async fn load_head_text(&self, relative_file_path: &Path) -> Option<String> {
fn logic(repo: &LibGitRepository, relative_file_path: &Path) -> Result<Option<String>> {
let object = repo
.head()?
@ -81,9 +100,21 @@ impl GitRepository {
}
None
}
fn git_repo(&self) -> Arc<Mutex<LibGitRepository>> {
self.libgit_repository.clone()
}
fn set_scan_id(&mut self, scan_id: usize) {
self.scan_id = scan_id;
}
fn boxed_clone(&self) -> Box<dyn GitRepository> {
Box::new(self.clone())
}
}
impl std::fmt::Debug for GitRepository {
impl std::fmt::Debug for RealGitRepository {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("GitRepository")
.field("content_path", &self.content_path)
@ -93,3 +124,59 @@ impl std::fmt::Debug for GitRepository {
.finish()
}
}
#[derive(Debug, Clone)]
pub struct FakeGitRepository {
content_path: Arc<Path>,
git_dir_path: Arc<Path>,
scan_id: usize,
}
impl FakeGitRepository {
pub fn open(dotgit_path: &Path, scan_id: usize) -> Box<dyn GitRepository> {
Box::new(FakeGitRepository {
content_path: dotgit_path.parent().unwrap().into(),
git_dir_path: dotgit_path.into(),
scan_id,
})
}
}
#[async_trait::async_trait]
impl GitRepository for FakeGitRepository {
fn manages(&self, path: &Path) -> bool {
path.starts_with(self.content_path())
}
fn in_dot_git(&self, path: &Path) -> bool {
path.starts_with(self.git_dir_path())
}
fn content_path(&self) -> &Path {
&self.content_path
}
fn git_dir_path(&self) -> &Path {
&self.git_dir_path
}
fn scan_id(&self) -> usize {
self.scan_id
}
async fn load_head_text(&self, _: &Path) -> Option<String> {
unimplemented!()
}
fn git_repo(&self) -> Arc<Mutex<LibGitRepository>> {
unimplemented!()
}
fn set_scan_id(&mut self, scan_id: usize) {
self.scan_id = scan_id;
}
fn boxed_clone(&self) -> Box<dyn GitRepository> {
Box::new(self.clone())
}
}