From 61d6604d3da3403137cc43f82f462b50dcb4e553 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Wed, 22 Feb 2023 19:02:52 +0900 Subject: [PATCH] cli: add workspace_command.get_wc_commit_id() for short We can't replace all callers, but I think it's common enough to add a helper function. --- src/cli_util.rs | 7 +++++-- src/commands/git.rs | 6 +----- src/commands/mod.rs | 26 ++++++-------------------- 3 files changed, 12 insertions(+), 27 deletions(-) diff --git a/src/cli_util.rs b/src/cli_util.rs index 7a4c93dd1..935d348d7 100644 --- a/src/cli_util.rs +++ b/src/cli_util.rs @@ -640,8 +640,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 = if let Some(wc_commit_id) = wc_commit_id { + let wc_commit = if let Some(wc_commit_id) = self.get_wc_commit_id() { self.repo.store().get_commit(wc_commit_id)? } else { return Err(user_error("Nothing checked out in this workspace")); @@ -670,6 +669,10 @@ impl WorkspaceCommandHelper { self.workspace.workspace_id() } + pub fn get_wc_commit_id(&self) -> Option<&CommitId> { + self.repo.view().get_wc_commit_id(self.workspace_id()) + } + pub fn working_copy_shared_with_git(&self) -> bool { self.working_copy_shared_with_git } diff --git a/src/commands/git.rs b/src/commands/git.rs index 2d18e80c5..8779fb777 100644 --- a/src/commands/git.rs +++ b/src/commands/git.rs @@ -647,11 +647,7 @@ fn cmd_git_push( } } } else { - match workspace_command - .repo() - .view() - .get_wc_commit_id(workspace_command.workspace_id()) - { + match workspace_command.get_wc_commit_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 ca1f9485e..2b3d2c1dc 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1314,10 +1314,8 @@ fn cmd_status( ) -> Result<(), CommandError> { let workspace_command = command.workspace_helper(ui)?; let repo = workspace_command.repo(); - let maybe_wc_commit_id = repo - .view() - .get_wc_commit_id(workspace_command.workspace_id()); - let maybe_wc_commit = maybe_wc_commit_id + let maybe_wc_commit = workspace_command + .get_wc_commit_id() .map(|id| repo.store().get_commit(id)) .transpose()?; ui.request_pager(); @@ -1416,8 +1414,7 @@ fn cmd_log(ui: &mut Ui, command: &CommandHelper, args: &LogArgs) -> Result<(), C let revset_expression = 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 wc_commit_id = repo.view().get_wc_commit_id(workspace_id); + let wc_commit_id = workspace_command.get_wc_commit_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() { @@ -1554,11 +1551,7 @@ fn cmd_obslog(ui: &mut Ui, command: &CommandHelper, args: &ObslogArgs) -> Result let workspace_command = command.workspace_helper(ui)?; let start_commit = workspace_command.resolve_single_rev(&args.revision)?; - let workspace_id = workspace_command.workspace_id(); - let wc_commit_id = workspace_command - .repo() - .view() - .get_wc_commit_id(workspace_id); + let wc_commit_id = workspace_command.get_wc_commit_id(); let diff_formats = diff_util::diff_formats_for_log(command.settings(), &args.diff_format, args.patch); @@ -1773,9 +1766,7 @@ fn cmd_commit(ui: &mut Ui, command: &CommandHelper, args: &CommitArgs) -> Result let mut workspace_command = command.workspace_helper(ui)?; let commit_id = workspace_command - .repo() - .view() - .get_wc_commit_id(workspace_command.workspace_id()) + .get_wc_commit_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 { @@ -1924,12 +1915,7 @@ 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_rewritable(&new_commit)?; - if workspace_command - .repo() - .view() - .get_wc_commit_id(workspace_command.workspace_id()) - == Some(new_commit.id()) - { + if workspace_command.get_wc_commit_id() == Some(new_commit.id()) { ui.write("Already editing that commit\n")?; } else { let mut tx =