ZIm/crates/fs/src/mac_watcher.rs
Thorsten Ball bd03dea296
git: Add support for opening git worktrees (#20164)
This adds support for [git
worktrees](https://matklad.github.io/2024/07/25/git-worktrees.html). It
fixes the errors that show up (git blame not working) and actually adds
support for detecting git changes in a `.git` folder that's outside of
our path (and not even in the ancestor chain of our root path).

(While working on this we discovered that our `.gitignore` handling is
not 100% correct. For example: we do stop processing `.gitignore` files
once we found a `.git` repository and don't go further up the ancestors,
which is correct, but then we also don't take into account the
`excludesFile` that a user might have configured, see:
https://git-scm.com/docs/gitignore)


Closes https://github.com/zed-industries/zed/issues/19842
Closes https://github.com/zed-industries/zed/issues/4670

Release Notes:

- Added support for git worktrees. Zed can now open git worktrees and
the git status in them is correctly handled.

---------

Co-authored-by: Antonio <antonio@zed.dev>
Co-authored-by: Bennet <bennet@zed.dev>
2024-11-06 09:43:39 +01:00

70 lines
1.8 KiB
Rust

use crate::Watcher;
use anyhow::{Context as _, Result};
use collections::{BTreeMap, Bound};
use fsevent::EventStream;
use parking_lot::Mutex;
use std::{
path::{Path, PathBuf},
sync::Weak,
time::Duration,
};
pub struct MacWatcher {
events_tx: smol::channel::Sender<Vec<fsevent::Event>>,
handles: Weak<Mutex<BTreeMap<PathBuf, fsevent::Handle>>>,
latency: Duration,
}
impl MacWatcher {
pub fn new(
events_tx: smol::channel::Sender<Vec<fsevent::Event>>,
handles: Weak<Mutex<BTreeMap<PathBuf, fsevent::Handle>>>,
latency: Duration,
) -> Self {
Self {
events_tx,
handles,
latency,
}
}
}
impl Watcher for MacWatcher {
fn add(&self, path: &Path) -> Result<()> {
let handles = self
.handles
.upgrade()
.context("unable to watch path, receiver dropped")?;
let mut handles = handles.lock();
// Return early if an ancestor of this path was already being watched.
if let Some((watched_path, _)) = handles
.range::<Path, _>((Bound::Unbounded, Bound::Included(path)))
.next_back()
{
if path.starts_with(watched_path) {
return Ok(());
}
}
let (stream, handle) = EventStream::new(&[path], self.latency);
let tx = self.events_tx.clone();
std::thread::spawn(move || {
stream.run(move |events| smol::block_on(tx.send(events)).is_ok());
});
handles.insert(path.into(), handle);
Ok(())
}
fn remove(&self, path: &Path) -> gpui::Result<()> {
let handles = self
.handles
.upgrade()
.context("unable to remove path, receiver dropped")?;
let mut handles = handles.lock();
handles.remove(path);
Ok(())
}
}