config: switch to etcetera from dirs

This commit is contained in:
Nicole Patricia Mazzuca 2025-04-09 20:33:52 +02:00 committed by nicole mazzuca
parent 19f997a466
commit f9966a644b
5 changed files with 36 additions and 7 deletions

13
Cargo.lock generated
View File

@ -852,6 +852,17 @@ version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5692dd7b5a1978a5aeb0ce83b7655c58ca8efdcb79d21036ea249da95afec2c6"
[[package]]
name = "etcetera"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26c7b13d0780cb82722fd59f6f57f925e143427e4a75313a6c77243bf5326ae6"
dependencies = [
"cfg-if",
"home",
"windows-sys 0.59.0",
]
[[package]]
name = "euclid"
version = "0.22.11"
@ -2352,8 +2363,8 @@ dependencies = [
"criterion",
"crossterm",
"datatest-stable",
"dirs",
"dunce",
"etcetera",
"futures 0.3.31",
"git2",
"gix",

View File

@ -41,8 +41,8 @@ criterion = "0.5.1"
crossterm = { version = "0.28", default-features = false, features = ["windows"] }
datatest-stable = "0.3.2"
digest = "0.10.7"
dirs = "6.0.0"
dunce = "1.0.5"
etcetera = "0.10.0"
either = "1.15.0"
futures = "0.3.31"
git2 = { version = "0.20.1", features = [

View File

@ -62,8 +62,8 @@ clap_complete_nushell = { workspace = true }
clap_mangen = { workspace = true }
criterion = { workspace = true, optional = true }
crossterm = { workspace = true }
dirs = { workspace = true }
dunce = { workspace = true }
etcetera = { workspace = true }
futures = { workspace = true }
git2 = { workspace = true, optional = true }
gix = { workspace = true, optional = true }

View File

@ -22,6 +22,7 @@ use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
use etcetera::BaseStrategy as _;
use itertools::Itertools as _;
use jj_lib::config::ConfigFile;
use jj_lib::config::ConfigGetError;
@ -281,11 +282,28 @@ pub struct ConfigEnv {
impl ConfigEnv {
/// Initializes configuration loader based on environment variables.
pub fn from_environment() -> Self {
let config_dir = if cfg!(target_os = "macos") {
etcetera::base_strategy::choose_native_strategy()
.ok()
.map(|s| {
// note that etcetera calls Library/Application Support the "data dir",
// Library/Preferences is supposed to be exclusively plists
s.data_dir()
})
} else {
etcetera::choose_base_strategy()
.ok()
.map(|s| s.config_dir())
};
// Canonicalize home as we do canonicalize cwd in CliRunner. $HOME might
// point to symlink.
let home_dir = dirs::home_dir().map(|path| dunce::canonicalize(&path).unwrap_or(path));
let home_dir = etcetera::home_dir()
.ok()
.map(|d| dunce::canonicalize(&d).unwrap_or(d));
let env = UnresolvedConfigEnv {
config_dir: dirs::config_dir(),
config_dir,
home_dir: home_dir.clone(),
jj_config: env::var("JJ_CONFIG").ok(),
};

View File

@ -153,8 +153,8 @@ fn pinentry_get_pw(url: &str) -> Option<String> {
#[tracing::instrument]
fn get_ssh_keys(_username: &str) -> Vec<PathBuf> {
let mut paths = vec![];
if let Some(home_dir) = dirs::home_dir() {
let ssh_dir = Path::new(&home_dir).join(".ssh");
if let Ok(home_dir) = etcetera::home_dir() {
let ssh_dir = home_dir.join(".ssh");
for filename in ["id_ed25519_sk", "id_ed25519", "id_rsa"] {
let key_path = ssh_dir.join(filename);
if key_path.is_file() {