From 1b8ecef968100cfea7dcb91fbd328547a5917808 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Tue, 24 Jan 2023 12:55:15 +0900 Subject: [PATCH] cli: have WorkspaceCommandHelper return &WorkspaceId, clone it by caller I didn't make this change before because there are many immutable/mutable borrow conflicts. Now most of the problems are consolidated to the transaction wrapper, we can simply make it return a reference. --- src/cli_util.rs | 29 +++++++++++++++-------------- src/commands/git.rs | 2 +- src/commands/mod.rs | 21 ++++++++++----------- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/cli_util.rs b/src/cli_util.rs index dd3fd8bad..49effcf70 100644 --- a/src/cli_util.rs +++ b/src/cli_util.rs @@ -513,7 +513,7 @@ impl WorkspaceCommandHelper { // If the Git HEAD has changed, abandon our old checkout and check out the new // Git HEAD. if new_git_head != old_git_head && new_git_head.is_some() { - let workspace_id = self.workspace_id(); + let workspace_id = self.workspace_id().to_owned(); let mut locked_working_copy = self.workspace.working_copy_mut().start_mutation(); if let Some(old_wc_commit_id) = self.repo.view().get_wc_commit_id(&workspace_id) { tx.mut_repo() @@ -554,7 +554,7 @@ impl WorkspaceCommandHelper { .peel_to_commit() .ok() .map(|commit| commit.id()); - if let Some(wc_commit_id) = mut_repo.view().get_wc_commit_id(&self.workspace_id()) { + if let Some(wc_commit_id) = mut_repo.view().get_wc_commit_id(self.workspace_id()) { let first_parent_id = mut_repo .index() .entry_by_id(wc_commit_id) @@ -591,7 +591,7 @@ impl WorkspaceCommandHelper { &mut self, ) -> Result<(LockedWorkingCopy, Commit), CommandError> { self.check_working_copy_writable()?; - let wc_commit_id = self.repo.view().get_wc_commit_id(&self.workspace_id()); + let wc_commit_id = self.repo.view().get_wc_commit_id(self.workspace_id()); let wc_commit = if let Some(wc_commit_id) = wc_commit_id { self.repo.store().get_commit(wc_commit_id)? } else { @@ -617,8 +617,8 @@ impl WorkspaceCommandHelper { self.workspace.workspace_root() } - pub fn workspace_id(&self) -> WorkspaceId { - self.workspace.workspace_id().clone() + pub fn workspace_id(&self) -> &WorkspaceId { + self.workspace.workspace_id() } pub fn working_copy_shared_with_git(&self) -> bool { @@ -756,7 +756,7 @@ impl WorkspaceCommandHelper { fn revset_context(&self) -> RevsetWorkspaceContext { RevsetWorkspaceContext { cwd: &self.cwd, - workspace_id: self.workspace.workspace_id(), + workspace_id: self.workspace_id(), workspace_root: self.workspace.workspace_root(), } } @@ -779,7 +779,7 @@ impl WorkspaceCommandHelper { write_commit_summary( formatter, self.repo.as_repo_ref(), - self.workspace.workspace_id(), + self.workspace_id(), commit, &self.settings, ) @@ -801,8 +801,8 @@ impl WorkspaceCommandHelper { pub fn commit_working_copy(&mut self, ui: &mut Ui) -> Result<(), CommandError> { let repo = self.repo.clone(); - let workspace_id = self.workspace_id(); - let wc_commit_id = match repo.view().get_wc_commit_id(&self.workspace_id()) { + let workspace_id = self.workspace_id().to_owned(); + let wc_commit_id = match repo.view().get_wc_commit_id(&workspace_id) { Some(wc_commit_id) => wc_commit_id.clone(), None => { // If the workspace has been deleted, it's unclear what to do, so we just skip @@ -974,15 +974,16 @@ impl WorkspaceCommandHelper { let maybe_old_commit = tx .base_repo() .view() - .get_wc_commit_id(&self.workspace_id()) + .get_wc_commit_id(self.workspace_id()) .map(|commit_id| store.get_commit(commit_id)) .transpose()?; self.repo = tx.commit(); if self.may_update_working_copy { + let workspace_id = self.workspace_id().to_owned(); let stats = update_working_copy( ui, &self.repo, - &self.workspace_id(), + &workspace_id, self.workspace.working_copy_mut(), maybe_old_commit.as_ref(), &self.settings, @@ -1031,13 +1032,13 @@ impl WorkspaceCommandTransaction<'_> { } pub fn check_out(&mut self, commit: &Commit) -> Result { - let workspace_id = self.helper.workspace_id(); + let workspace_id = self.helper.workspace_id().to_owned(); let settings = &self.helper.settings; self.tx.mut_repo().check_out(workspace_id, settings, commit) } pub fn edit(&mut self, commit: &Commit) -> Result<(), EditCommitError> { - let workspace_id = self.helper.workspace_id(); + let workspace_id = self.helper.workspace_id().to_owned(); self.tx.mut_repo().edit(workspace_id, commit) } @@ -1047,7 +1048,7 @@ impl WorkspaceCommandTransaction<'_> { commit: &Commit, ) -> std::io::Result<()> { let repo = self.tx.repo().as_repo_ref(); - let workspace_id = self.helper.workspace.workspace_id(); + let workspace_id = self.helper.workspace_id(); let settings = &self.helper.settings; write_commit_summary(formatter, repo, workspace_id, commit, settings) } diff --git a/src/commands/git.rs b/src/commands/git.rs index cd42f8f28..d626d310c 100644 --- a/src/commands/git.rs +++ b/src/commands/git.rs @@ -612,7 +612,7 @@ fn cmd_git_push( match workspace_command .repo() .view() - .get_wc_commit_id(&workspace_command.workspace_id()) + .get_wc_commit_id(workspace_command.workspace_id()) { None => { return Err(user_error("Nothing checked out in this workspace")); diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 6185fd633..653d92a0b 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1243,7 +1243,7 @@ fn cmd_show(ui: &mut Ui, command: &CommandHelper, args: &ShowArgs) -> Result<(), ); let template = crate::template_parser::parse_commit_template( workspace_command.repo().as_repo_ref(), - &workspace_command.workspace_id(), + workspace_command.workspace_id(), &template_string, ); ui.request_pager(); @@ -1269,7 +1269,7 @@ fn cmd_status( let repo = workspace_command.repo(); let maybe_checkout_id = repo .view() - .get_wc_commit_id(&workspace_command.workspace_id()); + .get_wc_commit_id(workspace_command.workspace_id()); let maybe_checkout = maybe_checkout_id .map(|id| repo.store().get_commit(id)) .transpose()?; @@ -1409,7 +1409,7 @@ fn cmd_log(ui: &mut Ui, command: &CommandHelper, args: &LogArgs) -> Result<(), C workspace_command.parse_revset(args.revisions.as_deref().unwrap_or(&default_revset))?; let repo = workspace_command.repo(); let workspace_id = workspace_command.workspace_id(); - let checkout_id = repo.view().get_wc_commit_id(&workspace_id); + let checkout_id = repo.view().get_wc_commit_id(workspace_id); let matcher = workspace_command.matcher_from_values(&args.paths)?; let revset = workspace_command.evaluate_revset(&revset_expression)?; let revset = if !args.paths.is_empty() { @@ -1428,7 +1428,7 @@ fn cmd_log(ui: &mut Ui, command: &CommandHelper, args: &LogArgs) -> Result<(), C }; let template = crate::template_parser::parse_commit_template( repo.as_repo_ref(), - &workspace_id, + workspace_id, &template_string, ); let format_commit_template = |commit: &Commit, formatter: &mut dyn Formatter| { @@ -1561,7 +1561,7 @@ fn cmd_obslog(ui: &mut Ui, command: &CommandHelper, args: &ObslogArgs) -> Result let wc_commit_id = workspace_command .repo() .view() - .get_wc_commit_id(&workspace_id); + .get_wc_commit_id(workspace_id); let diff_formats = diff_util::diff_formats_for_log(command.settings(), &args.diff_format, args.patch); @@ -1572,7 +1572,7 @@ fn cmd_obslog(ui: &mut Ui, command: &CommandHelper, args: &ObslogArgs) -> Result }; let template = crate::template_parser::parse_commit_template( workspace_command.repo().as_repo_ref(), - &workspace_id, + workspace_id, &template_string, ); @@ -1786,7 +1786,7 @@ fn cmd_commit(ui: &mut Ui, command: &CommandHelper, args: &CommitArgs) -> Result let commit_id = workspace_command .repo() .view() - .get_wc_commit_id(&workspace_command.workspace_id()) + .get_wc_commit_id(workspace_command.workspace_id()) .ok_or_else(|| user_error("This command requires a working copy"))?; let commit = workspace_command.repo().store().get_commit(commit_id)?; let description = if let Some(message) = &args.message { @@ -1943,11 +1943,10 @@ fn cmd_edit(ui: &mut Ui, command: &CommandHelper, args: &EditArgs) -> Result<(), let mut workspace_command = command.workspace_helper(ui)?; let new_commit = workspace_command.resolve_single_rev(&args.revision)?; workspace_command.check_rewriteable(&new_commit)?; - let workspace_id = workspace_command.workspace_id(); if workspace_command .repo() .view() - .get_wc_commit_id(&workspace_id) + .get_wc_commit_id(workspace_command.workspace_id()) == Some(new_commit.id()) { ui.write("Already editing that commit\n")?; @@ -3048,7 +3047,7 @@ fn cmd_workspace_add( let new_wc_commit = if let Some(old_checkout_id) = tx .base_repo() .view() - .get_wc_commit_id(&old_workspace_command.workspace_id()) + .get_wc_commit_id(old_workspace_command.workspace_id()) { tx.base_repo() .store() @@ -3073,7 +3072,7 @@ fn cmd_workspace_forget( let workspace_id = if let Some(workspace_str) = &args.workspace { WorkspaceId::new(workspace_str.to_string()) } else { - workspace_command.workspace_id() + workspace_command.workspace_id().to_owned() }; if workspace_command .repo()