cli: on init, don't crash when Git repo doesn't exist

This commit is contained in:
Martin von Zweigbergk 2022-11-03 11:24:00 -07:00 committed by Martin von Zweigbergk
parent 650853401d
commit 0870c47559
2 changed files with 15 additions and 1 deletions

View file

@ -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) {

View file

@ -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();