Finish removing git repository state and scanning logic from worktrees (#27568)
This PR completes the process of moving git repository state storage and scanning logic from the worktree crate to `project::git_store`. Release Notes: - N/A --------- Co-authored-by: Max Brunsfeld <maxbrunsfeld@gmail.com> Co-authored-by: Conrad <conrad@zed.dev>
This commit is contained in:
parent
8f25251faf
commit
e7290df02b
39 changed files with 3121 additions and 3529 deletions
|
@ -1,11 +1,13 @@
|
|||
use futures::{FutureExt, future::Shared};
|
||||
use futures::{
|
||||
FutureExt,
|
||||
future::{Shared, WeakShared},
|
||||
};
|
||||
use std::{path::Path, sync::Arc};
|
||||
use util::ResultExt;
|
||||
|
||||
use collections::HashMap;
|
||||
use gpui::{App, AppContext as _, Context, Entity, EventEmitter, Task};
|
||||
use settings::Settings as _;
|
||||
use worktree::WorktreeId;
|
||||
|
||||
use crate::{
|
||||
project_settings::{DirenvSettings, ProjectSettings},
|
||||
|
@ -13,10 +15,9 @@ use crate::{
|
|||
};
|
||||
|
||||
pub struct ProjectEnvironment {
|
||||
worktree_store: Entity<WorktreeStore>,
|
||||
cli_environment: Option<HashMap<String, String>>,
|
||||
environments: HashMap<WorktreeId, Shared<Task<Option<HashMap<String, String>>>>>,
|
||||
environment_error_messages: HashMap<WorktreeId, EnvironmentErrorMessage>,
|
||||
environments: HashMap<Arc<Path>, WeakShared<Task<Option<HashMap<String, String>>>>>,
|
||||
environment_error_messages: HashMap<Arc<Path>, EnvironmentErrorMessage>,
|
||||
}
|
||||
|
||||
pub enum ProjectEnvironmentEvent {
|
||||
|
@ -33,14 +34,15 @@ impl ProjectEnvironment {
|
|||
) -> Entity<Self> {
|
||||
cx.new(|cx| {
|
||||
cx.subscribe(worktree_store, |this: &mut Self, _, event, _| {
|
||||
if let WorktreeStoreEvent::WorktreeRemoved(_, id) = event {
|
||||
this.remove_worktree_environment(*id);
|
||||
if let WorktreeStoreEvent::WorktreeRemoved(_, _) = event {
|
||||
this.environments.retain(|_, weak| weak.upgrade().is_some());
|
||||
this.environment_error_messages
|
||||
.retain(|abs_path, _| this.environments.contains_key(abs_path));
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
|
||||
Self {
|
||||
worktree_store: worktree_store.clone(),
|
||||
cli_environment,
|
||||
environments: Default::default(),
|
||||
environment_error_messages: Default::default(),
|
||||
|
@ -48,11 +50,6 @@ impl ProjectEnvironment {
|
|||
})
|
||||
}
|
||||
|
||||
pub(crate) fn remove_worktree_environment(&mut self, worktree_id: WorktreeId) {
|
||||
self.environment_error_messages.remove(&worktree_id);
|
||||
self.environments.remove(&worktree_id);
|
||||
}
|
||||
|
||||
/// Returns the inherited CLI environment, if this project was opened from the Zed CLI.
|
||||
pub(crate) fn get_cli_environment(&self) -> Option<HashMap<String, String>> {
|
||||
if let Some(mut env) = self.cli_environment.clone() {
|
||||
|
@ -67,28 +64,22 @@ impl ProjectEnvironment {
|
|||
/// environment errors associated with this project environment.
|
||||
pub(crate) fn environment_errors(
|
||||
&self,
|
||||
) -> impl Iterator<Item = (&WorktreeId, &EnvironmentErrorMessage)> {
|
||||
) -> impl Iterator<Item = (&Arc<Path>, &EnvironmentErrorMessage)> {
|
||||
self.environment_error_messages.iter()
|
||||
}
|
||||
|
||||
pub(crate) fn remove_environment_error(
|
||||
&mut self,
|
||||
worktree_id: WorktreeId,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
self.environment_error_messages.remove(&worktree_id);
|
||||
pub(crate) fn remove_environment_error(&mut self, abs_path: &Path, cx: &mut Context<Self>) {
|
||||
self.environment_error_messages.remove(abs_path);
|
||||
cx.emit(ProjectEnvironmentEvent::ErrorsUpdated);
|
||||
}
|
||||
|
||||
/// Returns the project environment, if possible.
|
||||
/// If the project was opened from the CLI, then the inherited CLI environment is returned.
|
||||
/// If it wasn't opened from the CLI, and a worktree is given, then a shell is spawned in
|
||||
/// the worktree's path, to get environment variables as if the user has `cd`'d into
|
||||
/// the worktrees path.
|
||||
/// If it wasn't opened from the CLI, and an absolute path is given, then a shell is spawned in
|
||||
/// that directory, to get environment variables as if the user has `cd`'d there.
|
||||
pub(crate) fn get_environment(
|
||||
&mut self,
|
||||
worktree_id: Option<WorktreeId>,
|
||||
worktree_abs_path: Option<Arc<Path>>,
|
||||
abs_path: Option<Arc<Path>>,
|
||||
cx: &Context<Self>,
|
||||
) -> Shared<Task<Option<HashMap<String, String>>>> {
|
||||
if cfg!(any(test, feature = "test-support")) {
|
||||
|
@ -111,74 +102,26 @@ impl ProjectEnvironment {
|
|||
.shared();
|
||||
}
|
||||
|
||||
let Some((worktree_id, worktree_abs_path)) = worktree_id.zip(worktree_abs_path) else {
|
||||
let Some(abs_path) = abs_path else {
|
||||
return Task::ready(None).shared();
|
||||
};
|
||||
|
||||
if self
|
||||
.worktree_store
|
||||
.read(cx)
|
||||
.worktree_for_id(worktree_id, cx)
|
||||
.map(|w| !w.read(cx).is_local())
|
||||
.unwrap_or(true)
|
||||
if let Some(existing) = self
|
||||
.environments
|
||||
.get(&abs_path)
|
||||
.and_then(|weak| weak.upgrade())
|
||||
{
|
||||
return Task::ready(None).shared();
|
||||
}
|
||||
|
||||
if let Some(task) = self.environments.get(&worktree_id) {
|
||||
task.clone()
|
||||
existing
|
||||
} else {
|
||||
let task = self
|
||||
.get_worktree_env(worktree_id, worktree_abs_path, cx)
|
||||
.shared();
|
||||
self.environments.insert(worktree_id, task.clone());
|
||||
task
|
||||
let env = get_directory_env(abs_path.clone(), cx).shared();
|
||||
self.environments.insert(
|
||||
abs_path.clone(),
|
||||
env.downgrade()
|
||||
.expect("environment task has not been polled yet"),
|
||||
);
|
||||
env
|
||||
}
|
||||
}
|
||||
|
||||
fn get_worktree_env(
|
||||
&mut self,
|
||||
worktree_id: WorktreeId,
|
||||
worktree_abs_path: Arc<Path>,
|
||||
cx: &Context<Self>,
|
||||
) -> Task<Option<HashMap<String, String>>> {
|
||||
let load_direnv = ProjectSettings::get_global(cx).load_direnv.clone();
|
||||
|
||||
cx.spawn(async move |this, cx| {
|
||||
let (mut shell_env, error_message) = cx
|
||||
.background_spawn({
|
||||
let worktree_abs_path = worktree_abs_path.clone();
|
||||
async move {
|
||||
load_worktree_shell_environment(&worktree_abs_path, &load_direnv).await
|
||||
}
|
||||
})
|
||||
.await;
|
||||
|
||||
if let Some(shell_env) = shell_env.as_mut() {
|
||||
let path = shell_env
|
||||
.get("PATH")
|
||||
.map(|path| path.as_str())
|
||||
.unwrap_or_default();
|
||||
log::info!(
|
||||
"using project environment variables shell launched in {:?}. PATH={:?}",
|
||||
worktree_abs_path,
|
||||
path
|
||||
);
|
||||
|
||||
set_origin_marker(shell_env, EnvironmentOrigin::WorktreeShell);
|
||||
}
|
||||
|
||||
if let Some(error) = error_message {
|
||||
this.update(cx, |this, cx| {
|
||||
this.environment_error_messages.insert(worktree_id, error);
|
||||
cx.emit(ProjectEnvironmentEvent::ErrorsUpdated)
|
||||
})
|
||||
.log_err();
|
||||
}
|
||||
|
||||
shell_env
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn set_origin_marker(env: &mut HashMap<String, String>, origin: EnvironmentOrigin) {
|
||||
|
@ -210,25 +153,25 @@ impl EnvironmentErrorMessage {
|
|||
}
|
||||
}
|
||||
|
||||
async fn load_worktree_shell_environment(
|
||||
worktree_abs_path: &Path,
|
||||
async fn load_directory_shell_environment(
|
||||
abs_path: &Path,
|
||||
load_direnv: &DirenvSettings,
|
||||
) -> (
|
||||
Option<HashMap<String, String>>,
|
||||
Option<EnvironmentErrorMessage>,
|
||||
) {
|
||||
match smol::fs::metadata(worktree_abs_path).await {
|
||||
match smol::fs::metadata(abs_path).await {
|
||||
Ok(meta) => {
|
||||
let dir = if meta.is_dir() {
|
||||
worktree_abs_path
|
||||
} else if let Some(parent) = worktree_abs_path.parent() {
|
||||
abs_path
|
||||
} else if let Some(parent) = abs_path.parent() {
|
||||
parent
|
||||
} else {
|
||||
return (
|
||||
None,
|
||||
Some(EnvironmentErrorMessage(format!(
|
||||
"Failed to load shell environment in {}: not a directory",
|
||||
worktree_abs_path.display()
|
||||
abs_path.display()
|
||||
))),
|
||||
);
|
||||
};
|
||||
|
@ -239,7 +182,7 @@ async fn load_worktree_shell_environment(
|
|||
None,
|
||||
Some(EnvironmentErrorMessage(format!(
|
||||
"Failed to load shell environment in {}: {}",
|
||||
worktree_abs_path.display(),
|
||||
abs_path.display(),
|
||||
err
|
||||
))),
|
||||
),
|
||||
|
@ -387,3 +330,43 @@ async fn load_shell_environment(
|
|||
|
||||
(Some(parsed_env), direnv_error)
|
||||
}
|
||||
|
||||
fn get_directory_env(
|
||||
abs_path: Arc<Path>,
|
||||
cx: &Context<ProjectEnvironment>,
|
||||
) -> Task<Option<HashMap<String, String>>> {
|
||||
let load_direnv = ProjectSettings::get_global(cx).load_direnv.clone();
|
||||
|
||||
cx.spawn(async move |this, cx| {
|
||||
let (mut shell_env, error_message) = cx
|
||||
.background_spawn({
|
||||
let abs_path = abs_path.clone();
|
||||
async move { load_directory_shell_environment(&abs_path, &load_direnv).await }
|
||||
})
|
||||
.await;
|
||||
|
||||
if let Some(shell_env) = shell_env.as_mut() {
|
||||
let path = shell_env
|
||||
.get("PATH")
|
||||
.map(|path| path.as_str())
|
||||
.unwrap_or_default();
|
||||
log::info!(
|
||||
"using project environment variables shell launched in {:?}. PATH={:?}",
|
||||
abs_path,
|
||||
path
|
||||
);
|
||||
|
||||
set_origin_marker(shell_env, EnvironmentOrigin::WorktreeShell);
|
||||
}
|
||||
|
||||
if let Some(error) = error_message {
|
||||
this.update(cx, |this, cx| {
|
||||
this.environment_error_messages.insert(abs_path, error);
|
||||
cx.emit(ProjectEnvironmentEvent::ErrorsUpdated)
|
||||
})
|
||||
.log_err();
|
||||
}
|
||||
|
||||
shell_env
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue