mirror of
https://seed.radicle.dev/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5.git
synced 2026-10-18 19:10:30 +08:00
node/worker/garbage: Clean up
Clippy reports dead code on all variants except `Hours` on `Expiry`, since the type is defined in a binary crate. Allow dead code here, just in case we want to use the other variants in the future. They are not a maintenance burden, as the implementation is simple and does not see much change. Slightly refactor: - `impl std::fmt::Display for Expiry` to look cleaner. - `fn collect` to borrow `RepoId`, since it only needs a borrow, and a a borrow of `Expiry` to spare a copy at the only call site. - The constant for the default value of `Expiry` to be associated with the type `Expiry`. Remove a remark about the default value of `Expiry` from `FetchConfig`, as the default value is not a concern there, but rather wherever `Expiry` is initialized.
This commit is contained in:
@@ -61,8 +61,7 @@ pub struct TaskResult {
|
||||
pub struct FetchConfig {
|
||||
/// Public key of the local peer.
|
||||
pub local: crypto::PublicKey,
|
||||
/// Configuration for `git gc` garbage collection. Defaults to `1
|
||||
/// hour ago`.
|
||||
/// Configuration for `git gc` garbage collection.
|
||||
pub expiry: garbage::Expiry,
|
||||
}
|
||||
|
||||
@@ -267,7 +266,7 @@ impl Worker {
|
||||
refs_at,
|
||||
)?;
|
||||
|
||||
if let Err(e) = garbage::collect(&self.storage, rid, *expiry) {
|
||||
if let Err(e) = garbage::collect(&self.storage, &rid, expiry) {
|
||||
// N.b. ensure that `git gc` works in debug mode.
|
||||
debug_assert!(false, "`git gc` failed: {e}");
|
||||
|
||||
|
||||
@@ -4,40 +4,53 @@ use std::{fmt, io};
|
||||
use radicle::prelude::RepoId;
|
||||
use radicle::storage::ReadStorage;
|
||||
|
||||
/// Default expiry time for objects.
|
||||
pub const EXPIRY_DEFAULT: Expiry = Expiry::Hours(1);
|
||||
|
||||
/// Expiry of objects for garbage collector.
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||
pub enum Expiry {
|
||||
#[allow(dead_code)]
|
||||
Now,
|
||||
#[allow(dead_code)]
|
||||
Seconds(usize),
|
||||
Hours(usize),
|
||||
#[allow(dead_code)]
|
||||
Days(usize),
|
||||
#[allow(dead_code)]
|
||||
Weeks(usize),
|
||||
}
|
||||
|
||||
impl Expiry {
|
||||
const DEFAULT: Self = Expiry::Hours(1);
|
||||
}
|
||||
|
||||
impl Default for Expiry {
|
||||
fn default() -> Self {
|
||||
EXPIRY_DEFAULT
|
||||
Self::DEFAULT
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Expiry {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Now => f.write_str("now"),
|
||||
Self::Seconds(s) => write!(f, "{s}.seconds.ago"),
|
||||
Self::Hours(s) => write!(f, "{s}.hours.ago"),
|
||||
Self::Days(s) => write!(f, "{s}.days.ago"),
|
||||
Self::Weeks(s) => write!(f, "{s}.weeks.ago"),
|
||||
}
|
||||
#[rustfmt::skip]
|
||||
let (quantity, unit) = match self {
|
||||
Self::Now => return f.write_str("now"),
|
||||
|
||||
Self::Seconds(quantity) => (quantity, "seconds"),
|
||||
Self::Hours (quantity) => (quantity, "hours" ),
|
||||
Self::Days (quantity) => (quantity, "days" ),
|
||||
Self::Weeks (quantity) => (quantity, "weeks" ),
|
||||
};
|
||||
|
||||
write!(f, "{quantity}.{unit}.ago")
|
||||
}
|
||||
}
|
||||
|
||||
/// Run Git garbage collector.
|
||||
pub fn collect(storage: &impl ReadStorage, rid: RepoId, expiry: Expiry) -> io::Result<ExitStatus> {
|
||||
let git_dir = storage.path_of(&rid);
|
||||
pub fn collect(
|
||||
storage: &impl ReadStorage,
|
||||
rid: &RepoId,
|
||||
expiry: &Expiry,
|
||||
) -> io::Result<ExitStatus> {
|
||||
let git_dir = storage.path_of(rid);
|
||||
let mut gc = Command::new("git");
|
||||
gc.current_dir(git_dir)
|
||||
.env_clear()
|
||||
|
||||
Reference in New Issue
Block a user