diff --git a/CHANGELOG.md b/CHANGELOG.md index 02d95b2424..e73d6966af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * `jj branch set` no longer creates a new branch. Use `jj branch create` instead. + +* `jj init --git` in an existing Git repository now errors and exits rather than + creating a second Git store. ### New features diff --git a/cli/src/commands/init.rs b/cli/src/commands/init.rs index 6b2e2d12d0..ea8d6dc13b 100644 --- a/cli/src/commands/init.rs +++ b/cli/src/commands/init.rs @@ -65,6 +65,8 @@ pub(crate) fn cmd_init( let wc_path = wc_path .canonicalize() .map_err(|e| user_error(format!("Failed to create workspace: {e}")))?; // raced? + let cwd = command.cwd().canonicalize().unwrap(); + let relative_wc_path = file_util::relative_path(&cwd, &wc_path); if let Some(git_store_str) = &args.git_repo { let mut git_store_path = command.cwd().join(git_store_str); @@ -101,6 +103,16 @@ pub(crate) fn cmd_init( } print_trackable_remote_branches(ui, workspace_command.repo().view())?; } else if args.git { + if wc_path.join(".git").exists() { + return Err(user_error_with_hint( + "Did not create a jj repo because there is an existing Git repo in this directory.", + format!( + r#"To create a repo backed by the existing Git repo, run `jj init --git-repo={}` instead."#, + relative_wc_path.display() + ), + )); + } + Workspace::init_internal_git(command.settings(), &wc_path)?; } else { if !command.settings().allow_native_backend() { @@ -112,22 +124,12 @@ Set `ui.allow-init-native` to allow initializing a repo with the native backend. } Workspace::init_local(command.settings(), &wc_path)?; }; - let cwd = command.cwd().canonicalize().unwrap(); - let relative_wc_path = file_util::relative_path(&cwd, &wc_path); + writeln!( ui.stderr(), "Initialized repo in \"{}\"", relative_wc_path.display() )?; - if args.git && wc_path.join(".git").exists() { - writeln!(ui.warning(), "Empty repo created.")?; - writeln!( - ui.hint(), - "Hint: To create a repo backed by the existing Git repo, run `jj init --git-repo={}` \ - instead.", - relative_wc_path.display() - )?; - } Ok(()) } diff --git a/cli/tests/test_init_command.rs b/cli/tests/test_init_command.rs index eae125fd69..1e9cf8b300 100644 --- a/cli/tests/test_init_command.rs +++ b/cli/tests/test_init_command.rs @@ -418,16 +418,14 @@ fn test_init_git_external_but_git_dir_exists() { } #[test] -fn test_init_git_internal_but_could_be_colocated() { +fn test_init_git_internal_must_be_colocated() { let test_env = TestEnvironment::default(); let workspace_root = test_env.env_root().join("repo"); init_git_repo(&workspace_root, false); - let (stdout, stderr) = test_env.jj_cmd_ok(&workspace_root, &["init", "--git"]); - insta::assert_snapshot!(stdout, @""); + let stderr = test_env.jj_cmd_failure(&workspace_root, &["init", "--git"]); insta::assert_snapshot!(stderr, @r###" - Initialized repo in "." - Empty repo created. + Error: Did not create a jj repo because there is an existing Git repo in this directory. Hint: To create a repo backed by the existing Git repo, run `jj init --git-repo=.` instead. "###); }