diff --git a/CHANGELOG.md b/CHANGELOG.md index ad2232a3ee..e18ab7ad5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * `jj config list` now accepts `--user` or `--repo` option to specify config origin. +* New `jj config path` command to print the config file path without launching + an editor. + * `jj tag list` command prints imported git tags. * `jj next` and `jj prev` now prompt in the event of the next/previous commit diff --git a/cli/src/commands/config.rs b/cli/src/commands/config.rs index 513748c3b7..3ed0efbca5 100644 --- a/cli/src/commands/config.rs +++ b/cli/src/commands/config.rs @@ -67,6 +67,8 @@ pub(crate) enum ConfigCommand { Set(ConfigSetArgs), #[command(visible_alias("e"))] Edit(ConfigEditArgs), + #[command(visible_alias("p"))] + Path(ConfigPathArgs), } /// List variables set in config file, along with their values. @@ -132,12 +134,26 @@ pub(crate) struct ConfigSetArgs { } /// Start an editor on a jj config file. +/// +/// Creates the file if it doesn't already exist regardless of what the editor +/// does. #[derive(clap::Args, Clone, Debug)] pub(crate) struct ConfigEditArgs { #[clap(flatten)] pub config_args: ConfigArgs, } +/// Print the path to the config file +/// +/// A config file at that path may or may not exist. +/// +/// See `jj config edit` if you'd like to immediately edit the file. +#[derive(clap::Args, Clone, Debug)] +pub(crate) struct ConfigPathArgs { + #[clap(flatten)] + pub config_args: ConfigArgs, +} + #[instrument(skip_all)] pub(crate) fn cmd_config( ui: &mut Ui, @@ -149,6 +165,7 @@ pub(crate) fn cmd_config( ConfigCommand::Get(sub_args) => cmd_config_get(ui, command, sub_args), ConfigCommand::Set(sub_args) => cmd_config_set(ui, command, sub_args), ConfigCommand::Edit(sub_args) => cmd_config_edit(ui, command, sub_args), + ConfigCommand::Path(sub_args) => cmd_config_path(ui, command, sub_args), } } @@ -270,3 +287,20 @@ pub(crate) fn cmd_config_edit( let config_path = get_new_config_file_path(&args.config_args.get_source_kind(), command)?; run_ui_editor(command.settings(), &config_path) } + +#[instrument(skip_all)] +pub(crate) fn cmd_config_path( + ui: &mut Ui, + command: &CommandHelper, + args: &ConfigPathArgs, +) -> Result<(), CommandError> { + let config_path = get_new_config_file_path(&args.config_args.get_source_kind(), command)?; + writeln!( + ui.stdout(), + "{}", + config_path + .to_str() + .ok_or_else(|| user_error("The config path is not valid UTF-8"))? + )?; + Ok(()) +} diff --git a/cli/tests/cli-reference@.md.snap b/cli/tests/cli-reference@.md.snap index 173fe376f7..34e15a0fbc 100644 --- a/cli/tests/cli-reference@.md.snap +++ b/cli/tests/cli-reference@.md.snap @@ -29,6 +29,7 @@ This document contains the help content for the `jj` command-line program. * [`jj config get`↴](#jj-config-get) * [`jj config set`↴](#jj-config-set) * [`jj config edit`↴](#jj-config-edit) +* [`jj config path`↴](#jj-config-path) * [`jj describe`↴](#jj-describe) * [`jj diff`↴](#jj-diff) * [`jj diffedit`↴](#jj-diffedit) @@ -472,6 +473,7 @@ For file locations, supported config options, and other details about jj config, * `get` — Get the value of a given config option. * `set` — Update config file to set the given option to a given value * `edit` — Start an editor on a jj config file +* `path` — Print the path to the config file @@ -552,7 +554,9 @@ Update config file to set the given option to a given value ## `jj config edit` -Start an editor on a jj config file +Start an editor on a jj config file. + +Creates the file if it doesn't already exist regardless of what the editor does. **Usage:** `jj config edit <--user|--repo>` @@ -569,6 +573,29 @@ Start an editor on a jj config file +## `jj config path` + +Print the path to the config file + +A config file at that path may or may not exist. + +See `jj config edit` if you'd like to immediately edit the file. + +**Usage:** `jj config path <--user|--repo>` + +###### **Options:** + +* `--user` — Target the user-level config + + Possible values: `true`, `false` + +* `--repo` — Target the repo-level config + + Possible values: `true`, `false` + + + + ## `jj describe` Update the change description or other metadata diff --git a/cli/tests/test_config_command.rs b/cli/tests/test_config_command.rs index cecbe92928..8d45403682 100644 --- a/cli/tests/test_config_command.rs +++ b/cli/tests/test_config_command.rs @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +use insta::assert_snapshot; use itertools::Itertools; use regex::Regex; @@ -564,6 +565,26 @@ fn test_config_edit_repo() { test_env.jj_cmd_ok(&repo_path, &["config", "edit", "--repo"]); } +#[test] +fn test_config_path() { + let test_env = TestEnvironment::default(); + test_env.jj_cmd_ok(test_env.env_root(), &["init", "repo", "--git"]); + let repo_path = test_env.env_root().join("repo"); + + assert_snapshot!( + test_env.jj_cmd_success(&repo_path, &["config", "path", "--user"]), + @r###" + $TEST_ENV/config + "### + ); + assert_snapshot!( + test_env.jj_cmd_success(&repo_path, &["config", "path", "--repo"]), + @r###" + $TEST_ENV/repo/.jj/repo/config.toml + "### + ); +} + #[test] fn test_config_edit_repo_outside_repo() { let test_env = TestEnvironment::default(); diff --git a/docs/config.md b/docs/config.md index d6043042f5..297d2a2a93 100644 --- a/docs/config.md +++ b/docs/config.md @@ -5,15 +5,29 @@ These are the config settings available to jj/Jujutsu. ## Config files and TOML -The config settings are loaded from the following locations. Less common ways to -specify `jj` config settings are discussed in a later section. +`jj` loads several types of config settings: -* [The user config file] -* `.jj/repo/config.toml` (per-repository) +- The built-in settings. These cannot be edited. They can be viewed in the + `cli/src/config/` directory in `jj`'s source repo. -See the [TOML site] and the [syntax guide] for a description of the syntax. +- The user settings. These can be edited with `jj config edit --user`. User +settings are located in [the user config file], which can be found with `jj +config path --user`. -[The user config file]: #user-config-file +- The repo settings. These can be edited with `jj config edit --repo` and are +located in `.jj/repo/config.toml`. + +- Settings [specified in the command-line](#specifying-config-on-the-command-line). + +These are listed in the order they are loaded; the settings from earlier items +in +the list are overridden by the settings from later items if they disagree. Every +type of config except for the built-in settings is optional. + +See the [TOML site] and the [syntax guide] for a detailed description of the +syntax. We cover some of the basics below. + +[the user config file]: #user-config-file [TOML site]: https://toml.io/en/ [syntax guide]: https://toml.io/en/v1.0.0 @@ -561,7 +575,17 @@ executable on your system](https://facebook.github.io/watchman/docs/install). Debugging commands are available under `jj debug watchman`. -# User config file +## Ways to specify `jj` config: details + +### User config file + +An easy way to find the user config file is: + +```bash +jj config path --user +``` + +The rest of this section covers the details of where this file can be located. On all platforms, the user's global `jj` configuration file is located at either `~/.jjconfig.toml` (where `~` represents `$HOME` on Unix-likes, or @@ -584,6 +608,8 @@ default locations. For example, env JJ_CONFIG=/dev/null jj log # Ignores any settings specified in the config file. ``` +### Specifying config on the command-line + You can use one or more `--config-toml` options on the command line to specify additional configuration settings. This overrides settings defined in config files or environment variables. For example,