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.

search: Fix ignored file search in nested Git directories (#60772)

Vitaly Slobodin55242071f11b
diff --git a/crates/search/src/project_search.rs b/crates/search/src/project_search.rs
index 0ab303409d1..d451190c9ff 100644
--- a/crates/search/src/project_search.rs
+++ b/crates/search/src/project_search.rs
@@ -2823,6 +2823,175 @@ pub mod tests {
assert_eq!(split_glob_patterns(r"a\\\,b"), vec![r"a\\\,b"]);
}
+ #[perf]
+ #[gpui::test]
+ async fn test_ignored_dot_git_directory_results_follow_include_ignored_option(
+ cx: &mut TestAppContext,
+ ) {
+ init_test(cx);
+ let fs = FakeFs::new(cx.background_executor.clone());
+ fs.insert_tree(
+ path!("/dir"),
+ json!({
+ ".gitignore": "log/\n",
+ "app": {
+ "a.txt": "hello",
+ },
+ "log": {
+ ".git": {},
+ "b.txt": "hello",
+ },
+ }),
+ )
+ .await;
+ let project = Project::test(fs.clone(), [path!("/dir").as_ref()], cx).await;
+ let window =
+ cx.add_window(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
+ let workspace = window
+ .read_with(cx, |mw, _| mw.workspace().clone())
+ .unwrap();
+ let search = cx.new(|cx| ProjectSearch::new(project, cx));
+ let search_view = cx.add_window(|window, cx| {
+ ProjectSearchView::new(workspace.downgrade(), search, window, cx, None)
+ });
+
+ perform_search(search_view, "hello", cx);
+ assert_eq!(
+ search_view
+ .update(cx, |search_view, _, cx| {
+ search_view.entity.read(cx).match_ranges.len()
+ })
+ .unwrap(),
+ 1
+ );
+
+ search_view
+ .update(cx, |search_view, _, cx| {
+ search_view.toggle_search_option(SearchOptions::INCLUDE_IGNORED, cx);
+ })
+ .unwrap();
+ perform_search(search_view, "hello", cx);
+ assert_eq!(
+ search_view
+ .update(cx, |search_view, _, cx| {
+ search_view.entity.read(cx).match_ranges.len()
+ })
+ .unwrap(),
+ 2
+ );
+
+ search_view
+ .update(cx, |search_view, _, cx| {
+ search_view.toggle_search_option(SearchOptions::INCLUDE_IGNORED, cx);
+ })
+ .unwrap();
+ perform_search(search_view, "hello", cx);
+ assert_eq!(
+ search_view
+ .update(cx, |search_view, _, cx| {
+ search_view.entity.read(cx).match_ranges.len()
+ })
+ .unwrap(),
+ 1
+ );
+ }
+
+ #[perf]
+ #[gpui::test]
+ async fn test_unignored_dot_git_directory_results_are_included(cx: &mut TestAppContext) {
+ init_test(cx);
+ let fs = FakeFs::new(cx.background_executor.clone());
+ fs.insert_tree(
+ path!("/dir"),
+ json!({
+ "app": {
+ "a.txt": "hello",
+ },
+ "log": {
+ ".git": {},
+ "b.txt": "hello",
+ },
+ }),
+ )
+ .await;
+ let project = Project::test(fs.clone(), [path!("/dir").as_ref()], cx).await;
+ let window =
+ cx.add_window(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
+ let workspace = window
+ .read_with(cx, |mw, _| mw.workspace().clone())
+ .unwrap();
+ let search = cx.new(|cx| ProjectSearch::new(project, cx));
+ let search_view = cx.add_window(|window, cx| {
+ ProjectSearchView::new(workspace.downgrade(), search, window, cx, None)
+ });
+
+ perform_search(search_view, "hello", cx);
+ assert_eq!(
+ search_view
+ .update(cx, |search_view, _, cx| {
+ search_view.entity.read(cx).match_ranges.len()
+ })
+ .unwrap(),
+ 2
+ );
+ }
+
+ #[perf]
+ #[gpui::test]
+ async fn test_nested_gitignore_results_follow_include_ignored_option(cx: &mut TestAppContext) {
+ init_test(cx);
+ let fs = FakeFs::new(cx.background_executor.clone());
+ fs.insert_tree(
+ path!("/dir"),
+ json!({
+ "app": {
+ "a.txt": "hello",
+ },
+ "log": {
+ ".git": {},
+ ".gitignore": "b.txt\n",
+ "b.txt": "hello",
+ },
+ }),
+ )
+ .await;
+ let project = Project::test(fs.clone(), [path!("/dir").as_ref()], cx).await;
+ let window =
+ cx.add_window(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
+ let workspace = window
+ .read_with(cx, |mw, _| mw.workspace().clone())
+ .unwrap();
+ let search = cx.new(|cx| ProjectSearch::new(project, cx));
+ let search_view = cx.add_window(|window, cx| {
+ ProjectSearchView::new(workspace.downgrade(), search, window, cx, None)
+ });
+
+ perform_search(search_view, "hello", cx);
+ assert_eq!(
+ search_view
+ .update(cx, |search_view, _, cx| {
+ search_view.entity.read(cx).match_ranges.len()
+ })
+ .unwrap(),
+ 1
+ );
+
+ search_view
+ .update(cx, |search_view, _, cx| {
+ search_view.toggle_search_option(SearchOptions::INCLUDE_IGNORED, cx);
+ })
+ .unwrap();
+ perform_search(search_view, "hello", cx);
+ assert_eq!(
+ search_view
+ .update(cx, |search_view, _, cx| {
+ search_view.entity.read(cx).match_ranges.len()
+ })
+ .unwrap(),
+ 2
+ );
+ }
+
#[perf]
#[gpui::test]
async fn test_project_search(cx: &mut TestAppContext) {
diff --git a/crates/worktree/src/ignore.rs b/crates/worktree/src/ignore.rs
index 87487c36df6..5b0b3da52f1 100644
--- a/crates/worktree/src/ignore.rs
+++ b/crates/worktree/src/ignore.rs
@@ -85,12 +85,12 @@ impl IgnoreStack {
IgnoreStackEntry::Global { ignore } => {
let combined_path;
let abs_path = if let Some(repo_root) = self.repo_root.as_ref() {
- combined_path = ignore.path().join(
+ if let Ok(relative_path) = abs_path.strip_prefix(repo_root) {
+ combined_path = ignore.path().join(relative_path);
+ &combined_path
+ } else {
abs_path
- .strip_prefix(repo_root)
- .expect("repo root should be a parent of matched path"),
- );
- &combined_path
+ }
} else {
abs_path
};
diff --git a/crates/worktree/src/worktree.rs b/crates/worktree/src/worktree.rs
index b83532b1636..ee04ab6d7bc 100644
--- a/crates/worktree/src/worktree.rs
+++ b/crates/worktree/src/worktree.rs
@@ -2970,10 +2970,11 @@ impl LocalSnapshot {
}
}
- let metadata = fs.metadata(&ancestor.join(DOT_GIT)).await.ok().flatten();
- if metadata.is_some() {
- repo_root = Some(Arc::from(ancestor));
- break;
+ if repo_root.is_none() {
+ let metadata = fs.metadata(&ancestor.join(DOT_GIT)).await.ok().flatten();
+ if metadata.is_some() {
+ repo_root = Some(Arc::from(ancestor));
+ }
}
}
@@ -2990,11 +2991,14 @@ impl LocalSnapshot {
ignore_stack = ignore_stack.append(IgnoreKind::RepoExclude, repo_exclude.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() {
- if ignore_stack.is_abs_path_ignored(parent_abs_path, true) {
+ if ancestor_ignore_stack.is_abs_path_ignored(parent_abs_path, true) {
ignore_stack = IgnoreStack::all();
break;
} else if let Some(ignore) = ignore {
+ let kind = IgnoreKind::Gitignore(parent_abs_path.into());
+ ancestor_ignore_stack = ancestor_ignore_stack.append(kind, ignore.clone());
ignore_stack =
ignore_stack.append(IgnoreKind::Gitignore(parent_abs_path.into()), ignore);
}