summaryrefslogtreecommitdiffstats
path: root/src/watch/staleness.rs
blob: cc7242c3bf60fd79b94e293c52e085a1e2bbd8f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//! Staleness (§10.2): "would rebuilding this change what a consumer gets?"
//!
//! Computed **on read**, never stored. A stored staleness flag goes wrong the
//! moment anything else is published, and there is always something else being
//! published; deriving it from the world snapshots and the current index means
//! it can never lie.
//!
//! The pair comes from §10.1: each build recorded the versions of *our*
//! packages it was built against. If any of those differ from what the index
//! holds now, the artifact was built against a world that no longer exists.

use crate::db::{self, Db};
use crate::error::Result;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Kind {
    /// A dependency we publish has moved on.
    Outdated,
    /// A dependency we publish has disappeared from the index.
    Missing,
}

#[derive(Debug, Clone)]
pub struct Stale {
    pub kind: Kind,
    /// The pkgbase that would need rebuilding.
    pub pkgbase: String,
    /// The dependency, or the pkgbase itself for `Missing`.
    pub dependency: String,
    /// Version it was built against.
    pub built_with: String,
    /// Version the index serves now (empty when missing).
    pub now: String,
}

/// Every stale package, derived from world snapshots against the live index.
pub fn stale_rows(db: &Db) -> Result<Vec<Stale>> {
    let rows = db::published_world_members(db)?;

    let mut out = Vec::new();
    for (pkgbase, members_json) in rows {
        let members: Vec<db::WorldMember> = serde_json::from_str(&members_json).unwrap_or_default();
        for m in members {
            // A package is not stale relative to itself.
            if m.pkgbase == pkgbase {
                continue;
            }
            match db::index_version(db, &m.pkgbase)? {
                Some(now) if now != m.version => out.push(Stale {
                    kind: Kind::Outdated,
                    pkgbase: pkgbase.clone(),
                    dependency: m.pkgbase.clone(),
                    built_with: m.version.clone(),
                    now,
                }),
                None => out.push(Stale {
                    kind: Kind::Missing,
                    pkgbase: pkgbase.clone(),
                    dependency: m.pkgbase.clone(),
                    built_with: m.version.clone(),
                    now: String::new(),
                }),
                Some(_) => {}
            }
        }
    }
    out.sort_by(|a, b| a.pkgbase.cmp(&b.pkgbase).then(a.dependency.cmp(&b.dependency)));
    Ok(out)
}

/// Staleness for one pkgbase, rendered for the package page.
pub fn stale_for(db: &Db, pkgbase: &str) -> Result<Vec<Stale>> {
    Ok(stale_rows(db)?.into_iter().filter(|s| s.pkgbase == pkgbase).collect())
}

/// AUR now offers a **lower** version than we serve (§10.2). This is not a
/// build request; it is an announcement that the mirror and the upstream have
/// disagreed, and someone has to decide which one consumers follow.
///
/// Derived on read from the newest candidate per pkgbase against the accepted
/// release, so it disappears the moment either side moves.
pub fn downgrades(db: &Db) -> Result<Vec<Stale>> {
    let rows = db::served_vs_offered_pkgvers(db)?;
    let mut out = Vec::new();
    for (pkgbase, served, offered) in rows {
        if offered.is_empty() || !crate::pkgver::is_downgrade(&served, &offered) {
            continue;
        }
        out.push(Stale {
            kind: Kind::Outdated,
            pkgbase: pkgbase.clone(),
            dependency: "aur".into(),
            built_with: served,
            now: offered,
        });
    }
    Ok(out)
}

/// "If I approve this, what goes stale?" — the dependents question (§10.3).
/// A package depends on `name` if `name` appears in its recorded world or in
/// its recipe dependencies.
pub fn dependents_of(db: &Db, name: &str) -> Result<Vec<String>> {
    let mut out: Vec<String> = Vec::new();
    for p in db::index_packages(db)? {
        if p.name == name {
            continue;
        }
        let depends = db::recipe_json_of_index_package(db, &p.name)?
            .and_then(|j| serde_json::from_str::<crate::db::model::Recipe>(&j).ok())
            .map(|r| {
                crate::pkgver::dep_names(&r.depends)
                    .into_iter()
                    .any(|d| d == name)
            })
            .unwrap_or(false);
        if depends {
            out.push(p.name);
        }
    }
    Ok(out)
}