Skip to content

Commit

Permalink
no-op: Refactor out git_init() to encapsulate all git related work.
Browse files Browse the repository at this point in the history
* Create a git_init() function in cli/src/commands/init.rs where all git related work is done.
  This function will be moved to cli/src/commands/git.rs in a subsequent PR.
  • Loading branch information
essiene committed Jan 15, 2024
1 parent 3a6db44 commit ac52647
Showing 1 changed file with 70 additions and 39 deletions.
109 changes: 70 additions & 39 deletions cli/src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use std::io;
use std::io::Write;
use std::path::Path;

use clap::ArgGroup;
use itertools::Itertools as _;
Expand Down Expand Up @@ -55,45 +56,9 @@ pub(crate) fn cmd_init(
) -> Result<(), CommandError> {
let wc_path = file_util::create_or_reuse_dir(&command.cwd().join(&args.destination))
.map_err(|e| user_error(format!("Failed to create workspace: {e}")))?;
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 git_store_path = cwd.join(git_store_str);
let (workspace, repo) =
Workspace::init_external_git(command.settings(), &wc_path, &git_store_path)?;
let mut workspace_command = command.for_loaded_repo(ui, workspace, repo)?;
git::maybe_add_gitignore(&workspace_command)?;
workspace_command.snapshot(ui)?;
if !workspace_command.working_copy_shared_with_git() {
let mut tx = workspace_command.start_transaction();
let stats = jj_lib::git::import_some_refs(
tx.mut_repo(),
&command.settings().git_settings(),
|ref_name| !jj_lib::git::is_reserved_git_remote_ref(ref_name),
)?;
print_git_import_stats(ui, &stats)?;
if let Some(git_head_id) = tx.mut_repo().view().git_head().as_normal().cloned() {
let git_head_commit = tx.mut_repo().store().get_commit(&git_head_id)?;
tx.check_out(&git_head_commit)?;
}
if tx.mut_repo().has_changes() {
tx.finish(ui, "import git refs")?;
}
}
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)?;
if args.git || args.git_repo.is_some() {
git_init(ui, command, &wc_path, args.git, &args.git_repo)?;
} else {
if !command.settings().allow_native_backend() {
return Err(user_error_with_hint(
Expand All @@ -103,8 +68,10 @@ 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 \"{}\"",
Expand Down Expand Up @@ -146,3 +113,67 @@ fn print_trackable_remote_branches(ui: &Ui, view: &View) -> io::Result<()> {
)?;
Ok(())
}

// TODO(essiene): Move to cli/src/commands/git.rs for `jj git init`
fn git_init(
ui: &mut Ui,
command: &CommandHelper,
workspace_root: &Path,
use_git_backend: bool,
git_repo: &Option<String>,
) -> Result<(), CommandError> {
let cwd = command.cwd().canonicalize().unwrap();
let relative_wc_path = file_util::relative_path(&cwd, workspace_root);

match (use_git_backend, &git_repo) {
(true, None) => {
if workspace_root.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(), workspace_root)?;
}

(_, Some(git_store_str)) => {
let git_store_path = command.cwd().join(git_store_str);
let (workspace, repo) =
Workspace::init_external_git(command.settings(), workspace_root, &git_store_path)?;
let mut workspace_command = command.for_loaded_repo(ui, workspace, repo)?;
git::maybe_add_gitignore(&workspace_command)?;
workspace_command.snapshot(ui)?;
if !workspace_command.working_copy_shared_with_git() {
let mut tx = workspace_command.start_transaction();
let stats = jj_lib::git::import_some_refs(
tx.mut_repo(),
&command.settings().git_settings(),
|ref_name| !jj_lib::git::is_reserved_git_remote_ref(ref_name),
)?;
print_git_import_stats(ui, &stats)?;
if let Some(git_head_id) = tx.mut_repo().view().git_head().as_normal().cloned() {
let git_head_commit = tx.mut_repo().store().get_commit(&git_head_id)?;
tx.check_out(&git_head_commit)?;
}
if tx.mut_repo().has_changes() {
tx.finish(ui, "import git refs")?;
}
}
print_trackable_remote_branches(ui, workspace_command.repo().view())?;
}

_ => {
return Err(user_error_with_hint(
"Failed to create a Git backed jj repo.",
"To create a Git backed jj repo, run with --git or --git-repo.",
));
}
}

Ok(())
}

0 comments on commit ac52647

Please sign in to comment.