From e1be0f509616e9b10d45fdfcfca3f03cb6b7dd8e Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sun, 2 Oct 2022 15:03:44 -0700 Subject: [PATCH] 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. --- src/cli_util.rs | 55 +++++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/src/cli_util.rs b/src/cli_util.rs index 75246928f..74014c41a 100644 --- a/src/cli_util.rs +++ b/src/cli_util.rs @@ -242,38 +242,39 @@ impl CommandHelper { } pub fn workspace_helper(&self, ui: &mut Ui) -> Result { - let wc_path_str = self.global_args.repository.as_deref().unwrap_or("."); - 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 { - WorkspaceLoadError::NoWorkspaceHere(wc_path) => { - let message = format!("There is no jj repo in \"{}\"", wc_path_str); - let git_dir = wc_path.join(".git"); - if git_dir.is_dir() { - user_error_with_hint( - message, - "It looks like this is a git repo. You can create a jj repo \ - backed by it by running this: -jj init --git-repo=.", - ) - } else { - user_error(message) - } - } - WorkspaceLoadError::RepoDoesNotExist(repo_dir) => user_error(format!( - "The repository directory at {} is missing. Was it moved?", - repo_dir.to_str().unwrap() - )), - WorkspaceLoadError::Path(e) => user_error(format!("{}: {}", e, e.error)), - WorkspaceLoadError::NonUnicodePath => user_error(err.to_string()), - } - })?; + 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 { + let wc_path_str = self.global_args.repository.as_deref().unwrap_or("."); + let wc_path = ui.cwd().join(wc_path_str); + Workspace::load(ui.settings(), &wc_path, &self.backend_factories).map_err(|err| match err { + WorkspaceLoadError::NoWorkspaceHere(wc_path) => { + let message = format!("There is no jj repo in \"{}\"", wc_path_str); + let git_dir = wc_path.join(".git"); + if git_dir.is_dir() { + user_error_with_hint( + message, + "It looks like this is a git repo. You can create a jj repo backed by it \ + by running this: +jj init --git-repo=.", + ) + } else { + user_error(message) + } + } + WorkspaceLoadError::RepoDoesNotExist(repo_dir) => user_error(format!( + "The repository directory at {} is missing. Was it moved?", + repo_dir.to_str().unwrap() + )), + WorkspaceLoadError::Path(e) => user_error(format!("{}: {}", e, e.error)), + WorkspaceLoadError::NonUnicodePath => user_error(err.to_string()), + }) + } + fn resolve_operation( &self, ui: &mut Ui,