From 0870c47559a49937243ff48c0b606b77233979f2 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Thu, 3 Nov 2022 11:24:00 -0700 Subject: [PATCH] cli: on init, don't crash when Git repo doesn't exist --- src/commands.rs | 4 +++- tests/test_init_command.rs | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/commands.rs b/src/commands.rs index f921daa5a..5a40207f9 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1076,10 +1076,12 @@ fn cmd_init(ui: &mut Ui, command: &CommandHelper, args: &InitArgs) -> Result<(), if let Some(git_store_str) = &args.git_repo { let mut git_store_path = ui.cwd().join(git_store_str); + git_store_path = git_store_path + .canonicalize() + .map_err(|_| UserError(format!("{} doesn't exist", git_store_path.display())))?; if !git_store_path.ends_with(".git") { git_store_path = git_store_path.join(".git"); } - git_store_path = git_store_path.canonicalize().unwrap(); // If the git repo is inside the workspace, use a relative path to it so the // whole workspace can be moved without breaking. if let Ok(relative_path) = git_store_path.strip_prefix(&wc_path) { diff --git a/tests/test_init_command.rs b/tests/test_init_command.rs index 0a98e7299..660e55b83 100644 --- a/tests/test_init_command.rs +++ b/tests/test_init_command.rs @@ -112,6 +112,18 @@ fn test_init_git_external() { "###); } +#[test] +fn test_init_git_external_bad_path() { + let test_env = TestEnvironment::default(); + let stderr = test_env.jj_cmd_failure( + test_env.env_root(), + &["init", "repo", "--git-repo", "non-existent"], + ); + insta::assert_snapshot!(stderr, @r###" + Error: $TEST_ENV/non-existent doesn't exist + "###); +} + #[test] fn test_init_git_colocated() { let test_env = TestEnvironment::default();