From 043d118f1f8c08332846f49a3938959e4d6dc314 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sat, 8 Oct 2022 22:33:51 -0700 Subject: [PATCH] cli: disallow initializing repo with native backend by default The native backend is just a proof of concept and there's no real reason to use it other than for testing, so let's reduce the risk of accidentally creating repos using it. --- lib/src/settings.rs | 6 ++++++ src/commands.rs | 7 +++++++ tests/test_init_command.rs | 15 +++++++++++++++ tests/test_untrack_command.rs | 10 ++++++++++ 4 files changed, 38 insertions(+) diff --git a/lib/src/settings.rs b/lib/src/settings.rs index 69133702c..4a5dcf957 100644 --- a/lib/src/settings.rs +++ b/lib/src/settings.rs @@ -100,6 +100,12 @@ impl UserSettings { .unwrap_or(false) } + pub fn allow_native_backend(&self) -> bool { + self.config + .get_bool("ui.allow-init-native") + .unwrap_or(false) + } + pub fn config(&self) -> &config::Config { &self.config } diff --git a/src/commands.rs b/src/commands.rs index 7b22e0e75..fcec29723 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1097,6 +1097,13 @@ fn cmd_init(ui: &mut Ui, command: &CommandHelper, args: &InitArgs) -> Result<(), } else if args.git { Workspace::init_internal_git(ui.settings(), &wc_path)?; } else { + if !ui.settings().allow_native_backend() { + return Err(CommandError::UserError( + "The native backend is disallowed by default. Did you mean to pass `--git`? +Set `ui.allow-init-native` to allow initializing a repo with the native backend." + .to_string(), + )); + } Workspace::init_local(ui.settings(), &wc_path)?; }; let cwd = ui.cwd().canonicalize().unwrap(); diff --git a/tests/test_init_command.rs b/tests/test_init_command.rs index b61ffdc3f..1f668b657 100644 --- a/tests/test_init_command.rs +++ b/tests/test_init_command.rs @@ -143,9 +143,24 @@ fn test_init_git_colocated() { "###); } +#[test] +fn test_init_local_disallowed() { + let test_env = TestEnvironment::default(); + let stdout = test_env.jj_cmd_failure(test_env.env_root(), &["init", "repo"]); + insta::assert_snapshot!(stdout, @r###" + Error: The native backend is disallowed by default. Did you mean to pass `--git`? + Set `ui.allow-init-native` to allow initializing a repo with the native backend. + "###); +} + #[test] fn test_init_local() { let test_env = TestEnvironment::default(); + test_env.add_config( + br#"[ui] + allow-init-native = true + "#, + ); let stdout = test_env.jj_cmd_success(test_env.env_root(), &["init", "repo"]); insta::assert_snapshot!(stdout, @r###" Initialized repo in "repo" diff --git a/tests/test_untrack_command.rs b/tests/test_untrack_command.rs index 6ba9b121a..40be3ef92 100644 --- a/tests/test_untrack_command.rs +++ b/tests/test_untrack_command.rs @@ -21,6 +21,11 @@ pub mod common; #[test] fn test_untrack() { let test_env = TestEnvironment::default(); + test_env.add_config( + br#"[ui] + allow-init-native = true + "#, + ); test_env.jj_cmd_success(test_env.env_root(), &["init", "repo"]); let repo_path = test_env.env_root().join("repo"); @@ -88,6 +93,11 @@ fn test_untrack() { #[test] fn test_untrack_sparse() { let test_env = TestEnvironment::default(); + test_env.add_config( + br#"[ui] + allow-init-native = true + "#, + ); test_env.jj_cmd_success(test_env.env_root(), &["init", "repo"]); let repo_path = test_env.env_root().join("repo");