Find repos under worktree & return correct results for repo queries
Co-Authored-By: Mikayla Maki <mikayla@zed.dev>
This commit is contained in:
parent
c8e63d76a4
commit
4251e0f5f1
3 changed files with 66 additions and 180 deletions
|
@ -3,19 +3,8 @@ use parking_lot::Mutex;
|
|||
use std::{path::Path, sync::Arc};
|
||||
use util::ResultExt;
|
||||
|
||||
pub trait GitRepository: Send + Sync {
|
||||
fn boxed_clone(&self) -> Box<dyn GitRepository>;
|
||||
fn is_path_managed_by(&self, path: &Path) -> bool;
|
||||
fn is_path_in_git_folder(&self, path: &Path) -> bool;
|
||||
fn content_path(&self) -> &Path;
|
||||
fn git_dir_path(&self) -> &Path;
|
||||
fn last_scan_id(&self) -> usize;
|
||||
fn set_scan_id(&mut self, scan_id: usize);
|
||||
fn with_repo(&mut self, f: Box<dyn FnOnce(&mut git2::Repository)>);
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct RealGitRepository {
|
||||
pub struct GitRepository {
|
||||
// Path to folder containing the .git file or directory
|
||||
content_path: Arc<Path>,
|
||||
// Path to the actual .git folder.
|
||||
|
@ -25,118 +14,50 @@ pub struct RealGitRepository {
|
|||
libgit_repository: Arc<Mutex<git2::Repository>>,
|
||||
}
|
||||
|
||||
impl RealGitRepository {
|
||||
pub fn open(
|
||||
abs_dotgit_path: &Path,
|
||||
content_path: &Arc<Path>,
|
||||
) -> Option<Box<dyn GitRepository>> {
|
||||
Repository::open(&abs_dotgit_path)
|
||||
impl GitRepository {
|
||||
pub fn open(dotgit_path: &Path) -> Option<GitRepository> {
|
||||
Repository::open(&dotgit_path)
|
||||
.log_err()
|
||||
.map::<Box<dyn GitRepository>, _>(|libgit_repository| {
|
||||
Box::new(Self {
|
||||
content_path: content_path.clone(),
|
||||
git_dir_path: libgit_repository.path().into(),
|
||||
.and_then(|libgit_repository| {
|
||||
Some(Self {
|
||||
content_path: libgit_repository.workdir()?.into(),
|
||||
git_dir_path: dotgit_path.canonicalize().log_err()?.into(),
|
||||
last_scan_id: 0,
|
||||
libgit_repository: Arc::new(parking_lot::Mutex::new(libgit_repository)),
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl GitRepository for RealGitRepository {
|
||||
fn boxed_clone(&self) -> Box<dyn GitRepository> {
|
||||
Box::new(self.clone())
|
||||
pub fn is_path_managed_by(&self, path: &Path) -> bool {
|
||||
path.canonicalize()
|
||||
.map(|path| path.starts_with(&self.content_path))
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
fn is_path_managed_by(&self, path: &Path) -> bool {
|
||||
path.starts_with(&self.content_path)
|
||||
pub fn is_path_in_git_folder(&self, path: &Path) -> bool {
|
||||
path.canonicalize()
|
||||
.map(|path| path.starts_with(&self.git_dir_path))
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
fn is_path_in_git_folder(&self, path: &Path) -> bool {
|
||||
path.starts_with(&self.git_dir_path)
|
||||
}
|
||||
|
||||
fn content_path(&self) -> &Path {
|
||||
pub fn content_path(&self) -> &Path {
|
||||
self.content_path.as_ref()
|
||||
}
|
||||
|
||||
fn git_dir_path(&self) -> &Path {
|
||||
pub fn git_dir_path(&self) -> &Path {
|
||||
self.git_dir_path.as_ref()
|
||||
}
|
||||
|
||||
fn last_scan_id(&self) -> usize {
|
||||
pub fn last_scan_id(&self) -> usize {
|
||||
self.last_scan_id
|
||||
}
|
||||
|
||||
fn set_scan_id(&mut self, scan_id: usize) {
|
||||
pub fn set_scan_id(&mut self, scan_id: usize) {
|
||||
self.last_scan_id = scan_id;
|
||||
}
|
||||
|
||||
fn with_repo(&mut self, f: Box<dyn FnOnce(&mut git2::Repository)>) {
|
||||
pub fn with_repo(&mut self, f: Box<dyn FnOnce(&mut git2::Repository)>) {
|
||||
let mut git2 = self.libgit_repository.lock();
|
||||
f(&mut git2)
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for &Box<dyn GitRepository> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.content_path() == other.content_path()
|
||||
}
|
||||
}
|
||||
impl Eq for &Box<dyn GitRepository> {}
|
||||
|
||||
#[cfg(any(test, feature = "test-support"))]
|
||||
#[derive(Clone)]
|
||||
pub struct FakeGitRepository {
|
||||
// Path to folder containing the .git file or directory
|
||||
content_path: Arc<Path>,
|
||||
// Path to the actual .git folder.
|
||||
// Note: if .git is a file, this points to the folder indicated by the .git file
|
||||
git_dir_path: Arc<Path>,
|
||||
last_scan_id: usize,
|
||||
}
|
||||
|
||||
impl FakeGitRepository {
|
||||
pub fn new(abs_dotgit_path: &Path, content_path: &Arc<Path>) -> FakeGitRepository {
|
||||
Self {
|
||||
content_path: content_path.clone(),
|
||||
git_dir_path: abs_dotgit_path.into(),
|
||||
last_scan_id: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(test, feature = "test-support"))]
|
||||
impl GitRepository for FakeGitRepository {
|
||||
fn boxed_clone(&self) -> Box<dyn GitRepository> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
fn is_path_managed_by(&self, path: &Path) -> bool {
|
||||
path.starts_with(&self.content_path)
|
||||
}
|
||||
|
||||
fn is_path_in_git_folder(&self, path: &Path) -> bool {
|
||||
path.starts_with(&self.git_dir_path)
|
||||
}
|
||||
|
||||
fn content_path(&self) -> &Path {
|
||||
self.content_path.as_ref()
|
||||
}
|
||||
|
||||
fn git_dir_path(&self) -> &Path {
|
||||
self.git_dir_path.as_ref()
|
||||
}
|
||||
|
||||
fn last_scan_id(&self) -> usize {
|
||||
self.last_scan_id
|
||||
}
|
||||
|
||||
fn set_scan_id(&mut self, scan_id: usize) {
|
||||
self.last_scan_id = scan_id;
|
||||
}
|
||||
|
||||
fn with_repo(&mut self, _: Box<dyn FnOnce(&mut git2::Repository)>) {
|
||||
unimplemented!();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue