Skip to main content

fallow_engine/baseline/
stale_advice.rs

1//! Commands for stale baseline entries.
2
3use std::path::Path;
4
5use fallow_output::BaselineMode;
6
7use super::{BaselineKind, BaselineStaleness, BaselineStalenessWarning};
8use crate::shell_quote::shell_quote;
9
10impl BaselineKind {
11    /// The command that removes only the stale entries of the baseline at
12    /// `path`, in the mode that the file records. A prune in another mode
13    /// refuses the file.
14    #[must_use]
15    pub fn prune_command(self, path: &Path, mode: BaselineMode) -> String {
16        // The prune flags resolve a relative path against the project root,
17        // while `--baseline` resolves it against the working directory. An
18        // absolute path names the same file in both.
19        let absolute = std::path::absolute(path).unwrap_or_else(|_| path.to_path_buf());
20        let quoted = shell_quote(&absolute.display().to_string());
21        format!(
22            "fallow baselines prune{} --{}-baseline {quoted}",
23            mode.flags(),
24            self.as_str()
25        )
26    }
27
28    /// The prune command with the analyzed root when it differs from the working directory.
29    #[must_use]
30    pub fn prune_command_for_root(self, path: &Path, mode: BaselineMode, root: &Path) -> String {
31        let command = self.prune_command(path, mode);
32        if std::env::current_dir().is_ok_and(|cwd| cwd == root) {
33            return command;
34        }
35        format!("{command} --root {}", shell_quote(&root.to_string_lossy()))
36    }
37
38    /// One finding that an entry of this kind can match.
39    #[must_use]
40    pub const fn entry_noun(self) -> &'static str {
41        match self {
42            Self::DeadCode => "issue",
43            Self::Dupes => "clone group",
44            Self::Health => "finding",
45        }
46    }
47
48    /// Print the staleness warning for a loaded baseline.
49    #[expect(
50        clippy::print_stderr,
51        reason = "baseline warnings accompany the report on stderr"
52    )]
53    pub fn warn_on_staleness(
54        self,
55        staleness: BaselineStaleness,
56        path: &Path,
57        saved_mode: BaselineMode,
58        root: &Path,
59    ) {
60        let label = match self {
61            Self::DeadCode => "baseline",
62            Self::Dupes => "duplication baseline",
63            Self::Health => "health baseline",
64        };
65        let entries = staleness.entries;
66        let noun = self.entry_noun();
67        match staleness.warning() {
68            BaselineStalenessWarning::None => {}
69            BaselineStalenessWarning::ZeroOverlap => eprintln!(
70                "Warning: {label} has {entries} entries but matched 0 current {noun}s. \
71                 Your paths may have changed, or the baseline was saved on a different machine. \
72                 Re-save with: --save-baseline {}",
73                path.display(),
74            ),
75            BaselineStalenessWarning::Partial => eprintln!(
76                "Warning: {label} is partially stale: {} of {entries} entries matched no current {noun}, \
77                 so the gate protects less than what was saved. Remove only the stale entries with: {}, \
78                 or re-save with: --save-baseline {}",
79                staleness.stale_entries(),
80                self.prune_command_for_root(path, saved_mode, root),
81                path.display(),
82            ),
83        }
84    }
85}