Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
worktree: Anchor ignore rules at the repository they belong to (#62325)
Henrique Ferreiro83dc1967d05c
diff --git a/crates/worktree/src/ignore.rs b/crates/worktree/src/ignore.rsindex b1f801afd4e..c9efd6fd430 100644--- a/crates/worktree/src/ignore.rs+++ b/crates/worktree/src/ignore.rs@@ -86,8 +86,8 @@ impl IgnoreStack {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 folderabs_path is outside repo_root and cannot be ignored- // by this global ignore+ // The provided absolute path is outside of the repository's+ // folder and cannot be ignored by this global ignore.return false;};@@ -103,6 +103,15 @@ impl IgnoreStack {}}IgnoreStackEntry::RepoExclude { ignore, parent } => {+ // Ignore rules from a repository that does not contain this path.+ if !abs_path.starts_with(ignore.path()) {+ return IgnoreStack {+ repo_root: self.repo_root.clone(),+ top: parent.clone(),+ }+ .is_abs_path_ignored(abs_path, is_dir);+ }+match ignore.matched(abs_path, is_dir) {ignore::Match::None => IgnoreStack {repo_root: self.repo_root.clone(),diff --git a/crates/worktree/src/worktree.rs b/crates/worktree/src/worktree.rsindex 9f4d47ca5bd..5c7e3360237 100644--- a/crates/worktree/src/worktree.rs+++ b/crates/worktree/src/worktree.rs@@ -31,7 +31,7 @@ use gpui::{App, AppContext as _, AsyncApp, BackgroundExecutor, Context, Entity, EventEmitter, Priority,Task,};-use ignore::IgnoreStack;+pub use ignore::{IgnoreKind, IgnoreStack};use language::{ByteContent, DiskState, FILE_ANALYSIS_BYTES, analyze_byte_content};use async_channel::{self, Sender};@@ -77,8 +77,6 @@ use util::{};pub use worktree_settings::WorktreeSettings;-use crate::ignore::IgnoreKind;-pub const FS_WATCH_LATENCY: Duration = Duration::from_millis(100);/// A set of local or remote files that are being opened as part of a project.@@ -3079,11 +3077,20 @@ impl LocalSnapshot {repo_excludes.push(repo_exclude.clone());}- if repo_root.is_none() {- let metadata = fs.metadata(&ancestor.join(DOT_GIT)).await.ok().flatten();- if metadata.is_some() {+ let is_repo_root = fs+ .metadata(&ancestor.join(DOT_GIT))+ .await+ .is_ok_and(|metadata| metadata.is_some());+ if is_repo_root {+ if repo_root.is_none() {repo_root = Some(Arc::from(ancestor));}++ // Stop at the repository containing the worktree root, but not at+ // ones nested below it, where its rules still apply.+ if self.abs_path.as_path().starts_with(ancestor) {+ break;+ }}}diff --git a/crates/worktree/tests/integration/worktree_tests.rs b/crates/worktree/tests/integration/worktree_tests.rsindex d3fbf2824bc..71bd95b5bc9 100644--- a/crates/worktree/tests/integration/worktree_tests.rs+++ b/crates/worktree/tests/integration/worktree_tests.rs@@ -3801,6 +3801,62 @@ async fn test_repo_exclude_in_worktree(executor: BackgroundExecutor, cx: &mut Te});}+#[gpui::test]+async fn test_repo_exclude_naming_a_worktree_ancestor(+ executor: BackgroundExecutor,+ cx: &mut TestAppContext,+) {+ init_test(cx);++ let fs = FakeFs::new(executor);++ fs.insert_tree(+ path!("/scratch/proj"),+ json!({+ ".git": {+ "info": { "exclude": "scratch" }+ },+ "src": {+ "main.rs": "fn main() {}",+ }+ }),+ )+ .await;++ let worktree = Worktree::local(+ path!("/scratch/proj").as_ref(),+ true,+ fs.clone(),+ Default::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();++ worktree.update(cx, |worktree, _cx| {+ assert!(+ !worktree.root_entry().unwrap().is_ignored,+ "an exclude pattern matching an ancestor must not ignore the worktree"+ );+ check_worktree_entries(+ worktree,+ WorktreeExpectations {+ tracked_paths: &["src/main.rs"],+ ..Default::default()+ },+ );+ });+}+#[gpui::test]async fn test_repo_exclude(executor: BackgroundExecutor, cx: &mut TestAppContext) {init_test(cx);@@ -6299,3 +6355,25 @@ async fn test_deferred_watch_symlinks_pointing_outside(cx: &mut TestAppContext)}).await;}++#[test]+fn test_repo_exclude_does_not_match_outside_its_work_directory() {+ use ignore::gitignore::GitignoreBuilder;+ use worktree::{IgnoreKind, IgnoreStack};++ let mut builder = GitignoreBuilder::new("/repo/inner");+ builder.add_line(None, "build").unwrap();+ builder.add_line(None, "repo").unwrap();+ let exclude = Arc::new(builder.build().unwrap());++ let stack = IgnoreStack::none().append(IgnoreKind::RepoExclude, exclude);++ assert!(+ stack.is_abs_path_ignored(Path::new("/repo/inner/build"), true),+ "patterns must apply within the repository's work directory"+ );+ assert!(+ !stack.is_abs_path_ignored(Path::new("/repo"), true),+ "patterns must not apply outside the repository's work directory"+ );+}