Skip to main content

Module file_watcher

Module file_watcher 

Source
Expand description

Cross-process file-watcher convenience for the change-event surface.

watch_mem_repo starts a notify-backed file-system watcher against <gitdir>/refs/heads/ and surfaces a std::sync::mpsc::Receiver of MemChangedEvents. Consumers that do not share the writer’s crate::Engine instance (the bridge HEAD-watcher, the macOS live-update path, audit-log workers, webhook notifiers) consume events through this surface; the wire shape matches the in-process callback API exactly so the same downstream code paths work for both sources.

Gated behind the file-watcher Cargo feature — notify is a non-trivial dependency and consumers that only need the in-process crate::Engine::subscribe_mem_changes path should not pay for it. Without the feature enabled the module disappears entirely.

§Design

  • watch_mem_repo spawns a background thread running the notify-event loop and returns a MemRepoWatcher handle that owns the notify::PollWatcher (chosen over RecommendedWatcher for cross-platform determinism — see the rationale at the PollWatcher::new call site). A per-mem HashMap<mem_name, last_seen_sha> (a shared Arc<Mutex<…>> seeded before the thread starts) lets each emitted event’s previous field reflect the actual transition.
  • On startup the thread scans the existing refs/heads/ tree and seeds the SHA map without emitting synthetic history events — consumers see changes from this point forward, never replay.
  • Per file-system event the thread re-reads the touched ref file (or the directory holding it for hierarchical layouts) and emits a MemChangedEvent if the SHA changed. Notify can deliver bursts; the SHA-comparison gate deduplicates them.
  • The returned MemRepoWatcher handle owns the notify watcher; dropping it cancels the watch and the background thread joins on its event channel disconnecting.

§Limitations

  • Reads loose refs only (the typical case for an actively-written mem-repo). Packed refs (<gitdir>/packed-refs) are not parsed in v1 — production deployments that compact refs need a follow-up. Loose refs created after compaction still surface normally.
  • Read-only consumers must poll [crate::ops::changes_since] for any history that landed before the watcher started.

Structs§

MemRepoWatcher
RAII handle returned by watch_mem_repo. Dropping the handle stops the watcher (the underlying notify watcher is dropped, cancelling the OS-level subscription, and the background thread exits when its event channel disconnects).

Enums§

FileWatcherError
Errors surfaced by watch_mem_repo.

Functions§

watch_mem_repo
Start a file-system watcher on <gitdir>/refs/heads/ and surface a receiver of MemChangedEvents. See the module docs for the design and the limitations (loose-refs only, no synthetic replay).