| 1 |
// Copyright 2026 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::fs; |
| 16 |
use std::path::Path; |
| 17 |
use std::path::PathBuf; |
| 18 |
|
| 19 |
use jj_lib::secure_config; |
| 20 |
use tracing::instrument; |
| 21 |
|
| 22 |
use crate::cli_util::CommandHelper; |
| 23 |
use crate::command_error::CommandError; |
| 24 |
use crate::command_error::user_error; |
| 25 |
use crate::command_error::user_error_with_message; |
| 26 |
use crate::ui::Ui; |
| 27 |
|
| 28 |
/// Find and optionally delete repo-level config directories whose repo path |
| 29 |
/// no longer exists. |
| 30 |
// TODO: also gc workspace-level config directories under |
| 31 |
// `$HOME/.config/jj/workspaces/`. |
| 32 |
// TODO: support a path argument to filter which repo paths to delete, e.g. |
| 33 |
// `jj config gc /tmp` to only delete repo configs whose recorded path is |
| 34 |
// under `/tmp`. |
| 35 |
#[derive(clap::Args, Clone, Debug)] |
| 36 |
pub struct ConfigGcArgs {} |
| 37 |
|
| 38 |
#[instrument(skip_all)] |
| 39 |
pub async fn cmd_config_gc( |
| 40 |
ui: &mut Ui, |
| 41 |
command: &CommandHelper, |
| 42 |
_args: &ConfigGcArgs, |
| 43 |
) -> Result<(), CommandError> { |
| 44 |
let root = command |
| 45 |
.config_env() |
| 46 |
.repo_configs_root_dir() |
| 47 |
.ok_or_else(|| user_error("No config directory found"))?; |
| 48 |
|
| 49 |
let missing = find_missing_repo_configs(&root)?; |
| 50 |
|
| 51 |
if let Some(mut formatter) = ui.status_formatter() { |
| 52 |
writeln!( |
| 53 |
formatter, |
| 54 |
"Missing repo configs (repo path no longer exists):" |
| 55 |
)?; |
| 56 |
if missing.is_empty() { |
| 57 |
writeln!(formatter, " (none)")?; |
| 58 |
} else { |
| 59 |
for (config_dir, repo_path) in &missing { |
| 60 |
writeln!(formatter, " {}", config_dir.display())?; |
| 61 |
writeln!(formatter, " repo path: {}", repo_path.display())?; |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
if missing.is_empty() { |
| 67 |
return Ok(()); |
| 68 |
} |
| 69 |
|
| 70 |
let prompt = format!("Delete {} missing repo config directories?", missing.len()); |
| 71 |
if !ui.prompt_yes_no(&prompt, Some(false))? { |
| 72 |
writeln!(ui.status(), "Aborted; nothing was deleted.")?; |
| 73 |
return Ok(()); |
| 74 |
} |
| 75 |
|
| 76 |
let mut deleted = 0; |
| 77 |
for (config_dir, _) in &missing { |
| 78 |
match secure_config::remove_repo_config_dir(config_dir) { |
| 79 |
Ok(()) => deleted += 1, |
| 80 |
Err(err) => writeln!( |
| 81 |
ui.warning_default(), |
| 82 |
"Failed to delete {}: {err}", |
| 83 |
config_dir.display() |
| 84 |
)?, |
| 85 |
} |
| 86 |
} |
| 87 |
writeln!(ui.status(), "Deleted {deleted} config directories.")?; |
| 88 |
Ok(()) |
| 89 |
} |
| 90 |
|
| 91 |
/// Returns `(config_dir, repo_path)` pairs for every per-repo config |
| 92 |
/// directory under `root` whose recorded repo path no longer exists on |
| 93 |
/// disk. The list is sorted by config directory name. |
| 94 |
pub(crate) fn find_missing_repo_configs( |
| 95 |
root: &Path, |
| 96 |
) -> Result<Vec<(PathBuf, PathBuf)>, CommandError> { |
| 97 |
if !root.try_exists().map_err(|err| { |
| 98 |
user_error_with_message(format!("Failed to check {}", root.display()), err) |
| 99 |
})? { |
| 100 |
return Ok(Vec::new()); |
| 101 |
} |
| 102 |
let read_dir = fs::read_dir(root).map_err(|err| { |
| 103 |
user_error_with_message(format!("Failed to read {}", root.display()), err) |
| 104 |
})?; |
| 105 |
|
| 106 |
let mut missing = Vec::new(); |
| 107 |
for dir_entry in read_dir { |
| 108 |
let dir_entry = dir_entry.map_err(|err| { |
| 109 |
user_error_with_message(format!("Failed to read {}", root.display()), err) |
| 110 |
})?; |
| 111 |
let config_dir = dir_entry.path(); |
| 112 |
if !config_dir.is_dir() { |
| 113 |
continue; |
| 114 |
} |
| 115 |
let Ok(metadata) = secure_config::read_metadata(&config_dir) else { |
| 116 |
continue; |
| 117 |
}; |
| 118 |
let Ok(Some(repo_path)) = secure_config::metadata_path(&metadata) else { |
| 119 |
continue; |
| 120 |
}; |
| 121 |
// Treat "exists" errors (e.g. permission denied) as "still exists" so |
| 122 |
// we don't propose deleting configs we can't actually verify. |
| 123 |
if !repo_path.try_exists().unwrap_or(true) { |
| 124 |
missing.push((config_dir, repo_path.to_path_buf())); |
| 125 |
} |
| 126 |
} |
| 127 |
missing.sort(); |
| 128 |
Ok(missing) |
| 129 |
} |