From 5b6ef636669f55f350f5cb397291387eb276d277 Mon Sep 17 00:00:00 2001 From: Daehyeok Mun Date: Sat, 13 Jan 2024 17:38:55 -0800 Subject: [PATCH] Support `--user` and `--repo` argument for `config list` command --- CHANGELOG.md | 3 ++ cli/src/commands/config.rs | 29 +++++++++++++++-- cli/tests/test_config_command.rs | 54 ++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cff01d28d..70e8fe069 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * `jj config list` gained a `--include-overridden` option to allow printing overridden config values. +* `jj config list` now accepts `--user` or `--repo` option to specify + config origin. + ### Fixed bugs diff --git a/cli/src/commands/config.rs b/cli/src/commands/config.rs index 7e87d7e7d..467bdd759 100644 --- a/cli/src/commands/config.rs +++ b/cli/src/commands/config.rs @@ -71,18 +71,37 @@ pub(crate) enum ConfigSubcommand { /// List variables set in config file, along with their values. #[derive(clap::Args, Clone, Debug)] +#[command(group(clap::ArgGroup::new("specific").args(&["repo", "user"])))] pub(crate) struct ConfigListArgs { /// An optional name of a specific config option to look up. #[arg(value_parser = NonEmptyStringValueParser::new())] pub name: Option, /// Whether to explicitly include built-in default values in the list. - #[arg(long)] + #[arg(long, conflicts_with = "specific")] pub include_defaults: bool, /// Allow printing overridden values. #[arg(long)] pub include_overridden: bool, + /// Target the user-level config + #[arg(long)] + user: bool, + /// Target the repo-level config + #[arg(long)] + repo: bool, // TODO(#1047): Support --show-origin using LayeredConfigs. - // TODO(#1047): Support ConfigArgs (--user or --repo). +} + +impl ConfigListArgs { + fn get_source_kind(&self) -> Option { + if self.user { + Some(ConfigSource::User) + } else if self.repo { + Some(ConfigSource::Repo) + } else { + //List all variables + None + } + } } /// Get the value of a given config option. @@ -158,6 +177,12 @@ pub(crate) fn cmd_config_list( continue; } + if let Some(target_source) = args.get_source_kind() { + if target_source != *source { + continue; + } + } + // Skip built-ins if not included. if !args.include_defaults && *source == ConfigSource::Default { continue; diff --git a/cli/tests/test_config_command.rs b/cli/tests/test_config_command.rs index 7ad8dc0c1..cecbe9292 100644 --- a/cli/tests/test_config_command.rs +++ b/cli/tests/test_config_command.rs @@ -126,6 +126,60 @@ fn test_config_list_all() { "###); } +#[test] +fn test_config_list_layer() { + let mut test_env = TestEnvironment::default(); + test_env.jj_cmd_ok(test_env.env_root(), &["init", "repo", "--git"]); + let user_config_path = test_env.config_path().join("config.toml"); + test_env.set_config_path(user_config_path.to_owned()); + let repo_path = test_env.env_root().join("repo"); + + // User + test_env.jj_cmd_ok( + &repo_path, + &["config", "set", "--user", "test-key", "test-val"], + ); + + test_env.jj_cmd_ok( + &repo_path, + &[ + "config", + "set", + "--user", + "test-layered-key", + "test-original-val", + ], + ); + + let stdout = test_env.jj_cmd_success(&repo_path, &["config", "list", "--user"]); + insta::assert_snapshot!(stdout, @r###" + test-key="test-val" + test-layered-key="test-original-val" + "###); + + // Repo + test_env.jj_cmd_ok( + &repo_path, + &[ + "config", + "set", + "--repo", + "test-layered-key", + "test-layered-val", + ], + ); + + let stdout = test_env.jj_cmd_success(&repo_path, &["config", "list", "--user"]); + insta::assert_snapshot!(stdout, @r###" + test-key="test-val" + "###); + + let stdout = test_env.jj_cmd_success(&repo_path, &["config", "list", "--repo"]); + insta::assert_snapshot!(stdout, @r###" + test-layered-key="test-layered-val" + "###); +} + #[test] fn test_config_layer_override_default() { let test_env = TestEnvironment::default();