Refactored git repository code to seperate out repository entry tracking data and git2 mocking code.

Co-authored-by: Max <max@zed.dev>
Co-authored-by: Julia <julia@zed.dev>
This commit is contained in:
Mikayla Maki 2022-09-30 17:33:34 -07:00
parent c95646a298
commit af0974264c
7 changed files with 143 additions and 175 deletions

View file

@ -1,39 +1,20 @@
use anyhow::Result;
use collections::HashMap;
use git2::Repository as LibGitRepository;
use parking_lot::Mutex;
use util::ResultExt;
use std::{path::{Path, PathBuf}, sync::Arc};
use std::{
path::{Path, PathBuf},
sync::Arc,
};
pub use git2::Repository as LibGitRepository;
#[async_trait::async_trait]
pub trait GitRepository: Send {
// fn manages(&self, path: &Path) -> bool;
// fn reopen_git_repo(&mut self) -> bool;
// fn git_repo(&self) -> Arc<Mutex<LibGitRepository>>;
// fn boxed_clone(&self) -> Box<dyn GitRepository>;
fn load_head_text(&self, relative_file_path: &Path) -> Option<String>;
fn open_real(dotgit_path: &Path) -> Option<Arc<Mutex<dyn GitRepository>>>
where Self: Sized
{
LibGitRepository::open(&dotgit_path)
.log_err()
.and_then::<Arc<Mutex<dyn GitRepository>>, _>(|libgit_repository| {
Some(Arc::new(Mutex::new(libgit_repository)))
})
}
}
#[async_trait::async_trait]
impl GitRepository for LibGitRepository {
// fn manages(&self, path: &Path) -> bool {
// path.canonicalize()
// .map(|path| path.starts_with(&self.content_path))
// .unwrap_or(false)
// }
fn load_head_text(&self, relative_file_path: &Path) -> Option<String> {
fn logic(repo: &LibGitRepository, relative_file_path: &Path) -> Result<Option<String>> {
const STAGE_NORMAL: i32 = 0;
@ -56,10 +37,8 @@ impl GitRepository for LibGitRepository {
}
}
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub struct FakeGitRepository {
content_path: Arc<Path>,
git_dir_path: Arc<Path>,
state: Arc<Mutex<FakeGitRepositoryState>>,
}
@ -69,47 +48,15 @@ pub struct FakeGitRepositoryState {
}
impl FakeGitRepository {
pub fn open(dotgit_path: &Path, state: Arc<Mutex<FakeGitRepositoryState>>) -> Box<dyn GitRepository> {
Box::new(FakeGitRepository {
content_path: dotgit_path.parent().unwrap().into(),
git_dir_path: dotgit_path.into(),
state,
})
pub fn open(state: Arc<Mutex<FakeGitRepositoryState>>) -> Arc<Mutex<dyn GitRepository>> {
Arc::new(Mutex::new(FakeGitRepository { state }))
}
}
#[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
}
async fn load_head_text(&self, path: &Path) -> Option<String> {
fn load_head_text(&self, path: &Path) -> Option<String> {
let state = self.state.lock();
state.index_contents.get(path).cloned()
}
fn reopen_git_repo(&mut self) -> bool {
true
}
fn git_repo(&self) -> Arc<Mutex<LibGitRepository>> {
unimplemented!()
}
fn boxed_clone(&self) -> Box<dyn GitRepository> {
Box::new(self.clone())
}
}