Skip to content

Commit

Permalink
feat(Turborepo): file hash watching
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Soltis authored and Greg Soltis committed Apr 1, 2024
1 parent a33e99f commit b4f569b
Show file tree
Hide file tree
Showing 10 changed files with 714 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions crates/turborepo-filewatch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ tracing = "0.1.37"
tracing-test = "0.2.4"
turbopath = { workspace = true }
turborepo-repository = { version = "0.1.0", path = "../turborepo-repository" }
turborepo-scm = { workspace = true }
walkdir = "2.3.3"
wax = { workspace = true }

Expand All @@ -35,6 +36,7 @@ version = "1.0.4"
version = "0.2.4"

[dev-dependencies]
git2 = { version = "0.16.1", default-features = false }
tempfile = { workspace = true }
tokio-scoped = "0.2.0"

Expand Down
4 changes: 4 additions & 0 deletions crates/turborepo-filewatch/src/cookies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,10 @@ impl<T, U: CookieReady + Clone> CookiedOptionalWatch<T, U> {
self.value.wait_for(|f| f.is_some()).await?;
Ok(SomeRef(self.value.borrow()))
}

pub(crate) fn watch(&self) -> watch::Receiver<Option<T>> {
self.value.clone()
}
}

pub struct CookieRegister(watch::Sender<usize>, AbsoluteSystemPathBuf);
Expand Down
34 changes: 30 additions & 4 deletions crates/turborepo-filewatch/src/globwatcher.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
collections::{HashMap, HashSet},
collections::{BTreeSet, HashMap, HashSet},
fmt::Display,
future::IntoFuture,
str::FromStr,
Expand All @@ -20,10 +20,19 @@ use crate::{

type Hash = String;

#[derive(Clone)]
pub struct GlobSet {
include: HashMap<String, wax::Glob<'static>>,
exclude: Any<'static>,
exclude_raw: Vec<String>,
exclude_raw: BTreeSet<String>,
}

impl GlobSet {
pub fn as_inputs(&self) -> Vec<String> {
let mut inputs: Vec<String> = self.include.keys().cloned().collect();
inputs.extend(self.exclude_raw.iter().cloned());
inputs
}
}

impl std::fmt::Debug for GlobSet {
Expand All @@ -35,6 +44,22 @@ impl std::fmt::Debug for GlobSet {
}
}

impl PartialEq for GlobSet {
fn eq(&self, other: &Self) -> bool {
self.include.keys().collect::<HashSet<_>>() == other.include.keys().collect::<HashSet<_>>()
&& self.exclude_raw == other.exclude_raw
}
}

impl Eq for GlobSet {}

impl std::hash::Hash for GlobSet {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.include.keys().collect::<BTreeSet<_>>().hash(state);
self.exclude_raw.hash(state);
}
}

#[derive(Debug, Error)]
pub struct GlobError {
// Boxed to minimize error size
Expand Down Expand Up @@ -63,7 +88,8 @@ impl GlobSet {
raw_excludes: Vec<String>,
) -> Result<Self, GlobError> {
let include = raw_includes
.into_iter()
.iter()
.cloned()
.map(|raw_glob| {
let glob = compile_glob(&raw_glob)?;
Ok((raw_glob, glob))
Expand All @@ -86,7 +112,7 @@ impl GlobSet {
Ok(Self {
include,
exclude,
exclude_raw: raw_excludes,
exclude_raw: BTreeSet::from_iter(raw_excludes.into_iter()),
})
}
}
Expand Down
Loading

0 comments on commit b4f569b

Please sign in to comment.