diff --git a/cli/src/commands/git.rs b/cli/src/commands/git.rs index 57a5cf7e1a..506cd69c99 100644 --- a/cli/src/commands/git.rs +++ b/cli/src/commands/git.rs @@ -443,6 +443,7 @@ fn cmd_git_clone( if command.global_args().repository.is_some() { return Err(user_error("'--repository' cannot be used with 'git clone'")); } + let remote_name = "origin"; let source = absolute_git_source(command.cwd(), &args.source); let wc_path_str = args .destination @@ -466,7 +467,14 @@ fn cmd_git_clone( let canonical_wc_path: PathBuf = wc_path .canonicalize() .map_err(|err| user_error(format!("Failed to create {wc_path_str}: {err}")))?; - let clone_result = do_git_clone(ui, command, args.colocate, &source, &canonical_wc_path); + let clone_result = do_git_clone( + ui, + command, + args.colocate, + remote_name, + &source, + &canonical_wc_path, + ); if clone_result.is_err() { let clean_up_dirs = || -> io::Result<()> { fs::remove_dir_all(canonical_wc_path.join(".jj"))?; @@ -489,21 +497,15 @@ fn cmd_git_clone( } } - let (mut workspace_command, git_repo, stats) = clone_result?; + let (mut workspace_command, stats) = clone_result?; if let Some(default_branch) = &stats.default_branch { let default_branch_remote_ref = workspace_command .repo() .view() - .get_remote_branch(default_branch, "origin"); + .get_remote_branch(default_branch, remote_name); if let Some(commit_id) = default_branch_remote_ref.target.as_normal().cloned() { let mut checkout_tx = workspace_command.start_transaction("check out git remote's default branch"); - if args.colocate { - // HEAD can't be detached without specifying a commit, so we set the new default - // branch instead. Otherwise, the old "unborn" default branch would move - // alongside the new default branch. - git_repo.set_head(&format!("refs/heads/{default_branch}"))?; - } if let Ok(commit) = checkout_tx.repo().store().get_commit(&commit_id) { checkout_tx.check_out(&commit)?; } @@ -517,9 +519,10 @@ fn do_git_clone( ui: &mut Ui, command: &CommandHelper, colocate: bool, + remote_name: &str, source: &str, wc_path: &Path, -) -> Result<(WorkspaceCommandHelper, git2::Repository, GitFetchStats), CommandError> { +) -> Result<(WorkspaceCommandHelper, GitFetchStats), CommandError> { let (workspace, repo) = if colocate { let git_repo = git2::Repository::init(wc_path)?; add_to_git_exclude(ui, &git_repo)?; @@ -534,7 +537,6 @@ fn do_git_clone( wc_path.display() )?; let mut workspace_command = command.for_loaded_repo(ui, workspace, repo)?; - let remote_name = "origin"; git_repo.remote(remote_name, source).unwrap(); let mut fetch_tx = workspace_command.start_transaction("fetch from git remote into empty repo"); @@ -560,7 +562,7 @@ fn do_git_clone( })?; print_git_import_stats(ui, &stats.import_stats)?; fetch_tx.finish(ui)?; - Ok((workspace_command, git_repo, stats)) + Ok((workspace_command, stats)) } fn with_remote_callbacks(ui: &mut Ui, f: impl FnOnce(git::RemoteCallbacks<'_>) -> T) -> T {