Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove redundant workaround for unborn branch on "git clone --colocate" #2391

Merged
merged 2 commits into from
Oct 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions cli/src/commands/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"))?;
Expand All @@ -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)?;
}
Expand All @@ -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)?;
Expand All @@ -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");

Expand All @@ -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<T>(ui: &mut Ui, f: impl FnOnce(git::RemoteCallbacks<'_>) -> T) -> T {
Expand Down