Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

watcher refactor #3471

Merged
merged 17 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
collect filemonitor events into lists to avoid excessive recomputation.
Previously, each file change both in `.git` as well as in the worktree would
cause a complete recomputation. This computation included opening a git
repository at least once (probaby more often), to make an 'is-ignored' check.

The latter is very expensive in `git2` and gets more expensive the more
files there are.

Now the repository is opened when needed, and we re-use it for all applicable
file paths.
  • Loading branch information
Byron committed Apr 15, 2024
commit 2e969d1507ef8d50ef9b0fc8061b0144c9b2f836
6 changes: 3 additions & 3 deletions crates/gitbutler-core/src/deltas/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl<'writer> DeltasWriter<'writer> {
self.writer
.write_string(PathBuf::from("session/deltas").join(path), &raw_deltas)?;

tracing::debug!(
tracing::trace!(
Byron marked this conversation as resolved.
Show resolved Hide resolved
project_id = %self.repository.get_project_id(),
path = %path.display(),
"wrote deltas"
Expand All @@ -43,7 +43,7 @@ impl<'writer> DeltasWriter<'writer> {
let path = path.as_ref();
self.writer.remove(PathBuf::from("session/wd").join(path))?;

tracing::debug!(
tracing::trace!(
project_id = %self.repository.get_project_id(),
path = %path.display(),
"deleted session wd file"
Expand All @@ -61,7 +61,7 @@ impl<'writer> DeltasWriter<'writer> {
self.writer
.write_string(PathBuf::from("session/wd").join(path), contents)?;

tracing::debug!(
tracing::trace!(
project_id = %self.repository.get_project_id(),
path = %path.display(),
"wrote session wd file"
Expand Down
2 changes: 2 additions & 0 deletions crates/gitbutler-core/src/projects/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ pub struct Project {
pub id: ProjectId,
pub title: String,
pub description: Option<String>,
// TODO(ST): Keep track of the `git_dir` separately and use it, particularly in `file_monitor.rs` (#3062)
/// The worktree path of the projects repository.
pub path: path::PathBuf,
#[serde(default)]
pub preferred_key: AuthKey,
Expand Down
2 changes: 1 addition & 1 deletion crates/gitbutler-tauri/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Event {
app_handle
.emit_all(&self.name, Some(&self.payload))
.context("emit event")?;
tracing::debug!(event_name = self.name);
tracing::trace!(event_name = self.name);
Ok(())
}

Expand Down
41 changes: 34 additions & 7 deletions crates/gitbutler-tauri/src/watcher/events.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{fmt::Display, path};
use std::fmt::Display;
use std::path::PathBuf;

use gitbutler_core::{projects::ProjectId, sessions};

Expand All @@ -12,8 +13,8 @@ pub(super) enum InternalEvent {
PushGitbutlerData(ProjectId),

// From file monitor
GitFileChange(ProjectId, path::PathBuf),
ProjectFileChange(ProjectId, path::PathBuf),
GitFilesChange(ProjectId, Vec<PathBuf>),
ProjectFilesChange(ProjectId, Vec<PathBuf>),
}

/// This type captures all operations that can be fed into a watcher that runs in the background.
Expand Down Expand Up @@ -58,14 +59,40 @@ impl Display for InternalEvent {
InternalEvent::Flush(project_id, session) => {
write!(f, "Flush({}, {})", project_id, session.id)
}
InternalEvent::GitFileChange(project_id, path) => {
write!(f, "GitFileChange({}, {})", project_id, path.display())
InternalEvent::GitFilesChange(project_id, paths) => {
write!(
f,
"GitFileChange({}, {})",
project_id,
comma_separated_paths(paths)
)
}
InternalEvent::ProjectFileChange(project_id, path) => {
write!(f, "ProjectFileChange({}, {})", project_id, path.display())
InternalEvent::ProjectFilesChange(project_id, paths) => {
write!(
f,
"ProjectFileChange({}, {})",
project_id,
comma_separated_paths(paths)
)
}
InternalEvent::CalculateVirtualBranches(pid) => write!(f, "VirtualBranch({})", pid),
InternalEvent::PushGitbutlerData(pid) => write!(f, "PushGitbutlerData({})", pid),
}
}
}

fn comma_separated_paths(paths: &[PathBuf]) -> String {
const MAX_LISTING: usize = 5;
let listing = paths
.iter()
.take(MAX_LISTING)
.filter_map(|path| path.to_str())
.collect::<Vec<_>>()
.join(", ");
let remaining = paths.len().saturating_sub(MAX_LISTING);
if remaining > 0 {
format!("{listing} […{remaining} more]")
} else {
listing
}
}
150 changes: 111 additions & 39 deletions crates/gitbutler-tauri/src/watcher/file_monitor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashSet;
use std::path::Path;
use std::{path, time::Duration};

Expand All @@ -7,6 +8,7 @@ use gitbutler_core::{git, projects::ProjectId};
use notify::Watcher;
use notify_debouncer_full::new_debouncer;
use tokio::task;
use tracing::Level;

/// The timeout for debouncing file change events.
/// This is used to prevent multiple events from being sent for a single file change.
Expand Down Expand Up @@ -66,57 +68,109 @@ pub fn spawn(
})
.context("failed to start watcher")?;

let repo = git::Repository::open(worktree_path).context(format!(
"failed to open project repository: {}",
worktree_path.display()
))?;

tracing::debug!(%project_id, "file watcher started");

let path = worktree_path.to_owned();
let worktree_path = worktree_path.to_owned();
task::spawn_blocking(move || {
tracing::debug!(%project_id, "file watcher started");
let _debouncer = debouncer;
let _runtime = tracing::span!(Level::INFO, "file monitor", %project_id ).entered();
'outer: for result in notify_rx {
let stats = tracing::span!(
Level::INFO,
"handle debounced events",
ignored = tracing::field::Empty,
project = tracing::field::Empty,
project_dedup = tracing::field::Empty,
git = tracing::field::Empty,
git_dedup = tracing::field::Empty,
git_noop = tracing::field::Empty,
fs_events = tracing::field::Empty,
)
.entered();
let (mut ignored, mut git_noop) = (0, 0);
match result {
Err(err) => {
tracing::error!(?err, "file watcher error");
tracing::error!(?err, "ignored file watcher error");
}
Ok(events) => {
let file_paths = events
let maybe_repo = git::Repository::open(&worktree_path).with_context(
|| {
format!(
"failed to open project repository: {}",
worktree_path.display()
)
},
).map(Some).unwrap_or_else(|err| {
tracing::error!(?err, "will consider changes to all files as repository couldn't be opened");
None
});

let num_events = events.len();
let classified_file_paths = events
.into_iter()
.filter(|event| is_interesting_kind(event.kind))
.flat_map(|event| event.event.paths)
.filter(|file| is_interesting_file(&repo, file));
for file_path in file_paths {
match file_path.strip_prefix(&path) {
Ok(relative_file_path) => {
if relative_file_path.as_os_str().is_empty() {
continue;
}
let event = if let Ok(stripped) =
relative_file_path.strip_prefix(".git")
{
InternalEvent::GitFileChange(project_id, stripped.to_owned())
} else {
InternalEvent::ProjectFileChange(
project_id,
relative_file_path.to_path_buf(),
)
};
if out.send(event).is_err() {
tracing::info!("channel closed - stopping file watcher");
break 'outer;
.map(|file| {
let kind = maybe_repo
.as_ref()
.map_or(FileKind::Project, |repo| classify_file(repo, &file));
(file, kind)
});
let (mut stripped_git_paths, mut worktree_relative_paths) =
(HashSet::new(), HashSet::new());
for (file_path, kind) in classified_file_paths {
match kind {
FileKind::ProjectIgnored => ignored += 1,
FileKind::GitUninteresting => git_noop += 1,
FileKind::Project | FileKind::Git => {
match file_path.strip_prefix(&worktree_path) {
Ok(relative_file_path) => {
if relative_file_path.as_os_str().is_empty() {
continue;
}
if let Ok(stripped) =
relative_file_path.strip_prefix(".git")
{
stripped_git_paths.insert(stripped.to_owned());
} else {
worktree_relative_paths
.insert(relative_file_path.to_owned());
};
}
Err(err) => {
tracing::error!(%project_id, ?err, "failed to strip prefix");
}
}
}
Err(err) => {
tracing::error!(%project_id, ?err, "failed to strip prefix");
}
}
}

stats.record("fs_events", num_events);
stats.record("ignored", ignored);
stats.record("git_noop", git_noop);
stats.record("git", stripped_git_paths.len());
stats.record("project", worktree_relative_paths.len());

if !stripped_git_paths.is_empty() {
let paths_dedup: Vec<_> = stripped_git_paths.into_iter().collect();
stats.record("git_dedup", paths_dedup.len());
let event = InternalEvent::GitFilesChange(project_id, paths_dedup);
if out.send(event).is_err() {
tracing::info!("channel closed - stopping file watcher");
break 'outer;
}
}
if !worktree_relative_paths.is_empty() {
let paths_dedup: Vec<_> = worktree_relative_paths.into_iter().collect();
stats.record("project_dedup", paths_dedup.len());
let event = InternalEvent::ProjectFilesChange(project_id, paths_dedup);
if out.send(event).is_err() {
tracing::info!("channel closed - stopping file watcher");
break 'outer;
}
}
}
}
}
tracing::debug!(%project_id, "file watcher stopped");
});
Ok(())
}
Expand All @@ -140,15 +194,33 @@ fn is_interesting_kind(kind: notify::EventKind) -> bool {
)
}

fn is_interesting_file(git_repo: &git::Repository, file_path: &Path) -> bool {
if file_path.starts_with(git_repo.path()) {
let check_file_path = file_path.strip_prefix(git_repo.path()).unwrap();
check_file_path.ends_with("FETCH_HEAD")
/// A classification for a changed file.
enum FileKind {
/// A file in the `.git` repository of the current project itself.
Git,
/// Like `Git`, but shouldn't have any effect.
GitUninteresting,
/// A file in the worktree of the current project.
Project,
/// A file that was ignored in the project, and thus shouldn't trigger a computation.
ProjectIgnored,
}

fn classify_file(git_repo: &git::Repository, file_path: &Path) -> FileKind {
if let Ok(check_file_path) = file_path.strip_prefix(git_repo.path()) {
if check_file_path.ends_with("FETCH_HEAD")
|| check_file_path.eq(path::Path::new("logs/HEAD"))
|| check_file_path.eq(path::Path::new("HEAD"))
|| check_file_path.eq(path::Path::new("GB_FLUSH"))
|| check_file_path.eq(path::Path::new("index"))
{
FileKind::Git
} else {
FileKind::GitUninteresting
}
} else if git_repo.is_path_ignored(file_path).unwrap_or(false) {
FileKind::ProjectIgnored
} else {
!git_repo.is_path_ignored(file_path).unwrap_or(false)
FileKind::Project
}
}
Loading