Skip to main content

specfold_core/check/
scope.rs

1//! Which documents a run looks at: all of them, or the ones touched by a change and the
2//! documents that point at them.
3
4use crate::corpus::Corpus;
5use crate::graph::Graph;
6use crate::workspace::vcs;
7use anyhow::{Result, anyhow};
8use std::collections::BTreeSet;
9
10#[derive(Debug, Clone)]
11pub enum Range {
12    /// Everything.
13    All,
14    /// The index (pre-commit).
15    Staged,
16    /// Working tree and untracked files against a base revision (pre-push).
17    Changed(String),
18}
19
20#[derive(Debug, Clone)]
21pub struct Scope {
22    pub range: Range,
23    /// Changed repository paths; empty for `All`.
24    pub paths: BTreeSet<String>,
25    /// The part of `paths` inside submodules whose gitlink the change moves; the outer index
26    /// cannot show them, so a staged run reads them from the submodule's checkout.
27    pub in_submodules: BTreeSet<String>,
28    /// Indices of documents whose own files changed.
29    pub touched: BTreeSet<usize>,
30    /// `touched` plus documents that link to them.
31    pub with_neighbours: BTreeSet<usize>,
32    /// The revision to compare against for transition checks.
33    pub base: Option<String>,
34}
35
36impl Scope {
37    pub fn build(c: &Corpus, g: &Graph, range: Range) -> Result<Self> {
38        let root = &c.cfg.root;
39        let (mut paths, in_submodules, base): (BTreeSet<String>, BTreeSet<String>, _) = match &range {
40            Range::All => (BTreeSet::new(), BTreeSet::new(), None),
41            Range::Staged => {
42                let p = vcs::staged(root).ok_or_else(|| anyhow!("--staged needs a git repository"))?;
43                let sub = vcs::submodule_changes(root, vcs::Side::Staged);
44                (p.into_iter().collect(), sub.into_iter().collect(), Some("HEAD".to_string()))
45            }
46            Range::Changed(base) => {
47                let p = vcs::changed_since(root, base).ok_or_else(|| anyhow!("cannot diff against {base}"))?;
48                let sub = vcs::submodule_changes(root, vcs::Side::Since(base));
49                (p.into_iter().collect(), sub.into_iter().collect(), Some(base.clone()))
50            }
51        };
52        paths.extend(in_submodules.iter().cloned());
53        let mut touched = BTreeSet::new();
54        if !matches!(range, Range::All) {
55            for (i, d) in c.docs.iter().enumerate() {
56                let own = std::iter::once(&d.path).chain(&d.chapters).chain(&d.bundle_files);
57                if own.into_iter().any(|p| paths.contains(p)) {
58                    touched.insert(i);
59                }
60            }
61        }
62        let mut with_neighbours = touched.clone();
63        for &i in &touched {
64            for e in g.edges_to(i) {
65                with_neighbours.insert(e.from);
66            }
67        }
68        Ok(Scope { range, paths, in_submodules, touched, with_neighbours, base })
69    }
70
71    pub fn is_all(&self) -> bool {
72        matches!(self.range, Range::All)
73    }
74
75    /// Whether checks about document `i` itself run.
76    pub fn covers(&self, i: usize) -> bool {
77        self.is_all() || self.touched.contains(&i)
78    }
79
80    /// Whether checks about links from document `i` run (they may break when a target moves).
81    pub fn covers_links(&self, i: usize) -> bool {
82        self.is_all() || self.with_neighbours.contains(&i)
83    }
84
85    pub fn covers_path(&self, p: &str) -> bool {
86        self.is_all() || self.paths.contains(p)
87    }
88}