Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ etcetera = "0.10.0"
figment = { version = "0.10.19", features = ["toml"] }
git-version = "0.3.9"
git2 = { version = "0.20.1", default-features = false }
ignore = "0.4.23"
itertools = "0.14.0"
log = "0.4.27"
nom = "7.1.3"
Expand Down
4 changes: 0 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub enum Error {
Term(io::Error),
GitDirUtf8(string::FromUtf8Error),
Config(figment::Error),
FileWatcherGitignore(ignore::Error),
FileWatcher(notify::Error),
ReadRebaseStatusFile(io::Error),
ReadBranchName(io::Error),
Expand Down Expand Up @@ -64,9 +63,6 @@ impl Display for Error {
Error::Term(e) => f.write_fmt(format_args!("Terminal error: {}", e)),
Error::GitDirUtf8(_e) => f.write_str("Git directory not valid UTF-8"),
Error::Config(e) => f.write_fmt(format_args!("Configuration error: {}", e)),
Error::FileWatcherGitignore(e) => {
f.write_fmt(format_args!("File watcher gitignore error: {}", e))
}
Error::FileWatcher(e) => f.write_fmt(format_args!("File watcher error: {}", e)),
Error::ReadRebaseStatusFile(e) => {
f.write_fmt(format_args!("Couldn't read rebase status file: {}", e))
Expand Down
42 changes: 19 additions & 23 deletions src/file_watcher.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,33 @@
use crate::{error::Error, Res};
use ignore::gitignore::GitignoreBuilder;
use notify::{Event, EventKind, RecursiveMode, Watcher};
use crate::{error::Error, open_repo, Res};
use notify::{Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
use std::{
path::Path,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
thread,
};

pub struct FileWatcher {
_watcher: RecommendedWatcher,
pending_updates: Arc<AtomicBool>,
}

impl FileWatcher {
pub fn new(path: &Path) -> Res<Self> {
pub fn new(repo_dir: &Path) -> Res<Self> {

Check warning on line 17 in src/file_watcher.rs

View check run for this annotation

Codecov / codecov/patch

src/file_watcher.rs#L17

Added line #L17 was not covered by tests
let pending_updates = Arc::new(AtomicBool::new(false));
let pending_updates_w = pending_updates.clone();

let gitignore = GitignoreBuilder::new(path)
.add_line(None, super::LOG_FILE_NAME)
.map_err(Error::FileWatcherGitignore)?
.build()
.unwrap();

let repo = open_repo(repo_dir)?;

Check warning on line 21 in src/file_watcher.rs

View check run for this annotation

Codecov / codecov/patch

src/file_watcher.rs#L21

Added line #L21 was not covered by tests
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recently merged a whole bunch of refactoring, where the creation of the file watcher was moved. Now it'd be real easy to just accept a Repository instead of a Path as argument. Then you'd don't need to call open_repo anymore!

It's created here!

repo.workdir().expect("Bare repos unhandled"),

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I'll test the PR out again soon, but looks promising!)

let mut watcher = notify::recommended_watcher(move |res: Result<Event, notify::Error>| {
if let Ok(event) = res {
if !is_changed(&event) {
return;
}

for path in event.paths {
if !gitignore.matched(&path, path.is_dir()).is_ignore() {
log::info!("File changed: {:?}", path);
if !repo.status_should_ignore(&path).unwrap_or(false) {
log::info!("File changed: {:?} ({:?})", path, event.kind);

Check warning on line 30 in src/file_watcher.rs

View check run for this annotation

Codecov / codecov/patch

src/file_watcher.rs#L29-L30

Added lines #L29 - L30 were not covered by tests
pending_updates_w.store(true, Ordering::Relaxed);
break;
}
Expand All @@ -42,17 +36,19 @@
})
.map_err(Error::FileWatcher)?;

let path_buf = path.to_owned();
thread::spawn(move || {
if let Err(err) = watcher
.watch(path_buf.as_ref(), RecursiveMode::Recursive)
.map_err(Error::FileWatcher)
{
log::error!("Couldn't start file-watcher due to: {}", err);
}
});
let path_buf = repo_dir.to_owned();
watcher
.watch(&path_buf, RecursiveMode::Recursive)
.map_err(Error::FileWatcher)?;
log::info!(
"File watcher started (kind: {:?})",
RecommendedWatcher::kind()

Check warning on line 45 in src/file_watcher.rs

View check run for this annotation

Codecov / codecov/patch

src/file_watcher.rs#L39-L45

Added lines #L39 - L45 were not covered by tests
);

Ok(Self { pending_updates })
Ok(Self {
_watcher: watcher,
pending_updates,
})

Check warning on line 51 in src/file_watcher.rs

View check run for this annotation

Codecov / codecov/patch

src/file_watcher.rs#L48-L51

Added lines #L48 - L51 were not covered by tests
}

pub fn pending_updates(&self) -> bool {
Expand Down
Loading