OXYGENHUB

Coverage / crates/oxy-core/src/repo/revset.rs 89.8%

582 of 648 lines run, 68 of 150 functions · run #251 on main · run missed

1
//! Evaluating revsets (docs/revset_query_language.md §4) over a repository.
2
//!
3
//! The graph is the change metadata the state index already holds: a
4
//! symbol loads the closure of the heads it names, and `all()`, `X::`,
5
//! `dependents()` and `roots()` load every bookmark, remote bookmark, tag
6
//! and the working copy. Evaluation is read-only and has a time budget;
7
//! a query past it fails and says what to narrow.
8
9
use super::*;
10
use crate::ids::NodeId;
11
use crate::revset::{self, Expr};
12
use crate::state::ChangeMeta;
13
use std::time::{Duration, Instant};
14
15
type Set = BTreeSet<ChangeHash>;
16
17
/// The default budget for a command-line evaluation.
18
pub const REVSET_BUDGET: Duration = Duration::from_secs(10);
19
20
/// A revset's result with the metadata of every change it names.
21
#[derive(Clone, Debug)]
22
pub struct Revset {
23
    /// Newest first, as a log is ordered.
24
    pub hashes: Vec<ChangeHash>,
25
    pub metas: HashMap<ChangeHash, ChangeMeta>,
26
}
27
28
struct Eval<'a> {
29
    repo: &'a Repo,
30
    graph: HashMap<ChangeHash, ChangeMeta>,
31
    /// Which heads have had their closure loaded.
32
    loaded: HashSet<ChangeHash>,
33
    all_loaded: bool,
34
    dependents: Option<HashMap<ChangeHash, Vec<ChangeHash>>>,
35
    names: HashMap<KeyId, Option<(String, String)>>,
36
    /// Who authenticated each change, once asked (signature checks cost).
37
    auth: HashMap<ChangeHash, SignatureStatus>,
38
    deadline: Instant,
39
    budget: Duration,
40
    now_ms: u64,
41
    depth: usize,
42
}
43
444×
fn err(msg: impl Into<String>) -> RepoError {
454×
    RepoError::Msg(msg.into())
464×
}
47
48
impl<'a> Eval<'a> {
49213×
    fn tick(&self) -> Result<()> {
50213×
        if Instant::now() > self.deadline {
511×
            return Err(err(format!(
521×
                "the revset went past its budget of {:.0} s; narrow it with a range such as main.. or a date()",
531×
                self.budget.as_secs_f64()
541×
            )));
55212×
        }
56212×
        Ok(())
57213×
    }
58
59
    /// Load the closure of `heads` into the graph.
6078×
    fn ensure(&mut self, heads: &[ChangeHash]) -> Result<()> {
6178×
        let fresh: Vec<ChangeHash> = heads
6278×
            .iter()
6378×
            .copied()
64202×
            .filter(|h| !self.loaded.contains(h))
6578×
            .collect();
6678×
        if fresh.is_empty() {
678×
            return Ok(());
6870×
        }
69275×
        for (h, m) in self.repo.metas_of(&fresh)? {
70275×
            self.graph.insert(h, m);
71275×
        }
7270×
        self.loaded.extend(fresh);
7370×
        self.dependents = None;
7470×
        self.tick()
7578×
    }
76
77
    /// Load every change the repository names: bookmarks, remote
78
    /// bookmarks, tags, and the working copy.
7924×
    fn load_all(&mut self) -> Result<()> {
8024×
        if self.all_loaded {
810×
            return Ok(());
8224×
        }
8324×
        let view = self.repo.view()?;
8424×
        let mut heads: Vec<ChangeHash> = Vec::new();
8524×
        for st in view.bookmarks.values() {
8624×
            heads.extend(self.repo.store().get_state(st)?.heads);
87
        }
8824×
        for bookmarks in view.remotes.values() {
891×
            for st in bookmarks.values() {
901×
                heads.extend(self.repo.store().get_state(st)?.heads);
91
            }
92
        }
9324×
        for st in view.tags.values() {
944×
            heads.extend(self.repo.store().get_state(st)?.heads);
95
        }
9624×
        if let Some(wc) = self.working_copy() {
9724×
            heads.push(wc);
9824×
        }
9924×
        heads.sort();
10024×
        heads.dedup();
10124×
        self.ensure(&heads)?;
10224×
        self.all_loaded = true;
10324×
        Ok(())
10424×
    }
105
10635×
    fn working_copy(&self) -> Option<ChangeHash> {
10735×
        if self.repo.is_bare() {
1080×
            None
109
        } else {
11035×
            self.repo.working_copy().ok().map(|(h, _)| h)
111
        }
11235×
    }
113
11418×
    fn all(&mut self) -> Result<Set> {
11518×
        self.load_all()?;
11618×
        Ok(self.graph.keys().copied().collect())
11718×
    }
118
1196×
    fn dependents_map(&mut self) -> Result<&HashMap<ChangeHash, Vec<ChangeHash>>> {
1206×
        self.load_all()?;
1216×
        if self.dependents.is_none() {
1226×
            let mut map: HashMap<ChangeHash, Vec<ChangeHash>> = HashMap::new();
12330×
            for (h, m) in &self.graph {
12430×
                for d in &m.deps {
12530×
                    map.entry(*d).or_default().push(*h);
12630×
                }
127
            }
1286×
            self.dependents = Some(map);
1290×
        }
1306×
        Ok(self.dependents.as_ref().unwrap())
1316×
    }
132
133
    /// `::X`: the members and their dependency closures.
13435×
    fn ancestors(&mut self, set: &Set) -> Result<Set> {
13535×
        let mut out = set.clone();
13635×
        let mut stack: Vec<ChangeHash> = set.iter().copied().collect();
13735×
        let mut n = 0usize;
138149×
        while let Some(h) = stack.pop() {
139114×
            if let Some(m) = self.graph.get(&h) {
140114×
                for d in &m.deps {
14166×
                    if out.insert(*d) {
14238×
                        stack.push(*d);
14338×
                    }
144
                }
1450×
            }
146114×
            n += 1;
147114×
            if n.is_multiple_of(4096) {
1480×
                self.tick()?;
149114×
            }
150
        }
15135×
        Ok(out)
15235×
    }
153
154
    /// `X::`: the members and everything that transitively depends on them.
1554×
    fn descendants(&mut self, set: &Set) -> Result<Set> {
1564×
        let mut out = set.clone();
1574×
        let mut stack: Vec<ChangeHash> = set.iter().copied().collect();
1584×
        let deps = self.dependents_map()?;
15918×
        while let Some(h) = stack.pop() {
16014×
            if let Some(ds) = deps.get(&h) {
16113×
                for d in ds {
16213×
                    if out.insert(*d) {
16310×
                        stack.push(*d);
16410×
                    }
165
                }
1664×
            }
167
        }
1684×
        Ok(out)
1694×
    }
170
171
    /// Members no other member depends on, directly or through others.
1722×
    fn heads(&mut self, set: &Set) -> Result<Set> {
1732×
        let mut below: HashSet<ChangeHash> = HashSet::new();
1742×
        let mut stack: Vec<ChangeHash> = set
1752×
            .iter()
1767×
            .filter_map(|h| self.graph.get(h))
1777×
            .flat_map(|m| m.deps.iter().copied())
1782×
            .collect();
1795×
        while let Some(h) = stack.pop() {
1803×
            if below.insert(h) {
1812×
                if let Some(m) = self.graph.get(&h) {
1822×
                    stack.extend(m.deps.iter().copied());
1832×
                }
1841×
            }
185
        }
1867×
        Ok(set.iter().copied().filter(|h| !below.contains(h)).collect())
1872×
    }
188
189
    /// Members that depend on no other member, directly or through others.
1901×
    fn roots(&mut self, set: &Set) -> Result<Set> {
1911×
        let deps = self.dependents_map()?;
1921×
        let mut above: HashSet<ChangeHash> = HashSet::new();
1931×
        let mut stack: Vec<ChangeHash> = set
1941×
            .iter()
1954×
            .filter_map(|h| deps.get(h))
1964×
            .flat_map(|ds| ds.iter().copied())
1971×
            .collect();
1988×
        while let Some(h) = stack.pop() {
1997×
            if above.insert(h) {
2003×
                if let Some(ds) = deps.get(&h) {
2012×
                    stack.extend(ds.iter().copied());
2022×
                }
2034×
            }
204
        }
2054×
        Ok(set.iter().copied().filter(|h| !above.contains(h)).collect())
2061×
    }
207
20843×
    fn symbol(&mut self, name: &str) -> Result<Set> {
20943×
        if name == "@" {
2104×
            let Some(wc) = self.working_copy() else {
2110×
                return Err(err(
2120×
                    "@ names the working copy, and a bare repository has none",
2130×
                ));
214
            };
2154×
            self.ensure(&[wc])?;
2164×
            return Ok([wc].into_iter().collect());
21739×
        }
21839×
        let heads = self.repo.resolve_heads(name)?;
21938×
        self.ensure(&heads)?;
22038×
        Ok(heads.into_iter().collect())
22143×
    }
222
223
    /// The identity's name and email, if its object is known.
2245×
    fn who(&mut self, key: &KeyId) -> Option<(String, String)> {
2255×
        if let Some(w) = self.names.get(key) {
2260×
            return w.clone();
2275×
        }
2285×
        let w = self.repo.identity(key).ok().map(|id| (id.name, id.email));
2295×
        self.names.insert(*key, w.clone());
2305×
        w
2315×
    }
232
233
    /// The set a predicate ranges over: what it is intersected with when
234
    /// that is known, else everything.
23530×
    fn universe(&mut self, within: Option<&Set>) -> Result<Set> {
23630×
        match within {
23712×
            Some(s) => Ok(s.clone()),
23818×
            None => self.all(),
239
        }
24030×
    }
241
2425×
    fn filter_metas(
2435×
        &mut self,
2445×
        within: Option<&Set>,
2455×
        mut keep: impl FnMut(&ChangeHash, &ChangeMeta) -> bool,
2465×
    ) -> Result<Set> {
2475×
        let candidates = self.universe(within)?;
2485×
        let mut out = Set::new();
24924×
        for (i, h) in candidates.iter().enumerate() {
25024×
            if let Some(m) = self.graph.get(h) {
25124×
                if keep(h, m) {
2527×
                    out.insert(*h);
25317×
                }
2540×
            }
25524×
            if i % 8192 == 8191 {
2560×
                self.tick()?;
25724×
            }
258
        }
2595×
        Ok(out)
2605×
    }
261
262
    /// Changes touching any node whose path matches `glob` in the
263
    /// evaluation state: the working copy's base, or the bookmark's state
264
    /// in a bare repository. Nodes keep their identity through renames.
2654×
    fn touches(&mut self, within: Option<&Set>, glob: &str) -> Result<Set> {
2664×
        let matcher = globset::GlobBuilder::new(glob)
2674×
            .literal_separator(false)
2684×
            .build()
2694×
            .map_err(|e| err(format!("touches({glob:?}): {e}")))?
2704×
            .compile_matcher();
2714×
        let heads = match self.working_copy() {
2724×
            Some(wc) => self
2734×
                .graph
2744×
                .get(&wc)
2754×
                .map(|m| m.deps.clone())
2764×
                .or_else(|| self.repo.working_copy().ok().map(|(_, c)| c.deps))
2774×
                .unwrap_or_default(),
2780×
            None => self.repo.bookmark_heads()?,
279
        };
2804×
        let state = self.repo.load_state(&heads)?;
2814×
        let nodes: HashSet<NodeId> = state
2824×
            .tree()
2834×
            .view()
2844×
            .entries
2854×
            .iter()
28636×
            .filter(|(path, _)| matcher.is_match(path))
2874×
            .map(|(_, e)| e.node)
2884×
            .collect();
2894×
        if nodes.is_empty() {
2901×
            return Ok(Set::new());
2913×
        }
2923×
        let candidates = self.universe(within)?;
2933×
        let mut out = Set::new();
29414×
        for (i, h) in candidates.iter().enumerate() {
29514×
            let Ok(c) = self.repo.store().get_change(h) else {
2960×
                continue;
297
            };
29830×
            if c.tree_ops.iter().any(|o| nodes.contains(&o.node()))
2999×
                || c.edits.iter().any(|e| nodes.contains(&e.node()))
3007×
            {
3017×
                out.insert(*h);
3027×
            }
30314×
            if i % 256 == 255 {
3040×
                self.tick()?;
30514×
            }
306
        }
3073×
        Ok(out)
3084×
    }
309
31028×
    fn text_arg<'e>(&self, name: &str, args: &'e [Expr], i: usize) -> Result<&'e str> {
31128×
        match args.get(i) {
31228×
            Some(Expr::Symbol(s)) => Ok(s),
3130×
            _ => Err(err(format!("{name}() takes text, quoted if it has spaces"))),
314
        }
31528×
    }
316
31747×
    fn arity(&self, name: &str, args: &[Expr], n: usize) -> Result<()> {
31847×
        if args.len() != n {
3190×
            return Err(err(format!(
320
                "{name}() takes {n} argument{}, not {}",
3210×
                if n == 1 { "" } else { "s" },
3220×
                args.len()
323
            )));
32447×
        }
32547×
        Ok(())
32647×
    }
327
32852×
    fn call(&mut self, name: &str, args: &[Expr], within: Option<&Set>) -> Result<Set> {
32952×
        match name {
33052×
            "all" => {
3312×
                self.arity(name, args, 0)?;
3322×
                self.universe(within)
333
            }
33450×
            "none" => {
3351×
                self.arity(name, args, 0)?;
3361×
                Ok(Set::new())
337
            }
33849×
            "deps" => {
3391×
                self.arity(name, args, 1)?;
3401×
                let x = self.eval(&args[0], None)?;
3411×
                let mut out = Set::new();
3423×
                for h in &x {
3433×
                    if let Some(m) = self.graph.get(h) {
3443×
                        out.extend(m.deps.iter().copied());
3453×
                    }
346
                }
3471×
                Ok(out)
348
            }
34948×
            "dependents" => {
3501×
                self.arity(name, args, 1)?;
3511×
                let x = self.eval(&args[0], None)?;
3521×
                let deps = self.dependents_map()?;
3531×
                let mut out = Set::new();
3541×
                for h in &x {
3551×
                    if let Some(ds) = deps.get(h) {
3561×
                        out.extend(ds.iter().copied());
3571×
                    }
358
                }
3591×
                Ok(out)
360
            }
36147×
            "heads" => {
3622×
                self.arity(name, args, 1)?;
3632×
                let x = self.eval(&args[0], None)?;
3642×
                self.heads(&x)
365
            }
36645×
            "roots" => {
3671×
                self.arity(name, args, 1)?;
3681×
                let x = self.eval(&args[0], None)?;
3691×
                self.roots(&x)
370
            }
37144×
            "independent" => {
3722×
                self.arity(name, args, 2)?;
3732×
                let x = self.eval(&args[0], None)?;
3742×
                let inn = self.eval(&args[1], None)?;
3752×
                let below = self.ancestors(&x)?;
3762×
                let above = self.descendants(&x)?;
3772×
                Ok(inn
3782×
                    .into_iter()
3799×
                    .filter(|h| !below.contains(h) && !above.contains(h))
3802×
                    .collect())
381
            }
38242×
            "conflicts" => {
3834×
                if args.len() > 1 {
3840×
                    return Err(err(
3850×
                        "conflicts() takes a state, or nothing for the working copy's",
3860×
                    ));
3874×
                }
3884×
                let heads: Vec<ChangeHash> = match args.first() {
3891×
                    Some(e) => self.eval(e, None)?.into_iter().collect(),
3903×
                    None => match self.working_copy() {
3913×
                        Some(wc) => {
3923×
                            self.ensure(&[wc])?;
3933×
                            self.graph
3943×
                                .get(&wc)
3953×
                                .map(|m| m.deps.clone())
3963×
                                .unwrap_or_default()
397
                        }
3980×
                        None => self.repo.bookmark_heads()?,
399
                    },
400
                };
4014×
                self.conflicts(within, &heads)
402
            }
40338×
            "latest" => {
4041×
                self.arity(name, args, 2)?;
4051×
                let x = self.eval(&args[0], None)?;
4061×
                let n: usize = self
4071×
                    .text_arg(name, args, 1)?
4081×
                    .parse()
4091×
                    .map_err(|_| err("latest(X, n) takes a count"))?;
4101×
                let mut members: Vec<ChangeHash> = x.into_iter().collect();
4111×
                self.sort_newest_first(&mut members);
4121×
                Ok(members.into_iter().take(n).collect())
413
            }
41437×
            "author" => {
4157×
                self.arity(name, args, 1)?;
4167×
                let who = self.text_arg(name, args, 0)?.to_string();
4177×
                let mut matches: HashMap<KeyId, bool> = HashMap::new();
41827×
                self.filter_metas_with(within, |ev, _, m| {
41927×
                    if let Some(hit) = matches.get(&m.author) {
42020×
                        return *hit;
4217×
                    }
4227×
                    let hit = ev.names_key(&who, &m.author);
4237×
                    matches.insert(m.author, hit);
4247×
                    hit
42527×
                })
426
            }
42730×
            "description" => {
4282×
                self.arity(name, args, 1)?;
4292×
                let text = self.text_arg(name, args, 0)?.to_string();
4302×
                if text.starts_with("re:") {
4310×
                    return Err(err(
4320×
                        "description(re:…) is not supported yet; give a substring",
4330×
                    ));
4342×
                }
4352×
                let needle = text.to_lowercase();
43610×
                self.filter_metas(within, |_, m| m.message.to_lowercase().contains(&needle))
437
            }
43828×
            "date" => {
4392×
                self.arity(name, args, 1)?;
4402×
                let range = revset::parse_date_range(self.text_arg(name, args, 0)?, self.now_ms)
4412×
                    .map_err(err)?;
4429×
                self.filter_metas(within, |_, m| range.contains(m.hlc.wall_ms))
443
            }
44426×
            "kind" => {
4451×
                self.arity(name, args, 1)?;
4461×
                let kind = match self.text_arg(name, args, 0)? {
4471×
                    "normal" => ChangeKind::Normal,
4481×
                    "resolution" => ChangeKind::Resolution,
4491×
                    "working-copy" | "working_copy" => ChangeKind::WorkingCopy,
4500×
                    other => {
4510×
                        return Err(err(format!(
4520×
                            "kind({other:?}): use normal, resolution or working-copy"
4530×
                        )))
454
                    }
455
                };
4565×
                self.filter_metas(within, |_, m| m.kind == kind)
457
            }
45825×
            "touches" => {
4594×
                self.arity(name, args, 1)?;
4604×
                let glob = self.text_arg(name, args, 0)?.to_string();
4614×
                self.touches(within, &glob)
462
            }
463
            // Trust (§2.4): what authenticated a change, and what vouched
464
            // for the states it is in.
46521×
            "signed_by" => {
4665×
                self.arity(name, args, 1)?;
4675×
                let who = self.text_arg(name, args, 0)?.to_string();
46825×
                self.filter_metas_with(within, |ev, h, m| match ev.auth(h, m) {
4694×
                    SignatureStatus::Valid { key, .. } => ev.names_key(&who, &key),
47021×
                    _ => false,
47125×
                })
472
            }
47316×
            "delegated" => {
4742×
                self.arity(name, args, 0)?;
4758×
                self.filter_metas_with(within, |ev, h, m| {
4768×
                    !m.author.is_zero()
4778×
                        && matches!(ev.auth(h, m), SignatureStatus::Valid { key, .. } if key != m.author)
4788×
                })
479
            }
48014×
            "unsigned" => {
4813×
                self.arity(name, args, 0)?;
48210×
                self.filter_metas_with(within, |ev, h, m| {
48310×
                    m.kind != ChangeKind::WorkingCopy
48410×
                        && !matches!(ev.auth(h, m), SignatureStatus::Valid { .. })
48510×
                })
486
            }
48711×
            "revoked" => {
4883×
                self.arity(name, args, 0)?;
48911×
                self.filter_metas_with(within, |ev, h, m| {
49011×
                    matches!(ev.auth(h, m), SignatureStatus::Valid { revoked: true, .. })
49111×
                })
492
            }
4938×
            "attested" => {
4945×
                self.arity(name, args, 1)?;
4955×
                let kind = self.text_arg(name, args, 0)?.to_string();
4965×
                self.attested(within, &kind)
497
            }
4983×
            "built" => {
4991×
                self.arity(name, args, 1)?;
5001×
                let job = self.text_arg(name, args, 0)?.to_string();
5011×
                self.built(within, &job)
502
            }
5032×
            other => {
504
                // A saved revset: `oxy config revset.NAME 'EXPR'` makes NAME().
5052×
                if let Some(saved) = self.repo.config.revsets.get(other).cloned() {
5061×
                    self.arity(name, args, 0)?;
5071×
                    if self.depth >= 16 {
5080×
                        return Err(err(format!("revset.{other} refers to itself")));
5091×
                    }
5101×
                    let e = revset::parse(&saved)
5111×
                        .map_err(|e| err(format!("revset.{other} = {saved:?}: {e}")))?;
5121×
                    self.depth += 1;
5131×
                    let r = self.eval(&e, within);
5141×
                    self.depth -= 1;
5151×
                    return r;
5161×
                }
5171×
                Err(err(format!(
5181×
                    "unknown function {other}(); see docs/revset_query_language.md"
5191×
                )))
520
            }
521
        }
52252×
    }
523
524
    /// The authentication of a change: its signature or seal, checked once.
52554×
    fn auth(&mut self, h: &ChangeHash, m: &ChangeMeta) -> SignatureStatus {
52654×
        if let Some(s) = self.auth.get(h) {
5271×
            return s.clone();
52853×
        }
52953×
        let s = self
53053×
            .repo
53153×
            .authenticate_change_known(h, Some(m))
53253×
            .unwrap_or_else(|e| SignatureStatus::Invalid(e.to_string()));
53353×
        self.auth.insert(*h, s.clone());
53453×
        s
53554×
    }
536
537
    /// Whether `who` names key `key`: by hex prefix, `me`, or the name or
538
    /// email of its identity.
53911×
    fn names_key(&mut self, who: &str, key: &KeyId) -> bool {
54011×
        (who == "me" && Some(*key) == self.repo.config.author_key)
5417×
            || key.starts_with_hex(who)
5425×
            || self.who(key).is_some_and(|(n, e)| {
5435×
                let needle = who.to_lowercase();
5445×
                n.to_lowercase().contains(&needle) || e.to_lowercase().contains(&needle)
5455×
            })
54611×
    }
547
548
    /// The closure of the states whose heads these are, within `within`.
5495×
    fn closure_of(&mut self, mut heads: Vec<ChangeHash>, within: Option<&Set>) -> Result<Set> {
5505×
        heads.sort();
5515×
        heads.dedup();
5525×
        self.ensure(&heads)?;
5535×
        let set: Set = heads.into_iter().collect();
5545×
        let closure = self.ancestors(&set)?;
5555×
        Ok(match within {
5560×
            Some(w) => closure.intersection(w).copied().collect(),
5575×
            None => closure,
558
        })
5595×
    }
560
561
    /// The closure of every state that carries an attestation of `kind`:
562
    /// `tag` (tagged states), `receipt` (states a runner reported on) or
563
    /// `ref` (states a remote attested, as its tracking bookmarks hold).
5645×
    fn attested(&mut self, within: Option<&Set>, kind: &str) -> Result<Set> {
5655×
        let mut heads: Vec<ChangeHash> = Vec::new();
5665×
        match kind {
5675×
            "tag" => {
5683×
                for t in self.repo.tags()? {
5692×
                    heads.extend(t.heads);
5702×
                }
571
            }
5722×
            "receipt" => {
5731×
                for st in self.repo.store().receipt_states()? {
5740×
                    if let Ok(s) = self.repo.store().get_state(&st) {
5750×
                        heads.extend(s.heads);
5760×
                    }
577
                }
578
            }
5791×
            "ref" => {
5800×
                let view = self.repo.view()?;
5810×
                for bookmarks in view.remotes.values() {
5820×
                    for st in bookmarks.values() {
5830×
                        heads.extend(self.repo.store().get_state(st)?.heads);
584
                    }
585
                }
586
            }
5871×
            other => return Err(err(format!("attested({other:?}): use tag, receipt or ref"))),
588
        }
5894×
        self.closure_of(heads, within)
5905×
    }
591
592
    /// The closure of every state with a valid receipt for Flow job `job`
593
    /// from a runner whose key is not revoked.
5941×
    fn built(&mut self, within: Option<&Set>, job: &str) -> Result<Set> {
5951×
        let mut heads: Vec<ChangeHash> = Vec::new();
5961×
        for st in self.repo.store().receipt_states()? {
5970×
            let Ok(state) = self.repo.store().get_state(&st) else {
5980×
                continue;
599
            };
6000×
            let good = self
6010×
                .repo
6020×
                .receipts(&state.heads)?
6030×
                .into_iter()
6040×
                .any(|r| r.signed.receipt.job_name == job && r.problem.is_none() && !r.revoked);
6050×
            if good {
6060×
                heads.extend(state.heads);
6070×
            }
6080×
            self.tick()?;
609
        }
6101×
        self.closure_of(heads, within)
6111×
    }
612
613
    /// `conflicts(S)`: the changes that contributed atoms to a conflict in
614
    /// the state `heads` name (conflicts belong to states, not changes).
6154×
    fn conflicts(&mut self, within: Option<&Set>, heads: &[ChangeHash]) -> Result<Set> {
6164×
        let state = self.repo.load_state(heads)?;
6174×
        let snapshot = state.materialize();
6184×
        let mut out = Set::new();
61936×
        for f in snapshot.files.values() {
62036×
            if !f.is_conflicted() {
62134×
                continue;
6222×
            }
6232×
            match &f.content {
6242×
                crate::state::FileContent::Text { .. } => {
6252×
                    out.extend(state.conflict_regions(&f.node).contributors);
6262×
                }
6270×
                crate::state::FileContent::Opaque { blobs } => {
6280×
                    out.extend(state.blob_contributors(&f.node, blobs));
6290×
                }
6300×
                crate::state::FileContent::Dir => {}
631
            }
6322×
            self.tick()?;
633
        }
6344×
        self.ensure(heads)?;
6354×
        Ok(match within {
6360×
            Some(w) => out.intersection(w).copied().collect(),
6374×
            None => out,
638
        })
6394×
    }
640
641
    /// `filter_metas` for a test that needs the evaluator (identity lookups).
64220×
    fn filter_metas_with(
64320×
        &mut self,
64420×
        within: Option<&Set>,
64520×
        mut keep: impl FnMut(&mut Eval<'a>, &ChangeHash, &ChangeMeta) -> bool,
64620×
    ) -> Result<Set> {
64720×
        let candidates = self.universe(within)?;
64820×
        let mut out = Set::new();
64981×
        for (i, h) in candidates.iter().enumerate() {
65081×
            if let Some(m) = self.graph.get(h).cloned() {
65181×
                if keep(self, h, &m) {
65236×
                    out.insert(*h);
65345×
                }
6540×
            }
65581×
            if i % 8192 == 8191 {
6560×
                self.tick()?;
65781×
            }
658
        }
65920×
        Ok(out)
66020×
    }
661
662
    /// Whether an expression selects by a property, so that evaluating it
663
    /// within the other side of an intersection saves scanning everything.
66423×
    fn is_predicate(e: &Expr) -> bool {
66523×
        match e {
66613×
            Expr::Call(name, _) => matches!(
66713×
                name.as_str(),
66813×
                "author"
6697×
                    | "description"
6707×
                    | "date"
6716×
                    | "kind"
6726×
                    | "touches"
6735×
                    | "all"
6745×
                    | "signed_by"
6755×
                    | "delegated"
6765×
                    | "unsigned"
6772×
                    | "revoked"
6782×
                    | "attested"
6791×
                    | "built"
6801×
                    | "conflicts"
681
            ),
6820×
            Expr::Intersection(a, b) => Self::is_predicate(a) || Self::is_predicate(b),
6830×
            Expr::Union(a, b) => Self::is_predicate(a) && Self::is_predicate(b),
6840×
            Expr::Difference(a, _) => Self::is_predicate(a),
68510×
            _ => false,
686
        }
68723×
    }
688
689141×
    fn eval(&mut self, e: &Expr, within: Option<&Set>) -> Result<Set> {
690141×
        self.tick()?;
691140×
        match e {
69243×
            Expr::Symbol(s) => self.symbol(s),
69323×
            Expr::Ancestors(x) => {
69423×
                let x = self.eval(x, None)?;
69523×
                self.ancestors(&x)
696
            }
6971×
            Expr::Descendants(x) => {
6981×
                let x = self.eval(x, None)?;
6991×
                self.descendants(&x)
700
            }
7011×
            Expr::Between(x, y) => {
7021×
                let x = self.eval(x, None)?;
7031×
                let y = self.eval(y, None)?;
7041×
                let down = self.descendants(&x)?;
7051×
                let up = self.ancestors(&y)?;
7061×
                Ok(down.intersection(&up).copied().collect())
707
            }
7082×
            Expr::Range(x, y) => {
7092×
                let x = self.eval(x, None)?;
7102×
                let y = self.eval(y, None)?;
7112×
                let from = self.ancestors(&x)?;
7122×
                let to = self.ancestors(&y)?;
7132×
                Ok(to.difference(&from).copied().collect())
714
            }
7153×
            Expr::Union(a, b) => {
7163×
                let mut a = self.eval(a, within)?;
7173×
                a.extend(self.eval(b, within)?);
7183×
                Ok(a)
719
            }
72012×
            Expr::Intersection(a, b) => {
72112×
                let (first, second) = if Self::is_predicate(a) && !Self::is_predicate(b) {
7229×
                    (b, a)
723
                } else {
7243×
                    (a, b)
725
                };
72612×
                let left = self.eval(first, within)?;
72712×
                let right = self.eval(second, Some(&left))?;
72812×
                Ok(left.intersection(&right).copied().collect())
729
            }
7303×
            Expr::Difference(a, b) => {
7313×
                let left = self.eval(a, within)?;
7323×
                let right = self.eval(b, Some(&left))?;
7333×
                Ok(left.difference(&right).copied().collect())
734
            }
73552×
            Expr::Call(name, args) => self.call(name, args, within),
736
        }
737141×
    }
738
73960×
    fn sort_newest_first(&self, hashes: &mut [ChangeHash]) {
74091×
        hashes.sort_by(|a, b| {
74191×
            let (ma, mb) = (self.graph.get(a), self.graph.get(b));
74291×
            mb.map(|m| m.hlc).cmp(&ma.map(|m| m.hlc)).then(b.cmp(a))
74391×
        });
74460×
    }
745
}
746
747
impl Repo {
748
    /// Evaluate a revset within [`REVSET_BUDGET`].
74963×
    pub fn revset(&self, src: &str) -> Result<Revset> {
75063×
        self.revset_with_budget(src, REVSET_BUDGET)
75163×
    }
752
753
    /// Evaluate a revset: the changes it selects, newest first, with
754
    /// their metadata. Read-only. Fails past `budget`.
75564×
    pub fn revset_with_budget(&self, src: &str, budget: Duration) -> Result<Revset> {
75664×
        let expr = revset::parse(src).map_err(|e| err(format!("revset {src:?}: {e}")))?;
75763×
        let mut ev = Eval {
75863×
            repo: self,
75963×
            graph: HashMap::new(),
76063×
            loaded: HashSet::new(),
76163×
            all_loaded: false,
76263×
            dependents: None,
76363×
            names: HashMap::new(),
76463×
            auth: HashMap::new(),
76563×
            deadline: Instant::now() + budget,
76663×
            budget,
76763×
            now_ms: Hlc::now().wall_ms,
76863×
            depth: 0,
76963×
        };
77063×
        let set = ev.eval(&expr, None)?;
77159×
        let mut hashes: Vec<ChangeHash> = set.into_iter().collect();
77259×
        ev.sort_newest_first(&mut hashes);
77359×
        let metas = hashes
77459×
            .iter()
775112×
            .filter_map(|h| ev.graph.get(h).map(|m| (*h, m.clone())))
77659×
            .collect();
77759×
        Ok(Revset { hashes, metas })
77864×
    }
779
780
    /// The log entries a revset selects, newest first. A head of the
781
    /// current bookmark is marked as in `log`.
7821×
    pub fn log_revset(&self, src: &str) -> Result<Vec<LogEntry>> {
7831×
        let r = self.revset(src)?;
7841×
        let heads: BTreeSet<ChangeHash> = self.bookmark_heads()?.into_iter().collect();
7851×
        let wc = if self.is_bare() {
7860×
            None
787
        } else {
7881×
            self.working_copy().ok().map(|(h, _)| h)
789
        };
7901×
        let mut entries: Vec<LogEntry> = r
7911×
            .hashes
7921×
            .iter()
7935×
            .filter_map(|h| {
7945×
                r.metas.get(h).map(|m| LogEntry {
7955×
                    hash: *h,
7965×
                    change: m.clone(),
7975×
                    is_working_copy: Some(*h) == wc,
7985×
                    is_head: heads.contains(h),
7995×
                })
8005×
            })
8011×
            .collect();
802
        // The working copy first, as `log` shows it.
8038×
        entries.sort_by_key(|e| !e.is_working_copy);
8041×
        Ok(entries)
8051×
    }
806
}