| 1 |
// Copyright 2020 The Jujutsu Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
use std::slice; |
| 16 |
use std::time::Duration; |
| 17 |
use std::time::SystemTime; |
| 18 |
|
| 19 |
use jj_lib::repo::Repo as _; |
| 20 |
|
| 21 |
use crate::cli_util::CommandHelper; |
| 22 |
use crate::command_error::CommandError; |
| 23 |
use crate::command_error::user_error; |
| 24 |
use crate::ui::Ui; |
| 25 |
|
| 26 |
/// Run backend-dependent garbage collection. |
| 27 |
/// |
| 28 |
/// To garbage-collect old operations and the commits/objects referenced by |
| 29 |
/// them, run `jj op abandon ..<some old operation>` before `jj util gc`. |
| 30 |
#[derive(clap::Args, Clone, Debug)] |
| 31 |
pub struct UtilGcArgs { |
| 32 |
/// Time threshold |
| 33 |
/// |
| 34 |
/// By default, only obsolete objects and operations older than 2 weeks are |
| 35 |
/// pruned. |
| 36 |
/// |
| 37 |
/// Only the string "now" can be passed to this parameter. Support for |
| 38 |
/// arbitrary absolute and relative timestamps will come in a subsequent |
| 39 |
/// release. |
| 40 |
#[arg(long)] |
| 41 |
expire: Option<String>, |
| 42 |
} |
| 43 |
|
| 44 |
pub async fn cmd_util_gc( |
| 45 |
ui: &mut Ui, |
| 46 |
command: &CommandHelper, |
| 47 |
args: &UtilGcArgs, |
| 48 |
) -> Result<(), CommandError> { |
| 49 |
if !command.is_at_head_operation() { |
| 50 |
return Err(user_error( |
| 51 |
"Cannot garbage collect from a non-head operation", |
| 52 |
)); |
| 53 |
} |
| 54 |
let keep_newer = match args.expire.as_deref() { |
| 55 |
None => SystemTime::now() - Duration::from_secs(14 * 86400), |
| 56 |
Some("now") => SystemTime::now() - Duration::ZERO, |
| 57 |
_ => return Err(user_error("--expire only accepts 'now'")), |
| 58 |
}; |
| 59 |
let workspace_command = command.workspace_helper(ui).await?; |
| 60 |
|
| 61 |
let repo = workspace_command.repo(); |
| 62 |
repo.op_store() |
| 63 |
.gc(slice::from_ref(repo.op_id()), keep_newer) |
| 64 |
.await?; |
| 65 |
repo.store().gc(repo.index(), keep_newer)?; |
| 66 |
Ok(()) |
| 67 |
} |