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
|
//! The local AUR git mirror (§5.1).
//!
//! Clone, don't scrape. The mirror is load-bearing rather than a cache: every
//! build materializes its recipe from it at the pinned commit and never asks
//! the network for a recipe (§5.1a), so losing it would mean losing the ability
//! to reproduce any historical release.
use std::collections::BTreeMap;
use std::path::Path;
use crate::config::Config;
use crate::db::{self, Db};
use crate::error::{Error, Result};
use crate::git;
/// Result of a cheap poll. `Unchanged` means we did **not** fetch — the poll is
/// `git ls-remote`, one request per pkgbase (§5.1).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Sync {
/// Remote is gone (deleted from AUR).
Gone,
/// `ls-remote` matched the local tip; no fetch performed.
Unchanged { head: String },
/// The tip moved; we fetched and now have `head`.
Fetched { head: String, previous: Option<String> },
/// We had no local mirror yet.
Cloned { head: String },
}
/// Ensure a mirror exists and is polled. Updates `pkgbase.aur_head`.
pub fn sync_mirror(cfg: &Config, db: &Db, name: &str, url: &str) -> Result<Sync> {
let mirror = cfg.mirror_path(name);
let known_head = db::get_pkgbase(db, name)?.and_then(|p| p.aur_head.clone());
if !mirror.exists() {
git::clone(url, &mirror)?;
let head = git::head(&mirror)?;
record(db, name, &head, &mirror)?;
return Ok(Sync::Cloned { head });
}
let remote = match git::ls_remote_head(url)? {
Some(h) => h,
None => return Ok(Sync::Gone),
};
let local = git::head(&mirror).ok();
if let Some(l) = &local {
if *l == remote {
return Ok(Sync::Unchanged { head: remote });
}
}
let head = git::fetch_head(&mirror)?;
record(db, name, &head, &mirror)?;
Ok(Sync::Fetched { head, previous: known_head })
}
fn record(db: &Db, name: &str, head: &str, mirror: &Path) -> Result<()> {
let pb = db::get_pkgbase(db, name)?.ok_or_else(|| Error::not_found(format!("pkgbase {name}")))?;
db::set_aur_head(&db.writer, pb.id, head, &mirror.display().to_string())
}
/// A materialized snapshot tree: blob ids for hashing, text bodies for parsing
/// and diffing.
#[derive(Debug, Clone)]
pub struct Tree {
/// `(path, git blob id)` for every file in the tree — the input to
/// `recipe_hash`. Covers PKGBUILD, .SRCINFO, *.install and shipped patches
/// (§3): a patch file is part of the recipe.
pub blobs: Vec<(String, String)>,
/// Text bodies, keyed by path. Binary files are omitted from this map but
/// still counted in `blobs`.
pub files: BTreeMap<String, String>,
}
impl Tree {
pub fn recipe_hash(&self) -> String {
crate::hash::recipe_hash(&self.blobs)
}
pub fn get(&self, suffix: &str) -> Option<&String> {
self.files
.iter()
.find(|(k, _)| k.ends_with(suffix))
.map(|(_, v)| v)
}
pub fn json_list(&self) -> String {
serde_json::to_string(&self.blobs).unwrap_or_else(|_| "[]".into())
}
}
/// Read a commit's tree out of the mirror without touching the network.
pub fn read_tree(mirror: &Path, commit: &str) -> Result<Tree> {
// §16 mirror divergence: if a pin we are asked to read has vanished, that is
// a hard failure, not a silent re-pin.
if !git::has_commit(mirror, commit) {
return Err(Error::fail_closed(format!(
"commit {commit} is missing from the local mirror; history was rewritten"
)));
}
let blobs = git::ls_tree(mirror, commit)?;
let mut files = BTreeMap::new();
for (path, _) in &blobs {
let bytes = git::read_blob(mirror, commit, path)?;
if bytes.len() > 4 * 1024 * 1024 || !looks_textual(&bytes) {
continue;
}
files.insert(path.clone(), String::from_utf8_lossy(&bytes).into_owned());
}
Ok(Tree { blobs, files })
}
fn looks_textual(b: &[u8]) -> bool {
!b[..b.len().min(512)].contains(&0)
}
/// Raw bytes of one file at a commit — used to hash sources shipped in the AUR
/// repo (§5.3 "local file shipped in the AUR repo").
pub fn read_file(mirror: &Path, commit: &str, path: &str) -> Result<Vec<u8>> {
git::read_blob(mirror, commit, path)
}
|