1use crate::{
2 worktree_settings::WorktreeSettings, Entry, EntryKind, Event, PathChange, Snapshot,
3 WorkDirectory, Worktree, WorktreeModelHandle,
4};
5use anyhow::Result;
6use fs::{FakeFs, Fs, RealFs, RemoveOptions};
7use git::{
8 repository::RepoPath,
9 status::{
10 FileStatus, GitSummary, StatusCode, TrackedStatus, TrackedSummary, UnmergedStatus,
11 UnmergedStatusCode,
12 },
13 GITIGNORE,
14};
15use git2::RepositoryInitOptions;
16use gpui::{AppContext as _, BorrowAppContext, Context, Task, TestAppContext};
17use parking_lot::Mutex;
18use postage::stream::Stream;
19use pretty_assertions::assert_eq;
20use rand::prelude::*;
21use serde_json::json;
22use settings::{Settings, SettingsStore};
23use std::{
24 env,
25 fmt::Write,
26 mem,
27 path::{Path, PathBuf},
28 sync::Arc,
29 time::Duration,
30};
31use util::{path, test::TempTree, ResultExt};
32
33#[gpui::test]
34async fn test_traversal(cx: &mut TestAppContext) {
35 init_test(cx);
36 let fs = FakeFs::new(cx.background_executor.clone());
37 fs.insert_tree(
38 "/root",
39 json!({
40 ".gitignore": "a/b\n",
41 "a": {
42 "b": "",
43 "c": "",
44 }
45 }),
46 )
47 .await;
48
49 let tree = Worktree::local(
50 Path::new("/root"),
51 true,
52 fs,
53 Default::default(),
54 &mut cx.to_async(),
55 )
56 .await
57 .unwrap();
58 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
59 .await;
60
61 tree.read_with(cx, |tree, _| {
62 assert_eq!(
63 tree.entries(false, 0)
64 .map(|entry| entry.path.as_ref())
65 .collect::<Vec<_>>(),
66 vec![
67 Path::new(""),
68 Path::new(".gitignore"),
69 Path::new("a"),
70 Path::new("a/c"),
71 ]
72 );
73 assert_eq!(
74 tree.entries(true, 0)
75 .map(|entry| entry.path.as_ref())
76 .collect::<Vec<_>>(),
77 vec![
78 Path::new(""),
79 Path::new(".gitignore"),
80 Path::new("a"),
81 Path::new("a/b"),
82 Path::new("a/c"),
83 ]
84 );
85 })
86}
87
88#[gpui::test(iterations = 10)]
89async fn test_circular_symlinks(cx: &mut TestAppContext) {
90 init_test(cx);
91 let fs = FakeFs::new(cx.background_executor.clone());
92 fs.insert_tree(
93 "/root",
94 json!({
95 "lib": {
96 "a": {
97 "a.txt": ""
98 },
99 "b": {
100 "b.txt": ""
101 }
102 }
103 }),
104 )
105 .await;
106 fs.create_symlink("/root/lib/a/lib".as_ref(), "..".into())
107 .await
108 .unwrap();
109 fs.create_symlink("/root/lib/b/lib".as_ref(), "..".into())
110 .await
111 .unwrap();
112
113 let tree = Worktree::local(
114 Path::new("/root"),
115 true,
116 fs.clone(),
117 Default::default(),
118 &mut cx.to_async(),
119 )
120 .await
121 .unwrap();
122
123 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
124 .await;
125
126 tree.read_with(cx, |tree, _| {
127 assert_eq!(
128 tree.entries(false, 0)
129 .map(|entry| entry.path.as_ref())
130 .collect::<Vec<_>>(),
131 vec![
132 Path::new(""),
133 Path::new("lib"),
134 Path::new("lib/a"),
135 Path::new("lib/a/a.txt"),
136 Path::new("lib/a/lib"),
137 Path::new("lib/b"),
138 Path::new("lib/b/b.txt"),
139 Path::new("lib/b/lib"),
140 ]
141 );
142 });
143
144 fs.rename(
145 Path::new("/root/lib/a/lib"),
146 Path::new("/root/lib/a/lib-2"),
147 Default::default(),
148 )
149 .await
150 .unwrap();
151 cx.executor().run_until_parked();
152 tree.read_with(cx, |tree, _| {
153 assert_eq!(
154 tree.entries(false, 0)
155 .map(|entry| entry.path.as_ref())
156 .collect::<Vec<_>>(),
157 vec![
158 Path::new(""),
159 Path::new("lib"),
160 Path::new("lib/a"),
161 Path::new("lib/a/a.txt"),
162 Path::new("lib/a/lib-2"),
163 Path::new("lib/b"),
164 Path::new("lib/b/b.txt"),
165 Path::new("lib/b/lib"),
166 ]
167 );
168 });
169}
170
171#[gpui::test]
172async fn test_symlinks_pointing_outside(cx: &mut TestAppContext) {
173 init_test(cx);
174 let fs = FakeFs::new(cx.background_executor.clone());
175 fs.insert_tree(
176 "/root",
177 json!({
178 "dir1": {
179 "deps": {
180 // symlinks here
181 },
182 "src": {
183 "a.rs": "",
184 "b.rs": "",
185 },
186 },
187 "dir2": {
188 "src": {
189 "c.rs": "",
190 "d.rs": "",
191 }
192 },
193 "dir3": {
194 "deps": {},
195 "src": {
196 "e.rs": "",
197 "f.rs": "",
198 },
199 }
200 }),
201 )
202 .await;
203
204 // These symlinks point to directories outside of the worktree's root, dir1.
205 fs.create_symlink("/root/dir1/deps/dep-dir2".as_ref(), "../../dir2".into())
206 .await
207 .unwrap();
208 fs.create_symlink("/root/dir1/deps/dep-dir3".as_ref(), "../../dir3".into())
209 .await
210 .unwrap();
211
212 let tree = Worktree::local(
213 Path::new("/root/dir1"),
214 true,
215 fs.clone(),
216 Default::default(),
217 &mut cx.to_async(),
218 )
219 .await
220 .unwrap();
221
222 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
223 .await;
224
225 let tree_updates = Arc::new(Mutex::new(Vec::new()));
226 tree.update(cx, |_, cx| {
227 let tree_updates = tree_updates.clone();
228 cx.subscribe(&tree, move |_, _, event, _| {
229 if let Event::UpdatedEntries(update) = event {
230 tree_updates.lock().extend(
231 update
232 .iter()
233 .map(|(path, _, change)| (path.clone(), *change)),
234 );
235 }
236 })
237 .detach();
238 });
239
240 // The symlinked directories are not scanned by default.
241 tree.read_with(cx, |tree, _| {
242 assert_eq!(
243 tree.entries(true, 0)
244 .map(|entry| (entry.path.as_ref(), entry.is_external))
245 .collect::<Vec<_>>(),
246 vec![
247 (Path::new(""), false),
248 (Path::new("deps"), false),
249 (Path::new("deps/dep-dir2"), true),
250 (Path::new("deps/dep-dir3"), true),
251 (Path::new("src"), false),
252 (Path::new("src/a.rs"), false),
253 (Path::new("src/b.rs"), false),
254 ]
255 );
256
257 assert_eq!(
258 tree.entry_for_path("deps/dep-dir2").unwrap().kind,
259 EntryKind::UnloadedDir
260 );
261 });
262
263 // Expand one of the symlinked directories.
264 tree.read_with(cx, |tree, _| {
265 tree.as_local()
266 .unwrap()
267 .refresh_entries_for_paths(vec![Path::new("deps/dep-dir3").into()])
268 })
269 .recv()
270 .await;
271
272 // The expanded directory's contents are loaded. Subdirectories are
273 // not scanned yet.
274 tree.read_with(cx, |tree, _| {
275 assert_eq!(
276 tree.entries(true, 0)
277 .map(|entry| (entry.path.as_ref(), entry.is_external))
278 .collect::<Vec<_>>(),
279 vec![
280 (Path::new(""), false),
281 (Path::new("deps"), false),
282 (Path::new("deps/dep-dir2"), true),
283 (Path::new("deps/dep-dir3"), true),
284 (Path::new("deps/dep-dir3/deps"), true),
285 (Path::new("deps/dep-dir3/src"), true),
286 (Path::new("src"), false),
287 (Path::new("src/a.rs"), false),
288 (Path::new("src/b.rs"), false),
289 ]
290 );
291 });
292 assert_eq!(
293 mem::take(&mut *tree_updates.lock()),
294 &[
295 (Path::new("deps/dep-dir3").into(), PathChange::Loaded),
296 (Path::new("deps/dep-dir3/deps").into(), PathChange::Loaded),
297 (Path::new("deps/dep-dir3/src").into(), PathChange::Loaded)
298 ]
299 );
300
301 // Expand a subdirectory of one of the symlinked directories.
302 tree.read_with(cx, |tree, _| {
303 tree.as_local()
304 .unwrap()
305 .refresh_entries_for_paths(vec![Path::new("deps/dep-dir3/src").into()])
306 })
307 .recv()
308 .await;
309
310 // The expanded subdirectory's contents are loaded.
311 tree.read_with(cx, |tree, _| {
312 assert_eq!(
313 tree.entries(true, 0)
314 .map(|entry| (entry.path.as_ref(), entry.is_external))
315 .collect::<Vec<_>>(),
316 vec![
317 (Path::new(""), false),
318 (Path::new("deps"), false),
319 (Path::new("deps/dep-dir2"), true),
320 (Path::new("deps/dep-dir3"), true),
321 (Path::new("deps/dep-dir3/deps"), true),
322 (Path::new("deps/dep-dir3/src"), true),
323 (Path::new("deps/dep-dir3/src/e.rs"), true),
324 (Path::new("deps/dep-dir3/src/f.rs"), true),
325 (Path::new("src"), false),
326 (Path::new("src/a.rs"), false),
327 (Path::new("src/b.rs"), false),
328 ]
329 );
330 });
331
332 assert_eq!(
333 mem::take(&mut *tree_updates.lock()),
334 &[
335 (Path::new("deps/dep-dir3/src").into(), PathChange::Loaded),
336 (
337 Path::new("deps/dep-dir3/src/e.rs").into(),
338 PathChange::Loaded
339 ),
340 (
341 Path::new("deps/dep-dir3/src/f.rs").into(),
342 PathChange::Loaded
343 )
344 ]
345 );
346}
347
348#[cfg(target_os = "macos")]
349#[gpui::test]
350async fn test_renaming_case_only(cx: &mut TestAppContext) {
351 cx.executor().allow_parking();
352 init_test(cx);
353
354 const OLD_NAME: &str = "aaa.rs";
355 const NEW_NAME: &str = "AAA.rs";
356
357 let fs = Arc::new(RealFs::default());
358 let temp_root = TempTree::new(json!({
359 OLD_NAME: "",
360 }));
361
362 let tree = Worktree::local(
363 temp_root.path(),
364 true,
365 fs.clone(),
366 Default::default(),
367 &mut cx.to_async(),
368 )
369 .await
370 .unwrap();
371
372 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
373 .await;
374 tree.read_with(cx, |tree, _| {
375 assert_eq!(
376 tree.entries(true, 0)
377 .map(|entry| entry.path.as_ref())
378 .collect::<Vec<_>>(),
379 vec![Path::new(""), Path::new(OLD_NAME)]
380 );
381 });
382
383 fs.rename(
384 &temp_root.path().join(OLD_NAME),
385 &temp_root.path().join(NEW_NAME),
386 fs::RenameOptions {
387 overwrite: true,
388 ignore_if_exists: true,
389 },
390 )
391 .await
392 .unwrap();
393
394 tree.flush_fs_events(cx).await;
395
396 tree.read_with(cx, |tree, _| {
397 assert_eq!(
398 tree.entries(true, 0)
399 .map(|entry| entry.path.as_ref())
400 .collect::<Vec<_>>(),
401 vec![Path::new(""), Path::new(NEW_NAME)]
402 );
403 });
404}
405
406#[gpui::test]
407async fn test_open_gitignored_files(cx: &mut TestAppContext) {
408 init_test(cx);
409 let fs = FakeFs::new(cx.background_executor.clone());
410 fs.insert_tree(
411 "/root",
412 json!({
413 ".gitignore": "node_modules\n",
414 "one": {
415 "node_modules": {
416 "a": {
417 "a1.js": "a1",
418 "a2.js": "a2",
419 },
420 "b": {
421 "b1.js": "b1",
422 "b2.js": "b2",
423 },
424 "c": {
425 "c1.js": "c1",
426 "c2.js": "c2",
427 }
428 },
429 },
430 "two": {
431 "x.js": "",
432 "y.js": "",
433 },
434 }),
435 )
436 .await;
437
438 let tree = Worktree::local(
439 Path::new("/root"),
440 true,
441 fs.clone(),
442 Default::default(),
443 &mut cx.to_async(),
444 )
445 .await
446 .unwrap();
447
448 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
449 .await;
450
451 tree.read_with(cx, |tree, _| {
452 assert_eq!(
453 tree.entries(true, 0)
454 .map(|entry| (entry.path.as_ref(), entry.is_ignored))
455 .collect::<Vec<_>>(),
456 vec![
457 (Path::new(""), false),
458 (Path::new(".gitignore"), false),
459 (Path::new("one"), false),
460 (Path::new("one/node_modules"), true),
461 (Path::new("two"), false),
462 (Path::new("two/x.js"), false),
463 (Path::new("two/y.js"), false),
464 ]
465 );
466 });
467
468 // Open a file that is nested inside of a gitignored directory that
469 // has not yet been expanded.
470 let prev_read_dir_count = fs.read_dir_call_count();
471 let loaded = tree
472 .update(cx, |tree, cx| {
473 tree.load_file("one/node_modules/b/b1.js".as_ref(), cx)
474 })
475 .await
476 .unwrap();
477
478 tree.read_with(cx, |tree, _| {
479 assert_eq!(
480 tree.entries(true, 0)
481 .map(|entry| (entry.path.as_ref(), entry.is_ignored))
482 .collect::<Vec<_>>(),
483 vec![
484 (Path::new(""), false),
485 (Path::new(".gitignore"), false),
486 (Path::new("one"), false),
487 (Path::new("one/node_modules"), true),
488 (Path::new("one/node_modules/a"), true),
489 (Path::new("one/node_modules/b"), true),
490 (Path::new("one/node_modules/b/b1.js"), true),
491 (Path::new("one/node_modules/b/b2.js"), true),
492 (Path::new("one/node_modules/c"), true),
493 (Path::new("two"), false),
494 (Path::new("two/x.js"), false),
495 (Path::new("two/y.js"), false),
496 ]
497 );
498
499 assert_eq!(
500 loaded.file.path.as_ref(),
501 Path::new("one/node_modules/b/b1.js")
502 );
503
504 // Only the newly-expanded directories are scanned.
505 assert_eq!(fs.read_dir_call_count() - prev_read_dir_count, 2);
506 });
507
508 // Open another file in a different subdirectory of the same
509 // gitignored directory.
510 let prev_read_dir_count = fs.read_dir_call_count();
511 let loaded = tree
512 .update(cx, |tree, cx| {
513 tree.load_file("one/node_modules/a/a2.js".as_ref(), cx)
514 })
515 .await
516 .unwrap();
517
518 tree.read_with(cx, |tree, _| {
519 assert_eq!(
520 tree.entries(true, 0)
521 .map(|entry| (entry.path.as_ref(), entry.is_ignored))
522 .collect::<Vec<_>>(),
523 vec![
524 (Path::new(""), false),
525 (Path::new(".gitignore"), false),
526 (Path::new("one"), false),
527 (Path::new("one/node_modules"), true),
528 (Path::new("one/node_modules/a"), true),
529 (Path::new("one/node_modules/a/a1.js"), true),
530 (Path::new("one/node_modules/a/a2.js"), true),
531 (Path::new("one/node_modules/b"), true),
532 (Path::new("one/node_modules/b/b1.js"), true),
533 (Path::new("one/node_modules/b/b2.js"), true),
534 (Path::new("one/node_modules/c"), true),
535 (Path::new("two"), false),
536 (Path::new("two/x.js"), false),
537 (Path::new("two/y.js"), false),
538 ]
539 );
540
541 assert_eq!(
542 loaded.file.path.as_ref(),
543 Path::new("one/node_modules/a/a2.js")
544 );
545
546 // Only the newly-expanded directory is scanned.
547 assert_eq!(fs.read_dir_call_count() - prev_read_dir_count, 1);
548 });
549
550 let path = PathBuf::from("/root/one/node_modules/c/lib");
551
552 // No work happens when files and directories change within an unloaded directory.
553 let prev_fs_call_count = fs.read_dir_call_count() + fs.metadata_call_count();
554 // When we open a directory, we check each ancestor whether it's a git
555 // repository. That means we have an fs.metadata call per ancestor that we
556 // need to subtract here.
557 let ancestors = path.ancestors().count();
558
559 fs.create_dir(path.as_ref()).await.unwrap();
560 cx.executor().run_until_parked();
561
562 assert_eq!(
563 fs.read_dir_call_count() + fs.metadata_call_count() - prev_fs_call_count - ancestors,
564 0
565 );
566}
567
568#[gpui::test]
569async fn test_dirs_no_longer_ignored(cx: &mut TestAppContext) {
570 init_test(cx);
571 let fs = FakeFs::new(cx.background_executor.clone());
572 fs.insert_tree(
573 "/root",
574 json!({
575 ".gitignore": "node_modules\n",
576 "a": {
577 "a.js": "",
578 },
579 "b": {
580 "b.js": "",
581 },
582 "node_modules": {
583 "c": {
584 "c.js": "",
585 },
586 "d": {
587 "d.js": "",
588 "e": {
589 "e1.js": "",
590 "e2.js": "",
591 },
592 "f": {
593 "f1.js": "",
594 "f2.js": "",
595 }
596 },
597 },
598 }),
599 )
600 .await;
601
602 let tree = Worktree::local(
603 Path::new("/root"),
604 true,
605 fs.clone(),
606 Default::default(),
607 &mut cx.to_async(),
608 )
609 .await
610 .unwrap();
611
612 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
613 .await;
614
615 // Open a file within the gitignored directory, forcing some of its
616 // subdirectories to be read, but not all.
617 let read_dir_count_1 = fs.read_dir_call_count();
618 tree.read_with(cx, |tree, _| {
619 tree.as_local()
620 .unwrap()
621 .refresh_entries_for_paths(vec![Path::new("node_modules/d/d.js").into()])
622 })
623 .recv()
624 .await;
625
626 // Those subdirectories are now loaded.
627 tree.read_with(cx, |tree, _| {
628 assert_eq!(
629 tree.entries(true, 0)
630 .map(|e| (e.path.as_ref(), e.is_ignored))
631 .collect::<Vec<_>>(),
632 &[
633 (Path::new(""), false),
634 (Path::new(".gitignore"), false),
635 (Path::new("a"), false),
636 (Path::new("a/a.js"), false),
637 (Path::new("b"), false),
638 (Path::new("b/b.js"), false),
639 (Path::new("node_modules"), true),
640 (Path::new("node_modules/c"), true),
641 (Path::new("node_modules/d"), true),
642 (Path::new("node_modules/d/d.js"), true),
643 (Path::new("node_modules/d/e"), true),
644 (Path::new("node_modules/d/f"), true),
645 ]
646 );
647 });
648 let read_dir_count_2 = fs.read_dir_call_count();
649 assert_eq!(read_dir_count_2 - read_dir_count_1, 2);
650
651 // Update the gitignore so that node_modules is no longer ignored,
652 // but a subdirectory is ignored
653 fs.save("/root/.gitignore".as_ref(), &"e".into(), Default::default())
654 .await
655 .unwrap();
656 cx.executor().run_until_parked();
657
658 // All of the directories that are no longer ignored are now loaded.
659 tree.read_with(cx, |tree, _| {
660 assert_eq!(
661 tree.entries(true, 0)
662 .map(|e| (e.path.as_ref(), e.is_ignored))
663 .collect::<Vec<_>>(),
664 &[
665 (Path::new(""), false),
666 (Path::new(".gitignore"), false),
667 (Path::new("a"), false),
668 (Path::new("a/a.js"), false),
669 (Path::new("b"), false),
670 (Path::new("b/b.js"), false),
671 // This directory is no longer ignored
672 (Path::new("node_modules"), false),
673 (Path::new("node_modules/c"), false),
674 (Path::new("node_modules/c/c.js"), false),
675 (Path::new("node_modules/d"), false),
676 (Path::new("node_modules/d/d.js"), false),
677 // This subdirectory is now ignored
678 (Path::new("node_modules/d/e"), true),
679 (Path::new("node_modules/d/f"), false),
680 (Path::new("node_modules/d/f/f1.js"), false),
681 (Path::new("node_modules/d/f/f2.js"), false),
682 ]
683 );
684 });
685
686 // Each of the newly-loaded directories is scanned only once.
687 let read_dir_count_3 = fs.read_dir_call_count();
688 assert_eq!(read_dir_count_3 - read_dir_count_2, 2);
689}
690
691#[gpui::test(iterations = 10)]
692async fn test_rescan_with_gitignore(cx: &mut TestAppContext) {
693 init_test(cx);
694 cx.update(|cx| {
695 cx.update_global::<SettingsStore, _>(|store, cx| {
696 store.update_user_settings::<WorktreeSettings>(cx, |project_settings| {
697 project_settings.file_scan_exclusions = Some(Vec::new());
698 });
699 });
700 });
701 let fs = FakeFs::new(cx.background_executor.clone());
702 fs.insert_tree(
703 "/root",
704 json!({
705 ".gitignore": "ancestor-ignored-file1\nancestor-ignored-file2\n",
706 "tree": {
707 ".git": {},
708 ".gitignore": "ignored-dir\n",
709 "tracked-dir": {
710 "tracked-file1": "",
711 "ancestor-ignored-file1": "",
712 },
713 "ignored-dir": {
714 "ignored-file1": ""
715 }
716 }
717 }),
718 )
719 .await;
720
721 let tree = Worktree::local(
722 "/root/tree".as_ref(),
723 true,
724 fs.clone(),
725 Default::default(),
726 &mut cx.to_async(),
727 )
728 .await
729 .unwrap();
730 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
731 .await;
732
733 tree.read_with(cx, |tree, _| {
734 tree.as_local()
735 .unwrap()
736 .refresh_entries_for_paths(vec![Path::new("ignored-dir").into()])
737 })
738 .recv()
739 .await;
740
741 cx.read(|cx| {
742 let tree = tree.read(cx);
743 assert_entry_git_state(tree, "tracked-dir/tracked-file1", None, false);
744 assert_entry_git_state(tree, "tracked-dir/ancestor-ignored-file1", None, false);
745 assert_entry_git_state(tree, "ignored-dir/ignored-file1", None, true);
746 });
747
748 fs.set_status_for_repo_via_working_copy_change(
749 Path::new("/root/tree/.git"),
750 &[(
751 Path::new("tracked-dir/tracked-file2"),
752 StatusCode::Added.index(),
753 )],
754 );
755
756 fs.create_file(
757 "/root/tree/tracked-dir/tracked-file2".as_ref(),
758 Default::default(),
759 )
760 .await
761 .unwrap();
762 fs.create_file(
763 "/root/tree/tracked-dir/ancestor-ignored-file2".as_ref(),
764 Default::default(),
765 )
766 .await
767 .unwrap();
768 fs.create_file(
769 "/root/tree/ignored-dir/ignored-file2".as_ref(),
770 Default::default(),
771 )
772 .await
773 .unwrap();
774
775 cx.executor().run_until_parked();
776 cx.read(|cx| {
777 let tree = tree.read(cx);
778 assert_entry_git_state(
779 tree,
780 "tracked-dir/tracked-file2",
781 Some(StatusCode::Added),
782 false,
783 );
784 assert_entry_git_state(tree, "tracked-dir/ancestor-ignored-file2", None, false);
785 assert_entry_git_state(tree, "ignored-dir/ignored-file2", None, true);
786 assert!(tree.entry_for_path(".git").unwrap().is_ignored);
787 });
788}
789
790#[gpui::test]
791async fn test_update_gitignore(cx: &mut TestAppContext) {
792 init_test(cx);
793 let fs = FakeFs::new(cx.background_executor.clone());
794 fs.insert_tree(
795 "/root",
796 json!({
797 ".git": {},
798 ".gitignore": "*.txt\n",
799 "a.xml": "<a></a>",
800 "b.txt": "Some text"
801 }),
802 )
803 .await;
804
805 let tree = Worktree::local(
806 "/root".as_ref(),
807 true,
808 fs.clone(),
809 Default::default(),
810 &mut cx.to_async(),
811 )
812 .await
813 .unwrap();
814 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
815 .await;
816
817 tree.read_with(cx, |tree, _| {
818 tree.as_local()
819 .unwrap()
820 .refresh_entries_for_paths(vec![Path::new("").into()])
821 })
822 .recv()
823 .await;
824
825 cx.read(|cx| {
826 let tree = tree.read(cx);
827 assert_entry_git_state(tree, "a.xml", None, false);
828 assert_entry_git_state(tree, "b.txt", None, true);
829 });
830
831 fs.atomic_write("/root/.gitignore".into(), "*.xml".into())
832 .await
833 .unwrap();
834
835 fs.set_status_for_repo_via_working_copy_change(
836 Path::new("/root/.git"),
837 &[(Path::new("b.txt"), StatusCode::Added.index())],
838 );
839
840 cx.executor().run_until_parked();
841 cx.read(|cx| {
842 let tree = tree.read(cx);
843 assert_entry_git_state(tree, "a.xml", None, true);
844 assert_entry_git_state(tree, "b.txt", Some(StatusCode::Added), false);
845 });
846}
847
848// TODO: Fix flaky test.
849// #[gpui::test]
850#[allow(unused)]
851async fn test_write_file(cx: &mut TestAppContext) {
852 init_test(cx);
853 cx.executor().allow_parking();
854 let dir = TempTree::new(json!({
855 ".git": {},
856 ".gitignore": "ignored-dir\n",
857 "tracked-dir": {},
858 "ignored-dir": {}
859 }));
860
861 let worktree = Worktree::local(
862 dir.path(),
863 true,
864 Arc::new(RealFs::default()),
865 Default::default(),
866 &mut cx.to_async(),
867 )
868 .await
869 .unwrap();
870
871 #[cfg(not(target_os = "macos"))]
872 fs::fs_watcher::global(|_| {}).unwrap();
873
874 cx.read(|cx| worktree.read(cx).as_local().unwrap().scan_complete())
875 .await;
876 worktree.flush_fs_events(cx).await;
877
878 worktree
879 .update(cx, |tree, cx| {
880 tree.write_file(
881 Path::new("tracked-dir/file.txt"),
882 "hello".into(),
883 Default::default(),
884 cx,
885 )
886 })
887 .await
888 .unwrap();
889 worktree
890 .update(cx, |tree, cx| {
891 tree.write_file(
892 Path::new("ignored-dir/file.txt"),
893 "world".into(),
894 Default::default(),
895 cx,
896 )
897 })
898 .await
899 .unwrap();
900
901 worktree.read_with(cx, |tree, _| {
902 let tracked = tree.entry_for_path("tracked-dir/file.txt").unwrap();
903 let ignored = tree.entry_for_path("ignored-dir/file.txt").unwrap();
904 assert!(!tracked.is_ignored);
905 assert!(ignored.is_ignored);
906 });
907}
908
909#[gpui::test]
910async fn test_file_scan_inclusions(cx: &mut TestAppContext) {
911 init_test(cx);
912 cx.executor().allow_parking();
913 let dir = TempTree::new(json!({
914 ".gitignore": "**/target\n/node_modules\ntop_level.txt\n",
915 "target": {
916 "index": "blah2"
917 },
918 "node_modules": {
919 ".DS_Store": "",
920 "prettier": {
921 "package.json": "{}",
922 },
923 },
924 "src": {
925 ".DS_Store": "",
926 "foo": {
927 "foo.rs": "mod another;\n",
928 "another.rs": "// another",
929 },
930 "bar": {
931 "bar.rs": "// bar",
932 },
933 "lib.rs": "mod foo;\nmod bar;\n",
934 },
935 "top_level.txt": "top level file",
936 ".DS_Store": "",
937 }));
938 cx.update(|cx| {
939 cx.update_global::<SettingsStore, _>(|store, cx| {
940 store.update_user_settings::<WorktreeSettings>(cx, |project_settings| {
941 project_settings.file_scan_exclusions = Some(vec![]);
942 project_settings.file_scan_inclusions = Some(vec![
943 "node_modules/**/package.json".to_string(),
944 "**/.DS_Store".to_string(),
945 ]);
946 });
947 });
948 });
949
950 let tree = Worktree::local(
951 dir.path(),
952 true,
953 Arc::new(RealFs::default()),
954 Default::default(),
955 &mut cx.to_async(),
956 )
957 .await
958 .unwrap();
959 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
960 .await;
961 tree.flush_fs_events(cx).await;
962 tree.read_with(cx, |tree, _| {
963 // Assert that file_scan_inclusions overrides file_scan_exclusions.
964 check_worktree_entries(
965 tree,
966 &[],
967 &["target", "node_modules"],
968 &["src/lib.rs", "src/bar/bar.rs", ".gitignore"],
969 &[
970 "node_modules/prettier/package.json",
971 ".DS_Store",
972 "node_modules/.DS_Store",
973 "src/.DS_Store",
974 ],
975 )
976 });
977}
978
979#[gpui::test]
980async fn test_file_scan_exclusions_overrules_inclusions(cx: &mut TestAppContext) {
981 init_test(cx);
982 cx.executor().allow_parking();
983 let dir = TempTree::new(json!({
984 ".gitignore": "**/target\n/node_modules\n",
985 "target": {
986 "index": "blah2"
987 },
988 "node_modules": {
989 ".DS_Store": "",
990 "prettier": {
991 "package.json": "{}",
992 },
993 },
994 "src": {
995 ".DS_Store": "",
996 "foo": {
997 "foo.rs": "mod another;\n",
998 "another.rs": "// another",
999 },
1000 },
1001 ".DS_Store": "",
1002 }));
1003
1004 cx.update(|cx| {
1005 cx.update_global::<SettingsStore, _>(|store, cx| {
1006 store.update_user_settings::<WorktreeSettings>(cx, |project_settings| {
1007 project_settings.file_scan_exclusions = Some(vec!["**/.DS_Store".to_string()]);
1008 project_settings.file_scan_inclusions = Some(vec!["**/.DS_Store".to_string()]);
1009 });
1010 });
1011 });
1012
1013 let tree = Worktree::local(
1014 dir.path(),
1015 true,
1016 Arc::new(RealFs::default()),
1017 Default::default(),
1018 &mut cx.to_async(),
1019 )
1020 .await
1021 .unwrap();
1022 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
1023 .await;
1024 tree.flush_fs_events(cx).await;
1025 tree.read_with(cx, |tree, _| {
1026 // Assert that file_scan_inclusions overrides file_scan_exclusions.
1027 check_worktree_entries(
1028 tree,
1029 &[".DS_Store, src/.DS_Store"],
1030 &["target", "node_modules"],
1031 &["src/foo/another.rs", "src/foo/foo.rs", ".gitignore"],
1032 &[],
1033 )
1034 });
1035}
1036
1037#[gpui::test]
1038async fn test_file_scan_inclusions_reindexes_on_setting_change(cx: &mut TestAppContext) {
1039 init_test(cx);
1040 cx.executor().allow_parking();
1041 let dir = TempTree::new(json!({
1042 ".gitignore": "**/target\n/node_modules/\n",
1043 "target": {
1044 "index": "blah2"
1045 },
1046 "node_modules": {
1047 ".DS_Store": "",
1048 "prettier": {
1049 "package.json": "{}",
1050 },
1051 },
1052 "src": {
1053 ".DS_Store": "",
1054 "foo": {
1055 "foo.rs": "mod another;\n",
1056 "another.rs": "// another",
1057 },
1058 },
1059 ".DS_Store": "",
1060 }));
1061
1062 cx.update(|cx| {
1063 cx.update_global::<SettingsStore, _>(|store, cx| {
1064 store.update_user_settings::<WorktreeSettings>(cx, |project_settings| {
1065 project_settings.file_scan_exclusions = Some(vec![]);
1066 project_settings.file_scan_inclusions = Some(vec!["node_modules/**".to_string()]);
1067 });
1068 });
1069 });
1070 let tree = Worktree::local(
1071 dir.path(),
1072 true,
1073 Arc::new(RealFs::default()),
1074 Default::default(),
1075 &mut cx.to_async(),
1076 )
1077 .await
1078 .unwrap();
1079 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
1080 .await;
1081 tree.flush_fs_events(cx).await;
1082
1083 tree.read_with(cx, |tree, _| {
1084 assert!(tree
1085 .entry_for_path("node_modules")
1086 .is_some_and(|f| f.is_always_included));
1087 assert!(tree
1088 .entry_for_path("node_modules/prettier/package.json")
1089 .is_some_and(|f| f.is_always_included));
1090 });
1091
1092 cx.update(|cx| {
1093 cx.update_global::<SettingsStore, _>(|store, cx| {
1094 store.update_user_settings::<WorktreeSettings>(cx, |project_settings| {
1095 project_settings.file_scan_exclusions = Some(vec![]);
1096 project_settings.file_scan_inclusions = Some(vec![]);
1097 });
1098 });
1099 });
1100 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
1101 .await;
1102 tree.flush_fs_events(cx).await;
1103
1104 tree.read_with(cx, |tree, _| {
1105 assert!(tree
1106 .entry_for_path("node_modules")
1107 .is_some_and(|f| !f.is_always_included));
1108 assert!(tree
1109 .entry_for_path("node_modules/prettier/package.json")
1110 .is_some_and(|f| !f.is_always_included));
1111 });
1112}
1113
1114#[gpui::test]
1115async fn test_file_scan_exclusions(cx: &mut TestAppContext) {
1116 init_test(cx);
1117 cx.executor().allow_parking();
1118 let dir = TempTree::new(json!({
1119 ".gitignore": "**/target\n/node_modules\n",
1120 "target": {
1121 "index": "blah2"
1122 },
1123 "node_modules": {
1124 ".DS_Store": "",
1125 "prettier": {
1126 "package.json": "{}",
1127 },
1128 },
1129 "src": {
1130 ".DS_Store": "",
1131 "foo": {
1132 "foo.rs": "mod another;\n",
1133 "another.rs": "// another",
1134 },
1135 "bar": {
1136 "bar.rs": "// bar",
1137 },
1138 "lib.rs": "mod foo;\nmod bar;\n",
1139 },
1140 ".DS_Store": "",
1141 }));
1142 cx.update(|cx| {
1143 cx.update_global::<SettingsStore, _>(|store, cx| {
1144 store.update_user_settings::<WorktreeSettings>(cx, |project_settings| {
1145 project_settings.file_scan_exclusions =
1146 Some(vec!["**/foo/**".to_string(), "**/.DS_Store".to_string()]);
1147 });
1148 });
1149 });
1150
1151 let tree = Worktree::local(
1152 dir.path(),
1153 true,
1154 Arc::new(RealFs::default()),
1155 Default::default(),
1156 &mut cx.to_async(),
1157 )
1158 .await
1159 .unwrap();
1160 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
1161 .await;
1162 tree.flush_fs_events(cx).await;
1163 tree.read_with(cx, |tree, _| {
1164 check_worktree_entries(
1165 tree,
1166 &[
1167 "src/foo/foo.rs",
1168 "src/foo/another.rs",
1169 "node_modules/.DS_Store",
1170 "src/.DS_Store",
1171 ".DS_Store",
1172 ],
1173 &["target", "node_modules"],
1174 &["src/lib.rs", "src/bar/bar.rs", ".gitignore"],
1175 &[],
1176 )
1177 });
1178
1179 cx.update(|cx| {
1180 cx.update_global::<SettingsStore, _>(|store, cx| {
1181 store.update_user_settings::<WorktreeSettings>(cx, |project_settings| {
1182 project_settings.file_scan_exclusions =
1183 Some(vec!["**/node_modules/**".to_string()]);
1184 });
1185 });
1186 });
1187 tree.flush_fs_events(cx).await;
1188 cx.executor().run_until_parked();
1189 tree.read_with(cx, |tree, _| {
1190 check_worktree_entries(
1191 tree,
1192 &[
1193 "node_modules/prettier/package.json",
1194 "node_modules/.DS_Store",
1195 "node_modules",
1196 ],
1197 &["target"],
1198 &[
1199 ".gitignore",
1200 "src/lib.rs",
1201 "src/bar/bar.rs",
1202 "src/foo/foo.rs",
1203 "src/foo/another.rs",
1204 "src/.DS_Store",
1205 ".DS_Store",
1206 ],
1207 &[],
1208 )
1209 });
1210}
1211
1212#[gpui::test]
1213async fn test_fs_events_in_exclusions(cx: &mut TestAppContext) {
1214 init_test(cx);
1215 cx.executor().allow_parking();
1216 let dir = TempTree::new(json!({
1217 ".git": {
1218 "HEAD": "ref: refs/heads/main\n",
1219 "foo": "bar",
1220 },
1221 ".gitignore": "**/target\n/node_modules\ntest_output\n",
1222 "target": {
1223 "index": "blah2"
1224 },
1225 "node_modules": {
1226 ".DS_Store": "",
1227 "prettier": {
1228 "package.json": "{}",
1229 },
1230 },
1231 "src": {
1232 ".DS_Store": "",
1233 "foo": {
1234 "foo.rs": "mod another;\n",
1235 "another.rs": "// another",
1236 },
1237 "bar": {
1238 "bar.rs": "// bar",
1239 },
1240 "lib.rs": "mod foo;\nmod bar;\n",
1241 },
1242 ".DS_Store": "",
1243 }));
1244 cx.update(|cx| {
1245 cx.update_global::<SettingsStore, _>(|store, cx| {
1246 store.update_user_settings::<WorktreeSettings>(cx, |project_settings| {
1247 project_settings.file_scan_exclusions = Some(vec![
1248 "**/.git".to_string(),
1249 "node_modules/".to_string(),
1250 "build_output".to_string(),
1251 ]);
1252 });
1253 });
1254 });
1255
1256 let tree = Worktree::local(
1257 dir.path(),
1258 true,
1259 Arc::new(RealFs::default()),
1260 Default::default(),
1261 &mut cx.to_async(),
1262 )
1263 .await
1264 .unwrap();
1265 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
1266 .await;
1267 tree.flush_fs_events(cx).await;
1268 tree.read_with(cx, |tree, _| {
1269 check_worktree_entries(
1270 tree,
1271 &[
1272 ".git/HEAD",
1273 ".git/foo",
1274 "node_modules",
1275 "node_modules/.DS_Store",
1276 "node_modules/prettier",
1277 "node_modules/prettier/package.json",
1278 ],
1279 &["target"],
1280 &[
1281 ".DS_Store",
1282 "src/.DS_Store",
1283 "src/lib.rs",
1284 "src/foo/foo.rs",
1285 "src/foo/another.rs",
1286 "src/bar/bar.rs",
1287 ".gitignore",
1288 ],
1289 &[],
1290 )
1291 });
1292
1293 let new_excluded_dir = dir.path().join("build_output");
1294 let new_ignored_dir = dir.path().join("test_output");
1295 std::fs::create_dir_all(&new_excluded_dir)
1296 .unwrap_or_else(|e| panic!("Failed to create a {new_excluded_dir:?} directory: {e}"));
1297 std::fs::create_dir_all(&new_ignored_dir)
1298 .unwrap_or_else(|e| panic!("Failed to create a {new_ignored_dir:?} directory: {e}"));
1299 let node_modules_dir = dir.path().join("node_modules");
1300 let dot_git_dir = dir.path().join(".git");
1301 let src_dir = dir.path().join("src");
1302 for existing_dir in [&node_modules_dir, &dot_git_dir, &src_dir] {
1303 assert!(
1304 existing_dir.is_dir(),
1305 "Expect {existing_dir:?} to be present in the FS already"
1306 );
1307 }
1308
1309 for directory_for_new_file in [
1310 new_excluded_dir,
1311 new_ignored_dir,
1312 node_modules_dir,
1313 dot_git_dir,
1314 src_dir,
1315 ] {
1316 std::fs::write(directory_for_new_file.join("new_file"), "new file contents")
1317 .unwrap_or_else(|e| {
1318 panic!("Failed to create in {directory_for_new_file:?} a new file: {e}")
1319 });
1320 }
1321 tree.flush_fs_events(cx).await;
1322
1323 tree.read_with(cx, |tree, _| {
1324 check_worktree_entries(
1325 tree,
1326 &[
1327 ".git/HEAD",
1328 ".git/foo",
1329 ".git/new_file",
1330 "node_modules",
1331 "node_modules/.DS_Store",
1332 "node_modules/prettier",
1333 "node_modules/prettier/package.json",
1334 "node_modules/new_file",
1335 "build_output",
1336 "build_output/new_file",
1337 "test_output/new_file",
1338 ],
1339 &["target", "test_output"],
1340 &[
1341 ".DS_Store",
1342 "src/.DS_Store",
1343 "src/lib.rs",
1344 "src/foo/foo.rs",
1345 "src/foo/another.rs",
1346 "src/bar/bar.rs",
1347 "src/new_file",
1348 ".gitignore",
1349 ],
1350 &[],
1351 )
1352 });
1353}
1354
1355#[gpui::test]
1356async fn test_fs_events_in_dot_git_worktree(cx: &mut TestAppContext) {
1357 init_test(cx);
1358 cx.executor().allow_parking();
1359 let dir = TempTree::new(json!({
1360 ".git": {
1361 "HEAD": "ref: refs/heads/main\n",
1362 "foo": "foo contents",
1363 },
1364 }));
1365 let dot_git_worktree_dir = dir.path().join(".git");
1366
1367 let tree = Worktree::local(
1368 dot_git_worktree_dir.clone(),
1369 true,
1370 Arc::new(RealFs::default()),
1371 Default::default(),
1372 &mut cx.to_async(),
1373 )
1374 .await
1375 .unwrap();
1376 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
1377 .await;
1378 tree.flush_fs_events(cx).await;
1379 tree.read_with(cx, |tree, _| {
1380 check_worktree_entries(tree, &[], &["HEAD", "foo"], &[], &[])
1381 });
1382
1383 std::fs::write(dot_git_worktree_dir.join("new_file"), "new file contents")
1384 .unwrap_or_else(|e| panic!("Failed to create in {dot_git_worktree_dir:?} a new file: {e}"));
1385 tree.flush_fs_events(cx).await;
1386 tree.read_with(cx, |tree, _| {
1387 check_worktree_entries(tree, &[], &["HEAD", "foo", "new_file"], &[], &[])
1388 });
1389}
1390
1391#[gpui::test(iterations = 30)]
1392async fn test_create_directory_during_initial_scan(cx: &mut TestAppContext) {
1393 init_test(cx);
1394 let fs = FakeFs::new(cx.background_executor.clone());
1395 fs.insert_tree(
1396 "/root",
1397 json!({
1398 "b": {},
1399 "c": {},
1400 "d": {},
1401 }),
1402 )
1403 .await;
1404
1405 let tree = Worktree::local(
1406 "/root".as_ref(),
1407 true,
1408 fs,
1409 Default::default(),
1410 &mut cx.to_async(),
1411 )
1412 .await
1413 .unwrap();
1414
1415 let snapshot1 = tree.update(cx, |tree, cx| {
1416 let tree = tree.as_local_mut().unwrap();
1417 let snapshot = Arc::new(Mutex::new(tree.snapshot()));
1418 tree.observe_updates(0, cx, {
1419 let snapshot = snapshot.clone();
1420 let settings = tree.settings().clone();
1421 move |update| {
1422 snapshot
1423 .lock()
1424 .apply_remote_update(update, &settings.file_scan_inclusions)
1425 .unwrap();
1426 async { true }
1427 }
1428 });
1429 snapshot
1430 });
1431
1432 let entry = tree
1433 .update(cx, |tree, cx| {
1434 tree.as_local_mut()
1435 .unwrap()
1436 .create_entry("a/e".as_ref(), true, cx)
1437 })
1438 .await
1439 .unwrap()
1440 .to_included()
1441 .unwrap();
1442 assert!(entry.is_dir());
1443
1444 cx.executor().run_until_parked();
1445 tree.read_with(cx, |tree, _| {
1446 assert_eq!(tree.entry_for_path("a/e").unwrap().kind, EntryKind::Dir);
1447 });
1448
1449 let snapshot2 = tree.update(cx, |tree, _| tree.as_local().unwrap().snapshot());
1450 assert_eq!(
1451 snapshot1.lock().entries(true, 0).collect::<Vec<_>>(),
1452 snapshot2.entries(true, 0).collect::<Vec<_>>()
1453 );
1454}
1455
1456#[gpui::test]
1457async fn test_bump_mtime_of_git_repo_workdir(cx: &mut TestAppContext) {
1458 init_test(cx);
1459
1460 // Create a worktree with a git directory.
1461 let fs = FakeFs::new(cx.background_executor.clone());
1462 fs.insert_tree(
1463 "/root",
1464 json!({
1465 ".git": {},
1466 "a.txt": "",
1467 "b": {
1468 "c.txt": "",
1469 },
1470 }),
1471 )
1472 .await;
1473
1474 let tree = Worktree::local(
1475 "/root".as_ref(),
1476 true,
1477 fs.clone(),
1478 Default::default(),
1479 &mut cx.to_async(),
1480 )
1481 .await
1482 .unwrap();
1483 cx.executor().run_until_parked();
1484
1485 let (old_entry_ids, old_mtimes) = tree.read_with(cx, |tree, _| {
1486 (
1487 tree.entries(true, 0).map(|e| e.id).collect::<Vec<_>>(),
1488 tree.entries(true, 0).map(|e| e.mtime).collect::<Vec<_>>(),
1489 )
1490 });
1491
1492 // Regression test: after the directory is scanned, touch the git repo's
1493 // working directory, bumping its mtime. That directory keeps its project
1494 // entry id after the directories are re-scanned.
1495 fs.touch_path("/root").await;
1496 cx.executor().run_until_parked();
1497
1498 let (new_entry_ids, new_mtimes) = tree.read_with(cx, |tree, _| {
1499 (
1500 tree.entries(true, 0).map(|e| e.id).collect::<Vec<_>>(),
1501 tree.entries(true, 0).map(|e| e.mtime).collect::<Vec<_>>(),
1502 )
1503 });
1504 assert_eq!(new_entry_ids, old_entry_ids);
1505 assert_ne!(new_mtimes, old_mtimes);
1506
1507 // Regression test: changes to the git repository should still be
1508 // detected.
1509 fs.set_status_for_repo_via_git_operation(
1510 Path::new("/root/.git"),
1511 &[(Path::new("b/c.txt"), StatusCode::Modified.index())],
1512 );
1513 cx.executor().run_until_parked();
1514 cx.executor().advance_clock(Duration::from_secs(1));
1515
1516 let snapshot = tree.read_with(cx, |tree, _| tree.snapshot());
1517
1518 check_git_statuses(
1519 &snapshot,
1520 &[
1521 (Path::new(""), MODIFIED),
1522 (Path::new("a.txt"), GitSummary::UNCHANGED),
1523 (Path::new("b/c.txt"), MODIFIED),
1524 ],
1525 );
1526}
1527
1528#[gpui::test]
1529async fn test_create_dir_all_on_create_entry(cx: &mut TestAppContext) {
1530 init_test(cx);
1531 cx.executor().allow_parking();
1532
1533 let fs_fake = FakeFs::new(cx.background_executor.clone());
1534 fs_fake
1535 .insert_tree(
1536 "/root",
1537 json!({
1538 "a": {},
1539 }),
1540 )
1541 .await;
1542
1543 let tree_fake = Worktree::local(
1544 "/root".as_ref(),
1545 true,
1546 fs_fake,
1547 Default::default(),
1548 &mut cx.to_async(),
1549 )
1550 .await
1551 .unwrap();
1552
1553 let entry = tree_fake
1554 .update(cx, |tree, cx| {
1555 tree.as_local_mut()
1556 .unwrap()
1557 .create_entry("a/b/c/d.txt".as_ref(), false, cx)
1558 })
1559 .await
1560 .unwrap()
1561 .to_included()
1562 .unwrap();
1563 assert!(entry.is_file());
1564
1565 cx.executor().run_until_parked();
1566 tree_fake.read_with(cx, |tree, _| {
1567 assert!(tree.entry_for_path("a/b/c/d.txt").unwrap().is_file());
1568 assert!(tree.entry_for_path("a/b/c/").unwrap().is_dir());
1569 assert!(tree.entry_for_path("a/b/").unwrap().is_dir());
1570 });
1571
1572 let fs_real = Arc::new(RealFs::default());
1573 let temp_root = TempTree::new(json!({
1574 "a": {}
1575 }));
1576
1577 let tree_real = Worktree::local(
1578 temp_root.path(),
1579 true,
1580 fs_real,
1581 Default::default(),
1582 &mut cx.to_async(),
1583 )
1584 .await
1585 .unwrap();
1586
1587 let entry = tree_real
1588 .update(cx, |tree, cx| {
1589 tree.as_local_mut()
1590 .unwrap()
1591 .create_entry("a/b/c/d.txt".as_ref(), false, cx)
1592 })
1593 .await
1594 .unwrap()
1595 .to_included()
1596 .unwrap();
1597 assert!(entry.is_file());
1598
1599 cx.executor().run_until_parked();
1600 tree_real.read_with(cx, |tree, _| {
1601 assert!(tree.entry_for_path("a/b/c/d.txt").unwrap().is_file());
1602 assert!(tree.entry_for_path("a/b/c/").unwrap().is_dir());
1603 assert!(tree.entry_for_path("a/b/").unwrap().is_dir());
1604 });
1605
1606 // Test smallest change
1607 let entry = tree_real
1608 .update(cx, |tree, cx| {
1609 tree.as_local_mut()
1610 .unwrap()
1611 .create_entry("a/b/c/e.txt".as_ref(), false, cx)
1612 })
1613 .await
1614 .unwrap()
1615 .to_included()
1616 .unwrap();
1617 assert!(entry.is_file());
1618
1619 cx.executor().run_until_parked();
1620 tree_real.read_with(cx, |tree, _| {
1621 assert!(tree.entry_for_path("a/b/c/e.txt").unwrap().is_file());
1622 });
1623
1624 // Test largest change
1625 let entry = tree_real
1626 .update(cx, |tree, cx| {
1627 tree.as_local_mut()
1628 .unwrap()
1629 .create_entry("d/e/f/g.txt".as_ref(), false, cx)
1630 })
1631 .await
1632 .unwrap()
1633 .to_included()
1634 .unwrap();
1635 assert!(entry.is_file());
1636
1637 cx.executor().run_until_parked();
1638 tree_real.read_with(cx, |tree, _| {
1639 assert!(tree.entry_for_path("d/e/f/g.txt").unwrap().is_file());
1640 assert!(tree.entry_for_path("d/e/f").unwrap().is_dir());
1641 assert!(tree.entry_for_path("d/e/").unwrap().is_dir());
1642 assert!(tree.entry_for_path("d/").unwrap().is_dir());
1643 });
1644}
1645
1646#[gpui::test(iterations = 100)]
1647async fn test_random_worktree_operations_during_initial_scan(
1648 cx: &mut TestAppContext,
1649 mut rng: StdRng,
1650) {
1651 init_test(cx);
1652 let operations = env::var("OPERATIONS")
1653 .map(|o| o.parse().unwrap())
1654 .unwrap_or(5);
1655 let initial_entries = env::var("INITIAL_ENTRIES")
1656 .map(|o| o.parse().unwrap())
1657 .unwrap_or(20);
1658
1659 let root_dir = Path::new(path!("/test"));
1660 let fs = FakeFs::new(cx.background_executor.clone()) as Arc<dyn Fs>;
1661 fs.as_fake().insert_tree(root_dir, json!({})).await;
1662 for _ in 0..initial_entries {
1663 randomly_mutate_fs(&fs, root_dir, 1.0, &mut rng).await;
1664 }
1665 log::info!("generated initial tree");
1666
1667 let worktree = Worktree::local(
1668 root_dir,
1669 true,
1670 fs.clone(),
1671 Default::default(),
1672 &mut cx.to_async(),
1673 )
1674 .await
1675 .unwrap();
1676
1677 let mut snapshots = vec![worktree.read_with(cx, |tree, _| tree.as_local().unwrap().snapshot())];
1678 let updates = Arc::new(Mutex::new(Vec::new()));
1679 worktree.update(cx, |tree, cx| {
1680 check_worktree_change_events(tree, cx);
1681
1682 tree.as_local_mut().unwrap().observe_updates(0, cx, {
1683 let updates = updates.clone();
1684 move |update| {
1685 updates.lock().push(update);
1686 async { true }
1687 }
1688 });
1689 });
1690
1691 for _ in 0..operations {
1692 worktree
1693 .update(cx, |worktree, cx| {
1694 randomly_mutate_worktree(worktree, &mut rng, cx)
1695 })
1696 .await
1697 .log_err();
1698 worktree.read_with(cx, |tree, _| {
1699 tree.as_local().unwrap().snapshot().check_invariants(true)
1700 });
1701
1702 if rng.gen_bool(0.6) {
1703 snapshots.push(worktree.read_with(cx, |tree, _| tree.as_local().unwrap().snapshot()));
1704 }
1705 }
1706
1707 worktree
1708 .update(cx, |tree, _| tree.as_local_mut().unwrap().scan_complete())
1709 .await;
1710
1711 cx.executor().run_until_parked();
1712
1713 let final_snapshot = worktree.read_with(cx, |tree, _| {
1714 let tree = tree.as_local().unwrap();
1715 let snapshot = tree.snapshot();
1716 snapshot.check_invariants(true);
1717 snapshot
1718 });
1719
1720 let settings = worktree.read_with(cx, |tree, _| tree.as_local().unwrap().settings());
1721
1722 for (i, snapshot) in snapshots.into_iter().enumerate().rev() {
1723 let mut updated_snapshot = snapshot.clone();
1724 for update in updates.lock().iter() {
1725 if update.scan_id >= updated_snapshot.scan_id() as u64 {
1726 updated_snapshot
1727 .apply_remote_update(update.clone(), &settings.file_scan_inclusions)
1728 .unwrap();
1729 }
1730 }
1731
1732 assert_eq!(
1733 updated_snapshot.entries(true, 0).collect::<Vec<_>>(),
1734 final_snapshot.entries(true, 0).collect::<Vec<_>>(),
1735 "wrong updates after snapshot {i}: {snapshot:#?} {updates:#?}",
1736 );
1737 }
1738}
1739
1740#[gpui::test(iterations = 100)]
1741async fn test_random_worktree_changes(cx: &mut TestAppContext, mut rng: StdRng) {
1742 init_test(cx);
1743 let operations = env::var("OPERATIONS")
1744 .map(|o| o.parse().unwrap())
1745 .unwrap_or(40);
1746 let initial_entries = env::var("INITIAL_ENTRIES")
1747 .map(|o| o.parse().unwrap())
1748 .unwrap_or(20);
1749
1750 let root_dir = Path::new(path!("/test"));
1751 let fs = FakeFs::new(cx.background_executor.clone()) as Arc<dyn Fs>;
1752 fs.as_fake().insert_tree(root_dir, json!({})).await;
1753 for _ in 0..initial_entries {
1754 randomly_mutate_fs(&fs, root_dir, 1.0, &mut rng).await;
1755 }
1756 log::info!("generated initial tree");
1757
1758 let worktree = Worktree::local(
1759 root_dir,
1760 true,
1761 fs.clone(),
1762 Default::default(),
1763 &mut cx.to_async(),
1764 )
1765 .await
1766 .unwrap();
1767
1768 let updates = Arc::new(Mutex::new(Vec::new()));
1769 worktree.update(cx, |tree, cx| {
1770 check_worktree_change_events(tree, cx);
1771
1772 tree.as_local_mut().unwrap().observe_updates(0, cx, {
1773 let updates = updates.clone();
1774 move |update| {
1775 updates.lock().push(update);
1776 async { true }
1777 }
1778 });
1779 });
1780
1781 worktree
1782 .update(cx, |tree, _| tree.as_local_mut().unwrap().scan_complete())
1783 .await;
1784
1785 fs.as_fake().pause_events();
1786 let mut snapshots = Vec::new();
1787 let mut mutations_len = operations;
1788 while mutations_len > 1 {
1789 if rng.gen_bool(0.2) {
1790 worktree
1791 .update(cx, |worktree, cx| {
1792 randomly_mutate_worktree(worktree, &mut rng, cx)
1793 })
1794 .await
1795 .log_err();
1796 } else {
1797 randomly_mutate_fs(&fs, root_dir, 1.0, &mut rng).await;
1798 }
1799
1800 let buffered_event_count = fs.as_fake().buffered_event_count();
1801 if buffered_event_count > 0 && rng.gen_bool(0.3) {
1802 let len = rng.gen_range(0..=buffered_event_count);
1803 log::info!("flushing {} events", len);
1804 fs.as_fake().flush_events(len);
1805 } else {
1806 randomly_mutate_fs(&fs, root_dir, 0.6, &mut rng).await;
1807 mutations_len -= 1;
1808 }
1809
1810 cx.executor().run_until_parked();
1811 if rng.gen_bool(0.2) {
1812 log::info!("storing snapshot {}", snapshots.len());
1813 let snapshot = worktree.read_with(cx, |tree, _| tree.as_local().unwrap().snapshot());
1814 snapshots.push(snapshot);
1815 }
1816 }
1817
1818 log::info!("quiescing");
1819 fs.as_fake().flush_events(usize::MAX);
1820 cx.executor().run_until_parked();
1821
1822 let snapshot = worktree.read_with(cx, |tree, _| tree.as_local().unwrap().snapshot());
1823 snapshot.check_invariants(true);
1824 let expanded_paths = snapshot
1825 .expanded_entries()
1826 .map(|e| e.path.clone())
1827 .collect::<Vec<_>>();
1828
1829 {
1830 let new_worktree = Worktree::local(
1831 root_dir,
1832 true,
1833 fs.clone(),
1834 Default::default(),
1835 &mut cx.to_async(),
1836 )
1837 .await
1838 .unwrap();
1839 new_worktree
1840 .update(cx, |tree, _| tree.as_local_mut().unwrap().scan_complete())
1841 .await;
1842 new_worktree
1843 .update(cx, |tree, _| {
1844 tree.as_local_mut()
1845 .unwrap()
1846 .refresh_entries_for_paths(expanded_paths)
1847 })
1848 .recv()
1849 .await;
1850 let new_snapshot =
1851 new_worktree.read_with(cx, |tree, _| tree.as_local().unwrap().snapshot());
1852 assert_eq!(
1853 snapshot.entries_without_ids(true),
1854 new_snapshot.entries_without_ids(true)
1855 );
1856 }
1857
1858 let settings = worktree.read_with(cx, |tree, _| tree.as_local().unwrap().settings());
1859
1860 for (i, mut prev_snapshot) in snapshots.into_iter().enumerate().rev() {
1861 for update in updates.lock().iter() {
1862 if update.scan_id >= prev_snapshot.scan_id() as u64 {
1863 prev_snapshot
1864 .apply_remote_update(update.clone(), &settings.file_scan_inclusions)
1865 .unwrap();
1866 }
1867 }
1868
1869 assert_eq!(
1870 prev_snapshot
1871 .entries(true, 0)
1872 .map(ignore_pending_dir)
1873 .collect::<Vec<_>>(),
1874 snapshot
1875 .entries(true, 0)
1876 .map(ignore_pending_dir)
1877 .collect::<Vec<_>>(),
1878 "wrong updates after snapshot {i}: {updates:#?}",
1879 );
1880 }
1881
1882 fn ignore_pending_dir(entry: &Entry) -> Entry {
1883 let mut entry = entry.clone();
1884 if entry.kind.is_dir() {
1885 entry.kind = EntryKind::Dir
1886 }
1887 entry
1888 }
1889}
1890
1891// The worktree's `UpdatedEntries` event can be used to follow along with
1892// all changes to the worktree's snapshot.
1893fn check_worktree_change_events(tree: &mut Worktree, cx: &mut Context<Worktree>) {
1894 let mut entries = tree.entries(true, 0).cloned().collect::<Vec<_>>();
1895 cx.subscribe(&cx.entity(), move |tree, _, event, _| {
1896 if let Event::UpdatedEntries(changes) = event {
1897 for (path, _, change_type) in changes.iter() {
1898 let entry = tree.entry_for_path(path).cloned();
1899 let ix = match entries.binary_search_by_key(&path, |e| &e.path) {
1900 Ok(ix) | Err(ix) => ix,
1901 };
1902 match change_type {
1903 PathChange::Added => entries.insert(ix, entry.unwrap()),
1904 PathChange::Removed => drop(entries.remove(ix)),
1905 PathChange::Updated => {
1906 let entry = entry.unwrap();
1907 let existing_entry = entries.get_mut(ix).unwrap();
1908 assert_eq!(existing_entry.path, entry.path);
1909 *existing_entry = entry;
1910 }
1911 PathChange::AddedOrUpdated | PathChange::Loaded => {
1912 let entry = entry.unwrap();
1913 if entries.get(ix).map(|e| &e.path) == Some(&entry.path) {
1914 *entries.get_mut(ix).unwrap() = entry;
1915 } else {
1916 entries.insert(ix, entry);
1917 }
1918 }
1919 }
1920 }
1921
1922 let new_entries = tree.entries(true, 0).cloned().collect::<Vec<_>>();
1923 assert_eq!(entries, new_entries, "incorrect changes: {:?}", changes);
1924 }
1925 })
1926 .detach();
1927}
1928
1929fn randomly_mutate_worktree(
1930 worktree: &mut Worktree,
1931 rng: &mut impl Rng,
1932 cx: &mut Context<Worktree>,
1933) -> Task<Result<()>> {
1934 log::info!("mutating worktree");
1935 let worktree = worktree.as_local_mut().unwrap();
1936 let snapshot = worktree.snapshot();
1937 let entry = snapshot.entries(false, 0).choose(rng).unwrap();
1938
1939 match rng.gen_range(0_u32..100) {
1940 0..=33 if entry.path.as_ref() != Path::new("") => {
1941 log::info!("deleting entry {:?} ({})", entry.path, entry.id.0);
1942 worktree.delete_entry(entry.id, false, cx).unwrap()
1943 }
1944 ..=66 if entry.path.as_ref() != Path::new("") => {
1945 let other_entry = snapshot.entries(false, 0).choose(rng).unwrap();
1946 let new_parent_path = if other_entry.is_dir() {
1947 other_entry.path.clone()
1948 } else {
1949 other_entry.path.parent().unwrap().into()
1950 };
1951 let mut new_path = new_parent_path.join(random_filename(rng));
1952 if new_path.starts_with(&entry.path) {
1953 new_path = random_filename(rng).into();
1954 }
1955
1956 log::info!(
1957 "renaming entry {:?} ({}) to {:?}",
1958 entry.path,
1959 entry.id.0,
1960 new_path
1961 );
1962 let task = worktree.rename_entry(entry.id, new_path, cx);
1963 cx.background_spawn(async move {
1964 task.await?.to_included().unwrap();
1965 Ok(())
1966 })
1967 }
1968 _ => {
1969 if entry.is_dir() {
1970 let child_path = entry.path.join(random_filename(rng));
1971 let is_dir = rng.gen_bool(0.3);
1972 log::info!(
1973 "creating {} at {:?}",
1974 if is_dir { "dir" } else { "file" },
1975 child_path,
1976 );
1977 let task = worktree.create_entry(child_path, is_dir, cx);
1978 cx.background_spawn(async move {
1979 task.await?;
1980 Ok(())
1981 })
1982 } else {
1983 log::info!("overwriting file {:?} ({})", entry.path, entry.id.0);
1984 let task =
1985 worktree.write_file(entry.path.clone(), "".into(), Default::default(), cx);
1986 cx.background_spawn(async move {
1987 task.await?;
1988 Ok(())
1989 })
1990 }
1991 }
1992 }
1993}
1994
1995async fn randomly_mutate_fs(
1996 fs: &Arc<dyn Fs>,
1997 root_path: &Path,
1998 insertion_probability: f64,
1999 rng: &mut impl Rng,
2000) {
2001 log::info!("mutating fs");
2002 let mut files = Vec::new();
2003 let mut dirs = Vec::new();
2004 for path in fs.as_fake().paths(false) {
2005 if path.starts_with(root_path) {
2006 if fs.is_file(&path).await {
2007 files.push(path);
2008 } else {
2009 dirs.push(path);
2010 }
2011 }
2012 }
2013
2014 if (files.is_empty() && dirs.len() == 1) || rng.gen_bool(insertion_probability) {
2015 let path = dirs.choose(rng).unwrap();
2016 let new_path = path.join(random_filename(rng));
2017
2018 if rng.gen() {
2019 log::info!(
2020 "creating dir {:?}",
2021 new_path.strip_prefix(root_path).unwrap()
2022 );
2023 fs.create_dir(&new_path).await.unwrap();
2024 } else {
2025 log::info!(
2026 "creating file {:?}",
2027 new_path.strip_prefix(root_path).unwrap()
2028 );
2029 fs.create_file(&new_path, Default::default()).await.unwrap();
2030 }
2031 } else if rng.gen_bool(0.05) {
2032 let ignore_dir_path = dirs.choose(rng).unwrap();
2033 let ignore_path = ignore_dir_path.join(*GITIGNORE);
2034
2035 let subdirs = dirs
2036 .iter()
2037 .filter(|d| d.starts_with(ignore_dir_path))
2038 .cloned()
2039 .collect::<Vec<_>>();
2040 let subfiles = files
2041 .iter()
2042 .filter(|d| d.starts_with(ignore_dir_path))
2043 .cloned()
2044 .collect::<Vec<_>>();
2045 let files_to_ignore = {
2046 let len = rng.gen_range(0..=subfiles.len());
2047 subfiles.choose_multiple(rng, len)
2048 };
2049 let dirs_to_ignore = {
2050 let len = rng.gen_range(0..subdirs.len());
2051 subdirs.choose_multiple(rng, len)
2052 };
2053
2054 let mut ignore_contents = String::new();
2055 for path_to_ignore in files_to_ignore.chain(dirs_to_ignore) {
2056 writeln!(
2057 ignore_contents,
2058 "{}",
2059 path_to_ignore
2060 .strip_prefix(ignore_dir_path)
2061 .unwrap()
2062 .to_str()
2063 .unwrap()
2064 )
2065 .unwrap();
2066 }
2067 log::info!(
2068 "creating gitignore {:?} with contents:\n{}",
2069 ignore_path.strip_prefix(root_path).unwrap(),
2070 ignore_contents
2071 );
2072 fs.save(
2073 &ignore_path,
2074 &ignore_contents.as_str().into(),
2075 Default::default(),
2076 )
2077 .await
2078 .unwrap();
2079 } else {
2080 let old_path = {
2081 let file_path = files.choose(rng);
2082 let dir_path = dirs[1..].choose(rng);
2083 file_path.into_iter().chain(dir_path).choose(rng).unwrap()
2084 };
2085
2086 let is_rename = rng.gen();
2087 if is_rename {
2088 let new_path_parent = dirs
2089 .iter()
2090 .filter(|d| !d.starts_with(old_path))
2091 .choose(rng)
2092 .unwrap();
2093
2094 let overwrite_existing_dir =
2095 !old_path.starts_with(new_path_parent) && rng.gen_bool(0.3);
2096 let new_path = if overwrite_existing_dir {
2097 fs.remove_dir(
2098 new_path_parent,
2099 RemoveOptions {
2100 recursive: true,
2101 ignore_if_not_exists: true,
2102 },
2103 )
2104 .await
2105 .unwrap();
2106 new_path_parent.to_path_buf()
2107 } else {
2108 new_path_parent.join(random_filename(rng))
2109 };
2110
2111 log::info!(
2112 "renaming {:?} to {}{:?}",
2113 old_path.strip_prefix(root_path).unwrap(),
2114 if overwrite_existing_dir {
2115 "overwrite "
2116 } else {
2117 ""
2118 },
2119 new_path.strip_prefix(root_path).unwrap()
2120 );
2121 fs.rename(
2122 old_path,
2123 &new_path,
2124 fs::RenameOptions {
2125 overwrite: true,
2126 ignore_if_exists: true,
2127 },
2128 )
2129 .await
2130 .unwrap();
2131 } else if fs.is_file(old_path).await {
2132 log::info!(
2133 "deleting file {:?}",
2134 old_path.strip_prefix(root_path).unwrap()
2135 );
2136 fs.remove_file(old_path, Default::default()).await.unwrap();
2137 } else {
2138 log::info!(
2139 "deleting dir {:?}",
2140 old_path.strip_prefix(root_path).unwrap()
2141 );
2142 fs.remove_dir(
2143 old_path,
2144 RemoveOptions {
2145 recursive: true,
2146 ignore_if_not_exists: true,
2147 },
2148 )
2149 .await
2150 .unwrap();
2151 }
2152 }
2153}
2154
2155fn random_filename(rng: &mut impl Rng) -> String {
2156 (0..6)
2157 .map(|_| rng.sample(rand::distributions::Alphanumeric))
2158 .map(char::from)
2159 .collect()
2160}
2161
2162const CONFLICT: FileStatus = FileStatus::Unmerged(UnmergedStatus {
2163 first_head: UnmergedStatusCode::Updated,
2164 second_head: UnmergedStatusCode::Updated,
2165});
2166
2167// NOTE:
2168// This test always fails on Windows, because on Windows, unlike on Unix, you can't rename
2169// a directory which some program has already open.
2170// This is a limitation of the Windows.
2171// See: https://stackoverflow.com/questions/41365318/access-is-denied-when-renaming-folder
2172#[gpui::test]
2173#[cfg_attr(target_os = "windows", ignore)]
2174async fn test_rename_work_directory(cx: &mut TestAppContext) {
2175 init_test(cx);
2176 cx.executor().allow_parking();
2177 let root = TempTree::new(json!({
2178 "projects": {
2179 "project1": {
2180 "a": "",
2181 "b": "",
2182 }
2183 },
2184
2185 }));
2186 let root_path = root.path();
2187
2188 let tree = Worktree::local(
2189 root_path,
2190 true,
2191 Arc::new(RealFs::default()),
2192 Default::default(),
2193 &mut cx.to_async(),
2194 )
2195 .await
2196 .unwrap();
2197
2198 let repo = git_init(&root_path.join("projects/project1"));
2199 git_add("a", &repo);
2200 git_commit("init", &repo);
2201 std::fs::write(root_path.join("projects/project1/a"), "aa").unwrap();
2202
2203 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
2204 .await;
2205
2206 tree.flush_fs_events(cx).await;
2207
2208 cx.read(|cx| {
2209 let tree = tree.read(cx);
2210 let repo = tree.repositories().iter().next().unwrap();
2211 assert_eq!(
2212 repo.work_directory,
2213 WorkDirectory::in_project("projects/project1")
2214 );
2215 assert_eq!(
2216 tree.status_for_file(Path::new("projects/project1/a")),
2217 Some(StatusCode::Modified.worktree()),
2218 );
2219 assert_eq!(
2220 tree.status_for_file(Path::new("projects/project1/b")),
2221 Some(FileStatus::Untracked),
2222 );
2223 });
2224
2225 std::fs::rename(
2226 root_path.join("projects/project1"),
2227 root_path.join("projects/project2"),
2228 )
2229 .unwrap();
2230 tree.flush_fs_events(cx).await;
2231
2232 cx.read(|cx| {
2233 let tree = tree.read(cx);
2234 let repo = tree.repositories().iter().next().unwrap();
2235 assert_eq!(
2236 repo.work_directory,
2237 WorkDirectory::in_project("projects/project2")
2238 );
2239 assert_eq!(
2240 tree.status_for_file(Path::new("projects/project2/a")),
2241 Some(StatusCode::Modified.worktree()),
2242 );
2243 assert_eq!(
2244 tree.status_for_file(Path::new("projects/project2/b")),
2245 Some(FileStatus::Untracked),
2246 );
2247 });
2248}
2249
2250#[gpui::test]
2251async fn test_home_dir_as_git_repository(cx: &mut TestAppContext) {
2252 init_test(cx);
2253 cx.executor().allow_parking();
2254 let fs = FakeFs::new(cx.background_executor.clone());
2255 fs.insert_tree(
2256 "/root",
2257 json!({
2258 "home": {
2259 ".git": {},
2260 "project": {
2261 "a.txt": "A"
2262 },
2263 },
2264 }),
2265 )
2266 .await;
2267 fs.set_home_dir(Path::new(path!("/root/home")).to_owned());
2268
2269 let tree = Worktree::local(
2270 Path::new(path!("/root/home/project")),
2271 true,
2272 fs.clone(),
2273 Default::default(),
2274 &mut cx.to_async(),
2275 )
2276 .await
2277 .unwrap();
2278
2279 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
2280 .await;
2281 tree.flush_fs_events(cx).await;
2282
2283 tree.read_with(cx, |tree, _cx| {
2284 let tree = tree.as_local().unwrap();
2285
2286 let repo = tree.repository_for_path(path!("a.txt").as_ref());
2287 assert!(repo.is_none());
2288 });
2289
2290 let home_tree = Worktree::local(
2291 Path::new(path!("/root/home")),
2292 true,
2293 fs.clone(),
2294 Default::default(),
2295 &mut cx.to_async(),
2296 )
2297 .await
2298 .unwrap();
2299
2300 cx.read(|cx| home_tree.read(cx).as_local().unwrap().scan_complete())
2301 .await;
2302 home_tree.flush_fs_events(cx).await;
2303
2304 home_tree.read_with(cx, |home_tree, _cx| {
2305 let home_tree = home_tree.as_local().unwrap();
2306
2307 let repo = home_tree.repository_for_path(path!("project/a.txt").as_ref());
2308 assert_eq!(
2309 repo.map(|repo| &repo.work_directory),
2310 Some(&WorkDirectory::InProject {
2311 relative_path: Path::new("").into()
2312 })
2313 );
2314 })
2315}
2316
2317#[gpui::test]
2318async fn test_git_repository_for_path(cx: &mut TestAppContext) {
2319 init_test(cx);
2320 cx.executor().allow_parking();
2321 let root = TempTree::new(json!({
2322 "c.txt": "",
2323 "dir1": {
2324 ".git": {},
2325 "deps": {
2326 "dep1": {
2327 ".git": {},
2328 "src": {
2329 "a.txt": ""
2330 }
2331 }
2332 },
2333 "src": {
2334 "b.txt": ""
2335 }
2336 },
2337 }));
2338
2339 let tree = Worktree::local(
2340 root.path(),
2341 true,
2342 Arc::new(RealFs::default()),
2343 Default::default(),
2344 &mut cx.to_async(),
2345 )
2346 .await
2347 .unwrap();
2348
2349 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
2350 .await;
2351 tree.flush_fs_events(cx).await;
2352
2353 tree.read_with(cx, |tree, _cx| {
2354 let tree = tree.as_local().unwrap();
2355
2356 assert!(tree.repository_for_path("c.txt".as_ref()).is_none());
2357
2358 let repo = tree.repository_for_path("dir1/src/b.txt".as_ref()).unwrap();
2359 assert_eq!(repo.work_directory, WorkDirectory::in_project("dir1"));
2360
2361 let repo = tree
2362 .repository_for_path("dir1/deps/dep1/src/a.txt".as_ref())
2363 .unwrap();
2364 assert_eq!(
2365 repo.work_directory,
2366 WorkDirectory::in_project("dir1/deps/dep1")
2367 );
2368
2369 let entries = tree.files(false, 0);
2370
2371 let paths_with_repos = tree
2372 .entries_with_repositories(entries)
2373 .map(|(entry, repo)| {
2374 (
2375 entry.path.as_ref(),
2376 repo.map(|repo| repo.work_directory.clone()),
2377 )
2378 })
2379 .collect::<Vec<_>>();
2380
2381 assert_eq!(
2382 paths_with_repos,
2383 &[
2384 (Path::new("c.txt"), None),
2385 (
2386 Path::new("dir1/deps/dep1/src/a.txt"),
2387 Some(WorkDirectory::in_project("dir1/deps/dep1"))
2388 ),
2389 (
2390 Path::new("dir1/src/b.txt"),
2391 Some(WorkDirectory::in_project("dir1"))
2392 ),
2393 ]
2394 );
2395 });
2396
2397 let repo_update_events = Arc::new(Mutex::new(vec![]));
2398 tree.update(cx, |_, cx| {
2399 let repo_update_events = repo_update_events.clone();
2400 cx.subscribe(&tree, move |_, _, event, _| {
2401 if let Event::UpdatedGitRepositories(update) = event {
2402 repo_update_events.lock().push(update.clone());
2403 }
2404 })
2405 .detach();
2406 });
2407
2408 std::fs::write(root.path().join("dir1/.git/random_new_file"), "hello").unwrap();
2409 tree.flush_fs_events(cx).await;
2410
2411 assert_eq!(
2412 repo_update_events.lock()[0]
2413 .iter()
2414 .map(|e| e.0.clone())
2415 .collect::<Vec<Arc<Path>>>(),
2416 vec![Path::new("dir1").into()]
2417 );
2418
2419 std::fs::remove_dir_all(root.path().join("dir1/.git")).unwrap();
2420 tree.flush_fs_events(cx).await;
2421
2422 tree.read_with(cx, |tree, _cx| {
2423 let tree = tree.as_local().unwrap();
2424
2425 assert!(tree
2426 .repository_for_path("dir1/src/b.txt".as_ref())
2427 .is_none());
2428 });
2429}
2430
2431// NOTE:
2432// This test always fails on Windows, because on Windows, unlike on Unix, you can't rename
2433// a directory which some program has already open.
2434// This is a limitation of the Windows.
2435// See: https://stackoverflow.com/questions/41365318/access-is-denied-when-renaming-folder
2436#[gpui::test]
2437#[cfg_attr(target_os = "windows", ignore)]
2438async fn test_file_status(cx: &mut TestAppContext) {
2439 init_test(cx);
2440 cx.executor().allow_parking();
2441 const IGNORE_RULE: &str = "**/target";
2442
2443 let root = TempTree::new(json!({
2444 "project": {
2445 "a.txt": "a",
2446 "b.txt": "bb",
2447 "c": {
2448 "d": {
2449 "e.txt": "eee"
2450 }
2451 },
2452 "f.txt": "ffff",
2453 "target": {
2454 "build_file": "???"
2455 },
2456 ".gitignore": IGNORE_RULE
2457 },
2458
2459 }));
2460
2461 const A_TXT: &str = "a.txt";
2462 const B_TXT: &str = "b.txt";
2463 const E_TXT: &str = "c/d/e.txt";
2464 const F_TXT: &str = "f.txt";
2465 const DOTGITIGNORE: &str = ".gitignore";
2466 const BUILD_FILE: &str = "target/build_file";
2467 let project_path = Path::new("project");
2468
2469 // Set up git repository before creating the worktree.
2470 let work_dir = root.path().join("project");
2471 let mut repo = git_init(work_dir.as_path());
2472 repo.add_ignore_rule(IGNORE_RULE).unwrap();
2473 git_add(A_TXT, &repo);
2474 git_add(E_TXT, &repo);
2475 git_add(DOTGITIGNORE, &repo);
2476 git_commit("Initial commit", &repo);
2477
2478 let tree = Worktree::local(
2479 root.path(),
2480 true,
2481 Arc::new(RealFs::default()),
2482 Default::default(),
2483 &mut cx.to_async(),
2484 )
2485 .await
2486 .unwrap();
2487
2488 tree.flush_fs_events(cx).await;
2489 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
2490 .await;
2491 cx.executor().run_until_parked();
2492
2493 // Check that the right git state is observed on startup
2494 tree.read_with(cx, |tree, _cx| {
2495 let snapshot = tree.snapshot();
2496 assert_eq!(snapshot.repositories().iter().count(), 1);
2497 let repo_entry = snapshot.repositories().iter().next().unwrap();
2498 assert_eq!(
2499 repo_entry.work_directory,
2500 WorkDirectory::in_project("project")
2501 );
2502
2503 assert_eq!(
2504 snapshot.status_for_file(project_path.join(B_TXT)),
2505 Some(FileStatus::Untracked),
2506 );
2507 assert_eq!(
2508 snapshot.status_for_file(project_path.join(F_TXT)),
2509 Some(FileStatus::Untracked),
2510 );
2511 });
2512
2513 // Modify a file in the working copy.
2514 std::fs::write(work_dir.join(A_TXT), "aa").unwrap();
2515 tree.flush_fs_events(cx).await;
2516 cx.executor().run_until_parked();
2517
2518 // The worktree detects that the file's git status has changed.
2519 tree.read_with(cx, |tree, _cx| {
2520 let snapshot = tree.snapshot();
2521 assert_eq!(
2522 snapshot.status_for_file(project_path.join(A_TXT)),
2523 Some(StatusCode::Modified.worktree()),
2524 );
2525 });
2526
2527 // Create a commit in the git repository.
2528 git_add(A_TXT, &repo);
2529 git_add(B_TXT, &repo);
2530 git_commit("Committing modified and added", &repo);
2531 tree.flush_fs_events(cx).await;
2532 cx.executor().run_until_parked();
2533
2534 // The worktree detects that the files' git status have changed.
2535 tree.read_with(cx, |tree, _cx| {
2536 let snapshot = tree.snapshot();
2537 assert_eq!(
2538 snapshot.status_for_file(project_path.join(F_TXT)),
2539 Some(FileStatus::Untracked),
2540 );
2541 assert_eq!(snapshot.status_for_file(project_path.join(B_TXT)), None);
2542 assert_eq!(snapshot.status_for_file(project_path.join(A_TXT)), None);
2543 });
2544
2545 // Modify files in the working copy and perform git operations on other files.
2546 git_reset(0, &repo);
2547 git_remove_index(Path::new(B_TXT), &repo);
2548 git_stash(&mut repo);
2549 std::fs::write(work_dir.join(E_TXT), "eeee").unwrap();
2550 std::fs::write(work_dir.join(BUILD_FILE), "this should be ignored").unwrap();
2551 tree.flush_fs_events(cx).await;
2552 cx.executor().run_until_parked();
2553
2554 // Check that more complex repo changes are tracked
2555 tree.read_with(cx, |tree, _cx| {
2556 let snapshot = tree.snapshot();
2557
2558 assert_eq!(snapshot.status_for_file(project_path.join(A_TXT)), None);
2559 assert_eq!(
2560 snapshot.status_for_file(project_path.join(B_TXT)),
2561 Some(FileStatus::Untracked),
2562 );
2563 assert_eq!(
2564 snapshot.status_for_file(project_path.join(E_TXT)),
2565 Some(StatusCode::Modified.worktree()),
2566 );
2567 });
2568
2569 std::fs::remove_file(work_dir.join(B_TXT)).unwrap();
2570 std::fs::remove_dir_all(work_dir.join("c")).unwrap();
2571 std::fs::write(
2572 work_dir.join(DOTGITIGNORE),
2573 [IGNORE_RULE, "f.txt"].join("\n"),
2574 )
2575 .unwrap();
2576
2577 git_add(Path::new(DOTGITIGNORE), &repo);
2578 git_commit("Committing modified git ignore", &repo);
2579
2580 tree.flush_fs_events(cx).await;
2581 cx.executor().run_until_parked();
2582
2583 let mut renamed_dir_name = "first_directory/second_directory";
2584 const RENAMED_FILE: &str = "rf.txt";
2585
2586 std::fs::create_dir_all(work_dir.join(renamed_dir_name)).unwrap();
2587 std::fs::write(
2588 work_dir.join(renamed_dir_name).join(RENAMED_FILE),
2589 "new-contents",
2590 )
2591 .unwrap();
2592
2593 tree.flush_fs_events(cx).await;
2594 cx.executor().run_until_parked();
2595
2596 tree.read_with(cx, |tree, _cx| {
2597 let snapshot = tree.snapshot();
2598 assert_eq!(
2599 snapshot.status_for_file(project_path.join(renamed_dir_name).join(RENAMED_FILE)),
2600 Some(FileStatus::Untracked),
2601 );
2602 });
2603
2604 renamed_dir_name = "new_first_directory/second_directory";
2605
2606 std::fs::rename(
2607 work_dir.join("first_directory"),
2608 work_dir.join("new_first_directory"),
2609 )
2610 .unwrap();
2611
2612 tree.flush_fs_events(cx).await;
2613 cx.executor().run_until_parked();
2614
2615 tree.read_with(cx, |tree, _cx| {
2616 let snapshot = tree.snapshot();
2617
2618 assert_eq!(
2619 snapshot.status_for_file(
2620 project_path
2621 .join(Path::new(renamed_dir_name))
2622 .join(RENAMED_FILE)
2623 ),
2624 Some(FileStatus::Untracked),
2625 );
2626 });
2627}
2628
2629// TODO: Fix flaky test.
2630// #[gpui::test]
2631#[allow(unused)]
2632async fn test_git_repository_status(cx: &mut TestAppContext) {
2633 init_test(cx);
2634 cx.executor().allow_parking();
2635
2636 let root = TempTree::new(json!({
2637 "project": {
2638 "a.txt": "a", // Modified
2639 "b.txt": "bb", // Added
2640 "c.txt": "ccc", // Unchanged
2641 "d.txt": "dddd", // Deleted
2642 },
2643
2644 }));
2645
2646 // Set up git repository before creating the worktree.
2647 let work_dir = root.path().join("project");
2648 let repo = git_init(work_dir.as_path());
2649 git_add("a.txt", &repo);
2650 git_add("c.txt", &repo);
2651 git_add("d.txt", &repo);
2652 git_commit("Initial commit", &repo);
2653 std::fs::remove_file(work_dir.join("d.txt")).unwrap();
2654 std::fs::write(work_dir.join("a.txt"), "aa").unwrap();
2655
2656 let tree = Worktree::local(
2657 root.path(),
2658 true,
2659 Arc::new(RealFs::default()),
2660 Default::default(),
2661 &mut cx.to_async(),
2662 )
2663 .await
2664 .unwrap();
2665
2666 tree.flush_fs_events(cx).await;
2667 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
2668 .await;
2669 cx.executor().run_until_parked();
2670
2671 // Check that the right git state is observed on startup
2672 tree.read_with(cx, |tree, _cx| {
2673 let snapshot = tree.snapshot();
2674 let repo = snapshot.repositories().iter().next().unwrap();
2675 let entries = repo.status().collect::<Vec<_>>();
2676
2677 assert_eq!(entries.len(), 3);
2678 assert_eq!(entries[0].repo_path.as_ref(), Path::new("a.txt"));
2679 assert_eq!(entries[0].status, StatusCode::Modified.worktree());
2680 assert_eq!(entries[1].repo_path.as_ref(), Path::new("b.txt"));
2681 assert_eq!(entries[1].status, FileStatus::Untracked);
2682 assert_eq!(entries[2].repo_path.as_ref(), Path::new("d.txt"));
2683 assert_eq!(entries[2].status, StatusCode::Deleted.worktree());
2684 });
2685
2686 std::fs::write(work_dir.join("c.txt"), "some changes").unwrap();
2687 eprintln!("File c.txt has been modified");
2688
2689 tree.flush_fs_events(cx).await;
2690 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
2691 .await;
2692 cx.executor().run_until_parked();
2693
2694 tree.read_with(cx, |tree, _cx| {
2695 let snapshot = tree.snapshot();
2696 let repository = snapshot.repositories().iter().next().unwrap();
2697 let entries = repository.status().collect::<Vec<_>>();
2698
2699 std::assert_eq!(entries.len(), 4, "entries: {entries:?}");
2700 assert_eq!(entries[0].repo_path.as_ref(), Path::new("a.txt"));
2701 assert_eq!(entries[0].status, StatusCode::Modified.worktree());
2702 assert_eq!(entries[1].repo_path.as_ref(), Path::new("b.txt"));
2703 assert_eq!(entries[1].status, FileStatus::Untracked);
2704 // Status updated
2705 assert_eq!(entries[2].repo_path.as_ref(), Path::new("c.txt"));
2706 assert_eq!(entries[2].status, StatusCode::Modified.worktree());
2707 assert_eq!(entries[3].repo_path.as_ref(), Path::new("d.txt"));
2708 assert_eq!(entries[3].status, StatusCode::Deleted.worktree());
2709 });
2710
2711 git_add("a.txt", &repo);
2712 git_add("c.txt", &repo);
2713 git_remove_index(Path::new("d.txt"), &repo);
2714 git_commit("Another commit", &repo);
2715 tree.flush_fs_events(cx).await;
2716 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
2717 .await;
2718 cx.executor().run_until_parked();
2719
2720 std::fs::remove_file(work_dir.join("a.txt")).unwrap();
2721 std::fs::remove_file(work_dir.join("b.txt")).unwrap();
2722 tree.flush_fs_events(cx).await;
2723 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
2724 .await;
2725 cx.executor().run_until_parked();
2726
2727 tree.read_with(cx, |tree, _cx| {
2728 let snapshot = tree.snapshot();
2729 let repo = snapshot.repositories().iter().next().unwrap();
2730 let entries = repo.status().collect::<Vec<_>>();
2731
2732 // Deleting an untracked entry, b.txt, should leave no status
2733 // a.txt was tracked, and so should have a status
2734 assert_eq!(
2735 entries.len(),
2736 1,
2737 "Entries length was incorrect\n{:#?}",
2738 &entries
2739 );
2740 assert_eq!(entries[0].repo_path.as_ref(), Path::new("a.txt"));
2741 assert_eq!(entries[0].status, StatusCode::Deleted.worktree());
2742 });
2743}
2744
2745// TODO: Fix flaky test.
2746// #[gpui::test]
2747#[allow(unused)]
2748async fn test_git_status_postprocessing(cx: &mut TestAppContext) {
2749 init_test(cx);
2750 cx.executor().allow_parking();
2751
2752 let root = TempTree::new(json!({
2753 "project": {
2754 "sub": {},
2755 "a.txt": "",
2756 },
2757 }));
2758
2759 let work_dir = root.path().join("project");
2760 let repo = git_init(work_dir.as_path());
2761 // a.txt exists in HEAD and the working copy but is deleted in the index.
2762 git_add("a.txt", &repo);
2763 git_commit("Initial commit", &repo);
2764 git_remove_index("a.txt".as_ref(), &repo);
2765 // `sub` is a nested git repository.
2766 let _sub = git_init(&work_dir.join("sub"));
2767
2768 let tree = Worktree::local(
2769 root.path(),
2770 true,
2771 Arc::new(RealFs::default()),
2772 Default::default(),
2773 &mut cx.to_async(),
2774 )
2775 .await
2776 .unwrap();
2777
2778 tree.flush_fs_events(cx).await;
2779 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
2780 .await;
2781 cx.executor().run_until_parked();
2782
2783 tree.read_with(cx, |tree, _cx| {
2784 let snapshot = tree.snapshot();
2785 let repo = snapshot.repositories().iter().next().unwrap();
2786 let entries = repo.status().collect::<Vec<_>>();
2787
2788 // `sub` doesn't appear in our computed statuses.
2789 assert_eq!(entries.len(), 1);
2790 assert_eq!(entries[0].repo_path.as_ref(), Path::new("a.txt"));
2791 // a.txt appears with a combined `DA` status.
2792 assert_eq!(
2793 entries[0].status,
2794 TrackedStatus {
2795 index_status: StatusCode::Deleted,
2796 worktree_status: StatusCode::Added
2797 }
2798 .into()
2799 );
2800 });
2801}
2802
2803#[gpui::test]
2804async fn test_repository_subfolder_git_status(cx: &mut TestAppContext) {
2805 init_test(cx);
2806 cx.executor().allow_parking();
2807
2808 let root = TempTree::new(json!({
2809 "my-repo": {
2810 // .git folder will go here
2811 "a.txt": "a",
2812 "sub-folder-1": {
2813 "sub-folder-2": {
2814 "c.txt": "cc",
2815 "d": {
2816 "e.txt": "eee"
2817 }
2818 },
2819 }
2820 },
2821
2822 }));
2823
2824 const C_TXT: &str = "sub-folder-1/sub-folder-2/c.txt";
2825 const E_TXT: &str = "sub-folder-1/sub-folder-2/d/e.txt";
2826
2827 // Set up git repository before creating the worktree.
2828 let git_repo_work_dir = root.path().join("my-repo");
2829 let repo = git_init(git_repo_work_dir.as_path());
2830 git_add(C_TXT, &repo);
2831 git_commit("Initial commit", &repo);
2832
2833 // Open the worktree in subfolder
2834 let project_root = Path::new("my-repo/sub-folder-1/sub-folder-2");
2835 let tree = Worktree::local(
2836 root.path().join(project_root),
2837 true,
2838 Arc::new(RealFs::default()),
2839 Default::default(),
2840 &mut cx.to_async(),
2841 )
2842 .await
2843 .unwrap();
2844
2845 tree.flush_fs_events(cx).await;
2846 tree.flush_fs_events_in_root_git_repository(cx).await;
2847 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
2848 .await;
2849 cx.executor().run_until_parked();
2850
2851 // Ensure that the git status is loaded correctly
2852 tree.read_with(cx, |tree, _cx| {
2853 let snapshot = tree.snapshot();
2854 assert_eq!(snapshot.repositories().iter().count(), 1);
2855 let repo = snapshot.repositories().iter().next().unwrap();
2856 assert_eq!(
2857 repo.work_directory.canonicalize(),
2858 WorkDirectory::AboveProject {
2859 absolute_path: Arc::from(root.path().join("my-repo").canonicalize().unwrap()),
2860 location_in_repo: Arc::from(Path::new(util::separator!(
2861 "sub-folder-1/sub-folder-2"
2862 )))
2863 }
2864 );
2865
2866 assert_eq!(snapshot.status_for_file("c.txt"), None);
2867 assert_eq!(
2868 snapshot.status_for_file("d/e.txt"),
2869 Some(FileStatus::Untracked)
2870 );
2871 });
2872
2873 // Now we simulate FS events, but ONLY in the .git folder that's outside
2874 // of out project root.
2875 // Meaning: we don't produce any FS events for files inside the project.
2876 git_add(E_TXT, &repo);
2877 git_commit("Second commit", &repo);
2878 tree.flush_fs_events_in_root_git_repository(cx).await;
2879 cx.executor().run_until_parked();
2880
2881 tree.read_with(cx, |tree, _cx| {
2882 let snapshot = tree.snapshot();
2883
2884 assert!(snapshot.repositories().iter().next().is_some());
2885
2886 assert_eq!(snapshot.status_for_file("c.txt"), None);
2887 assert_eq!(snapshot.status_for_file("d/e.txt"), None);
2888 });
2889}
2890
2891#[gpui::test]
2892async fn test_traverse_with_git_status(cx: &mut TestAppContext) {
2893 init_test(cx);
2894 let fs = FakeFs::new(cx.background_executor.clone());
2895 fs.insert_tree(
2896 "/root",
2897 json!({
2898 "x": {
2899 ".git": {},
2900 "x1.txt": "foo",
2901 "x2.txt": "bar",
2902 "y": {
2903 ".git": {},
2904 "y1.txt": "baz",
2905 "y2.txt": "qux"
2906 },
2907 "z.txt": "sneaky..."
2908 },
2909 "z": {
2910 ".git": {},
2911 "z1.txt": "quux",
2912 "z2.txt": "quuux"
2913 }
2914 }),
2915 )
2916 .await;
2917
2918 fs.set_status_for_repo_via_git_operation(
2919 Path::new("/root/x/.git"),
2920 &[
2921 (Path::new("x2.txt"), StatusCode::Modified.index()),
2922 (Path::new("z.txt"), StatusCode::Added.index()),
2923 ],
2924 );
2925 fs.set_status_for_repo_via_git_operation(
2926 Path::new("/root/x/y/.git"),
2927 &[(Path::new("y1.txt"), CONFLICT)],
2928 );
2929 fs.set_status_for_repo_via_git_operation(
2930 Path::new("/root/z/.git"),
2931 &[(Path::new("z2.txt"), StatusCode::Added.index())],
2932 );
2933
2934 let tree = Worktree::local(
2935 Path::new("/root"),
2936 true,
2937 fs.clone(),
2938 Default::default(),
2939 &mut cx.to_async(),
2940 )
2941 .await
2942 .unwrap();
2943
2944 tree.flush_fs_events(cx).await;
2945 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
2946 .await;
2947 cx.executor().run_until_parked();
2948
2949 let snapshot = tree.read_with(cx, |tree, _| tree.snapshot());
2950
2951 let mut traversal = snapshot
2952 .traverse_from_path(true, false, true, Path::new("x"))
2953 .with_git_statuses();
2954
2955 let entry = traversal.next().unwrap();
2956 assert_eq!(entry.path.as_ref(), Path::new("x/x1.txt"));
2957 assert_eq!(entry.git_summary, GitSummary::UNCHANGED);
2958 let entry = traversal.next().unwrap();
2959 assert_eq!(entry.path.as_ref(), Path::new("x/x2.txt"));
2960 assert_eq!(entry.git_summary, MODIFIED);
2961 let entry = traversal.next().unwrap();
2962 assert_eq!(entry.path.as_ref(), Path::new("x/y/y1.txt"));
2963 assert_eq!(entry.git_summary, GitSummary::CONFLICT);
2964 let entry = traversal.next().unwrap();
2965 assert_eq!(entry.path.as_ref(), Path::new("x/y/y2.txt"));
2966 assert_eq!(entry.git_summary, GitSummary::UNCHANGED);
2967 let entry = traversal.next().unwrap();
2968 assert_eq!(entry.path.as_ref(), Path::new("x/z.txt"));
2969 assert_eq!(entry.git_summary, ADDED);
2970 let entry = traversal.next().unwrap();
2971 assert_eq!(entry.path.as_ref(), Path::new("z/z1.txt"));
2972 assert_eq!(entry.git_summary, GitSummary::UNCHANGED);
2973 let entry = traversal.next().unwrap();
2974 assert_eq!(entry.path.as_ref(), Path::new("z/z2.txt"));
2975 assert_eq!(entry.git_summary, ADDED);
2976}
2977
2978#[gpui::test]
2979async fn test_propagate_git_statuses(cx: &mut TestAppContext) {
2980 init_test(cx);
2981 let fs = FakeFs::new(cx.background_executor.clone());
2982 fs.insert_tree(
2983 "/root",
2984 json!({
2985 ".git": {},
2986 "a": {
2987 "b": {
2988 "c1.txt": "",
2989 "c2.txt": "",
2990 },
2991 "d": {
2992 "e1.txt": "",
2993 "e2.txt": "",
2994 "e3.txt": "",
2995 }
2996 },
2997 "f": {
2998 "no-status.txt": ""
2999 },
3000 "g": {
3001 "h1.txt": "",
3002 "h2.txt": ""
3003 },
3004 }),
3005 )
3006 .await;
3007
3008 fs.set_status_for_repo_via_git_operation(
3009 Path::new("/root/.git"),
3010 &[
3011 (Path::new("a/b/c1.txt"), StatusCode::Added.index()),
3012 (Path::new("a/d/e2.txt"), StatusCode::Modified.index()),
3013 (Path::new("g/h2.txt"), CONFLICT),
3014 ],
3015 );
3016
3017 let tree = Worktree::local(
3018 Path::new("/root"),
3019 true,
3020 fs.clone(),
3021 Default::default(),
3022 &mut cx.to_async(),
3023 )
3024 .await
3025 .unwrap();
3026
3027 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3028 .await;
3029
3030 cx.executor().run_until_parked();
3031 let snapshot = tree.read_with(cx, |tree, _| tree.snapshot());
3032
3033 check_git_statuses(
3034 &snapshot,
3035 &[
3036 (Path::new(""), GitSummary::CONFLICT + MODIFIED + ADDED),
3037 (Path::new("g"), GitSummary::CONFLICT),
3038 (Path::new("g/h2.txt"), GitSummary::CONFLICT),
3039 ],
3040 );
3041
3042 check_git_statuses(
3043 &snapshot,
3044 &[
3045 (Path::new(""), GitSummary::CONFLICT + ADDED + MODIFIED),
3046 (Path::new("a"), ADDED + MODIFIED),
3047 (Path::new("a/b"), ADDED),
3048 (Path::new("a/b/c1.txt"), ADDED),
3049 (Path::new("a/b/c2.txt"), GitSummary::UNCHANGED),
3050 (Path::new("a/d"), MODIFIED),
3051 (Path::new("a/d/e2.txt"), MODIFIED),
3052 (Path::new("f"), GitSummary::UNCHANGED),
3053 (Path::new("f/no-status.txt"), GitSummary::UNCHANGED),
3054 (Path::new("g"), GitSummary::CONFLICT),
3055 (Path::new("g/h2.txt"), GitSummary::CONFLICT),
3056 ],
3057 );
3058
3059 check_git_statuses(
3060 &snapshot,
3061 &[
3062 (Path::new("a/b"), ADDED),
3063 (Path::new("a/b/c1.txt"), ADDED),
3064 (Path::new("a/b/c2.txt"), GitSummary::UNCHANGED),
3065 (Path::new("a/d"), MODIFIED),
3066 (Path::new("a/d/e1.txt"), GitSummary::UNCHANGED),
3067 (Path::new("a/d/e2.txt"), MODIFIED),
3068 (Path::new("f"), GitSummary::UNCHANGED),
3069 (Path::new("f/no-status.txt"), GitSummary::UNCHANGED),
3070 (Path::new("g"), GitSummary::CONFLICT),
3071 ],
3072 );
3073
3074 check_git_statuses(
3075 &snapshot,
3076 &[
3077 (Path::new("a/b/c1.txt"), ADDED),
3078 (Path::new("a/b/c2.txt"), GitSummary::UNCHANGED),
3079 (Path::new("a/d/e1.txt"), GitSummary::UNCHANGED),
3080 (Path::new("a/d/e2.txt"), MODIFIED),
3081 (Path::new("f/no-status.txt"), GitSummary::UNCHANGED),
3082 ],
3083 );
3084}
3085
3086#[gpui::test]
3087async fn test_propagate_statuses_for_repos_under_project(cx: &mut TestAppContext) {
3088 init_test(cx);
3089 let fs = FakeFs::new(cx.background_executor.clone());
3090 fs.insert_tree(
3091 "/root",
3092 json!({
3093 "x": {
3094 ".git": {},
3095 "x1.txt": "foo",
3096 "x2.txt": "bar"
3097 },
3098 "y": {
3099 ".git": {},
3100 "y1.txt": "baz",
3101 "y2.txt": "qux"
3102 },
3103 "z": {
3104 ".git": {},
3105 "z1.txt": "quux",
3106 "z2.txt": "quuux"
3107 }
3108 }),
3109 )
3110 .await;
3111
3112 fs.set_status_for_repo_via_git_operation(
3113 Path::new("/root/x/.git"),
3114 &[(Path::new("x1.txt"), StatusCode::Added.index())],
3115 );
3116 fs.set_status_for_repo_via_git_operation(
3117 Path::new("/root/y/.git"),
3118 &[
3119 (Path::new("y1.txt"), CONFLICT),
3120 (Path::new("y2.txt"), StatusCode::Modified.index()),
3121 ],
3122 );
3123 fs.set_status_for_repo_via_git_operation(
3124 Path::new("/root/z/.git"),
3125 &[(Path::new("z2.txt"), StatusCode::Modified.index())],
3126 );
3127
3128 let tree = Worktree::local(
3129 Path::new("/root"),
3130 true,
3131 fs.clone(),
3132 Default::default(),
3133 &mut cx.to_async(),
3134 )
3135 .await
3136 .unwrap();
3137
3138 tree.flush_fs_events(cx).await;
3139 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3140 .await;
3141 cx.executor().run_until_parked();
3142
3143 let snapshot = tree.read_with(cx, |tree, _| tree.snapshot());
3144
3145 check_git_statuses(
3146 &snapshot,
3147 &[(Path::new("x"), ADDED), (Path::new("x/x1.txt"), ADDED)],
3148 );
3149
3150 check_git_statuses(
3151 &snapshot,
3152 &[
3153 (Path::new("y"), GitSummary::CONFLICT + MODIFIED),
3154 (Path::new("y/y1.txt"), GitSummary::CONFLICT),
3155 (Path::new("y/y2.txt"), MODIFIED),
3156 ],
3157 );
3158
3159 check_git_statuses(
3160 &snapshot,
3161 &[
3162 (Path::new("z"), MODIFIED),
3163 (Path::new("z/z2.txt"), MODIFIED),
3164 ],
3165 );
3166
3167 check_git_statuses(
3168 &snapshot,
3169 &[(Path::new("x"), ADDED), (Path::new("x/x1.txt"), ADDED)],
3170 );
3171
3172 check_git_statuses(
3173 &snapshot,
3174 &[
3175 (Path::new("x"), ADDED),
3176 (Path::new("x/x1.txt"), ADDED),
3177 (Path::new("x/x2.txt"), GitSummary::UNCHANGED),
3178 (Path::new("y"), GitSummary::CONFLICT + MODIFIED),
3179 (Path::new("y/y1.txt"), GitSummary::CONFLICT),
3180 (Path::new("y/y2.txt"), MODIFIED),
3181 (Path::new("z"), MODIFIED),
3182 (Path::new("z/z1.txt"), GitSummary::UNCHANGED),
3183 (Path::new("z/z2.txt"), MODIFIED),
3184 ],
3185 );
3186}
3187
3188#[gpui::test]
3189async fn test_propagate_statuses_for_nested_repos(cx: &mut TestAppContext) {
3190 init_test(cx);
3191 let fs = FakeFs::new(cx.background_executor.clone());
3192 fs.insert_tree(
3193 "/root",
3194 json!({
3195 "x": {
3196 ".git": {},
3197 "x1.txt": "foo",
3198 "x2.txt": "bar",
3199 "y": {
3200 ".git": {},
3201 "y1.txt": "baz",
3202 "y2.txt": "qux"
3203 },
3204 "z.txt": "sneaky..."
3205 },
3206 "z": {
3207 ".git": {},
3208 "z1.txt": "quux",
3209 "z2.txt": "quuux"
3210 }
3211 }),
3212 )
3213 .await;
3214
3215 fs.set_status_for_repo_via_git_operation(
3216 Path::new("/root/x/.git"),
3217 &[
3218 (Path::new("x2.txt"), StatusCode::Modified.index()),
3219 (Path::new("z.txt"), StatusCode::Added.index()),
3220 ],
3221 );
3222 fs.set_status_for_repo_via_git_operation(
3223 Path::new("/root/x/y/.git"),
3224 &[(Path::new("y1.txt"), CONFLICT)],
3225 );
3226
3227 fs.set_status_for_repo_via_git_operation(
3228 Path::new("/root/z/.git"),
3229 &[(Path::new("z2.txt"), StatusCode::Added.index())],
3230 );
3231
3232 let tree = Worktree::local(
3233 Path::new("/root"),
3234 true,
3235 fs.clone(),
3236 Default::default(),
3237 &mut cx.to_async(),
3238 )
3239 .await
3240 .unwrap();
3241
3242 tree.flush_fs_events(cx).await;
3243 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3244 .await;
3245 cx.executor().run_until_parked();
3246
3247 let snapshot = tree.read_with(cx, |tree, _| tree.snapshot());
3248
3249 // Sanity check the propagation for x/y and z
3250 check_git_statuses(
3251 &snapshot,
3252 &[
3253 (Path::new("x/y"), GitSummary::CONFLICT),
3254 (Path::new("x/y/y1.txt"), GitSummary::CONFLICT),
3255 (Path::new("x/y/y2.txt"), GitSummary::UNCHANGED),
3256 ],
3257 );
3258 check_git_statuses(
3259 &snapshot,
3260 &[
3261 (Path::new("z"), ADDED),
3262 (Path::new("z/z1.txt"), GitSummary::UNCHANGED),
3263 (Path::new("z/z2.txt"), ADDED),
3264 ],
3265 );
3266
3267 // Test one of the fundamental cases of propagation blocking, the transition from one git repository to another
3268 check_git_statuses(
3269 &snapshot,
3270 &[
3271 (Path::new("x"), MODIFIED + ADDED),
3272 (Path::new("x/y"), GitSummary::CONFLICT),
3273 (Path::new("x/y/y1.txt"), GitSummary::CONFLICT),
3274 ],
3275 );
3276
3277 // Sanity check everything around it
3278 check_git_statuses(
3279 &snapshot,
3280 &[
3281 (Path::new("x"), MODIFIED + ADDED),
3282 (Path::new("x/x1.txt"), GitSummary::UNCHANGED),
3283 (Path::new("x/x2.txt"), MODIFIED),
3284 (Path::new("x/y"), GitSummary::CONFLICT),
3285 (Path::new("x/y/y1.txt"), GitSummary::CONFLICT),
3286 (Path::new("x/y/y2.txt"), GitSummary::UNCHANGED),
3287 (Path::new("x/z.txt"), ADDED),
3288 ],
3289 );
3290
3291 // Test the other fundamental case, transitioning from git repository to non-git repository
3292 check_git_statuses(
3293 &snapshot,
3294 &[
3295 (Path::new(""), GitSummary::UNCHANGED),
3296 (Path::new("x"), MODIFIED + ADDED),
3297 (Path::new("x/x1.txt"), GitSummary::UNCHANGED),
3298 ],
3299 );
3300
3301 // And all together now
3302 check_git_statuses(
3303 &snapshot,
3304 &[
3305 (Path::new(""), GitSummary::UNCHANGED),
3306 (Path::new("x"), MODIFIED + ADDED),
3307 (Path::new("x/x1.txt"), GitSummary::UNCHANGED),
3308 (Path::new("x/x2.txt"), MODIFIED),
3309 (Path::new("x/y"), GitSummary::CONFLICT),
3310 (Path::new("x/y/y1.txt"), GitSummary::CONFLICT),
3311 (Path::new("x/y/y2.txt"), GitSummary::UNCHANGED),
3312 (Path::new("x/z.txt"), ADDED),
3313 (Path::new("z"), ADDED),
3314 (Path::new("z/z1.txt"), GitSummary::UNCHANGED),
3315 (Path::new("z/z2.txt"), ADDED),
3316 ],
3317 );
3318}
3319
3320#[gpui::test]
3321async fn test_conflicted_cherry_pick(cx: &mut TestAppContext) {
3322 init_test(cx);
3323 cx.executor().allow_parking();
3324
3325 let root = TempTree::new(json!({
3326 "project": {
3327 "a.txt": "a",
3328 },
3329 }));
3330 let root_path = root.path();
3331
3332 let tree = Worktree::local(
3333 root_path,
3334 true,
3335 Arc::new(RealFs::default()),
3336 Default::default(),
3337 &mut cx.to_async(),
3338 )
3339 .await
3340 .unwrap();
3341
3342 let repo = git_init(&root_path.join("project"));
3343 git_add("a.txt", &repo);
3344 git_commit("init", &repo);
3345
3346 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3347 .await;
3348
3349 tree.flush_fs_events(cx).await;
3350
3351 git_branch("other-branch", &repo);
3352 git_checkout("refs/heads/other-branch", &repo);
3353 std::fs::write(root_path.join("project/a.txt"), "A").unwrap();
3354 git_add("a.txt", &repo);
3355 git_commit("capitalize", &repo);
3356 let commit = repo
3357 .head()
3358 .expect("Failed to get HEAD")
3359 .peel_to_commit()
3360 .expect("HEAD is not a commit");
3361 git_checkout("refs/heads/main", &repo);
3362 std::fs::write(root_path.join("project/a.txt"), "b").unwrap();
3363 git_add("a.txt", &repo);
3364 git_commit("improve letter", &repo);
3365 git_cherry_pick(&commit, &repo);
3366 std::fs::read_to_string(root_path.join("project/.git/CHERRY_PICK_HEAD"))
3367 .expect("No CHERRY_PICK_HEAD");
3368 pretty_assertions::assert_eq!(
3369 git_status(&repo),
3370 collections::HashMap::from_iter([("a.txt".to_owned(), git2::Status::CONFLICTED)])
3371 );
3372 tree.flush_fs_events(cx).await;
3373 let conflicts = tree.update(cx, |tree, _| {
3374 let entry = tree.git_entries().nth(0).expect("No git entry").clone();
3375 entry
3376 .current_merge_conflicts
3377 .iter()
3378 .cloned()
3379 .collect::<Vec<_>>()
3380 });
3381 pretty_assertions::assert_eq!(conflicts, [RepoPath::from("a.txt")]);
3382
3383 git_add("a.txt", &repo);
3384 // Attempt to manually simulate what `git cherry-pick --continue` would do.
3385 git_commit("whatevs", &repo);
3386 std::fs::remove_file(root.path().join("project/.git/CHERRY_PICK_HEAD"))
3387 .expect("Failed to remove CHERRY_PICK_HEAD");
3388 pretty_assertions::assert_eq!(git_status(&repo), collections::HashMap::default());
3389 tree.flush_fs_events(cx).await;
3390 let conflicts = tree.update(cx, |tree, _| {
3391 let entry = tree.git_entries().nth(0).expect("No git entry").clone();
3392 entry
3393 .current_merge_conflicts
3394 .iter()
3395 .cloned()
3396 .collect::<Vec<_>>()
3397 });
3398 pretty_assertions::assert_eq!(conflicts, []);
3399}
3400
3401#[gpui::test]
3402async fn test_private_single_file_worktree(cx: &mut TestAppContext) {
3403 init_test(cx);
3404 let fs = FakeFs::new(cx.background_executor.clone());
3405 fs.insert_tree("/", json!({".env": "PRIVATE=secret\n"}))
3406 .await;
3407 let tree = Worktree::local(
3408 Path::new("/.env"),
3409 true,
3410 fs.clone(),
3411 Default::default(),
3412 &mut cx.to_async(),
3413 )
3414 .await
3415 .unwrap();
3416 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3417 .await;
3418 tree.read_with(cx, |tree, _| {
3419 let entry = tree.entry_for_path("").unwrap();
3420 assert!(entry.is_private);
3421 });
3422}
3423
3424#[gpui::test]
3425fn test_unrelativize() {
3426 let work_directory = WorkDirectory::in_project("");
3427 pretty_assertions::assert_eq!(
3428 work_directory.try_unrelativize(&"crates/gpui/gpui.rs".into()),
3429 Some(Path::new("crates/gpui/gpui.rs").into())
3430 );
3431
3432 let work_directory = WorkDirectory::in_project("vendor/some-submodule");
3433 pretty_assertions::assert_eq!(
3434 work_directory.try_unrelativize(&"src/thing.c".into()),
3435 Some(Path::new("vendor/some-submodule/src/thing.c").into())
3436 );
3437
3438 let work_directory = WorkDirectory::AboveProject {
3439 absolute_path: Path::new("/projects/zed").into(),
3440 location_in_repo: Path::new("crates/gpui").into(),
3441 };
3442
3443 pretty_assertions::assert_eq!(
3444 work_directory.try_unrelativize(&"crates/util/util.rs".into()),
3445 None,
3446 );
3447
3448 pretty_assertions::assert_eq!(
3449 work_directory.unrelativize(&"crates/util/util.rs".into()),
3450 Path::new("../util/util.rs").into()
3451 );
3452
3453 pretty_assertions::assert_eq!(work_directory.try_unrelativize(&"README.md".into()), None,);
3454
3455 pretty_assertions::assert_eq!(
3456 work_directory.unrelativize(&"README.md".into()),
3457 Path::new("../../README.md").into()
3458 );
3459}
3460
3461#[track_caller]
3462fn check_git_statuses(snapshot: &Snapshot, expected_statuses: &[(&Path, GitSummary)]) {
3463 let mut traversal = snapshot
3464 .traverse_from_path(true, true, false, "".as_ref())
3465 .with_git_statuses();
3466 let found_statuses = expected_statuses
3467 .iter()
3468 .map(|&(path, _)| {
3469 let git_entry = traversal
3470 .find(|git_entry| &*git_entry.path == path)
3471 .unwrap_or_else(|| panic!("Traversal has no entry for {path:?}"));
3472 (path, git_entry.git_summary)
3473 })
3474 .collect::<Vec<_>>();
3475 assert_eq!(found_statuses, expected_statuses);
3476}
3477
3478const ADDED: GitSummary = GitSummary {
3479 index: TrackedSummary::ADDED,
3480 count: 1,
3481 ..GitSummary::UNCHANGED
3482};
3483const MODIFIED: GitSummary = GitSummary {
3484 index: TrackedSummary::MODIFIED,
3485 count: 1,
3486 ..GitSummary::UNCHANGED
3487};
3488
3489#[track_caller]
3490fn git_init(path: &Path) -> git2::Repository {
3491 let mut init_opts = RepositoryInitOptions::new();
3492 init_opts.initial_head("main");
3493 git2::Repository::init_opts(path, &init_opts).expect("Failed to initialize git repository")
3494}
3495
3496#[track_caller]
3497fn git_add<P: AsRef<Path>>(path: P, repo: &git2::Repository) {
3498 let path = path.as_ref();
3499 let mut index = repo.index().expect("Failed to get index");
3500 index.add_path(path).expect("Failed to add file");
3501 index.write().expect("Failed to write index");
3502}
3503
3504#[track_caller]
3505fn git_remove_index(path: &Path, repo: &git2::Repository) {
3506 let mut index = repo.index().expect("Failed to get index");
3507 index.remove_path(path).expect("Failed to add file");
3508 index.write().expect("Failed to write index");
3509}
3510
3511#[track_caller]
3512fn git_commit(msg: &'static str, repo: &git2::Repository) {
3513 use git2::Signature;
3514
3515 let signature = Signature::now("test", "test@zed.dev").unwrap();
3516 let oid = repo.index().unwrap().write_tree().unwrap();
3517 let tree = repo.find_tree(oid).unwrap();
3518 if let Ok(head) = repo.head() {
3519 let parent_obj = head.peel(git2::ObjectType::Commit).unwrap();
3520
3521 let parent_commit = parent_obj.as_commit().unwrap();
3522
3523 repo.commit(
3524 Some("HEAD"),
3525 &signature,
3526 &signature,
3527 msg,
3528 &tree,
3529 &[parent_commit],
3530 )
3531 .expect("Failed to commit with parent");
3532 } else {
3533 repo.commit(Some("HEAD"), &signature, &signature, msg, &tree, &[])
3534 .expect("Failed to commit");
3535 }
3536}
3537
3538#[track_caller]
3539fn git_cherry_pick(commit: &git2::Commit<'_>, repo: &git2::Repository) {
3540 repo.cherrypick(commit, None).expect("Failed to cherrypick");
3541}
3542
3543#[track_caller]
3544fn git_stash(repo: &mut git2::Repository) {
3545 use git2::Signature;
3546
3547 let signature = Signature::now("test", "test@zed.dev").unwrap();
3548 repo.stash_save(&signature, "N/A", None)
3549 .expect("Failed to stash");
3550}
3551
3552#[track_caller]
3553fn git_reset(offset: usize, repo: &git2::Repository) {
3554 let head = repo.head().expect("Couldn't get repo head");
3555 let object = head.peel(git2::ObjectType::Commit).unwrap();
3556 let commit = object.as_commit().unwrap();
3557 let new_head = commit
3558 .parents()
3559 .inspect(|parnet| {
3560 parnet.message();
3561 })
3562 .nth(offset)
3563 .expect("Not enough history");
3564 repo.reset(new_head.as_object(), git2::ResetType::Soft, None)
3565 .expect("Could not reset");
3566}
3567
3568#[track_caller]
3569fn git_branch(name: &str, repo: &git2::Repository) {
3570 let head = repo
3571 .head()
3572 .expect("Couldn't get repo head")
3573 .peel_to_commit()
3574 .expect("HEAD is not a commit");
3575 repo.branch(name, &head, false).expect("Failed to commit");
3576}
3577
3578#[track_caller]
3579fn git_checkout(name: &str, repo: &git2::Repository) {
3580 repo.set_head(name).expect("Failed to set head");
3581 repo.checkout_head(None).expect("Failed to check out head");
3582}
3583
3584#[allow(dead_code)]
3585#[track_caller]
3586fn git_status(repo: &git2::Repository) -> collections::HashMap<String, git2::Status> {
3587 repo.statuses(None)
3588 .unwrap()
3589 .iter()
3590 .map(|status| (status.path().unwrap().to_string(), status.status()))
3591 .collect()
3592}
3593
3594#[track_caller]
3595fn check_worktree_entries(
3596 tree: &Worktree,
3597 expected_excluded_paths: &[&str],
3598 expected_ignored_paths: &[&str],
3599 expected_tracked_paths: &[&str],
3600 expected_included_paths: &[&str],
3601) {
3602 for path in expected_excluded_paths {
3603 let entry = tree.entry_for_path(path);
3604 assert!(
3605 entry.is_none(),
3606 "expected path '{path}' to be excluded, but got entry: {entry:?}",
3607 );
3608 }
3609 for path in expected_ignored_paths {
3610 let entry = tree
3611 .entry_for_path(path)
3612 .unwrap_or_else(|| panic!("Missing entry for expected ignored path '{path}'"));
3613 assert!(
3614 entry.is_ignored,
3615 "expected path '{path}' to be ignored, but got entry: {entry:?}",
3616 );
3617 }
3618 for path in expected_tracked_paths {
3619 let entry = tree
3620 .entry_for_path(path)
3621 .unwrap_or_else(|| panic!("Missing entry for expected tracked path '{path}'"));
3622 assert!(
3623 !entry.is_ignored || entry.is_always_included,
3624 "expected path '{path}' to be tracked, but got entry: {entry:?}",
3625 );
3626 }
3627 for path in expected_included_paths {
3628 let entry = tree
3629 .entry_for_path(path)
3630 .unwrap_or_else(|| panic!("Missing entry for expected included path '{path}'"));
3631 assert!(
3632 entry.is_always_included,
3633 "expected path '{path}' to always be included, but got entry: {entry:?}",
3634 );
3635 }
3636}
3637
3638fn init_test(cx: &mut gpui::TestAppContext) {
3639 if std::env::var("RUST_LOG").is_ok() {
3640 env_logger::try_init().ok();
3641 }
3642
3643 cx.update(|cx| {
3644 let settings_store = SettingsStore::test(cx);
3645 cx.set_global(settings_store);
3646 WorktreeSettings::register(cx);
3647 });
3648}
3649
3650fn assert_entry_git_state(
3651 tree: &Worktree,
3652 path: &str,
3653 index_status: Option<StatusCode>,
3654 is_ignored: bool,
3655) {
3656 let entry = tree.entry_for_path(path).expect("entry {path} not found");
3657 let status = tree.status_for_file(Path::new(path));
3658 let expected = index_status.map(|index_status| {
3659 TrackedStatus {
3660 index_status,
3661 worktree_status: StatusCode::Unmodified,
3662 }
3663 .into()
3664 });
3665 assert_eq!(
3666 status, expected,
3667 "expected {path} to have git status: {expected:?}"
3668 );
3669 assert_eq!(
3670 entry.is_ignored, is_ignored,
3671 "expected {path} to have is_ignored: {is_ignored}"
3672 );
3673}