2022-09-28 00:06:18 +00:00
|
|
|
use anyhow::Result;
|
2022-09-30 19:50:55 +00:00
|
|
|
use collections::HashMap;
|
2022-09-28 15:43:33 +00:00
|
|
|
use git2::Repository as LibGitRepository;
|
2022-09-22 23:55:24 +00:00
|
|
|
use parking_lot::Mutex;
|
|
|
|
use util::ResultExt;
|
2022-09-30 22:25:25 +00:00
|
|
|
use std::{path::{Path, PathBuf}, sync::Arc};
|
2022-09-22 23:55:24 +00:00
|
|
|
|
2022-09-28 18:42:22 +00:00
|
|
|
#[async_trait::async_trait]
|
2022-09-30 22:25:25 +00:00
|
|
|
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
|
|
|
|
{
|
2022-09-28 00:06:18 +00:00
|
|
|
LibGitRepository::open(&dotgit_path)
|
2022-09-22 23:55:24 +00:00
|
|
|
.log_err()
|
2022-09-30 22:25:25 +00:00
|
|
|
.and_then::<Arc<Mutex<dyn GitRepository>>, _>(|libgit_repository| {
|
|
|
|
Some(Arc::new(Mutex::new(libgit_repository)))
|
2022-09-22 23:55:24 +00:00
|
|
|
})
|
|
|
|
}
|
2022-09-28 18:42:22 +00:00
|
|
|
}
|
2022-09-22 23:55:24 +00:00
|
|
|
|
2022-09-28 18:42:22 +00:00
|
|
|
#[async_trait::async_trait]
|
2022-09-30 22:25:25 +00:00
|
|
|
impl GitRepository for LibGitRepository {
|
|
|
|
// fn manages(&self, path: &Path) -> bool {
|
|
|
|
// path.canonicalize()
|
|
|
|
// .map(|path| path.starts_with(&self.content_path))
|
|
|
|
// .unwrap_or(false)
|
|
|
|
// }
|
2022-09-22 23:55:24 +00:00
|
|
|
|
|
|
|
|
2022-09-30 22:25:25 +00:00
|
|
|
fn load_head_text(&self, relative_file_path: &Path) -> Option<String> {
|
2022-09-28 15:43:33 +00:00
|
|
|
fn logic(repo: &LibGitRepository, relative_file_path: &Path) -> Result<Option<String>> {
|
2022-09-29 17:10:39 +00:00
|
|
|
const STAGE_NORMAL: i32 = 0;
|
|
|
|
let index = repo.index()?;
|
|
|
|
let oid = match index.get_path(relative_file_path, STAGE_NORMAL) {
|
|
|
|
Some(entry) => entry.id,
|
2022-09-28 00:06:18 +00:00
|
|
|
None => return Ok(None),
|
|
|
|
};
|
|
|
|
|
2022-09-29 17:10:39 +00:00
|
|
|
let content = repo.find_blob(oid)?.content().to_owned();
|
|
|
|
let head_text = String::from_utf8(content)?;
|
2022-09-28 00:06:18 +00:00
|
|
|
Ok(Some(head_text))
|
|
|
|
}
|
|
|
|
|
2022-09-30 22:25:25 +00:00
|
|
|
match logic(&self, relative_file_path) {
|
2022-09-28 00:06:18 +00:00
|
|
|
Ok(value) => return value,
|
|
|
|
Err(err) => log::error!("Error loading head text: {:?}", err),
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2022-09-22 23:55:24 +00:00
|
|
|
}
|
2022-09-28 18:42:22 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct FakeGitRepository {
|
|
|
|
content_path: Arc<Path>,
|
|
|
|
git_dir_path: Arc<Path>,
|
2022-09-30 19:50:55 +00:00
|
|
|
state: Arc<Mutex<FakeGitRepositoryState>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Default)]
|
|
|
|
pub struct FakeGitRepositoryState {
|
|
|
|
pub index_contents: HashMap<PathBuf, String>,
|
2022-09-28 18:42:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FakeGitRepository {
|
2022-09-30 22:25:25 +00:00
|
|
|
pub fn open(dotgit_path: &Path, state: Arc<Mutex<FakeGitRepositoryState>>) -> Box<dyn GitRepository> {
|
2022-09-28 18:42:22 +00:00
|
|
|
Box::new(FakeGitRepository {
|
|
|
|
content_path: dotgit_path.parent().unwrap().into(),
|
|
|
|
git_dir_path: dotgit_path.into(),
|
2022-09-30 19:50:55 +00:00
|
|
|
state,
|
2022-09-28 18:42:22 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl GitRepository for FakeGitRepository {
|
|
|
|
fn manages(&self, path: &Path) -> bool {
|
|
|
|
path.starts_with(self.content_path())
|
|
|
|
}
|
|
|
|
|
2022-09-30 22:25:25 +00:00
|
|
|
// fn in_dot_git(&self, path: &Path) -> bool {
|
|
|
|
// path.starts_with(self.git_dir_path())
|
|
|
|
// }
|
2022-09-28 18:42:22 +00:00
|
|
|
|
|
|
|
fn content_path(&self) -> &Path {
|
|
|
|
&self.content_path
|
|
|
|
}
|
|
|
|
|
|
|
|
fn git_dir_path(&self) -> &Path {
|
|
|
|
&self.git_dir_path
|
|
|
|
}
|
|
|
|
|
2022-09-30 19:50:55 +00:00
|
|
|
async fn load_head_text(&self, path: &Path) -> Option<String> {
|
|
|
|
let state = self.state.lock();
|
|
|
|
state.index_contents.get(path).cloned()
|
2022-09-28 18:42:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-29 17:10:39 +00:00
|
|
|
fn reopen_git_repo(&mut self) -> bool {
|
2022-09-30 19:50:55 +00:00
|
|
|
true
|
2022-09-29 17:10:39 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 18:42:22 +00:00
|
|
|
fn git_repo(&self) -> Arc<Mutex<LibGitRepository>> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn boxed_clone(&self) -> Box<dyn GitRepository> {
|
|
|
|
Box::new(self.clone())
|
|
|
|
}
|
|
|
|
}
|