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:
parent
c95646a298
commit
af0974264c
7 changed files with 143 additions and 175 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -2235,6 +2235,7 @@ dependencies = [
|
||||||
"async-trait",
|
"async-trait",
|
||||||
"clock",
|
"clock",
|
||||||
"collections",
|
"collections",
|
||||||
|
"futures",
|
||||||
"git2",
|
"git2",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
"log",
|
"log",
|
||||||
|
|
|
@ -966,7 +966,8 @@ async fn test_git_head_text(
|
||||||
.insert_tree(
|
.insert_tree(
|
||||||
"/dir",
|
"/dir",
|
||||||
json!({
|
json!({
|
||||||
".git": {},
|
".git": {
|
||||||
|
},
|
||||||
"a.txt": "
|
"a.txt": "
|
||||||
one
|
one
|
||||||
two
|
two
|
||||||
|
@ -983,9 +984,8 @@ async fn test_git_head_text(
|
||||||
.unindent();
|
.unindent();
|
||||||
|
|
||||||
let new_head_text = "
|
let new_head_text = "
|
||||||
1
|
one
|
||||||
two
|
two
|
||||||
three
|
|
||||||
"
|
"
|
||||||
.unindent();
|
.unindent();
|
||||||
|
|
||||||
|
@ -1041,7 +1041,6 @@ async fn test_git_head_text(
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO: Create a dummy file event
|
|
||||||
client_a
|
client_a
|
||||||
.fs
|
.fs
|
||||||
.as_fake()
|
.as_fake()
|
||||||
|
@ -1051,19 +1050,18 @@ async fn test_git_head_text(
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
// TODO: Flush this file event
|
|
||||||
|
|
||||||
// Wait for buffer_a to receive it
|
// Wait for buffer_a to receive it
|
||||||
executor.run_until_parked();
|
executor.run_until_parked();
|
||||||
|
|
||||||
// Smoke test new diffing
|
// Smoke test new diffing
|
||||||
buffer_a.read_with(cx_a, |buffer, _| {
|
buffer_a.read_with(cx_a, |buffer, _| {
|
||||||
assert_eq!(buffer.head_text(), Some(new_head_text.as_ref()));
|
assert_eq!(buffer.head_text(), Some(new_head_text.as_ref()));
|
||||||
|
|
||||||
git::diff::assert_hunks(
|
git::diff::assert_hunks(
|
||||||
buffer.snapshot().git_diff_hunks_in_range(0..4),
|
buffer.snapshot().git_diff_hunks_in_range(0..4),
|
||||||
&buffer,
|
&buffer,
|
||||||
&head_text,
|
&head_text,
|
||||||
&[(0..1, "1", "one\n")],
|
&[(2..3, "", "three\n")],
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,7 @@ log = { version = "0.4.16", features = ["kv_unstable_serde"] }
|
||||||
smol = "1.2"
|
smol = "1.2"
|
||||||
parking_lot = "0.11.1"
|
parking_lot = "0.11.1"
|
||||||
async-trait = "0.1"
|
async-trait = "0.1"
|
||||||
|
futures = "0.3"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
unindent = "0.1.7"
|
unindent = "0.1.7"
|
||||||
|
|
|
@ -1,39 +1,20 @@
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use collections::HashMap;
|
use collections::HashMap;
|
||||||
use git2::Repository as LibGitRepository;
|
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use util::ResultExt;
|
use std::{
|
||||||
use std::{path::{Path, PathBuf}, sync::Arc};
|
path::{Path, PathBuf},
|
||||||
|
sync::Arc,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub use git2::Repository as LibGitRepository;
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
pub trait GitRepository: Send {
|
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 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]
|
#[async_trait::async_trait]
|
||||||
impl GitRepository for LibGitRepository {
|
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 load_head_text(&self, relative_file_path: &Path) -> Option<String> {
|
||||||
fn logic(repo: &LibGitRepository, relative_file_path: &Path) -> Result<Option<String>> {
|
fn logic(repo: &LibGitRepository, relative_file_path: &Path) -> Result<Option<String>> {
|
||||||
const STAGE_NORMAL: i32 = 0;
|
const STAGE_NORMAL: i32 = 0;
|
||||||
|
@ -56,10 +37,8 @@ impl GitRepository for LibGitRepository {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone, Default)]
|
||||||
pub struct FakeGitRepository {
|
pub struct FakeGitRepository {
|
||||||
content_path: Arc<Path>,
|
|
||||||
git_dir_path: Arc<Path>,
|
|
||||||
state: Arc<Mutex<FakeGitRepositoryState>>,
|
state: Arc<Mutex<FakeGitRepositoryState>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,47 +48,15 @@ pub struct FakeGitRepositoryState {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FakeGitRepository {
|
impl FakeGitRepository {
|
||||||
pub fn open(dotgit_path: &Path, state: Arc<Mutex<FakeGitRepositoryState>>) -> Box<dyn GitRepository> {
|
pub fn open(state: Arc<Mutex<FakeGitRepositoryState>>) -> Arc<Mutex<dyn GitRepository>> {
|
||||||
Box::new(FakeGitRepository {
|
Arc::new(Mutex::new(FakeGitRepository { state }))
|
||||||
content_path: dotgit_path.parent().unwrap().into(),
|
|
||||||
git_dir_path: dotgit_path.into(),
|
|
||||||
state,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
impl GitRepository for FakeGitRepository {
|
impl GitRepository for FakeGitRepository {
|
||||||
fn manages(&self, path: &Path) -> bool {
|
fn load_head_text(&self, path: &Path) -> Option<String> {
|
||||||
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> {
|
|
||||||
let state = self.state.lock();
|
let state = self.state.lock();
|
||||||
state.index_contents.get(path).cloned()
|
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())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use fsevent::EventStream;
|
use fsevent::EventStream;
|
||||||
use futures::{future::BoxFuture, Stream, StreamExt};
|
use futures::{future::BoxFuture, Stream, StreamExt};
|
||||||
use git::repository::{FakeGitRepositoryState, GitRepository, Git2Repo};
|
use git::repository::{FakeGitRepositoryState, GitRepository, LibGitRepository};
|
||||||
use language::LineEnding;
|
use language::LineEnding;
|
||||||
|
use parking_lot::Mutex as SyncMutex;
|
||||||
use smol::io::{AsyncReadExt, AsyncWriteExt};
|
use smol::io::{AsyncReadExt, AsyncWriteExt};
|
||||||
use std::{
|
use std::{
|
||||||
io,
|
io,
|
||||||
|
@ -11,6 +12,7 @@ use std::{
|
||||||
pin::Pin,
|
pin::Pin,
|
||||||
time::{Duration, SystemTime},
|
time::{Duration, SystemTime},
|
||||||
};
|
};
|
||||||
|
use util::ResultExt;
|
||||||
|
|
||||||
use text::Rope;
|
use text::Rope;
|
||||||
|
|
||||||
|
@ -44,7 +46,7 @@ pub trait Fs: Send + Sync {
|
||||||
path: &Path,
|
path: &Path,
|
||||||
latency: Duration,
|
latency: Duration,
|
||||||
) -> Pin<Box<dyn Send + Stream<Item = Vec<fsevent::Event>>>>;
|
) -> Pin<Box<dyn Send + Stream<Item = Vec<fsevent::Event>>>>;
|
||||||
fn open_repo(&self, abs_dot_git: &Path) -> Option<Box<dyn GitRepository>>;
|
fn open_repo(&self, abs_dot_git: &Path) -> Option<Arc<SyncMutex<dyn GitRepository>>>;
|
||||||
fn is_fake(&self) -> bool;
|
fn is_fake(&self) -> bool;
|
||||||
#[cfg(any(test, feature = "test-support"))]
|
#[cfg(any(test, feature = "test-support"))]
|
||||||
fn as_fake(&self) -> &FakeFs;
|
fn as_fake(&self) -> &FakeFs;
|
||||||
|
@ -238,8 +240,12 @@ impl Fs for RealFs {
|
||||||
})))
|
})))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn open_repo(&self, abs_dot_git: &Path) -> Option<Box<dyn GitRepository>> {
|
fn open_repo(&self, dotgit_path: &Path) -> Option<Arc<SyncMutex<dyn GitRepository>>> {
|
||||||
Git2Repo::open(&abs_dot_git)
|
LibGitRepository::open(&dotgit_path)
|
||||||
|
.log_err()
|
||||||
|
.and_then::<Arc<SyncMutex<dyn GitRepository>>, _>(|libgit_repository| {
|
||||||
|
Some(Arc::new(SyncMutex::new(libgit_repository)))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_fake(&self) -> bool {
|
fn is_fake(&self) -> bool {
|
||||||
|
@ -277,7 +283,7 @@ enum FakeFsEntry {
|
||||||
inode: u64,
|
inode: u64,
|
||||||
mtime: SystemTime,
|
mtime: SystemTime,
|
||||||
entries: BTreeMap<String, Arc<Mutex<FakeFsEntry>>>,
|
entries: BTreeMap<String, Arc<Mutex<FakeFsEntry>>>,
|
||||||
git_repo_state: Option<Arc<parking_lot::Mutex<git::repository::FakeGitRepositoryState>>>,
|
git_repo_state: Option<Arc<SyncMutex<git::repository::FakeGitRepositoryState>>>,
|
||||||
},
|
},
|
||||||
Symlink {
|
Symlink {
|
||||||
target: PathBuf,
|
target: PathBuf,
|
||||||
|
@ -488,7 +494,7 @@ impl FakeFs {
|
||||||
head_state: &[(&Path, String)],
|
head_state: &[(&Path, String)],
|
||||||
) {
|
) {
|
||||||
let content_path = dot_git.parent().unwrap();
|
let content_path = dot_git.parent().unwrap();
|
||||||
let state = self.state.lock().await;
|
let mut state = self.state.lock().await;
|
||||||
let entry = state.read_path(dot_git).await.unwrap();
|
let entry = state.read_path(dot_git).await.unwrap();
|
||||||
let mut entry = entry.lock().await;
|
let mut entry = entry.lock().await;
|
||||||
|
|
||||||
|
@ -502,6 +508,8 @@ impl FakeFs {
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(path, content)| (content_path.join(path), content.clone())),
|
.map(|(path, content)| (content_path.join(path), content.clone())),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
state.emit_event([dot_git]);
|
||||||
} else {
|
} else {
|
||||||
panic!("not a directory");
|
panic!("not a directory");
|
||||||
}
|
}
|
||||||
|
@ -881,7 +889,7 @@ impl Fs for FakeFs {
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn open_repo(&self, abs_dot_git: &Path) -> Option<Box<dyn GitRepository>> {
|
fn open_repo(&self, abs_dot_git: &Path) -> Option<Arc<SyncMutex<dyn GitRepository>>> {
|
||||||
let executor = self.executor.upgrade().unwrap();
|
let executor = self.executor.upgrade().unwrap();
|
||||||
executor.block(async move {
|
executor.block(async move {
|
||||||
let state = self.state.lock().await;
|
let state = self.state.lock().await;
|
||||||
|
@ -890,13 +898,10 @@ impl Fs for FakeFs {
|
||||||
if let FakeFsEntry::Dir { git_repo_state, .. } = &mut *entry {
|
if let FakeFsEntry::Dir { git_repo_state, .. } = &mut *entry {
|
||||||
let state = git_repo_state
|
let state = git_repo_state
|
||||||
.get_or_insert_with(|| {
|
.get_or_insert_with(|| {
|
||||||
Arc::new(parking_lot::Mutex::new(FakeGitRepositoryState::default()))
|
Arc::new(SyncMutex::new(FakeGitRepositoryState::default()))
|
||||||
})
|
})
|
||||||
.clone();
|
.clone();
|
||||||
Some(git::repository::FakeGitRepository::open(
|
Some(git::repository::FakeGitRepository::open(state))
|
||||||
abs_dot_git.into(),
|
|
||||||
state,
|
|
||||||
))
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ use client::{proto, Client, PeerId, TypedEnvelope, User, UserStore};
|
||||||
use clock::ReplicaId;
|
use clock::ReplicaId;
|
||||||
use collections::{hash_map, BTreeMap, HashMap, HashSet};
|
use collections::{hash_map, BTreeMap, HashMap, HashSet};
|
||||||
use futures::{future::Shared, AsyncWriteExt, Future, FutureExt, StreamExt, TryFutureExt};
|
use futures::{future::Shared, AsyncWriteExt, Future, FutureExt, StreamExt, TryFutureExt};
|
||||||
use git::repository::GitRepository;
|
|
||||||
use gpui::{
|
use gpui::{
|
||||||
AnyModelHandle, AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle,
|
AnyModelHandle, AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle,
|
||||||
MutableAppContext, Task, UpgradeModelHandle, WeakModelHandle,
|
MutableAppContext, Task, UpgradeModelHandle, WeakModelHandle,
|
||||||
|
@ -4648,7 +4648,7 @@ impl Project {
|
||||||
|
|
||||||
fn update_local_worktree_buffers_git_repos(
|
fn update_local_worktree_buffers_git_repos(
|
||||||
&mut self,
|
&mut self,
|
||||||
repos: &[Box<dyn GitRepository>],
|
repos: &[GitRepositoryEntry],
|
||||||
cx: &mut ModelContext<Self>,
|
cx: &mut ModelContext<Self>,
|
||||||
) {
|
) {
|
||||||
//TODO: Produce protos
|
//TODO: Produce protos
|
||||||
|
@ -4663,12 +4663,15 @@ impl Project {
|
||||||
let abs_path = file.abs_path(cx);
|
let abs_path = file.abs_path(cx);
|
||||||
|
|
||||||
let repo = match repos.iter().find(|repo| repo.manages(&abs_path)) {
|
let repo = match repos.iter().find(|repo| repo.manages(&abs_path)) {
|
||||||
Some(repo) => repo.boxed_clone(),
|
Some(repo) => repo.clone(),
|
||||||
None => return,
|
None => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
cx.spawn(|_, mut cx| async move {
|
cx.spawn(|_, mut cx| async move {
|
||||||
let head_text = repo.load_head_text(&path).await;
|
let head_text = cx
|
||||||
|
.background()
|
||||||
|
.spawn(async move { repo.repo.lock().load_head_text(&path) })
|
||||||
|
.await;
|
||||||
buffer.update(&mut cx, |buffer, cx| {
|
buffer.update(&mut cx, |buffer, cx| {
|
||||||
buffer.update_head_text(head_text, cx);
|
buffer.update_head_text(head_text, cx);
|
||||||
});
|
});
|
||||||
|
|
|
@ -98,16 +98,15 @@ pub struct Snapshot {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct GitRepositoryEntry {
|
pub struct GitRepositoryEntry {
|
||||||
repo: Arc<Mutex<dyn GitRepository>>,
|
pub(crate) repo: Arc<Mutex<dyn GitRepository>>,
|
||||||
|
|
||||||
// repo: Box<dyn GitRepository>,
|
pub(crate) scan_id: usize,
|
||||||
scan_id: usize,
|
|
||||||
// Path to folder containing the .git file or directory
|
// Path to folder containing the .git file or directory
|
||||||
content_path: Arc<Path>,
|
pub(crate) content_path: Arc<Path>,
|
||||||
// Path to the actual .git folder.
|
// Path to the actual .git folder.
|
||||||
// Note: if .git is a file, this points to the folder indicated by the .git file
|
// Note: if .git is a file, this points to the folder indicated by the .git file
|
||||||
git_dir_path: Arc<Path>,
|
pub(crate) git_dir_path: Arc<Path>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Debug for GitRepositoryEntry {
|
impl std::fmt::Debug for GitRepositoryEntry {
|
||||||
|
@ -141,11 +140,7 @@ impl Clone for LocalSnapshot {
|
||||||
Self {
|
Self {
|
||||||
abs_path: self.abs_path.clone(),
|
abs_path: self.abs_path.clone(),
|
||||||
ignores_by_parent_abs_path: self.ignores_by_parent_abs_path.clone(),
|
ignores_by_parent_abs_path: self.ignores_by_parent_abs_path.clone(),
|
||||||
git_repositories: self
|
git_repositories: self.git_repositories.iter().cloned().collect(),
|
||||||
.git_repositories
|
|
||||||
.iter()
|
|
||||||
.cloned()
|
|
||||||
.collect(),
|
|
||||||
removed_entry_ids: self.removed_entry_ids.clone(),
|
removed_entry_ids: self.removed_entry_ids.clone(),
|
||||||
next_entry_id: self.next_entry_id.clone(),
|
next_entry_id: self.next_entry_id.clone(),
|
||||||
snapshot: self.snapshot.clone(),
|
snapshot: self.snapshot.clone(),
|
||||||
|
@ -186,7 +181,7 @@ struct ShareState {
|
||||||
|
|
||||||
pub enum Event {
|
pub enum Event {
|
||||||
UpdatedEntries,
|
UpdatedEntries,
|
||||||
UpdatedGitRepositories(Vec<Box<dyn GitRepository>>),
|
UpdatedGitRepositories(Vec<GitRepositoryEntry>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Entity for Worktree {
|
impl Entity for Worktree {
|
||||||
|
@ -610,27 +605,26 @@ impl LocalWorktree {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn changed_repos(
|
fn changed_repos(
|
||||||
old_repos: &[Box<dyn GitRepository>],
|
old_repos: &[GitRepositoryEntry],
|
||||||
new_repos: &[Box<dyn GitRepository>],
|
new_repos: &[GitRepositoryEntry],
|
||||||
) -> Vec<Box<dyn GitRepository>> {
|
) -> Vec<GitRepositoryEntry> {
|
||||||
fn diff<'a>(
|
fn diff<'a>(
|
||||||
a: &'a [Box<dyn GitRepository>],
|
a: &'a [GitRepositoryEntry],
|
||||||
b: &'a [Box<dyn GitRepository>],
|
b: &'a [GitRepositoryEntry],
|
||||||
updated: &mut HashMap<&'a Path, Box<dyn GitRepository>>,
|
updated: &mut HashMap<&'a Path, GitRepositoryEntry>,
|
||||||
) {
|
) {
|
||||||
for a_repo in a {
|
for a_repo in a {
|
||||||
let matched = b.iter().find(|b_repo| {
|
let matched = b.iter().find(|b_repo| {
|
||||||
a_repo.git_dir_path() == b_repo.git_dir_path()
|
a_repo.git_dir_path == b_repo.git_dir_path && a_repo.scan_id == b_repo.scan_id
|
||||||
&& a_repo.scan_id() == b_repo.scan_id()
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if matched.is_none() {
|
if matched.is_none() {
|
||||||
updated.insert(a_repo.git_dir_path(), a_repo.boxed_clone());
|
updated.insert(a_repo.git_dir_path.as_ref(), a_repo.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut updated = HashMap::<&Path, Box<dyn GitRepository>>::default();
|
let mut updated = HashMap::<&Path, GitRepositoryEntry>::default();
|
||||||
|
|
||||||
diff(old_repos, new_repos, &mut updated);
|
diff(old_repos, new_repos, &mut updated);
|
||||||
diff(new_repos, old_repos, &mut updated);
|
diff(new_repos, old_repos, &mut updated);
|
||||||
|
@ -690,7 +684,12 @@ impl LocalWorktree {
|
||||||
settings::GitFilesIncluded::All | settings::GitFilesIncluded::OnlyTracked
|
settings::GitFilesIncluded::All | settings::GitFilesIncluded::OnlyTracked
|
||||||
) {
|
) {
|
||||||
let results = if let Some(repo) = snapshot.repo_for(&abs_path) {
|
let results = if let Some(repo) = snapshot.repo_for(&abs_path) {
|
||||||
repo.load_head_text(&path).await
|
cx.background()
|
||||||
|
.spawn({
|
||||||
|
let path = path.clone();
|
||||||
|
async move { repo.repo.lock().load_head_text(&path) }
|
||||||
|
})
|
||||||
|
.await
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
@ -1390,25 +1389,19 @@ impl LocalSnapshot {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gives the most specific git repository for a given path
|
// Gives the most specific git repository for a given path
|
||||||
pub(crate) fn repo_for(&self, path: &Path) -> Option<Box<dyn GitRepository>> {
|
pub(crate) fn repo_for(&self, path: &Path) -> Option<GitRepositoryEntry> {
|
||||||
self.git_repositories
|
self.git_repositories
|
||||||
.iter()
|
.iter()
|
||||||
.rev() //git_repository is ordered lexicographically
|
.rev() //git_repository is ordered lexicographically
|
||||||
.find(|repo| repo.manages(&self.abs_path.join(path)))
|
.find(|repo| repo.manages(path))
|
||||||
.map(|repo| repo.boxed_clone())
|
.cloned()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn in_dot_git(&mut self, path: &Path) -> Option<&mut Box<dyn GitRepository>> {
|
pub(crate) fn in_dot_git(&mut self, path: &Path) -> Option<&mut GitRepositoryEntry> {
|
||||||
|
// Git repositories cannot be nested, so we don't need to reverse the order
|
||||||
self.git_repositories
|
self.git_repositories
|
||||||
.iter_mut()
|
.iter_mut()
|
||||||
.rev() //git_repository is ordered lexicographically
|
.find(|repo| repo.in_dot_git(path))
|
||||||
.find(|repo| repo.in_dot_git(&self.abs_path.join(path)))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn _tracks_filepath(&self, repo: &dyn GitRepository, file_path: &Path) -> bool {
|
|
||||||
// Depends on git_repository_for_file_path returning the most specific git repository for a given path
|
|
||||||
self.repo_for(&self.abs_path.join(file_path))
|
|
||||||
.map_or(false, |r| r.git_dir_path() == repo.git_dir_path())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -1575,12 +1568,21 @@ impl LocalSnapshot {
|
||||||
|
|
||||||
if parent_path.file_name() == Some(&DOT_GIT) {
|
if parent_path.file_name() == Some(&DOT_GIT) {
|
||||||
let abs_path = self.abs_path.join(&parent_path);
|
let abs_path = self.abs_path.join(&parent_path);
|
||||||
|
let content_path: Arc<Path> = parent_path.parent().unwrap().into();
|
||||||
if let Err(ix) = self
|
if let Err(ix) = self
|
||||||
.git_repositories
|
.git_repositories
|
||||||
.binary_search_by_key(&abs_path.as_path(), |repo| repo.git_dir_path())
|
.binary_search_by_key(&&content_path, |repo| &repo.content_path)
|
||||||
{
|
{
|
||||||
if let Some(repository) = fs.open_repo(abs_path.as_path()) {
|
if let Some(repo) = fs.open_repo(abs_path.as_path()) {
|
||||||
self.git_repositories.insert(ix, repository);
|
self.git_repositories.insert(
|
||||||
|
ix,
|
||||||
|
GitRepositoryEntry {
|
||||||
|
repo,
|
||||||
|
scan_id: 0,
|
||||||
|
content_path,
|
||||||
|
git_dir_path: parent_path,
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1673,9 +1675,9 @@ impl LocalSnapshot {
|
||||||
let parent_path = path.parent().unwrap();
|
let parent_path = path.parent().unwrap();
|
||||||
if let Ok(ix) = self
|
if let Ok(ix) = self
|
||||||
.git_repositories
|
.git_repositories
|
||||||
.binary_search_by_key(&parent_path, |repo| repo.content_path().as_ref())
|
.binary_search_by_key(&parent_path, |repo| repo.git_dir_path.as_ref())
|
||||||
{
|
{
|
||||||
self.git_repositories[ix].set_scan_id(self.snapshot.scan_id);
|
self.git_repositories[ix].scan_id = self.snapshot.scan_id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1716,6 +1718,25 @@ impl LocalSnapshot {
|
||||||
|
|
||||||
ignore_stack
|
ignore_stack
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn git_repo_entries(&self) -> &[GitRepositoryEntry] {
|
||||||
|
&self.git_repositories
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Worktree root
|
||||||
|
// |
|
||||||
|
// git_dir_path: c/d/.git
|
||||||
|
//in_dot_git Query: c/d/.git/HEAD
|
||||||
|
// Manages Query: c/d/e/f/a.txt
|
||||||
|
|
||||||
|
impl GitRepositoryEntry {
|
||||||
|
pub(crate) fn manages(&self, path: &Path) -> bool {
|
||||||
|
path.starts_with(self.content_path.as_ref())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn in_dot_git(&self, path: &Path) -> bool {
|
||||||
|
path.starts_with(self.git_dir_path.as_ref())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn build_gitignore(abs_path: &Path, fs: &dyn Fs) -> Result<Gitignore> {
|
async fn build_gitignore(abs_path: &Path, fs: &dyn Fs) -> Result<Gitignore> {
|
||||||
|
@ -2509,8 +2530,8 @@ impl BackgroundScanner {
|
||||||
snapshot.insert_entry(fs_entry, self.fs.as_ref());
|
snapshot.insert_entry(fs_entry, self.fs.as_ref());
|
||||||
|
|
||||||
let scan_id = snapshot.scan_id;
|
let scan_id = snapshot.scan_id;
|
||||||
if let Some(repo) = snapshot.in_dot_git(&abs_path) {
|
if let Some(repo) = snapshot.in_dot_git(&path) {
|
||||||
repo.set_scan_id(scan_id);
|
repo.scan_id = scan_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut ancestor_inodes = snapshot.ancestor_inodes_for_path(&path);
|
let mut ancestor_inodes = snapshot.ancestor_inodes_for_path(&path);
|
||||||
|
@ -2625,19 +2646,21 @@ impl BackgroundScanner {
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Clarify what is going on here because re-loading every git repository
|
||||||
|
// on every file system event seems wrong
|
||||||
async fn update_git_repositories(&self) {
|
async fn update_git_repositories(&self) {
|
||||||
let mut snapshot = self.snapshot.lock();
|
let mut snapshot = self.snapshot.lock();
|
||||||
|
|
||||||
let new_repos = snapshot
|
let new_repos = snapshot
|
||||||
.git_repositories
|
.git_repositories
|
||||||
.iter()
|
.iter()
|
||||||
.map(|repo| repo.boxed_clone())
|
.cloned()
|
||||||
.filter_map(|mut repo| {
|
.filter_map(|mut repo_entry| {
|
||||||
if repo.reopen_git_repo() {
|
let repo = self
|
||||||
Some(repo)
|
.fs
|
||||||
} else {
|
.open_repo(&snapshot.abs_path.join(&repo_entry.git_dir_path))?;
|
||||||
None
|
repo_entry.repo = repo;
|
||||||
}
|
Some(repo_entry)
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
@ -3262,34 +3285,17 @@ mod tests {
|
||||||
assert!(tree.repo_for("c.txt".as_ref()).is_none());
|
assert!(tree.repo_for("c.txt".as_ref()).is_none());
|
||||||
|
|
||||||
let repo = tree.repo_for("dir1/src/b.txt".as_ref()).unwrap();
|
let repo = tree.repo_for("dir1/src/b.txt".as_ref()).unwrap();
|
||||||
|
assert_eq!(repo.content_path.as_ref(), Path::new("dir1"));
|
||||||
assert_eq!(
|
assert_eq!(repo.git_dir_path.as_ref(), Path::new("dir1/.git"));
|
||||||
repo.content_path(),
|
|
||||||
root.path().join("dir1").canonicalize().unwrap()
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
repo.git_dir_path(),
|
|
||||||
root.path().join("dir1/.git").canonicalize().unwrap()
|
|
||||||
);
|
|
||||||
|
|
||||||
let repo = tree.repo_for("dir1/deps/dep1/src/a.txt".as_ref()).unwrap();
|
let repo = tree.repo_for("dir1/deps/dep1/src/a.txt".as_ref()).unwrap();
|
||||||
|
assert_eq!(repo.content_path.as_ref(), Path::new("dir1/deps/dep1"));
|
||||||
assert_eq!(
|
assert_eq!(repo.git_dir_path.as_ref(), Path::new("dir1/deps/dep1/.git"),);
|
||||||
repo.content_path(),
|
|
||||||
root.path().join("dir1/deps/dep1").canonicalize().unwrap()
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
repo.git_dir_path(),
|
|
||||||
root.path()
|
|
||||||
.join("dir1/deps/dep1/.git")
|
|
||||||
.canonicalize()
|
|
||||||
.unwrap()
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let original_scan_id = tree.read_with(cx, |tree, _cx| {
|
let original_scan_id = tree.read_with(cx, |tree, _cx| {
|
||||||
let tree = tree.as_local().unwrap();
|
let tree = tree.as_local().unwrap();
|
||||||
tree.repo_for("dir1/src/b.txt".as_ref()).unwrap().scan_id()
|
tree.repo_for("dir1/src/b.txt".as_ref()).unwrap().scan_id
|
||||||
});
|
});
|
||||||
|
|
||||||
std::fs::write(root.path().join("dir1/.git/random_new_file"), "hello").unwrap();
|
std::fs::write(root.path().join("dir1/.git/random_new_file"), "hello").unwrap();
|
||||||
|
@ -3297,7 +3303,7 @@ mod tests {
|
||||||
|
|
||||||
tree.read_with(cx, |tree, _cx| {
|
tree.read_with(cx, |tree, _cx| {
|
||||||
let tree = tree.as_local().unwrap();
|
let tree = tree.as_local().unwrap();
|
||||||
let new_scan_id = tree.repo_for("dir1/src/b.txt".as_ref()).unwrap().scan_id();
|
let new_scan_id = tree.repo_for("dir1/src/b.txt".as_ref()).unwrap().scan_id;
|
||||||
assert_ne!(
|
assert_ne!(
|
||||||
original_scan_id, new_scan_id,
|
original_scan_id, new_scan_id,
|
||||||
"original {original_scan_id}, new {new_scan_id}"
|
"original {original_scan_id}, new {new_scan_id}"
|
||||||
|
@ -3316,44 +3322,51 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_changed_repos() {
|
fn test_changed_repos() {
|
||||||
// let prev_repos: Vec<Box<dyn GitRepository>> = vec![
|
fn fake_entry(git_dir_path: impl AsRef<Path>, scan_id: usize) -> GitRepositoryEntry {
|
||||||
// FakeGitRepository::open(Path::new("/.git"), 0, Default::default()),
|
GitRepositoryEntry {
|
||||||
// FakeGitRepository::open(Path::new("/a/.git"), 0, Default::default()),
|
repo: Arc::new(Mutex::new(FakeGitRepository::default())),
|
||||||
// FakeGitRepository::open(Path::new("/a/b/.git"), 0, Default::default()),
|
scan_id,
|
||||||
// ];
|
content_path: git_dir_path.as_ref().parent().unwrap().into(),
|
||||||
|
git_dir_path: git_dir_path.as_ref().into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// let new_repos: Vec<Box<dyn GitRepository>> = vec![
|
let prev_repos: Vec<GitRepositoryEntry> = vec![
|
||||||
// FakeGitRepository::open(Path::new("/a/.git"), 1, Default::default()),
|
fake_entry("/.git", 0),
|
||||||
// FakeGitRepository::open(Path::new("/a/b/.git"), 0, Default::default()),
|
fake_entry("/a/.git", 0),
|
||||||
// FakeGitRepository::open(Path::new("/a/c/.git"), 0, Default::default()),
|
fake_entry("/a/b/.git", 0),
|
||||||
// ];
|
];
|
||||||
|
|
||||||
|
let new_repos: Vec<GitRepositoryEntry> = vec![
|
||||||
|
fake_entry("/a/.git", 1),
|
||||||
|
fake_entry("/a/b/.git", 0),
|
||||||
|
fake_entry("/a/c/.git", 0),
|
||||||
|
];
|
||||||
|
|
||||||
let res = LocalWorktree::changed_repos(&prev_repos, &new_repos);
|
let res = LocalWorktree::changed_repos(&prev_repos, &new_repos);
|
||||||
|
|
||||||
dbg!(&res);
|
|
||||||
|
|
||||||
// Deletion retained
|
// Deletion retained
|
||||||
assert!(res
|
assert!(res
|
||||||
.iter()
|
.iter()
|
||||||
.find(|repo| repo.git_dir_path() == Path::new("/.git") && repo.scan_id() == 0)
|
.find(|repo| repo.git_dir_path.as_ref() == Path::new("/.git") && repo.scan_id == 0)
|
||||||
.is_some());
|
.is_some());
|
||||||
|
|
||||||
// Update retained
|
// Update retained
|
||||||
assert!(res
|
assert!(res
|
||||||
.iter()
|
.iter()
|
||||||
.find(|repo| repo.git_dir_path() == Path::new("/a/.git") && repo.scan_id() == 1)
|
.find(|repo| repo.git_dir_path.as_ref() == Path::new("/a/.git") && repo.scan_id == 1)
|
||||||
.is_some());
|
.is_some());
|
||||||
|
|
||||||
// Addition retained
|
// Addition retained
|
||||||
assert!(res
|
assert!(res
|
||||||
.iter()
|
.iter()
|
||||||
.find(|repo| repo.git_dir_path() == Path::new("/a/c/.git") && repo.scan_id() == 0)
|
.find(|repo| repo.git_dir_path.as_ref() == Path::new("/a/c/.git") && repo.scan_id == 0)
|
||||||
.is_some());
|
.is_some());
|
||||||
|
|
||||||
// Nochange, not retained
|
// Nochange, not retained
|
||||||
assert!(res
|
assert!(res
|
||||||
.iter()
|
.iter()
|
||||||
.find(|repo| repo.git_dir_path() == Path::new("/a/b/.git") && repo.scan_id() == 0)
|
.find(|repo| repo.git_dir_path.as_ref() == Path::new("/a/b/.git") && repo.scan_id == 0)
|
||||||
.is_none());
|
.is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue