1
1
use crate :: state:: SharedState ;
2
2
use crate :: Command ;
3
- use notify:: { Error , Event , RecommendedWatcher , RecursiveMode , Watcher } ;
3
+ use notify:: { recommended_watcher , Error , Event , RecursiveMode , Watcher } ;
4
4
use std:: path:: Path ;
5
5
use std:: result:: Result ;
6
- use std:: sync:: Arc ;
7
- use tokio:: sync:: { mpsc, Mutex } ;
8
- use tracing:: { debug, info, trace} ;
9
- use wax:: { Glob , Pattern } ;
6
+ use std:: sync:: { Arc , Mutex } ;
7
+ use tokio:: sync:: mpsc;
8
+ use tracing:: { debug, trace} ;
10
9
11
10
// TODO: Stop handle
12
11
@@ -42,9 +41,9 @@ pub async fn update(state: SharedState, _msg: Command) {
42
41
///
43
42
/// This will compare last_seen with path, updates `last_seen` if not match,
44
43
/// else returns true.
45
- async fn should_ignore ( last_seen : Arc < Mutex < String > > , path : & str ) -> bool {
44
+ fn is_seen ( last_seen : Arc < Mutex < String > > , path : & str ) -> bool {
46
45
let path = path. to_string ( ) ;
47
- let mut last_seen = last_seen. lock ( ) . await ;
46
+ let mut last_seen = last_seen. lock ( ) . unwrap ( ) ;
48
47
if last_seen. to_string ( ) == path {
49
48
return true ;
50
49
} else {
@@ -54,34 +53,51 @@ async fn should_ignore(last_seen: Arc<Mutex<String>>, path: &str) -> bool {
54
53
}
55
54
56
55
fn new ( state : SharedState , root : String ) -> tokio:: task:: JoinHandle < anyhow:: Result < ( ) > > {
57
- // NOTE: should watch for registerd directories?
58
- let ignore = wax:: any :: < Glob , _ > ( [ "**/.git/**" , "**/*.xcodeproj/**" , "**/.*" ] ) . unwrap ( ) ;
56
+ type NotifyResult = Result < Event , Error > ;
57
+ use gitignore:: File ;
58
+ use wax:: { any, Glob , Pattern } ;
59
59
60
60
tokio:: spawn ( async move {
61
61
let ( tx, mut rx) = mpsc:: channel ( 100 ) ;
62
62
63
- let mut watcher = RecommendedWatcher :: new ( move |res : Result < Event , Error > | {
63
+ let custom_ignore = any :: < Glob , _ > ( [ "**/.git/**" , "**/*.xcodeproj/**" , "**/.*" ] ) . unwrap ( ) ;
64
+ let root_path = Path :: new ( & root) ;
65
+ let gitignore_path = root_path. join ( ".gitignore" ) ;
66
+
67
+ // HACK: To ignore seen paths.
68
+ let last_seen = Arc :: new ( Mutex :: new ( String :: default ( ) ) ) ;
69
+
70
+ // NOTE: outdate API
71
+ let gitignore = Arc :: new ( Mutex :: new ( File :: new ( & gitignore_path) . unwrap ( ) ) ) ;
72
+
73
+ recommended_watcher ( move |res : NotifyResult | {
64
74
if res. is_ok ( ) {
65
75
tx. blocking_send ( res. unwrap ( ) ) . unwrap ( )
66
76
} ;
67
- } ) ?;
68
-
69
- watcher. watch ( Path :: new ( & root) , RecursiveMode :: Recursive ) ?;
70
-
71
- // HACK: ignore seen paths.
72
- let last_seen = Arc :: new ( Mutex :: new ( String :: default ( ) ) ) ;
77
+ } ) ?
78
+ . watch ( root_path, RecursiveMode :: Recursive ) ?;
73
79
74
80
while let Some ( event) = rx. recv ( ) . await {
75
81
let state = state. clone ( ) ;
76
82
let path = match event. paths . get ( 0 ) {
77
- Some ( p) => match p. clone ( ) . to_str ( ) {
78
- Some ( s) => s. to_string ( ) ,
79
- None => continue ,
80
- } ,
83
+ Some ( p) => p. clone ( ) ,
84
+ None => continue ,
85
+ } ;
86
+
87
+ let path_string = match path. to_str ( ) {
88
+ Some ( s) => s. to_string ( ) ,
81
89
None => continue ,
82
90
} ;
83
91
84
- if should_ignore ( last_seen. clone ( ) , & path) . await || ignore. is_match ( & * path) {
92
+ if is_seen ( last_seen. clone ( ) , & path_string)
93
+ || custom_ignore. is_match ( & * path_string)
94
+ || !gitignore
95
+ . lock ( )
96
+ . unwrap ( )
97
+ . included_files ( )
98
+ . unwrap ( )
99
+ . contains ( & path)
100
+ {
85
101
continue ;
86
102
}
87
103
0 commit comments