use std::{
collections::{BTreeMap, HashSet},
path::{Path, PathBuf},
time::{Duration, Instant},
};
use classify::{MetadataScope, classify_dependencies, classify_metadata, classify_metadata_scoped};
use gix_error::{ErrorExt, ResultExt, message};
use gix_hashtable::hash_table::HashTable;
pub use gix_notify as plumbing;
use plumbing::{Event, EventKind, PathKind};
use crate::{Error, Repository, Result, bstr::BString};
#[cfg(feature = "async-notify")]
mod asynchronous;
mod classify;
mod inventory;
#[cfg(test)]
mod tests;
mod worktrees;
const POLL_INTERVAL: Duration = Duration::from_millis(250);
const MAX_SCOPES: usize = 4096;
const MAX_SCOPE_BYTES: usize = 4 * 1024 * 1024;
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub enum DependencyKind {
Configuration,
Ignore,
Attributes,
}
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct Dependency {
pub path: PathBuf,
pub kind: DependencyKind,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Layout {
pub git_dir: PathBuf,
pub common_dir: PathBuf,
pub worktree: Option<PathBuf>,
pub index: PathBuf,
pub dependencies: Vec<Dependency>,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum WorktreeCoverage {
#[default]
IgnoreAware,
All,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct InvalidationPath {
pub path: PathBuf,
pub changes: Changes,
}
#[derive(Clone, Debug)]
pub struct Options {
pub metadata: bool,
pub all_worktrees: bool,
pub worktree_filter: Option<Vec<PathBuf>>,
pub additional_paths: Vec<InvalidationPath>,
pub worktree_coverage: Option<WorktreeCoverage>,
pub metadata_debounce: Duration,
pub max_latency: Duration,
pub retry_interval: Duration,
}
impl Default for Options {
fn default() -> Self {
Self {
metadata: true,
all_worktrees: false,
worktree_filter: None,
additional_paths: Vec::new(),
worktree_coverage: Some(WorktreeCoverage::default()),
metadata_debounce: Duration::from_millis(100),
max_latency: Duration::from_millis(250),
retry_interval: Duration::from_secs(5),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PathChange {
pub path: BString,
pub kind: plumbing::EventKind,
pub path_kind: plumbing::PathKind,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Scope {
Paths(Vec<PathChange>),
All,
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Changes {
pub references: bool,
pub index: bool,
pub configuration: bool,
pub ignores: bool,
pub attributes: bool,
pub operations: bool,
pub layout: bool,
pub worktree: Option<Scope>,
}
impl Changes {
fn metadata_all_for(options: &Options) -> Self {
let mut changes = Self::metadata_all();
for path in &options.additional_paths {
changes.merge(path.changes.clone());
}
changes
}
pub fn is_empty(&self) -> bool {
!self.references
&& !self.index
&& !self.configuration
&& !self.ignores
&& !self.attributes
&& !self.operations
&& !self.layout
&& self.worktree.is_none()
}
fn merge(&mut self, other: Self) {
self.references |= other.references;
self.index |= other.index;
self.configuration |= other.configuration;
self.ignores |= other.ignores;
self.attributes |= other.attributes;
self.operations |= other.operations;
self.layout |= other.layout;
match other.worktree {
None => {}
Some(Scope::All) => self.worktree = Some(Scope::All),
Some(Scope::Paths(paths)) => paths.into_iter().for_each(|path| self.insert_worktree_path(path)),
}
}
fn insert_worktree_path(&mut self, change: PathChange) {
let Scope::Paths(paths) = self.worktree.get_or_insert_with(|| Scope::Paths(Vec::new())) else {
return;
};
if let Some(previous) = paths.iter_mut().find(|previous| previous.path == change.path) {
if previous.kind != change.kind {
previous.kind = EventKind::Any;
}
if previous.path_kind != change.path_kind {
previous.path_kind = PathKind::Any;
}
return;
}
if paths.len() >= MAX_SCOPES
|| paths
.iter()
.map(|p| p.path.len())
.sum::<usize>()
.saturating_add(change.path.len())
> MAX_SCOPE_BYTES
{
self.worktree = Some(Scope::All);
} else {
paths.push(change);
}
}
fn metadata_all() -> Self {
Self {
references: true,
configuration: true,
ignores: true,
attributes: true,
operations: true,
layout: true,
..Self::default()
}
}
fn worktree_all() -> Self {
Self {
index: true,
worktree: Some(Scope::All),
..Self::default()
}
}
}
#[derive(Clone, Debug, Default)]
pub struct Statistics {
pub received: usize,
pub rescans: usize,
pub added: usize,
pub removed: usize,
}
impl Statistics {
fn merge(&mut self, other: Self) {
self.received = self.received.saturating_add(other.received);
self.rescans = self.rescans.saturating_add(other.rescans);
self.added = self.added.saturating_add(other.added);
self.removed = self.removed.saturating_add(other.removed);
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct WorktreeChanges {
pub git_dir: PathBuf,
pub worktree: PathBuf,
pub kind: WorktreeChange,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum WorktreeChange {
Added,
Moved {
previous_worktree: PathBuf,
},
Removed,
Changed(Changes),
}
#[derive(Default)]
pub struct Outcome {
pub changes: Changes,
pub other_worktrees: Vec<WorktreeChanges>,
pub errors: Vec<Error>,
pub recovery_pending: bool,
pub statistics: Statistics,
}
#[derive(Default)]
struct Pending {
changes: Changes,
first: Option<Instant>,
deadline: Option<Instant>,
}
impl Pending {
fn add(&mut self, changes: Changes, now: Instant, options: &Options, immediate: bool) {
if changes.is_empty() {
return;
}
self.changes.merge(changes);
let first = *self.first.get_or_insert(now);
let deadline = if immediate {
now
} else {
(now + options.metadata_debounce).min(first + options.max_latency)
};
self.deadline = Some(
self.deadline
.filter(|old| *old <= now)
.map_or(deadline, |old| old.min(deadline)),
);
}
fn take_due(&mut self, now: Instant) -> Changes {
if self.deadline.is_some_and(|deadline| deadline <= now) {
return std::mem::take(self).changes;
}
Changes::default()
}
}
#[derive(Default)]
struct Domain {
watcher: Option<gix_notify::Watcher>,
registered: BTreeMap<PathBuf, bool>,
rebuild: bool,
}
impl Domain {
fn replace(&mut self, desired: BTreeMap<PathBuf, bool>, statistics: &mut Statistics) -> Result<bool> {
if self.rebuild {
self.watcher = None;
}
let changed = self.watcher.is_none() || desired != self.registered;
if !changed {
return Ok(false);
}
let watcher = match self.watcher.as_mut() {
Some(watcher) => watcher,
None => self
.watcher
.insert(gix_notify::Watcher::new(gix_notify::Options::default())?),
};
if let Err(err) = watcher.replace(desired.iter().map(|(path, recursive)| gix_notify::Watch {
path: path.clone(),
recursive: *recursive,
})) {
self.rebuild = true;
return Err(err);
}
statistics.added += desired
.iter()
.filter(|(path, mode)| self.registered.get(*path) != Some(*mode))
.count();
statistics.removed += self
.registered
.iter()
.filter(|(path, mode)| desired.get(*path) != Some(*mode))
.count();
self.registered = desired;
self.rebuild = false;
Ok(true)
}
}
pub struct RepositoryMonitor {
#[cfg(feature = "async-notify")]
wake: gix_parallel::OwnShared<tokio::sync::Notify>,
layout: Layout,
options: Options,
metadata: Domain,
worktree: Domain,
directories: HashTable<PathBuf>,
nested_repositories: HashSet<PathBuf>,
index_fingerprint: Option<gix_hash::ObjectId>,
directory_inventory: Option<inventory::Directories>,
changed_directories: HashSet<PathBuf>,
pending_metadata: Pending,
pending_worktree: Changes,
pending_statistics: Statistics,
metadata_dirty: bool,
inventory_dirty: bool,
inventory_verify: bool,
index_dirty: bool,
maintenance: Option<Instant>,
backlog: bool,
started: bool,
precompose_unicode: bool,
ignore_case: bool,
shared_metadata: bool,
other_worktrees: BTreeMap<PathBuf, RepositoryMonitor>,
retired_worktrees: Vec<WorktreeChanges>,
worktree_transition: Option<WorktreeChange>,
maintenance_failed: bool,
undiscovered_worktrees: BTreeMap<PathBuf, Instant>,
}
impl Repository {
pub fn monitor(&self, mut options: Options) -> Result<RepositoryMonitor> {
if options.all_worktrees && !options.metadata {
return Err(gix_error::validation("all-worktree monitoring requires metadata monitoring").raise());
}
if options.retry_interval.is_zero() {
return Err(message("monitor retry interval must be positive").raise());
}
let now = Instant::now();
for (name, duration) in [
("metadata debounce", options.metadata_debounce),
("maximum latency", options.max_latency),
("retry interval", options.retry_interval),
] {
if now.checked_add(duration).is_none() {
return Err(message!("monitor {name} exceeds the supported clock range")
.validation()
.raise());
}
}
let filesystem = self
.config
.fs_capabilities(None)
.or_raise(|| message("could not read filesystem options"))?;
for path in options.worktree_filter.iter_mut().flatten() {
*path = absolute_path(path, self.current_dir(), filesystem.precompose_unicode)?;
}
for path in &mut options.additional_paths {
path.path = absolute_path(&path.path, self.current_dir(), filesystem.precompose_unicode)?;
}
let layout = Layout::for_options(self, &options)?;
Ok(RepositoryMonitor {
#[cfg(feature = "async-notify")]
wake: gix_parallel::OwnShared::new(tokio::sync::Notify::new()),
layout,
options,
metadata: Domain::default(),
worktree: Domain::default(),
directories: HashTable::new(),
nested_repositories: HashSet::new(),
index_fingerprint: None,
directory_inventory: None,
changed_directories: HashSet::new(),
pending_metadata: Pending::default(),
pending_worktree: Changes::default(),
pending_statistics: Statistics::default(),
metadata_dirty: true,
inventory_dirty: true,
inventory_verify: false,
index_dirty: true,
maintenance: Some(Instant::now()),
backlog: false,
started: false,
precompose_unicode: filesystem.precompose_unicode,
ignore_case: filesystem.ignore_case,
shared_metadata: false,
other_worktrees: BTreeMap::new(),
retired_worktrees: Vec::new(),
worktree_transition: None,
maintenance_failed: false,
undiscovered_worktrees: BTreeMap::new(),
})
}
}
impl Layout {
#[cfg(test)]
fn from_repository(repo: &Repository) -> Result<Self> {
Self::for_options(repo, &Options::default())
}
fn for_options(repo: &Repository, options: &Options) -> Result<Self> {
let precompose = repo
.config
.fs_capabilities(None)
.or_raise(|| message("could not read filesystem options"))?
.precompose_unicode;
let absolute = |path: &Path| absolute_path(path, repo.current_dir(), precompose);
let dependencies = options.metadata || options.worktree_coverage == Some(WorktreeCoverage::IgnoreAware);
let index = if dependencies {
absolute(&repo.index_path())?
} else {
repo.current_dir().join(repo.index_path())
};
let mut layout = Self {
git_dir: absolute(repo.git_dir())?,
common_dir: absolute(repo.common_dir())?,
worktree: repo.workdir().map(absolute).transpose()?,
index,
dependencies: Vec::new(),
};
if !dependencies {
return Ok(layout);
}
for dependency in repo.notification_dependencies(options.metadata)? {
if let (Some(parent), Some(name)) = (dependency.path.parent(), dependency.path.file_name()) {
let parent = if parent.as_os_str().is_empty() {
repo.current_dir()
} else {
parent
};
let path = absolute(parent)?.join(name);
if layout.contains(&path)
|| options.worktree_coverage == Some(WorktreeCoverage::IgnoreAware)
&& layout.worktree.is_some()
&& dependency.kind == DependencyKind::Ignore
{
layout.dependencies.push(Dependency {
path,
kind: dependency.kind,
});
}
}
let path = absolute(&dependency.path)?;
if layout.contains(&path)
|| options.worktree_coverage == Some(WorktreeCoverage::IgnoreAware)
&& layout.worktree.is_some()
&& dependency.kind == DependencyKind::Ignore
{
layout.dependencies.push(Dependency {
path,
kind: dependency.kind,
});
}
}
layout.dependencies.sort();
layout.dependencies.dedup();
Ok(layout)
}
fn contains(&self, path: &Path) -> bool {
path.starts_with(&self.git_dir)
|| path.starts_with(&self.common_dir)
|| self.worktree.as_ref().is_some_and(|root| path.starts_with(root))
}
}
fn absolute_path(path: &Path, current_dir: &Path, precompose: bool) -> Result<PathBuf> {
let path = gix_path::realpath_opts(path, current_dir, gix_path::realpath::MAX_SYMLINKS)
.or_raise(|| message("could not resolve repository monitor path"))?;
#[cfg(windows)]
let path = path
.ancestors()
.find_map(|ancestor| match ancestor.canonicalize() {
Ok(native) => Some(Ok(native.join(
path.strip_prefix(ancestor)
.expect("ancestors are prefixes of the resolved path"),
))),
Err(error)
if matches!(
error.kind(),
std::io::ErrorKind::NotFound | std::io::ErrorKind::NotADirectory
) =>
{
None
}
Err(error) => Some(Err(error).or_raise(|| message("could not resolve native repository monitor path"))),
})
.ok_or_else(|| message("repository monitor path has no existing ancestor").raise())??;
let path = std::borrow::Cow::Owned(path);
Ok(if precompose {
gix_utils::str::precompose_path(path).into_owned()
} else {
path.into_owned()
})
}
fn strip_prefix<'a>(path: &'a Path, prefix: &Path, ignore_case: bool) -> Option<&'a Path> {
if !ignore_case {
return path.strip_prefix(prefix).ok();
}
let mut components = path.components();
for expected in prefix.components() {
if !components
.next()?
.as_os_str()
.as_encoded_bytes()
.eq_ignore_ascii_case(expected.as_os_str().as_encoded_bytes())
{
return None;
}
}
Some(components.as_path())
}
fn same_path(a: &Path, b: &Path, ignore_case: bool) -> bool {
strip_prefix(a, b, ignore_case).is_some_and(|relative| relative.as_os_str().is_empty())
}
impl RepositoryMonitor {
pub fn layout(&self) -> &Layout {
&self.layout
}
pub fn options(&self) -> &Options {
&self.options
}
pub fn rescan(&mut self) {
for other in self.other_worktrees.values_mut() {
other.rescan();
}
self.metadata.rebuild = true;
self.worktree.rebuild = true;
self.coverage_lost(false, Instant::now());
}
fn worktree_enabled(&self) -> bool {
self.options.worktree_coverage.is_some() && self.layout.worktree.is_some()
}
fn ignore_aware(&self) -> bool {
self.options.worktree_coverage == Some(WorktreeCoverage::IgnoreAware)
}
fn active(&self) -> bool {
self.options.metadata || self.worktree_enabled() || !self.options.additional_paths.is_empty()
}
fn recovery_pending(&self) -> bool {
self.maintenance_failed
|| self.metadata.rebuild
&& (self.options.metadata
|| self.worktree_enabled() && self.ignore_aware()
|| !self.options.additional_paths.is_empty())
|| self.worktree.rebuild && self.worktree_enabled()
|| !self.undiscovered_worktrees.is_empty()
|| self.other_worktrees.values().any(Self::recovery_pending)
}
fn poll(&mut self, now: Instant, out: &mut Outcome) {
if !self.active() {
return;
}
self.started = true;
self.backlog = false;
for worktree in [false, true] {
let domain = if worktree {
&mut self.worktree
} else {
&mut self.metadata
};
let Some(watcher) = domain.watcher.as_mut() else {
continue;
};
let batch = watcher.drain(gix_notify::Budget {
max_events: 256,
max_bytes: 1024 * 1024,
});
self.backlog |= batch.more;
if let Some(loss) = batch.loss {
out.statistics.rescans += 1;
if matches!(loss, gix_notify::Loss::BackendError | gix_notify::Loss::BackendStopped) {
domain.rebuild = true;
}
if loss != gix_notify::Loss::WatchSetChanged {
self.coverage_lost(worktree, now);
}
}
out.statistics.received += batch.events.len();
for event in batch.events {
self.observe(&event, worktree, now);
if !worktree && self.all_worktrees_enabled() {
self.observe_index(&event, now);
for other in self.other_worktrees.values_mut() {
other.observe(&event, false, now);
other.observe_index(&event, now);
}
}
}
if let Some(error) = batch.error {
self.coverage_lost(worktree, now);
let domain = if worktree {
&mut self.worktree
} else {
&mut self.metadata
};
domain.watcher = None;
domain.rebuild = true;
out.statistics.removed += domain.registered.len();
domain.registered.clear();
out.errors.push(error.and_raise(message!(
"filesystem monitoring failed for {}",
self.layout.git_dir.display()
)));
self.maintenance = Some(now + self.options.retry_interval);
}
}
}
pub fn service(&mut self, now: Instant, mut opener: impl FnMut() -> Result<Repository>) -> Outcome {
let mut out = Outcome::default();
if !self.active() {
return out;
}
self.poll(now, &mut out);
for other in self.other_worktrees.values_mut() {
other.poll(now, &mut out);
if other.maintenance.is_some_and(|deadline| deadline <= now) {
self.maintenance = Some(now);
}
}
if self.maintenance.is_some_and(|deadline| deadline <= now) {
self.maintenance = None;
let result = if !self.options.metadata && !self.ignore_aware() {
self.maintain_all(now, &mut out.statistics)
.map(|_| self.maintenance_recovered(now))
} else {
opener()
.or_raise(|| message("could not reopen repository for filesystem monitoring"))
.and_then(|repo| {
if self.all_worktrees_enabled() && !same_path(
&absolute_path(repo.git_dir(), repo.current_dir(), self.precompose_unicode)?,
&self.layout.git_dir,
self.ignore_case,
) {
return Err(gix_error::validation("all-worktree monitoring requires the same primary repository; drop this monitor and create a new instance to switch").raise());
}
if self.all_worktrees_enabled() {
self.maintain_other_worktrees(&repo, now, &mut out.statistics, &mut out.errors)?;
}
self.maintain(&repo, now, &mut out.statistics, &mut out.errors)
})
};
if let Err(err) = result {
out.errors.push(err);
self.maintenance_failed(now);
let retry = now + self.options.retry_interval;
for other in self.other_worktrees.values_mut() {
if other.maintenance.is_some_and(|deadline| deadline <= now) {
other.maintenance = Some(retry);
}
}
for deadline in self.undiscovered_worktrees.values_mut() {
if *deadline <= now {
*deadline = retry;
}
}
}
}
out.changes = self.take_changes(now);
out.other_worktrees.append(&mut self.retired_worktrees);
for other in self.other_worktrees.values_mut() {
let changes = other.take_changes(now);
let kind = other
.worktree_transition
.take()
.or_else(|| (!changes.is_empty()).then_some(WorktreeChange::Changed(changes)));
if let Some(change) = kind.and_then(|kind| other.worktree_change(kind)) {
out.other_worktrees.push(change);
}
}
if let Some(deadline) = self.undiscovered_worktrees.values().copied().min() {
self.maintenance = Some(self.maintenance.map_or(deadline, |current| current.min(deadline)));
}
out.recovery_pending = self.recovery_pending();
self.pending_statistics.merge(std::mem::take(&mut out.statistics));
if !out.changes.is_empty() || !out.other_worktrees.is_empty() || !out.errors.is_empty() {
out.statistics = std::mem::take(&mut self.pending_statistics);
}
#[cfg(feature = "async-notify")]
self.install_async_wakers();
out
}
pub fn next_timeout(&self, now: Instant) -> Option<Duration> {
if !self.active() {
return None;
}
if self.backlog {
return Some(Duration::ZERO);
}
let poll = (self.metadata.watcher.is_some() || self.worktree.watcher.is_some()).then_some(POLL_INTERVAL);
[self.maintenance, self.pending_metadata.deadline]
.into_iter()
.flatten()
.map(|deadline| deadline.saturating_duration_since(now))
.chain(poll)
.chain(
self.other_worktrees
.values()
.filter_map(|other| other.next_timeout(now)),
)
.min()
}
fn maintenance_failed(&mut self, now: Instant) {
if !self.maintenance_failed {
self.pending_metadata
.add(Changes::metadata_all_for(&self.options), now, &self.options, true);
if self.worktree_enabled() {
self.pending_worktree.merge(Changes::worktree_all());
}
self.maintenance_failed = true;
}
self.maintenance = Some(now + self.options.retry_interval);
}
fn maintenance_recovered(&mut self, now: Instant) {
if std::mem::take(&mut self.maintenance_failed) {
self.pending_metadata
.add(Changes::metadata_all_for(&self.options), now, &self.options, true);
if self.worktree_enabled() {
self.pending_worktree.merge(Changes::worktree_all());
}
}
}
fn coverage_lost(&mut self, worktree: bool, now: Instant) {
self.directory_inventory = None;
if worktree {
self.pending_worktree.merge(Changes::worktree_all());
self.inventory_dirty = true;
self.index_dirty = true;
} else {
for other in self.other_worktrees.values_mut() {
other.coverage_lost(false, now);
}
self.pending_metadata
.add(Changes::metadata_all_for(&self.options), now, &self.options, true);
self.metadata_dirty = true;
self.inventory_dirty = true;
self.index_dirty = true;
if self.worktree_enabled() {
self.pending_worktree.merge(Changes::worktree_all());
}
}
self.maintenance = Some(now);
}
fn maintain(
&mut self,
repo: &Repository,
now: Instant,
statistics: &mut Statistics,
_errors: &mut Vec<Error>,
) -> Result {
let layout = Layout::for_options(repo, &self.options)?;
if self.layout != layout {
self.metadata_dirty = true;
self.inventory_dirty = true;
self.index_dirty = true;
if self.layout.git_dir != layout.git_dir
|| self.layout.common_dir != layout.common_dir
|| self.layout.worktree != layout.worktree
|| self.layout.index != layout.index
{
self.metadata.rebuild = true;
self.worktree.rebuild = true;
self.directory_inventory = None;
}
self.layout = layout;
self.pending_metadata
.add(Changes::metadata_all_for(&self.options), now, &self.options, true);
if self.worktree_enabled() {
self.pending_worktree.merge(Changes::worktree_all());
}
}
let filesystem = repo
.config
.fs_capabilities(None)
.or_raise(|| message("could not read filesystem options"))?;
self.precompose_unicode = filesystem.precompose_unicode;
self.ignore_case = filesystem.ignore_case;
let mut verify = false;
if (self.options.metadata || self.worktree_enabled() && self.ignore_aware())
&& (self.metadata_dirty
|| !self.shared_metadata && self.metadata.watcher.is_none()
|| self.metadata.rebuild)
{
let mut watches = if self.shared_metadata {
inventory::private_metadata_watches(&self.layout)
} else if self.options.metadata {
inventory::metadata_watches(
&self.layout,
self.options
.worktree_filter
.as_deref()
.filter(|_| self.all_worktrees_enabled()),
self.ignore_case,
)?
} else {
inventory::dependency_watches(&self.layout)
};
inventory::merge_watches(
&mut watches,
inventory::additional_watches(&self.options.additional_paths)?,
);
if self.metadata_is_shared() && self.worktree_enabled() {
inventory::watch_parent(&self.layout, &mut watches, &self.layout.index);
}
if self.shared_metadata {
self.metadata.registered = watches;
self.metadata_dirty = false;
self.metadata.rebuild = false;
} else {
for other in self.other_worktrees.values() {
inventory::merge_watches(&mut watches, other.metadata.registered.clone());
}
match self.metadata.replace(watches, statistics) {
Ok(changed) => {
self.metadata_dirty = false;
if changed {
self.pending_metadata.add(
Changes::metadata_all_for(&self.options),
now,
&self.options,
true,
);
if self.worktree_enabled() {
self.pending_worktree.merge(Changes::worktree_all());
}
for other in self.other_worktrees.values_mut() {
other.coverage_lost(false, now);
}
verify = true;
}
}
Err(err) => {
self.coverage_lost(false, now);
return Err(err);
}
}
}
}
if self.worktree_enabled() && !self.ignore_aware() {
verify |= self.maintain_all(now, statistics)?;
} else if self.worktree_enabled()
&& (self.inventory_dirty
|| self.inventory_verify
|| self.index_dirty
|| !self.changed_directories.is_empty()
|| self.worktree.watcher.is_none()
|| self.worktree.rebuild)
{
let index = repo
.index_or_empty()
.or_raise(|| message("could not load index for filesystem monitoring"))?;
let fingerprint = self
.directory_inventory
.as_ref()
.filter(|_| self.index_dirty)
.map(|inventory| inventory.index_fingerprint(&index, self.ignore_case))
.transpose()?;
let changed_index = fingerprint.is_some() && fingerprint != self.index_fingerprint;
if self.inventory_dirty
|| self.inventory_verify
|| changed_index
|| !self.changed_directories.is_empty()
|| self.worktree.watcher.is_none()
|| self.worktree.rebuild
{
let native_recursive = plumbing::Watcher::supports_native_recursive_watches();
let mut directories = match self.directory_inventory.take() {
Some(_) if self.inventory_verify && !native_recursive => {
inventory::worktree_directories(repo, &index)?
}
Some(mut directories) => {
if self.inventory_dirty || changed_index {
self.changed_directories.extend(directories.recheck(repo, &index)?);
}
if self.changed_directories.is_empty() && !self.inventory_verify {
directories
} else {
inventory::worktree_directories_reusing(
repo,
&index,
Some(&directories),
&self.changed_directories,
)?
}
}
_ => inventory::worktree_directories(repo, &index)?,
};
let mut watches = directories.watches_for_backend(native_recursive);
if !self.metadata_is_shared() {
inventory::watch_parent(&self.layout, &mut watches, &self.layout.index);
}
if self.replace_worktree_watches(watches, now, statistics)? {
self.pending_worktree.merge(Changes::worktree_all());
verify = true;
}
self.directories = inventory::directory_lookup(directories.paths.keys().cloned());
self.nested_repositories = std::mem::take(&mut directories.repositories);
self.index_fingerprint = Some(directories.index_fingerprint(&index, self.ignore_case)?);
self.directory_inventory = Some(directories);
}
self.index_dirty = false;
self.inventory_dirty = false;
self.inventory_verify = false;
self.changed_directories.clear();
}
self.maintenance_recovered(now);
if verify {
self.metadata_dirty = true;
self.inventory_verify = true;
self.maintenance = Some(now);
}
Ok(())
}
fn replace_worktree_watches(
&mut self,
watches: BTreeMap<PathBuf, bool>,
now: Instant,
statistics: &mut Statistics,
) -> Result<bool> {
self.worktree.replace(watches, statistics).inspect_err(|_| {
self.coverage_lost(true, now);
})
}
fn maintain_all(&mut self, now: Instant, statistics: &mut Statistics) -> Result<bool> {
if !self.options.metadata && !self.options.additional_paths.is_empty() {
let watches = inventory::additional_watches(&self.options.additional_paths)?;
let changed = self
.metadata
.replace(watches, statistics)
.inspect_err(|_| self.coverage_lost(false, now))?;
self.metadata_dirty = false;
if changed {
self.pending_metadata
.add(Changes::metadata_all_for(&self.options), now, &self.options, true);
self.maintenance = Some(now);
}
}
let Some(root) = self.layout.worktree.clone() else {
return Ok(false);
};
let watch = inventory::recursive_root_watch(&root)?;
let mut watches = BTreeMap::from([(watch.path.clone(), watch.recursive)]);
if self.options.metadata && !self.metadata_is_shared() {
inventory::watch_parent(&self.layout, &mut watches, &self.layout.index);
}
let changed = self.replace_worktree_watches(watches, now, statistics)?;
self.directories = inventory::directory_lookup([root]);
self.nested_repositories.clear();
self.index_fingerprint = None;
self.directory_inventory = None;
self.changed_directories.clear();
self.inventory_dirty = false;
self.inventory_verify = false;
self.index_dirty = false;
if changed {
self.pending_worktree.merge(Changes::worktree_all());
self.maintenance = Some(now);
}
Ok(changed)
}
fn observe(&mut self, event: &Event, worktree: bool, now: Instant) {
if event.paths.is_empty() || matches!(event.kind, EventKind::Any) {
self.coverage_lost(worktree, now);
return;
}
let mut changes = Changes::default();
for original in &event.paths {
let path = if self.precompose_unicode {
gix_utils::str::precompose_path(original.as_path().into())
} else {
original.as_path().into()
};
let path = path.as_ref();
if worktree {
let index_lock = || {
let mut name = self.layout.index.as_os_str().to_os_string();
name.push(".lock");
PathBuf::from(name)
};
let is_index = same_path(path, &self.layout.index, self.ignore_case);
let is_index_lock = same_path(path, &index_lock(), self.ignore_case);
if !self.metadata_is_shared()
&& (self.options.metadata || self.ignore_aware())
&& (is_index || is_index_lock && event.kind == EventKind::Rename)
{
changes.index = true;
self.index_dirty = true;
self.maintenance = Some(now);
continue;
}
let Some(root) = self.layout.worktree.as_deref() else {
continue;
};
if strip_prefix(path, root, self.ignore_case).is_none()
|| strip_prefix(path, &self.layout.git_dir, self.ignore_case).is_some()
|| strip_prefix(root, &self.layout.common_dir, self.ignore_case).is_none()
&& strip_prefix(path, &self.layout.common_dir, self.ignore_case).is_some()
|| strip_prefix(path, &root.join(".git"), self.ignore_case).is_some()
|| !self.ignore_aware()
&& strip_prefix(path, root, self.ignore_case).is_some_and(|relative| {
relative.components().any(|component| {
same_path(Path::new(component.as_os_str()), Path::new(".git"), self.ignore_case)
})
})
{
continue;
}
let known_directory = self
.directories
.find(inventory::path_hash(path), |directory| {
same_path(path, directory, self.ignore_case)
})
.is_some();
let registered_directory = known_directory
&& self
.worktree
.registered
.keys()
.any(|directory| same_path(path, directory, self.ignore_case));
let is_directory = event.path_kind == PathKind::Directory
|| known_directory
|| std::fs::symlink_metadata(path).is_ok_and(|m| m.is_dir());
if is_directory
&& (self.ignore_aware() || registered_directory)
&& matches!(
event.kind,
EventKind::Create | EventKind::Remove | EventKind::Rename | EventKind::Modify
)
{
if !self.ignore_aware() {
self.inventory_dirty = true;
} else if event.kind != EventKind::Modify || self.worktree.registered.get(path) != Some(&true) {
if self.changed_directories.len() >= MAX_SCOPES {
self.inventory_dirty = true;
self.directory_inventory = None;
self.changed_directories.clear();
} else {
self.changed_directories.insert(path.to_owned());
}
}
if self.inventory_dirty || !self.changed_directories.is_empty() {
self.maintenance = Some(now);
}
if registered_directory && matches!(event.kind, EventKind::Remove | EventKind::Rename) {
self.worktree.rebuild = true;
}
}
let name = path.file_name().map(std::ffi::OsStr::as_encoded_bytes);
let name = name.map(|name| {
if self.ignore_case {
std::borrow::Cow::Owned(name.to_ascii_lowercase())
} else {
std::borrow::Cow::Borrowed(name)
}
});
let name = name.as_deref();
let scope = if !self.ignore_aware() {
if self.options.metadata {
match name {
Some(b".gitignore") => changes.ignores = true,
Some(b".gitattributes" | b".gitmodules") => changes.attributes = true,
_ => {}
}
}
path
} else if let Some(repository) = self
.nested_repositories
.iter()
.find(|repository| strip_prefix(path, repository, self.ignore_case).is_some())
{
if name == Some(b".git".as_slice()) {
self.inventory_dirty = true;
self.changed_directories.insert(repository.clone());
self.maintenance = Some(now);
}
repository.as_path()
} else {
match name {
Some(b".git") => {
self.inventory_dirty = true;
self.changed_directories
.insert(path.parent().unwrap_or(root).to_owned());
self.maintenance = Some(now);
path.parent().unwrap_or(root)
}
Some(b".gitignore") => {
changes.ignores = true;
self.inventory_dirty = true;
self.maintenance = Some(now);
path.parent().unwrap_or(root)
}
Some(b".gitattributes" | b".gitmodules") => {
changes.attributes = true;
changes.worktree = Some(Scope::All);
continue;
}
_ => path,
}
};
let (kind, path_kind) = if same_path(scope, path, self.ignore_case) {
(event.kind, event.path_kind)
} else {
(EventKind::Any, PathKind::Directory)
};
match strip_prefix(scope, root, self.ignore_case) {
Some(relative)
if !relative.as_os_str().is_empty()
&& relative
.components()
.all(|c| matches!(c, std::path::Component::Normal(_))) =>
{
match gix_path::into_bstr(relative) {
Ok(relative) => changes.insert_worktree_path(PathChange {
path: gix_path::to_unix_separators_on_windows(relative).into_owned(),
kind,
path_kind,
}),
Err(_) => changes.worktree = Some(Scope::All),
}
}
_ => changes.worktree = Some(Scope::All),
}
} else {
let mut additional = Changes::default();
for dependency in &self.options.additional_paths {
if strip_prefix(path, &dependency.path, self.ignore_case).is_some()
|| strip_prefix(&dependency.path, path, self.ignore_case).is_some()
{
additional.merge(dependency.changes.clone());
if strip_prefix(&dependency.path, path, self.ignore_case).is_some()
&& (event.path_kind != PathKind::File
|| !same_path(path, &dependency.path, self.ignore_case))
{
self.metadata_dirty = true;
self.maintenance = Some(now);
}
}
}
if path
.file_name()
.is_some_and(|name| name.as_encoded_bytes().ends_with(b".lock"))
&& !matches!(event.kind, EventKind::Rename)
&& !self
.layout
.dependencies
.iter()
.any(|dependency| same_path(path, &dependency.path, self.ignore_case))
&& additional.is_empty()
{
continue;
}
if matches!(event.kind, EventKind::Remove | EventKind::Rename)
&& self
.metadata
.registered
.keys()
.any(|registered| strip_prefix(registered, path, self.ignore_case).is_some())
{
self.metadata.rebuild = true;
self.metadata_dirty = true;
self.maintenance = Some(now);
}
let (mut classified, refresh) = if self.shared_metadata {
classify_metadata_scoped(path, &self.layout, self.ignore_case, MetadataScope::Other)
} else if self.all_worktrees_enabled() {
classify_metadata_scoped(path, &self.layout, self.ignore_case, MetadataScope::Primary)
} else if self.options.metadata {
classify_metadata(path, &self.layout, self.ignore_case)
} else {
classify_dependencies(path, &self.layout, self.ignore_case)
};
let refresh = refresh
|| additional.configuration
|| additional.ignores
|| additional.attributes
|| additional.layout;
classified.merge(additional);
if refresh || self.all_worktrees_enabled() && classified.layout {
self.metadata_dirty = true;
if classified.configuration || classified.ignores || classified.attributes || classified.layout {
self.inventory_dirty = true;
self.index_dirty = true;
}
self.maintenance = Some(now);
}
changes.merge(classified);
}
}
if worktree {
self.pending_worktree.merge(changes);
} else {
if self.worktree_enabled() && (changes.configuration || changes.ignores || changes.attributes) {
changes.worktree = Some(Scope::All);
changes.index = true;
}
self.pending_metadata.add(changes, now, &self.options, false);
}
}
}