From 5ec5f4bb5f4e0b03281eb757816767313cf1eb72 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Thu, 26 Jan 2023 14:26:05 -0800 Subject: [PATCH] config: rename [alias] to [aliases], still supporting old name The name of the [alias] section is inconsistent with other table-valued sections ([revset-aliases], [colors], [merge-tools]), so let's rename it. For comparison, `Cargo.toml` also uses plural names (e.g. `[dependencies]`). --- CHANGELOG.md | 3 +++ src/cli_util.rs | 13 ++++++++++- src/commands/config-schema.json | 2 +- src/config/misc.toml | 2 +- tests/test_alias.rs | 40 ++++++++++++++++++++++++++------- 5 files changed, 49 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce9a12bc1..4874540db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -83,6 +83,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * `jj workspace root` prints the root path of the current workspace. +* The `[alias]` config section was renamed to `[aliases]`. The old name is + still accepted for backwards compatibility for some time. + ### Fixed bugs * When sharing the working copy with a Git repo, we used to forget to export diff --git a/src/cli_util.rs b/src/cli_util.rs index 0edd7f70a..c8b5053f0 100644 --- a/src/cli_util.rs +++ b/src/cli_util.rs @@ -1718,7 +1718,18 @@ fn resolve_aliases( app: &Command, string_args: &[String], ) -> Result, CommandError> { - let mut aliases_map = config.get_table("alias")?; + let mut aliases_map = config.get_table("aliases")?; + if let Ok(alias_map) = config.get_table("alias") { + for (alias, definition) in alias_map { + if aliases_map.insert(alias.clone(), definition).is_some() { + return Err(user_error_with_hint( + format!(r#"Alias "{alias}" is defined in both [aliases] and [alias]"#), + "[aliases] is the preferred section for aliases. Please remove the alias from \ + [alias].", + )); + } + } + } let mut resolved_aliases = HashSet::new(); let mut string_args = string_args.to_vec(); let mut real_commands = HashSet::new(); diff --git a/src/commands/config-schema.json b/src/commands/config-schema.json index 90f5fda58..1c176e81b 100644 --- a/src/commands/config-schema.json +++ b/src/commands/config-schema.json @@ -140,7 +140,7 @@ "type": "string" } }, - "alias": { + "aliases": { "type": "object", "description": "Custom subcommand aliases to be supported by the jj command", "additionalProperties": { diff --git a/src/config/misc.toml b/src/config/misc.toml index fb4339866..74f19aa0c 100644 --- a/src/config/misc.toml +++ b/src/config/misc.toml @@ -1,4 +1,4 @@ -[alias] +[aliases] # Placeholder: added by user [git] diff --git a/tests/test_alias.rs b/tests/test_alias.rs index b7939af57..524bf05e5 100644 --- a/tests/test_alias.rs +++ b/tests/test_alias.rs @@ -24,6 +24,22 @@ fn test_alias_basic() { test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]); let repo_path = test_env.env_root().join("repo"); + test_env.add_config(r#"aliases.b = ["log", "-r", "@", "-T", "branches"]"#); + test_env.jj_cmd_success(&repo_path, &["branch", "create", "my-branch"]); + let stdout = test_env.jj_cmd_success(&repo_path, &["b"]); + insta::assert_snapshot!(stdout, @r###" + @ my-branch + ~ + "###); +} + +#[test] +fn test_alias_legacy_section() { + let test_env = TestEnvironment::default(); + test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]); + let repo_path = test_env.env_root().join("repo"); + + // Can define aliases in [alias] section test_env.add_config(r#"alias.b = ["log", "-r", "@", "-T", "branches"]"#); test_env.jj_cmd_success(&repo_path, &["branch", "create", "my-branch"]); let stdout = test_env.jj_cmd_success(&repo_path, &["b"]); @@ -31,6 +47,14 @@ fn test_alias_basic() { @ my-branch ~ "###); + + // The same alias (name) in both [alias] and [aliases] sections is an error + test_env.add_config(r#"aliases.b = ["branch", "list"]"#); + let stderr = test_env.jj_cmd_failure(&repo_path, &["b"]); + insta::assert_snapshot!(stderr, @r###" + Error: Alias "b" is defined in both [aliases] and [alias] + Hint: [aliases] is the preferred section for aliases. Please remove the alias from [alias]. + "###); } #[test] @@ -55,7 +79,7 @@ fn test_alias_calls_unknown_command() { test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]); let repo_path = test_env.env_root().join("repo"); - test_env.add_config(r#"alias.foo = ["nonexistent"]"#); + test_env.add_config(r#"aliases.foo = ["nonexistent"]"#); let stderr = test_env.jj_cmd_cli_error(&repo_path, &["foo"]); insta::assert_snapshot!(stderr, @r###" error: The subcommand 'nonexistent' wasn't recognized @@ -72,7 +96,7 @@ fn test_alias_calls_command_with_invalid_option() { test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]); let repo_path = test_env.env_root().join("repo"); - test_env.add_config(r#"alias.foo = ["log", "--nonexistent"]"#); + test_env.add_config(r#"aliases.foo = ["log", "--nonexistent"]"#); let stderr = test_env.jj_cmd_cli_error(&repo_path, &["foo"]); insta::assert_snapshot!(stderr, @r###" error: Found argument '--nonexistent' which wasn't expected, or isn't valid in this context @@ -90,7 +114,7 @@ fn test_alias_calls_help() { let test_env = TestEnvironment::default(); test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]); let repo_path = test_env.env_root().join("repo"); - test_env.add_config(r#"alias.h = ["--help"]"#); + test_env.add_config(r#"aliases.h = ["--help"]"#); let stdout = test_env.jj_cmd_success(&repo_path, &["h"]); insta::assert_snapshot!(stdout.lines().take(5).join("\n"), @r###" Jujutsu (An experimental VCS) @@ -107,7 +131,7 @@ fn test_alias_cannot_override_builtin() { test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]); let repo_path = test_env.env_root().join("repo"); - test_env.add_config(r#"alias.log = ["rebase"]"#); + test_env.add_config(r#"aliases.log = ["rebase"]"#); // Alias should be ignored let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-r", "root"]); insta::assert_snapshot!(stdout, @r###" @@ -123,7 +147,7 @@ fn test_alias_recursive() { let repo_path = test_env.env_root().join("repo"); test_env.add_config( - r#"[alias] + r#"[aliases] foo = ["foo"] bar = ["baz"] baz = ["bar"] @@ -146,7 +170,7 @@ fn test_alias_global_args_before_and_after() { let test_env = TestEnvironment::default(); test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]); let repo_path = test_env.env_root().join("repo"); - test_env.add_config(r#"alias.l = ["log", "-T", "commit_id", "-r", "all()"]"#); + test_env.add_config(r#"aliases.l = ["log", "-T", "commit_id", "-r", "all()"]"#); // Test the setup let stdout = test_env.jj_cmd_success(&repo_path, &["l"]); insta::assert_snapshot!(stdout, @r###" @@ -182,7 +206,7 @@ fn test_alias_global_args_in_definition() { test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]); let repo_path = test_env.env_root().join("repo"); test_env.add_config( - r#"alias.l = ["log", "-T", "commit_id", "--at-op", "@-", "-r", "all()", "--color=always"]"#, + r#"aliases.l = ["log", "-T", "commit_id", "--at-op", "@-", "-r", "all()", "--color=always"]"#, ); // The global argument in the alias is respected @@ -197,7 +221,7 @@ fn test_alias_invalid_definition() { let test_env = TestEnvironment::default(); test_env.add_config( - r#"[alias] + r#"[aliases] non-list = 5 non-string-list = [[]] "#,