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.
This commit is contained in:
Yuya Nishihara 2023-02-22 19:02:52 +09:00
parent 49fd177c47
commit 61d6604d3d
3 changed files with 12 additions and 27 deletions

View file

@ -640,8 +640,7 @@ impl WorkspaceCommandHelper {
&mut self, &mut self,
) -> Result<(LockedWorkingCopy, Commit), CommandError> { ) -> Result<(LockedWorkingCopy, Commit), CommandError> {
self.check_working_copy_writable()?; 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) = self.get_wc_commit_id() {
let wc_commit = if let Some(wc_commit_id) = wc_commit_id {
self.repo.store().get_commit(wc_commit_id)? self.repo.store().get_commit(wc_commit_id)?
} else { } else {
return Err(user_error("Nothing checked out in this workspace")); return Err(user_error("Nothing checked out in this workspace"));
@ -670,6 +669,10 @@ impl WorkspaceCommandHelper {
self.workspace.workspace_id() 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 { pub fn working_copy_shared_with_git(&self) -> bool {
self.working_copy_shared_with_git self.working_copy_shared_with_git
} }

View file

@ -647,11 +647,7 @@ fn cmd_git_push(
} }
} }
} else { } else {
match workspace_command match workspace_command.get_wc_commit_id() {
.repo()
.view()
.get_wc_commit_id(workspace_command.workspace_id())
{
None => { None => {
return Err(user_error("Nothing checked out in this workspace")); return Err(user_error("Nothing checked out in this workspace"));
} }

View file

@ -1314,10 +1314,8 @@ fn cmd_status(
) -> Result<(), CommandError> { ) -> Result<(), CommandError> {
let workspace_command = command.workspace_helper(ui)?; let workspace_command = command.workspace_helper(ui)?;
let repo = workspace_command.repo(); let repo = workspace_command.repo();
let maybe_wc_commit_id = repo let maybe_wc_commit = workspace_command
.view() .get_wc_commit_id()
.get_wc_commit_id(workspace_command.workspace_id());
let maybe_wc_commit = maybe_wc_commit_id
.map(|id| repo.store().get_commit(id)) .map(|id| repo.store().get_commit(id))
.transpose()?; .transpose()?;
ui.request_pager(); ui.request_pager();
@ -1416,8 +1414,7 @@ fn cmd_log(ui: &mut Ui, command: &CommandHelper, args: &LogArgs) -> Result<(), C
let revset_expression = let revset_expression =
workspace_command.parse_revset(args.revisions.as_deref().unwrap_or(&default_revset))?; workspace_command.parse_revset(args.revisions.as_deref().unwrap_or(&default_revset))?;
let repo = workspace_command.repo(); let repo = workspace_command.repo();
let workspace_id = workspace_command.workspace_id(); let wc_commit_id = workspace_command.get_wc_commit_id();
let wc_commit_id = repo.view().get_wc_commit_id(workspace_id);
let matcher = workspace_command.matcher_from_values(&args.paths)?; let matcher = workspace_command.matcher_from_values(&args.paths)?;
let revset = workspace_command.evaluate_revset(&revset_expression)?; let revset = workspace_command.evaluate_revset(&revset_expression)?;
let revset = if !args.paths.is_empty() { 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 workspace_command = command.workspace_helper(ui)?;
let start_commit = workspace_command.resolve_single_rev(&args.revision)?; let start_commit = workspace_command.resolve_single_rev(&args.revision)?;
let workspace_id = workspace_command.workspace_id(); let wc_commit_id = workspace_command.get_wc_commit_id();
let wc_commit_id = workspace_command
.repo()
.view()
.get_wc_commit_id(workspace_id);
let diff_formats = let diff_formats =
diff_util::diff_formats_for_log(command.settings(), &args.diff_format, args.patch); 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 mut workspace_command = command.workspace_helper(ui)?;
let commit_id = workspace_command let commit_id = workspace_command
.repo() .get_wc_commit_id()
.view()
.get_wc_commit_id(workspace_command.workspace_id())
.ok_or_else(|| user_error("This command requires a working copy"))?; .ok_or_else(|| user_error("This command requires a working copy"))?;
let commit = workspace_command.repo().store().get_commit(commit_id)?; let commit = workspace_command.repo().store().get_commit(commit_id)?;
let description = if let Some(message) = &args.message { 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 mut workspace_command = command.workspace_helper(ui)?;
let new_commit = workspace_command.resolve_single_rev(&args.revision)?; let new_commit = workspace_command.resolve_single_rev(&args.revision)?;
workspace_command.check_rewritable(&new_commit)?; workspace_command.check_rewritable(&new_commit)?;
if workspace_command if workspace_command.get_wc_commit_id() == Some(new_commit.id()) {
.repo()
.view()
.get_wc_commit_id(workspace_command.workspace_id())
== Some(new_commit.id())
{
ui.write("Already editing that commit\n")?; ui.write("Already editing that commit\n")?;
} else { } else {
let mut tx = let mut tx =