Skip to content

Commit dbedb9a

Browse files
committed
Fixed unnecessary refreshes due to file openings
1 parent c17bf69 commit dbedb9a

File tree

1 file changed

+37
-29
lines changed

1 file changed

+37
-29
lines changed

rs/src/lib.rs

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use kotars::jni_init;
1111
use libssh_rs::{PollStatus, SshOption};
1212
#[allow(unused_imports)]
1313
use libssh_rs::AuthStatus;
14-
use notify::{Config, Error, ErrorKind, Event, RecommendedWatcher, RecursiveMode, Watcher};
14+
use notify::{Config, Error, ErrorKind, Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
1515

1616
jni_init!("");
1717

@@ -102,7 +102,7 @@ impl FileWatcher {
102102
last_update = current_time_as_millis();
103103
}
104104

105-
// println!("Event: {e:?}");
105+
println!("Event: {e:?}");
106106
}
107107
}
108108
Err(e) => {
@@ -172,36 +172,17 @@ fn error_to_code(error_kind: ErrorKind) -> i32 {
172172
pub fn get_paths_from_event_result(event_result: &Result<Event, Error>, git_dir_path: &str) -> Option<Vec<String>> {
173173
match event_result {
174174
Ok(event) => {
175-
let events: Vec<String> = event
176-
.paths
177-
.clone()
178-
.into_iter()
179-
.filter_map(|path| {
180-
// Directories are not tracked by Git so we don't care about them (just about their content)
181-
// We won't be able to check if it's a dir if it has been deleted but that's good enough
182-
// if path.is_dir() {
183-
// println!("Ignoring directory {path:#?}");
184-
// None
185-
// } else {
186-
let path_str = path.into_os_string()
187-
.into_string()
188-
.ok()?;
189-
190-
// JGit may create .probe-UUID files for its internal stuff, we don't care about it
191-
let probe_prefix = format!("{git_dir_path}.probe-");
192-
if path_str.starts_with(probe_prefix.as_str()) {
175+
match event.kind {
176+
EventKind::Create(_) | EventKind::Modify(_) | EventKind::Remove(_) => {
177+
let events: Vec<String> = get_event_paths(event, git_dir_path);
178+
179+
if events.is_empty() {
193180
None
194181
} else {
195-
Some(path_str)
182+
Some(events)
196183
}
197-
// }
198-
})
199-
.collect();
200-
201-
if events.is_empty() {
202-
None
203-
} else {
204-
Some(events)
184+
}
185+
_ => { None }
205186
}
206187
}
207188
Err(err) => {
@@ -211,6 +192,33 @@ pub fn get_paths_from_event_result(event_result: &Result<Event, Error>, git_dir_
211192
}
212193
}
213194

195+
fn get_event_paths(event: &Event, git_dir_path: &str) -> Vec<String> {
196+
event
197+
.paths
198+
.clone()
199+
.into_iter()
200+
.filter_map(|path| {
201+
// Directories are not tracked by Git so we don't care about them (just about their content)
202+
// We won't be able to check if it's a dir if it has been deleted but that's good enough
203+
// if path.is_dir() {
204+
// println!("Ignoring directory {path:#?}");
205+
// None
206+
// } else {
207+
let path_str = path.into_os_string()
208+
.into_string()
209+
.ok()?;
210+
211+
// JGit may create .probe-UUID files for its internal stuff, we don't care about it
212+
let probe_prefix = format!("{git_dir_path}.probe-");
213+
if path_str.starts_with(probe_prefix.as_str()) {
214+
None
215+
} else {
216+
Some(path_str)
217+
}
218+
// }
219+
})
220+
.collect()
221+
}
214222
#[jni_interface]
215223
pub trait WatchDirectoryNotifier {
216224
fn should_keep_looping(&self) -> bool;

0 commit comments

Comments
 (0)