Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

worktree: Fix global gitignore matching outside the worktree root (#62130)

Frank Loesche53b39e8e8916
diff --git a/crates/worktree/src/ignore.rs b/crates/worktree/src/ignore.rs
index c9efd6fd430..e8b92c2e252 100644
--- a/crates/worktree/src/ignore.rs
+++ b/crates/worktree/src/ignore.rs
@@ -4,6 +4,10 @@ use std::{ffi::OsStr, path::Path, sync::Arc};
#[derive(Clone, Debug)]
pub struct IgnoreStack {
pub repo_root: Option<Arc<Path>>,
+ /// The repository root, or the worktree root when no repository contains
+ /// the worktree.
+ /// Global ignore patterns are matched relative to this path.
+ pub global_ignore_root: Option<Arc<Path>>,
pub top: Arc<IgnoreStackEntry>,
}
@@ -35,6 +39,7 @@ impl IgnoreStack {
pub fn none() -> Self {
Self {
repo_root: None,
+ global_ignore_root: None,
top: Arc::new(IgnoreStackEntry::None),
}
}
@@ -42,6 +47,7 @@ impl IgnoreStack {
pub fn all() -> Self {
Self {
repo_root: None,
+ global_ignore_root: None,
top: Arc::new(IgnoreStackEntry::All),
}
}
@@ -49,6 +55,7 @@ impl IgnoreStack {
pub fn global(ignore: Arc<Gitignore>) -> Self {
Self {
repo_root: None,
+ global_ignore_root: None,
top: Arc::new(IgnoreStackEntry::Global { ignore }),
}
}
@@ -70,6 +77,7 @@ impl IgnoreStack {
};
Self {
repo_root: self.repo_root,
+ global_ignore_root: self.global_ignore_root,
top,
}
}
@@ -84,10 +92,10 @@ impl IgnoreStack {
IgnoreStackEntry::All => true,
IgnoreStackEntry::Global { ignore } => {
let combined_path;
- let abs_path = if let Some(repo_root) = self.repo_root.as_ref() {
- let Ok(relative_path) = abs_path.strip_prefix(repo_root) else {
- // The provided absolute path is outside of the repository's
- // folder and cannot be ignored by this global ignore.
+ let abs_path = if let Some(global_ignore_root) = self.global_ignore_root.as_ref() {
+ let Ok(relative_path) = abs_path.strip_prefix(global_ignore_root) else {
+ // The provided absolute path is outside of the repository or
+ // worktree and cannot be ignored by this global ignore.
return false;
};
@@ -107,6 +115,7 @@ impl IgnoreStack {
if !abs_path.starts_with(ignore.path()) {
return IgnoreStack {
repo_root: self.repo_root.clone(),
+ global_ignore_root: self.global_ignore_root.clone(),
top: parent.clone(),
}
.is_abs_path_ignored(abs_path, is_dir);
@@ -115,6 +124,7 @@ impl IgnoreStack {
match ignore.matched(abs_path, is_dir) {
ignore::Match::None => IgnoreStack {
repo_root: self.repo_root.clone(),
+ global_ignore_root: self.global_ignore_root.clone(),
top: parent.clone(),
}
.is_abs_path_ignored(abs_path, is_dir),
@@ -129,6 +139,7 @@ impl IgnoreStack {
} => match ignore.matched(abs_path.strip_prefix(abs_base_path).unwrap(), is_dir) {
ignore::Match::None => IgnoreStack {
repo_root: self.repo_root.clone(),
+ global_ignore_root: self.global_ignore_root.clone(),
top: prev.clone(),
}
.is_abs_path_ignored(abs_path, is_dir),
diff --git a/crates/worktree/src/worktree.rs b/crates/worktree/src/worktree.rs
index 16e5f480cea..bffbe72449c 100644
--- a/crates/worktree/src/worktree.rs
+++ b/crates/worktree/src/worktree.rs
@@ -3108,6 +3108,8 @@ impl LocalSnapshot {
for repo_exclude in repo_excludes.into_iter().rev() {
ignore_stack = ignore_stack.append(IgnoreKind::RepoExclude, repo_exclude);
}
+ ignore_stack.global_ignore_root =
+ Some(repo_root.clone().unwrap_or_else(|| self.abs_path().clone()));
ignore_stack.repo_root = repo_root;
let mut ancestor_ignore_stack = ignore_stack.clone();
for (parent_abs_path, ignore) in new_ignores.into_iter().rev() {
@@ -5384,6 +5386,7 @@ impl BackgroundScanner {
&& path.ends_with(DOT_GIT)
{
ignore_stack.repo_root = Some(job.abs_path.clone());
+ ignore_stack.global_ignore_root = Some(job.abs_path.clone());
}
for child_abs_path in child_paths {
@@ -6016,6 +6019,7 @@ impl BackgroundScanner {
if let Ok(Some(_)) = self.fs.metadata(&job.abs_path.join(DOT_GIT)).await {
ignore_stack.repo_root = Some(job.abs_path.clone());
+ ignore_stack.global_ignore_root = Some(job.abs_path.clone());
}
for mut entry in snapshot.child_entries(&path).cloned() {
diff --git a/crates/worktree/tests/integration/worktree_tests.rs b/crates/worktree/tests/integration/worktree_tests.rs
index 6ca65a10b72..be84ed14366 100644
--- a/crates/worktree/tests/integration/worktree_tests.rs
+++ b/crates/worktree/tests/integration/worktree_tests.rs
@@ -3876,6 +3876,65 @@ async fn test_global_gitignore(executor: BackgroundExecutor, cx: &mut TestAppCon
});
}
+#[gpui::test]
+async fn test_global_gitignore_without_repository(
+ executor: BackgroundExecutor,
+ cx: &mut TestAppContext,
+) {
+ init_test(cx);
+
+ let home = paths::home_dir();
+ let project_path = home.join("builds").join("my-project.com");
+ let fs = FakeFs::new(executor);
+ fs.insert_tree(
+ home,
+ json!({
+ ".config": {
+ "git": {
+ "ignore": "foo\nbuilds\n*.com\n"
+ }
+ },
+ "builds": {
+ "my-project.com": {
+ "foo": "",
+ "keep_me": "",
+ }
+ }
+ }),
+ )
+ .await;
+
+ let worktree = Worktree::local(
+ project_path.clone(),
+ true,
+ fs.clone(),
+ Arc::default(),
+ true,
+ WorktreeId::from_proto(0),
+ &mut cx.to_async(),
+ )
+ .await
+ .unwrap();
+ worktree
+ .update(cx, |worktree, _| {
+ worktree.as_local().unwrap().scan_complete()
+ })
+ .await;
+ cx.run_until_parked();
+
+ // Neither `builds` (an outside ancestor) nor `.com` (the root's own name) may ignore everything, but `foo` still should.
+ worktree.update(cx, |worktree, _cx| {
+ check_worktree_entries(
+ worktree,
+ WorktreeExpectations {
+ ignored_paths: &["foo"],
+ tracked_paths: &["keep_me"],
+ ..Default::default()
+ },
+ );
+ });
+}
+
#[gpui::test]
async fn test_repo_exclude_in_worktree(executor: BackgroundExecutor, cx: &mut TestAppContext) {
init_test(cx);