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
|
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;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Sync {
Gone,
Unchanged { head: String },
Fetched { head: String, previous: Option<String> },
Cloned { head: String },
}
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())
}
#[derive(Debug, Clone)]
pub struct Tree {
pub blobs: Vec<(String, String)>,
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())
}
}
pub fn read_tree(mirror: &Path, commit: &str) -> Result<Tree> {
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)
}
pub fn read_file(mirror: &Path, commit: &str, path: &str) -> Result<Vec<u8>> {
git::read_blob(mirror, commit, path)
}
|