zed/crates/git/src/repository.rs

63 lines
1.8 KiB
Rust
Raw Normal View History

use anyhow::Result;
use collections::HashMap;
use parking_lot::Mutex;
use std::{
path::{Path, PathBuf},
sync::Arc,
};
pub use git2::Repository as LibGitRepository;
2022-09-28 18:42:22 +00:00
#[async_trait::async_trait]
pub trait GitRepository: Send {
fn load_head_text(&self, relative_file_path: &Path) -> Option<String>;
2022-09-28 18:42:22 +00:00
}
2022-09-28 18:42:22 +00:00
#[async_trait::async_trait]
impl GitRepository for LibGitRepository {
fn load_head_text(&self, relative_file_path: &Path) -> Option<String> {
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,
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)?;
Ok(Some(head_text))
}
match logic(&self, relative_file_path) {
Ok(value) => return value,
Err(err) => log::error!("Error loading head text: {:?}", err),
}
None
}
}
2022-09-28 18:42:22 +00:00
#[derive(Debug, Clone, Default)]
2022-09-28 18:42:22 +00:00
pub struct FakeGitRepository {
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 {
pub fn open(state: Arc<Mutex<FakeGitRepositoryState>>) -> Arc<Mutex<dyn GitRepository>> {
Arc::new(Mutex::new(FakeGitRepository { state }))
2022-09-28 18:42:22 +00:00
}
}
#[async_trait::async_trait]
impl GitRepository for FakeGitRepository {
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
}
}