From 63aa484046af419784c19e187916c41c0bd55404 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sun, 22 Jan 2023 21:38:06 -0800 Subject: [PATCH] repo: add a specific error type for `MutableRepo::check_out()` --- lib/src/repo.rs | 15 ++++++++++++--- lib/src/workspace.rs | 8 +++++--- src/cli_util.rs | 10 ++++++++-- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/lib/src/repo.rs b/lib/src/repo.rs index 96490f2e3..7b5b2a607 100644 --- a/lib/src/repo.rs +++ b/lib/src/repo.rs @@ -746,7 +746,7 @@ impl MutableRepo { workspace_id: WorkspaceId, settings: &UserSettings, commit: &Commit, - ) -> BackendResult { + ) -> Result { let wc_commit = self .new_commit( settings, @@ -754,7 +754,7 @@ impl MutableRepo { commit.tree_id().clone(), ) .write()?; - self.edit(workspace_id, &wc_commit).unwrap(); + self.edit(workspace_id, &wc_commit)?; Ok(wc_commit) } @@ -1101,7 +1101,7 @@ impl MutableRepo { } /// Error from attempts to check out the root commit for editing -#[derive(Debug, Copy, Clone, Error)] +#[derive(Debug, Error)] #[error("Cannot rewrite the root commit")] pub struct RewriteRootCommit; @@ -1112,6 +1112,15 @@ pub enum EditCommitError { RewriteRootCommit, } +/// Error from attempts to check out a commit +#[derive(Debug, Error)] +pub enum CheckOutCommitError { + #[error("Failed to create new working-copy commit: {0}")] + CreateCommit(#[from] BackendError), + #[error("Failed to edit commit: {0}")] + EditCommit(#[from] EditCommitError), +} + #[derive(Debug, Error)] #[error("Cannot access {path}")] pub struct PathError { diff --git a/lib/src/workspace.rs b/lib/src/workspace.rs index 10930cab7..a57dc993f 100644 --- a/lib/src/workspace.rs +++ b/lib/src/workspace.rs @@ -19,13 +19,15 @@ use std::sync::Arc; use thiserror::Error; -use crate::backend::{Backend, BackendError}; +use crate::backend::Backend; use crate::git_backend::GitBackend; use crate::local_backend::LocalBackend; use crate::op_heads_store::OpHeadsStore; use crate::op_store::{self, OpStore, OperationMetadata, WorkspaceId}; use crate::operation::Operation; -use crate::repo::{IoResultExt, PathError, ReadonlyRepo, RepoLoader, StoreFactories}; +use crate::repo::{ + CheckOutCommitError, IoResultExt, PathError, ReadonlyRepo, RepoLoader, StoreFactories, +}; use crate::settings::UserSettings; use crate::working_copy::WorkingCopy; @@ -36,7 +38,7 @@ pub enum WorkspaceInitError { #[error("Repo path could not be interpreted as Unicode text")] NonUnicodePath, #[error(transparent)] - BackendError(#[from] BackendError), + CheckOutCommit(#[from] CheckOutCommitError), #[error(transparent)] Path(#[from] PathError), } diff --git a/src/cli_util.rs b/src/cli_util.rs index aecd3162a..e7e11b9fd 100644 --- a/src/cli_util.rs +++ b/src/cli_util.rs @@ -37,8 +37,8 @@ use jujutsu_lib::op_heads_store::{self, OpHeadResolutionError, OpHeadsStore}; use jujutsu_lib::op_store::{OpStore, OpStoreError, OperationId, WorkspaceId}; use jujutsu_lib::operation::Operation; use jujutsu_lib::repo::{ - EditCommitError, MutableRepo, ReadonlyRepo, RepoLoader, RepoRef, RewriteRootCommit, - StoreFactories, + CheckOutCommitError, EditCommitError, MutableRepo, ReadonlyRepo, RepoLoader, RepoRef, + RewriteRootCommit, StoreFactories, }; use jujutsu_lib::repo_path::{FsPathParseError, RepoPath}; use jujutsu_lib::revset::{ @@ -124,6 +124,12 @@ impl From for CommandError { } } +impl From for CommandError { + fn from(err: CheckOutCommitError) -> Self { + CommandError::InternalError(format!("Failed to check out a commit: {err}")) + } +} + impl From for CommandError { fn from(err: BackendError) -> Self { user_error(format!("Unexpected error from backend: {err}"))