1use std::path::Path;
2
3use anyhow::Context as _;
4use settings::{RegisterSetting, Settings};
5use util::{
6 ResultExt,
7 paths::{PathMatcher, PathStyle},
8 rel_path::RelPath,
9};
10
11#[derive(Clone, PartialEq, Eq, RegisterSetting)]
12pub struct WorktreeSettings {
13 /// Whether to prevent this project from being shared in public channels.
14 pub prevent_sharing_in_public_channels: bool,
15 pub file_scan_exclusions: PathMatcher,
16 pub file_scan_inclusions: PathMatcher,
17 /// This field contains all ancestors of the `file_scan_inclusions`. It's used to
18 /// determine whether to terminate worktree scanning for a given dir.
19 pub parent_dir_scan_inclusions: PathMatcher,
20 pub private_files: PathMatcher,
21 pub hidden_files: PathMatcher,
22 pub read_only_files: PathMatcher,
23}
24
25impl WorktreeSettings {
26 pub fn is_path_private(&self, path: &RelPath) -> bool {
27 path.ancestors()
28 .any(|ancestor| self.private_files.is_match(ancestor))
29 }
30
31 pub fn is_path_excluded(&self, path: &RelPath) -> bool {
32 path.ancestors()
33 .any(|ancestor| self.file_scan_exclusions.is_match(ancestor))
34 }
35
36 pub fn is_path_always_included(&self, path: &RelPath, is_dir: bool) -> bool {
37 if is_dir {
38 self.parent_dir_scan_inclusions.is_match(path)
39 } else {
40 self.file_scan_inclusions.is_match(path)
41 }
42 }
43
44 pub fn is_path_hidden(&self, path: &RelPath) -> bool {
45 path.ancestors()
46 .any(|ancestor| self.hidden_files.is_match(ancestor))
47 }
48
49 pub fn is_path_read_only(&self, path: &RelPath) -> bool {
50 self.read_only_files.is_match(path)
51 }
52
53 pub fn is_std_path_read_only(&self, path: &Path) -> bool {
54 self.read_only_files.is_match_std_path(path)
55 }
56}
57
58impl Settings for WorktreeSettings {
59 fn from_settings(content: &settings::SettingsContent) -> Self {
60 let worktree = content.project.worktree.clone();
61 let file_scan_exclusions = worktree.file_scan_exclusions.unwrap();
62 let file_scan_inclusions = worktree.file_scan_inclusions.unwrap();
63 let private_files = worktree.private_files.unwrap().0;
64 let hidden_files = worktree.hidden_files.unwrap();
65 let read_only_files = worktree.read_only_files.unwrap_or_default();
66 let parsed_file_scan_inclusions: Vec<String> = file_scan_inclusions
67 .iter()
68 .flat_map(|glob| {
69 Path::new(glob)
70 .ancestors()
71 .skip(1)
72 .map(|a| a.to_string_lossy().into())
73 })
74 .filter(|p: &String| !p.is_empty())
75 .collect();
76
77 Self {
78 prevent_sharing_in_public_channels: worktree.prevent_sharing_in_public_channels,
79 file_scan_exclusions: path_matchers(file_scan_exclusions, "file_scan_exclusions")
80 .log_err()
81 .unwrap_or_default(),
82 parent_dir_scan_inclusions: path_matchers(
83 parsed_file_scan_inclusions,
84 "file_scan_inclusions",
85 )
86 .unwrap(),
87 file_scan_inclusions: path_matchers(file_scan_inclusions, "file_scan_inclusions")
88 .unwrap(),
89 private_files: path_matchers(private_files, "private_files")
90 .log_err()
91 .unwrap_or_default(),
92 hidden_files: path_matchers(hidden_files, "hidden_files")
93 .log_err()
94 .unwrap_or_default(),
95 read_only_files: path_matchers(read_only_files, "read_only_files")
96 .log_err()
97 .unwrap_or_default(),
98 }
99 }
100}
101
102fn path_matchers(mut values: Vec<String>, context: &'static str) -> anyhow::Result<PathMatcher> {
103 values.sort();
104 PathMatcher::new(values, PathStyle::local())
105 .with_context(|| format!("Failed to parse globs from {}", context))
106}