cli: extract a function for loading the workspace

I want to be able to create a `WorkspaceCommandHelper` without
snapshotting the working copy. That will be useful when adding a
command for updating a stale working copy.
This commit is contained in:
Martin von Zweigbergk 2022-10-02 15:03:44 -07:00 committed by Martin von Zweigbergk
parent 18b80f4008
commit e1be0f5096

View file

@ -242,19 +242,24 @@ impl CommandHelper {
} }
pub fn workspace_helper(&self, ui: &mut Ui) -> Result<WorkspaceCommandHelper, CommandError> { pub fn workspace_helper(&self, ui: &mut Ui) -> Result<WorkspaceCommandHelper, CommandError> {
let workspace = self.load_workspace(ui)?;
let mut workspace_command = self.resolve_operation(ui, workspace)?;
workspace_command.snapshot(ui)?;
Ok(workspace_command)
}
pub fn load_workspace(&self, ui: &Ui) -> Result<Workspace, CommandError> {
let wc_path_str = self.global_args.repository.as_deref().unwrap_or("."); let wc_path_str = self.global_args.repository.as_deref().unwrap_or(".");
let wc_path = ui.cwd().join(wc_path_str); let wc_path = ui.cwd().join(wc_path_str);
let workspace = Workspace::load(ui.settings(), &wc_path, &self.backend_factories).map_err(|err| match err {
Workspace::load(ui.settings(), &wc_path, &self.backend_factories).map_err(|err| {
match err {
WorkspaceLoadError::NoWorkspaceHere(wc_path) => { WorkspaceLoadError::NoWorkspaceHere(wc_path) => {
let message = format!("There is no jj repo in \"{}\"", wc_path_str); let message = format!("There is no jj repo in \"{}\"", wc_path_str);
let git_dir = wc_path.join(".git"); let git_dir = wc_path.join(".git");
if git_dir.is_dir() { if git_dir.is_dir() {
user_error_with_hint( user_error_with_hint(
message, message,
"It looks like this is a git repo. You can create a jj repo \ "It looks like this is a git repo. You can create a jj repo backed by it \
backed by it by running this: by running this:
jj init --git-repo=.", jj init --git-repo=.",
) )
} else { } else {
@ -267,11 +272,7 @@ jj init --git-repo=.",
)), )),
WorkspaceLoadError::Path(e) => user_error(format!("{}: {}", e, e.error)), WorkspaceLoadError::Path(e) => user_error(format!("{}: {}", e, e.error)),
WorkspaceLoadError::NonUnicodePath => user_error(err.to_string()), WorkspaceLoadError::NonUnicodePath => user_error(err.to_string()),
} })
})?;
let mut workspace_command = self.resolve_operation(ui, workspace)?;
workspace_command.snapshot(ui)?;
Ok(workspace_command)
} }
fn resolve_operation( fn resolve_operation(