diff --git a/cli/src/cli_util.rs b/cli/src/cli_util.rs index c61bf4caa..b3eff2739 100644 --- a/cli/src/cli_util.rs +++ b/cli/src/cli_util.rs @@ -41,9 +41,7 @@ use jj_lib::git_backend::GitBackend; use jj_lib::gitignore::GitIgnoreFile; use jj_lib::hex_util::to_reverse_hex; use jj_lib::id_prefix::IdPrefixContext; -use jj_lib::local_working_copy::{ - LocalWorkingCopy, LockedLocalWorkingCopy, ResetError, WorkingCopyStateError, -}; +use jj_lib::local_working_copy::{LocalWorkingCopy, LockedLocalWorkingCopy, WorkingCopyStateError}; use jj_lib::matchers::{EverythingMatcher, Matcher, PrefixMatcher, Visit}; use jj_lib::merged_tree::{MergedTree, MergedTreeBuilder}; use jj_lib::op_heads_store::{self, OpHeadResolutionError, OpHeadsStore}; @@ -63,7 +61,7 @@ use jj_lib::settings::{ConfigResultExt as _, UserSettings}; use jj_lib::transaction::Transaction; use jj_lib::tree::TreeMergeError; use jj_lib::working_copy::{ - CheckoutStats, LockedWorkingCopy, SnapshotError, SnapshotOptions, WorkingCopy, + CheckoutStats, LockedWorkingCopy, ResetError, SnapshotError, SnapshotOptions, WorkingCopy, }; use jj_lib::workspace::{ LockedWorkspace, Workspace, WorkspaceInitError, WorkspaceLoadError, WorkspaceLoader, diff --git a/lib/src/local_working_copy.rs b/lib/src/local_working_copy.rs index bc60f5811..ffb29c550 100644 --- a/lib/src/local_working_copy.rs +++ b/lib/src/local_working_copy.rs @@ -59,7 +59,7 @@ use crate::settings::HumanByteSize; use crate::store::Store; use crate::tree::Tree; use crate::working_copy::{ - CheckoutError, CheckoutStats, LockedWorkingCopy, SnapshotError, SnapshotOptions, + CheckoutError, CheckoutStats, LockedWorkingCopy, ResetError, SnapshotError, SnapshotOptions, SnapshotProgress, WorkingCopy, }; @@ -308,24 +308,6 @@ struct FsmonitorMatcher { watchman_clock: Option, } -#[derive(Debug, Error)] -pub enum ResetError { - // The current working-copy commit was deleted, maybe by an overly aggressive GC that happened - // while the current process was running. - #[error("Current working-copy commit not found: {source}")] - SourceNotFound { - source: Box, - }, - #[error("Internal error: {0}")] - InternalBackendError(#[from] BackendError), - #[error("{message}: {err:?}")] - Other { - message: String, - #[source] - err: Box, - }, -} - struct DirectoryToVisit { dir: RepoPath, disk_dir: PathBuf, @@ -1549,6 +1531,18 @@ impl LockedWorkingCopy for LockedLocalWorkingCopy { self.tree_state_dirty = true; Ok(stats) } + + fn reset(&mut self, new_tree: &MergedTree) -> Result<(), ResetError> { + self.wc + .tree_state_mut() + .map_err(|err| ResetError::Other { + message: "Failed to read the working copy state".to_string(), + err: err.into(), + })? + .reset(new_tree)?; + self.tree_state_dirty = true; + Ok(()) + } } impl LockedLocalWorkingCopy { @@ -1564,18 +1558,6 @@ impl LockedLocalWorkingCopy { Ok(()) } - pub fn reset(&mut self, new_tree: &MergedTree) -> Result<(), ResetError> { - self.wc - .tree_state_mut() - .map_err(|err| ResetError::Other { - message: "Failed to read the working copy state".to_string(), - err: err.into(), - })? - .reset(new_tree)?; - self.tree_state_dirty = true; - Ok(()) - } - pub fn sparse_patterns(&self) -> Result<&[RepoPath], WorkingCopyStateError> { self.wc.sparse_patterns() } diff --git a/lib/src/working_copy.rs b/lib/src/working_copy.rs index c9b98dd2a..7561b7e20 100644 --- a/lib/src/working_copy.rs +++ b/lib/src/working_copy.rs @@ -66,6 +66,9 @@ pub trait LockedWorkingCopy { /// Check out the specified tree in the working copy. // TODO: Pass a Commit object here because some implementations need that. fn check_out(&mut self, new_tree: &MergedTree) -> Result; + + /// Update to another tree without touching the files in the working copy. + fn reset(&mut self, new_tree: &MergedTree) -> Result<(), ResetError>; } /// An error while snapshotting the working copy. @@ -193,3 +196,27 @@ pub enum CheckoutError { err: Box, }, } + +/// An error while resetting the working copy. +#[derive(Debug, Error)] +pub enum ResetError { + /// The current working-copy commit was deleted, maybe by an overly + /// aggressive GC that happened while the current process was running. + #[error("Current working-copy commit not found: {source}")] + SourceNotFound { + /// The underlying error. + source: Box, + }, + /// Reading or writing from the commit backend failed. + #[error("Internal error: {0}")] + InternalBackendError(#[from] BackendError), + /// Some other error happened while checking out the working copy. + #[error("{message}: {err:?}")] + Other { + /// Error message. + message: String, + #[source] + /// The underlying error. + err: Box, + }, +}