From 3dc71b2f5160ef9bd46143eed0edb5d2557b58e6 Mon Sep 17 00:00:00 2001 From: aur-build Date: Sun, 22 Nov 2026 10:36:24 +0000 Subject: rustfmt,lints: pin the formatting, adopt the idiom lints, pay them off rustfmt.toml fixes the shape the tree is written in (120 columns, small items kept on one line) and one canonical pass makes it the truth. [lints.clippy] names the idiom lints worth enforcing and leaves out the two that would add doc-comment boilerplate rather than remove it. The tree pays them off here: write!/writeln! instead of push_str(&format!()), let-else instead of match-on-Option, borrows where a thread entry point only reads what it owns, no `&self` on a Copy state and no `self` nobody reads. ops::to_prometheus gains one gauge helper: a metric's HELP, TYPE and value are now emitted by the same call instead of three. --- src/watch/detect.rs | 71 +++++++++++++++++++------------------------------- src/watch/digests.rs | 26 ++++++------------ src/watch/mirror.rs | 29 +++++++++------------ src/watch/scheduler.rs | 16 +++--------- src/watch/staleness.rs | 6 ++--- src/watch/upstream.rs | 52 +++++++++++++++--------------------- 6 files changed, 74 insertions(+), 126 deletions(-) (limited to 'src/watch') diff --git a/src/watch/detect.rs b/src/watch/detect.rs index 1c72131..d51ca03 100644 --- a/src/watch/detect.rs +++ b/src/watch/detect.rs @@ -1,5 +1,8 @@ use crate::config::Config; -use crate::db::{self, *}; +use crate::db::{ + self, get_pkgbase, notice_kind, Db, Identity, Pkgbase, Recipe, ReleaseRow, SourceDigest, SubscriptionState, + UpstreamPin, +}; use crate::error::{Missing, Result}; use crate::pkgver; use crate::recipe::recipe_from_tree; @@ -31,10 +34,10 @@ pub struct CheckReport { } pub fn check_pkgbase(cfg: &Config, db: &Db, name: &str) -> Result { - let pb = get_pkgbase(db, name)? - .ok_or_else(|| crate::error::Error::not_found(Missing::Pkgbase(name.to_string())))?; + let pb = + get_pkgbase(db, name)?.ok_or_else(|| crate::error::Error::not_found(Missing::Pkgbase(name.to_string())))?; let sub = db::get_subscription(db, name)?; - let paused = sub.as_ref().map(|s| s.state == SubscriptionState::Paused).unwrap_or(false); + let paused = sub.as_ref().is_some_and(|s| s.state == SubscriptionState::Paused); if paused { return Ok(CheckReport { pkgbase: name.into(), head: String::new(), outcome: Outcome::Paused }); } @@ -46,7 +49,7 @@ pub fn check_pkgbase(cfg: &Config, db: &Db, name: &str) -> Result { pb.id, notice_kind::DELETED, "high", - serde_json::json!({"aur_url": pb.aur_url, "note": "repository no longer exists"}), + &serde_json::json!({"aur_url": pb.aur_url, "note": "repository no longer exists"}), )?; db::set_subscription_state(&db.writer, name, SubscriptionState::Orphaned)?; return Ok(CheckReport { pkgbase: name.into(), head: String::new(), outcome: Outcome::Gone }); @@ -77,12 +80,9 @@ fn analyse(cfg: &Config, db: &Db, pb: &Pkgbase, head: &str) -> Result Result = accepted.as_ref().map(|a| a.pins()).unwrap_or_default(); - let stage_a = if has_vcs { - Some(crate::watch::upstream::probe(&recipe, &accepted_pins)?) - } else { - None - }; + let has_vcs = recipe.source_entries.iter().any(super::super::db::model::SourceEntry::is_vcs); + let accepted_pins: Vec = accepted.as_ref().map_or_default(super::super::db::model::ReleaseRow::pins); + let stage_a = if has_vcs { Some(crate::watch::upstream::probe(&recipe, &accepted_pins)?) } else { None }; let resolved = if !has_vcs { None } else if stage_a.as_ref().unwrap().needs_stage_b() { @@ -124,14 +120,8 @@ fn analyse(cfg: &Config, db: &Db, pb: &Pkgbase, head: &str) -> Result Result Outcome { } } -fn non_publishable( - db: &Db, - pb: &Pkgbase, - base: &ReleaseRow, - nc: &NewCandidate, - recipe: &Recipe, -) -> Result { +fn non_publishable(db: &Db, pb: &Pkgbase, base: &ReleaseRow, nc: &NewCandidate, recipe: &Recipe) -> Result { let old_digests: Vec = base.digests(); let new_digests: Vec = serde_json::from_str(&nc.source_digests_json).unwrap_or_default(); - let vcs_indices: std::collections::BTreeSet = recipe - .source_entries - .iter() - .filter(|e| e.is_vcs()) - .map(|e| e.index) - .collect(); + let vcs_indices: std::collections::BTreeSet = + recipe.source_entries.iter().filter(|e| e.is_vcs()).map(|e| e.index).collect(); let changed_remote: Vec<&SourceDigest> = new_digests .iter() .filter(|n| n.kind != "local" && !vcs_indices.contains(&n.index)) @@ -223,7 +203,7 @@ fn non_publishable( .map(|n| { serde_json::json!({"index": n.index, "url": n.url, "old": old_digests.iter().find(|o| o.index == n.index) - .map(|o| o.digest.clone()).unwrap_or_default(), + .map_or_default(|o| o.digest.clone()), "new": n.digest}) }) .collect(); @@ -232,13 +212,16 @@ fn non_publishable( pb.id, notice_kind::INTEGRITY_ALERT, "high", - serde_json::json!({ + &serde_json::json!({ "version": nc.version(), "changed": changed, "message": "the artifact in your index was built from bytes upstream no longer serves; the version string is unchanged", }), )?; - return Ok(Outcome::Notice { kind: notice_kind::INTEGRITY_ALERT.into(), detail: serde_json::json!({"changed": changed}) }); + return Ok(Outcome::Notice { + kind: notice_kind::INTEGRITY_ALERT.into(), + detail: serde_json::json!({"changed": changed}), + }); } if pins_changed { db::put_notice_once( @@ -246,7 +229,7 @@ fn non_publishable( pb.id, notice_kind::PIN_DRIFT, "info", - serde_json::json!({ + &serde_json::json!({ "version": nc.version(), "message": "upstream moved; pkgver unchanged, nothing publishable", }), @@ -258,7 +241,7 @@ fn non_publishable( pb.id, notice_kind::RECIPE_DRIFT, "warning", - serde_json::json!({ + &serde_json::json!({ "version": nc.version(), "accepted_aur_commit": base.aur_commit, "aur_commit": "moved", diff --git a/src/watch/digests.rs b/src/watch/digests.rs index 027062b..ae53191 100644 --- a/src/watch/digests.rs +++ b/src/watch/digests.rs @@ -1,5 +1,5 @@ use crate::config::Config; -use crate::db::{self, *}; +use crate::db::{self, Db, Recipe, SourceDigest, UpstreamPin}; use crate::error::Result; use crate::recipe::{declared_sums, is_hex_digest, SKIP}; use crate::watch::mirror::Tree; @@ -23,10 +23,7 @@ pub(crate) fn resolve_sources( if entry.is_vcs() { let pin = pins.iter().find(|p| p.index == entry.index); - let digest = pin - .map(|p| p.commit.clone()) - .or_else(|| entry.commit.clone()) - .unwrap_or_default(); + let digest = pin.map(|p| p.commit.clone()).or_else(|| entry.commit.clone()).unwrap_or_default(); out.push(SourceDigest { index: entry.index, url: entry.url.clone(), @@ -43,11 +40,8 @@ pub(crate) fn resolve_sources( if entry.local { let name = entry.filename.clone().unwrap_or_default(); - let path = tree - .blobs - .iter() - .find(|(p, _)| p == &name || p.ends_with(&format!("/{name}"))) - .map(|(p, _)| p.clone()); + let path = + tree.blobs.iter().find(|(p, _)| p == &name || p.ends_with(&format!("/{name}"))).map(|(p, _)| p.clone()); let (digest, size) = match path { Some(p) => { let bytes = crate::watch::mirror::read_file(mirror, commit, &p)?; @@ -96,13 +90,13 @@ pub(crate) fn download_and_cache(cfg: &Config, db: &Db, url: &str) -> Result<(St let tmp = cfg.source_cache_dir().join(format!("tmp-{}", crate::hash::short(url.as_bytes(), 16))); let (digest, size) = crate::fetch::fetch_to(url, &tmp)?; let dest = cfg.source_cache_path(&digest); - if !dest.exists() { + if dest.exists() { + let _ = std::fs::remove_file(&tmp); + } else { if let Some(parent) = dest.parent() { std::fs::create_dir_all(parent)?; } std::fs::rename(&tmp, &dest)?; - } else { - let _ = std::fs::remove_file(&tmp); } let dest_s = dest.display().to_string(); db::put_source_cache(&db.writer, url, &digest, size, &dest_s)?; @@ -110,9 +104,5 @@ pub(crate) fn download_and_cache(cfg: &Config, db: &Db, url: &str) -> Result<(St } pub(crate) fn cached_path(db: &Db, url: &str, digest: &str) -> Option { - db::source_cache_path(db, url, digest) - .ok() - .flatten() - .map(std::path::PathBuf::from) - .filter(|p| p.exists()) + db::source_cache_path(db, url, digest).ok().flatten().map(std::path::PathBuf::from).filter(|p| p.exists()) } diff --git a/src/watch/mirror.rs b/src/watch/mirror.rs index 5b7e2b1..0eb9a77 100644 --- a/src/watch/mirror.rs +++ b/src/watch/mirror.rs @@ -39,10 +39,7 @@ fn sync_mirror_impl(cfg: &Config, db: &Db, name: &str, url: &str) -> Result h, - None => return Ok(Sync::Gone), - }; + let Some(remote) = git::ls_remote_head(url)? else { return Ok(Sync::Gone) }; let local = git::head(&mirror).ok(); if let Some(l) = &local { if *l == remote { @@ -95,9 +92,7 @@ pub(crate) fn read_tree(mirror: &Path, commit: &str) -> Result { let contents = git::cat_file_batch(mirror, &oids)?; let mut files = BTreeMap::new(); for (path, sha) in &blobs { - let bytes = contents - .get(sha) - .ok_or_else(|| Error::not_found(Missing::MirrorPath(path.to_string())))?; + let bytes = contents.get(sha).ok_or_else(|| Error::not_found(Missing::MirrorPath(path.to_string())))?; if bytes.len() > 4 * 1024 * 1024 || !looks_textual(bytes) { continue; } @@ -114,7 +109,6 @@ pub(crate) fn read_file(mirror: &Path, commit: &str, path: &str) -> Result (std::path::PathBuf, String) { - let nonce = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap() - .as_nanos(); + let nonce = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos(); let dir = std::env::temp_dir().join(format!("aur-read-tree-{}-{}", std::process::id(), nonce)); std::fs::create_dir_all(&dir).unwrap(); let git = |args: &[&str]| { @@ -157,10 +148,13 @@ mod tests { #[test] fn read_tree_returns_every_blob_from_one_batch_read() { let (dir, head) = repo(&[ - ("PKGBUILD", b"pkgbase=foo + ( + "PKGBUILD", + b"pkgbase=foo pkgver=1.0 pkgrel=1 -"), +", + ), ("empty.txt", b""), ("blob.bin", b"\x00not text"), ]); @@ -179,8 +173,11 @@ pkgrel=1 #[test] fn read_tree_fails_closed_when_the_commit_is_gone_from_the_mirror() { - let (dir, _head) = repo(&[("PKGBUILD", b"pkgbase=foo -")]); + let (dir, _head) = repo(&[( + "PKGBUILD", + b"pkgbase=foo +", + )]); for gone in ["0123456789012345678901234567890123456789", "deadbeef"] { let err = read_tree(&dir, gone).unwrap_err().to_string(); assert!(err.contains("missing from the local mirror"), "{gone}: {err}"); diff --git a/src/watch/scheduler.rs b/src/watch/scheduler.rs index 4508a03..5aae7ea 100644 --- a/src/watch/scheduler.rs +++ b/src/watch/scheduler.rs @@ -27,17 +27,12 @@ pub fn spawn_scheduler(cfg: Arc, db: Db, stop: Arc) -> JoinH pub fn sweep(cfg: &Config, db: &Db) -> usize { let now = crate::time::now(); - let due = match db::due_subscriptions(db, &now) { - Ok(d) => d, - Err(_) => return 0, - }; + let Ok(due) = db::due_subscriptions(db, &now) else { return 0 }; let mut n = 0; for sub in due { // Mark it checked first so a slow or hanging source cannot make the // scheduler re-enter it on the next tick. - let interval = sub - .poll_interval_secs - .unwrap_or(crate::db::setting_i64(db, "poll_interval", 86400)); + let interval = sub.poll_interval_secs.unwrap_or(crate::db::setting_i64(db, "poll_interval", 86400)); let _ = db::note_check(&db.writer, &sub.pkgbase, &crate::time::secs_ahead(interval), None); match check_pkgbase(cfg, db, &sub.pkgbase) { Ok(report) => { @@ -51,12 +46,7 @@ pub fn sweep(cfg: &Config, db: &Db) -> usize { } } Err(e) => { - let _ = db::note_check( - &db.writer, - &sub.pkgbase, - &crate::time::secs_ahead(600), - Some(&e.to_string()), - ); + let _ = db::note_check(&db.writer, &sub.pkgbase, &crate::time::secs_ahead(600), Some(&e.to_string())); } } n += 1; diff --git a/src/watch/staleness.rs b/src/watch/staleness.rs index 94813eb..6b83032 100644 --- a/src/watch/staleness.rs +++ b/src/watch/staleness.rs @@ -12,7 +12,7 @@ pub enum Kind { /// only way to reach it, and the alias is what carries the /// feature-conditional visibility. mod stale { - use super::*; + use super::Kind; #[derive(Debug, Clone)] pub struct Stale { @@ -97,9 +97,7 @@ pub fn dependents_of(db: &Db, name: &str) -> Result> { continue; } let depends = match serde_json::from_str::(&json) { - Ok(r) => crate::pkgver::dep_names(&r.depends) - .into_iter() - .any(|d| d == name), + Ok(r) => crate::pkgver::dep_names(&r.depends).into_iter().any(|d| d == name), Err(_) => true, }; if depends { diff --git a/src/watch/upstream.rs b/src/watch/upstream.rs index 7987178..523574a 100644 --- a/src/watch/upstream.rs +++ b/src/watch/upstream.rs @@ -5,7 +5,11 @@ use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Condvar, Mutex}; use crate::config::Config; -use crate::db::{self, model::*, Db, Pkgbase}; +use crate::db::{ + self, + model::{Recipe, StageBCacheRow, UpstreamPin}, + Db, Pkgbase, +}; use crate::error::{Error, Result, TransientCause}; use crate::git; @@ -116,12 +120,7 @@ pub(crate) struct StageB { impl StageB { fn from_cache(row: StageBCacheRow) -> Self { - StageB { - epoch: row.epoch, - pkgver: row.pkgver, - pkgrel: row.pkgrel, - pins: row.pins, - } + StageB { epoch: row.epoch, pkgver: row.pkgver, pkgrel: row.pkgrel, pins: row.pins } } fn as_row(&self) -> StageBCacheRow { @@ -143,14 +142,14 @@ static SLOT_LOCK: Mutex<()> = Mutex::new(()); /// only way to reach it, and the alias is what carries the /// feature-conditional visibility. mod stage_b_slot { - use super::*; + use super::{Ordering, RUNNING, SLOT_FREE, SLOT_LOCK}; pub struct StageBSlot(()); impl StageBSlot { pub fn acquire(cap: usize) -> Self { let cap = cap.max(1); - let guard = SLOT_LOCK.lock().unwrap_or_else(|e| e.into_inner()); + let guard = SLOT_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner); let mut guard = guard; loop { let n = RUNNING.load(Ordering::SeqCst); @@ -159,7 +158,7 @@ mod stage_b_slot { drop(guard); return StageBSlot(()); } - guard = SLOT_FREE.wait(guard).unwrap_or_else(|e| e.into_inner()); + guard = SLOT_FREE.wait(guard).unwrap_or_else(std::sync::PoisonError::into_inner); } } } @@ -191,7 +190,7 @@ pub(crate) fn resolve( recipe_hash: &str, stage_a: &StageA, ) -> Result> { - if !recipe.source_entries.iter().any(|e| e.is_vcs()) { + if !recipe.source_entries.iter().any(super::super::db::model::SourceEntry::is_vcs) { return Ok(None); } let tip_hash = tip_hash(&stage_a.pins); @@ -225,11 +224,11 @@ fn run( continue; } let cache = cfg.vcs_cache_path(&pin.url); - if !cache.exists() { + if cache.exists() { + let _ = git::fetch_and_head(&cache); + } else { git::clone_bare(&pin.url, &cache) .map_err(|e| Error::transient(TransientCause::Upstream(format!("vcs_cache clone {}: {e}", pin.url))))?; - } else { - let _ = git::fetch_and_head(&cache); } if let Ok(local) = git::head(&cache) { if !local.is_empty() && git::has_commit(&cache, &pin.commit) { @@ -251,10 +250,7 @@ fn run( let out = cmd.output()?; let _ = std::fs::remove_dir_all(&scratch); if !out.status.success() { - return Err(Error::transient(format!( - "makepkg --vercheck: {}", - String::from_utf8_lossy(&out.stderr).trim() - ))); + return Err(Error::transient(format!("makepkg --vercheck: {}", String::from_utf8_lossy(&out.stderr).trim()))); } let text = String::from_utf8_lossy(&out.stdout).trim().to_string(); let (pkgver, pkgrel) = match text.rsplit_once('-') { @@ -264,12 +260,7 @@ fn run( if pkgver.is_empty() { return Err(Error::other(format!("makepkg --vercheck produced {text:?}"))); } - Ok(StageB { - epoch: recipe.epoch.clone(), - pkgver, - pkgrel, - pins, - }) + Ok(StageB { epoch: recipe.epoch.clone(), pkgver, pkgrel, pins }) } fn default_branch(cache: &std::path::Path) -> Option { @@ -280,16 +271,15 @@ fn default_branch(cache: &std::path::Path) -> Option { } pub fn tip_hash(pins: &[UpstreamPin]) -> String { - let mut v: Vec = pins - .iter() - .map(|p| format!("{}:{}:{}", p.index, p.url, p.commit)) - .collect(); + let mut v: Vec = pins.iter().map(|p| format!("{}:{}:{}", p.index, p.url, p.commit)).collect(); v.sort(); crate::hash::hex_str(&v.join("\n")) } fn scratch_dir(cfg: &Config, pkgbase: &str, recipe_hash: &str, tip_hash: &str) -> PathBuf { - cfg.staging_dir() - .join("vercheck") - .join(format!("{pkgbase}-{}-{}", &recipe_hash[..8.min(recipe_hash.len())], &tip_hash[..8.min(tip_hash.len())])) + cfg.staging_dir().join("vercheck").join(format!( + "{pkgbase}-{}-{}", + &recipe_hash[..8.min(recipe_hash.len())], + &tip_hash[..8.min(tip_hash.len())] + )) } -- cgit v1.2.3