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 pub project_name: Option<String>,
14 /// Whether to prevent this project from being shared in public channels.
15 pub prevent_sharing_in_public_channels: bool,
16 pub file_scan_exclusions: PathMatcher,
17 pub file_scan_inclusions: PathMatcher,
18 /// This field contains all ancestors of the `file_scan_inclusions`. It's used to
19 /// determine whether to terminate worktree scanning for a given dir.
20 pub parent_dir_scan_inclusions: PathMatcher,
21 pub private_files: PathMatcher,
22 pub hidden_files: PathMatcher,
23 pub read_only_files: PathMatcher,
24}
25
26impl WorktreeSettings {
27 pub fn is_path_private(&self, path: &RelPath) -> bool {
28 path.ancestors()
29 .any(|ancestor| self.private_files.is_match(ancestor))
30 }
31
32 pub fn is_path_excluded(&self, path: &RelPath) -> bool {
33 path.ancestors()
34 .any(|ancestor| self.file_scan_exclusions.is_match(ancestor))
35 }
36
37 pub fn is_path_always_included(&self, path: &RelPath, is_dir: bool) -> bool {
38 if is_dir {
39 self.parent_dir_scan_inclusions.is_match(path)
40 } else {
41 self.file_scan_inclusions.is_match(path)
42 }
43 }
44
45 pub fn is_path_hidden(&self, path: &RelPath) -> bool {
46 path.ancestors()
47 .any(|ancestor| self.hidden_files.is_match(ancestor))
48 }
49
50 pub fn is_path_read_only(&self, path: &RelPath) -> bool {
51 self.read_only_files.is_match(path)
52 }
53
54 pub fn is_std_path_read_only(&self, path: &Path) -> bool {
55 self.read_only_files.is_match_std_path(path)
56 }
57}
58
59impl Settings for WorktreeSettings {
60 fn from_settings(content: &settings::SettingsContent) -> Self {
61 let worktree = content.project.worktree.clone();
62 let file_scan_exclusions = worktree.file_scan_exclusions.unwrap();
63 let file_scan_inclusions = worktree.file_scan_inclusions.unwrap();
64 let private_files = worktree.private_files.unwrap().0;
65 let hidden_files = worktree.hidden_files.unwrap();
66 let read_only_files = worktree.read_only_files.unwrap_or_default();
67 let parsed_file_scan_inclusions: Vec<String> = file_scan_inclusions
68 .iter()
69 .flat_map(|glob| {
70 Path::new(glob)
71 .ancestors()
72 .skip(1)
73 .map(|a| a.to_string_lossy().into())
74 })
75 .filter(|p: &String| !p.is_empty())
76 .collect();
77
78 Self {
79 project_name: worktree.project_name,
80 prevent_sharing_in_public_channels: worktree.prevent_sharing_in_public_channels,
81 file_scan_exclusions: path_matchers(file_scan_exclusions, "file_scan_exclusions")
82 .log_err()
83 .unwrap_or_default(),
84 parent_dir_scan_inclusions: path_matchers(
85 parsed_file_scan_inclusions,
86 "file_scan_inclusions",
87 )
88 .unwrap(),
89 file_scan_inclusions: path_matchers(file_scan_inclusions, "file_scan_inclusions")
90 .unwrap(),
91 private_files: path_matchers(private_files, "private_files")
92 .log_err()
93 .unwrap_or_default(),
94 hidden_files: path_matchers(hidden_files, "hidden_files")
95 .log_err()
96 .unwrap_or_default(),
97 read_only_files: path_matchers(read_only_files, "read_only_files")
98 .log_err()
99 .unwrap_or_default(),
100 }
101 }
102}
103
104fn path_matchers(mut values: Vec<String>, context: &'static str) -> anyhow::Result<PathMatcher> {
105 values.sort();
106 PathMatcher::new(values, PathStyle::local())
107 .with_context(|| format!("Failed to parse globs from {}", context))
108}
109
110#[cfg(test)]
111mod tests {
112 use super::*;
113 use std::path::Path;
114
115 fn make_settings_with_read_only(patterns: &[&str]) -> WorktreeSettings {
116 WorktreeSettings {
117 project_name: None,
118 prevent_sharing_in_public_channels: false,
119 file_scan_exclusions: PathMatcher::default(),
120 file_scan_inclusions: PathMatcher::default(),
121 parent_dir_scan_inclusions: PathMatcher::default(),
122 private_files: PathMatcher::default(),
123 hidden_files: PathMatcher::default(),
124 read_only_files: PathMatcher::new(
125 patterns.iter().map(|s| s.to_string()),
126 PathStyle::local(),
127 )
128 .unwrap(),
129 }
130 }
131
132 #[test]
133 fn test_is_path_read_only_with_glob_patterns() {
134 let settings = make_settings_with_read_only(&["**/generated/**", "**/*.gen.rs"]);
135
136 let generated_file =
137 RelPath::new(Path::new("src/generated/schema.rs"), PathStyle::local()).unwrap();
138 assert!(
139 settings.is_path_read_only(&generated_file),
140 "Files in generated directory should be read-only"
141 );
142
143 let gen_rs_file = RelPath::new(Path::new("src/types.gen.rs"), PathStyle::local()).unwrap();
144 assert!(
145 settings.is_path_read_only(&gen_rs_file),
146 "Files with .gen.rs extension should be read-only"
147 );
148
149 let regular_file = RelPath::new(Path::new("src/main.rs"), PathStyle::local()).unwrap();
150 assert!(
151 !settings.is_path_read_only(®ular_file),
152 "Regular files should not be read-only"
153 );
154
155 let similar_name = RelPath::new(Path::new("src/generator.rs"), PathStyle::local()).unwrap();
156 assert!(
157 !settings.is_path_read_only(&similar_name),
158 "Files with 'generator' in name but not in generated dir should not be read-only"
159 );
160 }
161
162 #[test]
163 fn test_is_path_read_only_with_specific_paths() {
164 let settings = make_settings_with_read_only(&["vendor/**", "node_modules/**"]);
165
166 let vendor_file =
167 RelPath::new(Path::new("vendor/lib/package.js"), PathStyle::local()).unwrap();
168 assert!(
169 settings.is_path_read_only(&vendor_file),
170 "Files in vendor directory should be read-only"
171 );
172
173 let node_modules_file = RelPath::new(
174 Path::new("node_modules/lodash/index.js"),
175 PathStyle::local(),
176 )
177 .unwrap();
178 assert!(
179 settings.is_path_read_only(&node_modules_file),
180 "Files in node_modules should be read-only"
181 );
182
183 let src_file = RelPath::new(Path::new("src/app.js"), PathStyle::local()).unwrap();
184 assert!(
185 !settings.is_path_read_only(&src_file),
186 "Files in src should not be read-only"
187 );
188 }
189
190 #[test]
191 fn test_is_path_read_only_empty_patterns() {
192 let settings = make_settings_with_read_only(&[]);
193
194 let any_file = RelPath::new(Path::new("src/main.rs"), PathStyle::local()).unwrap();
195 assert!(
196 !settings.is_path_read_only(&any_file),
197 "No files should be read-only when patterns are empty"
198 );
199 }
200
201 #[test]
202 fn test_is_path_read_only_with_extension_pattern() {
203 let settings = make_settings_with_read_only(&["**/*.lock", "**/*.min.js"]);
204
205 let lock_file = RelPath::new(Path::new("Cargo.lock"), PathStyle::local()).unwrap();
206 assert!(
207 settings.is_path_read_only(&lock_file),
208 "Lock files should be read-only"
209 );
210
211 let nested_lock =
212 RelPath::new(Path::new("packages/app/yarn.lock"), PathStyle::local()).unwrap();
213 assert!(
214 settings.is_path_read_only(&nested_lock),
215 "Nested lock files should be read-only"
216 );
217
218 let minified_js =
219 RelPath::new(Path::new("dist/bundle.min.js"), PathStyle::local()).unwrap();
220 assert!(
221 settings.is_path_read_only(&minified_js),
222 "Minified JS files should be read-only"
223 );
224
225 let regular_js = RelPath::new(Path::new("src/app.js"), PathStyle::local()).unwrap();
226 assert!(
227 !settings.is_path_read_only(®ular_js),
228 "Regular JS files should not be read-only"
229 );
230 }
231}