use std::slice;
use std::time::Duration;
use std::time::SystemTime;
use jj_lib::repo::Repo as _;
use crate::cli_util::CommandHelper;
use crate::command_error::CommandError;
use crate::command_error::user_error;
use crate::ui::Ui;
#[derive(clap::Args, Clone, Debug)]
pub struct UtilGcArgs {
#[arg(long)]
expire: Option<String>,
}
pub async fn cmd_util_gc(
ui: &mut Ui,
command: &CommandHelper,
args: &UtilGcArgs,
) -> Result<(), CommandError> {
if !command.is_at_head_operation() {
return Err(user_error(
"Cannot garbage collect from a non-head operation",
));
}
let keep_newer = match args.expire.as_deref() {
None => SystemTime::now() - Duration::from_secs(14 * 86400),
Some("now") => SystemTime::now() - Duration::ZERO,
_ => return Err(user_error("--expire only accepts 'now'")),
};
let workspace_command = command.workspace_helper(ui)?;
let repo = workspace_command.repo();
repo.op_store()
.gc(slice::from_ref(repo.op_id()), keep_newer)
.await?;
repo.store().gc(repo.index(), keep_newer)?;
Ok(())
}