Skip to content

Commit 2d0437b

Browse files
committed
working_copy: Add WorkingCopyStateError conversions for ergonomics
Note that the actual error message is almost always the same. They all end up with the message "Failed to read working copy state" in `tree_state`. So the original code adding another equivalent message is unnecessary.
1 parent f3d41b1 commit 2d0437b

File tree

3 files changed

+17
-49
lines changed

3 files changed

+17
-49
lines changed

lib/src/local_working_copy.rs

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2370,13 +2370,7 @@ impl LockedWorkingCopy for LockedLocalWorkingCopy {
23702370
&mut self,
23712371
options: &SnapshotOptions,
23722372
) -> Result<(MergedTreeId, SnapshotStats), SnapshotError> {
2373-
let tree_state = self
2374-
.wc
2375-
.tree_state_mut()
2376-
.map_err(|err| SnapshotError::Other {
2377-
message: "Failed to read the working copy state".to_string(),
2378-
err: err.into(),
2379-
})?;
2373+
let tree_state = self.wc.tree_state_mut()?;
23802374
let (is_dirty, stats) = tree_state.snapshot(options)?;
23812375
self.tree_state_dirty |= is_dirty;
23822376
Ok((tree_state.current_tree_id().clone(), stats))
@@ -2386,13 +2380,7 @@ impl LockedWorkingCopy for LockedLocalWorkingCopy {
23862380
// TODO: Write a "pending_checkout" file with the new TreeId so we can
23872381
// continue an interrupted update if we find such a file.
23882382
let new_tree = commit.tree()?;
2389-
let tree_state = self
2390-
.wc
2391-
.tree_state_mut()
2392-
.map_err(|err| CheckoutError::Other {
2393-
message: "Failed to load the working copy state".to_string(),
2394-
err: err.into(),
2395-
})?;
2383+
let tree_state = self.wc.tree_state_mut()?;
23962384
if tree_state.tree_id != *commit.tree_id() {
23972385
let stats = tree_state.check_out(&new_tree)?;
23982386
self.tree_state_dirty = true;
@@ -2408,28 +2396,14 @@ impl LockedWorkingCopy for LockedLocalWorkingCopy {
24082396

24092397
fn reset(&mut self, commit: &Commit) -> Result<(), ResetError> {
24102398
let new_tree = commit.tree()?;
2411-
self.wc
2412-
.tree_state_mut()
2413-
.map_err(|err| ResetError::Other {
2414-
message: "Failed to read the working copy state".to_string(),
2415-
err: err.into(),
2416-
})?
2417-
.reset(&new_tree)
2418-
.block_on()?;
2399+
self.wc.tree_state_mut()?.reset(&new_tree).block_on()?;
24192400
self.tree_state_dirty = true;
24202401
Ok(())
24212402
}
24222403

24232404
fn recover(&mut self, commit: &Commit) -> Result<(), ResetError> {
24242405
let new_tree = commit.tree()?;
2425-
self.wc
2426-
.tree_state_mut()
2427-
.map_err(|err| ResetError::Other {
2428-
message: "Failed to read the working copy state".to_string(),
2429-
err: err.into(),
2430-
})?
2431-
.recover(&new_tree)
2432-
.block_on()?;
2406+
self.wc.tree_state_mut()?.recover(&new_tree).block_on()?;
24332407
self.tree_state_dirty = true;
24342408
Ok(())
24352409
}
@@ -2446,11 +2420,7 @@ impl LockedWorkingCopy for LockedLocalWorkingCopy {
24462420
// continue an interrupted update if we find such a file.
24472421
let stats = self
24482422
.wc
2449-
.tree_state_mut()
2450-
.map_err(|err| CheckoutError::Other {
2451-
message: "Failed to load the working copy state".to_string(),
2452-
err: err.into(),
2453-
})?
2423+
.tree_state_mut()?
24542424
.set_sparse_patterns(new_sparse_patterns)?;
24552425
self.tree_state_dirty = true;
24562426
Ok(stats)
@@ -2485,13 +2455,7 @@ impl LockedWorkingCopy for LockedLocalWorkingCopy {
24852455

24862456
impl LockedLocalWorkingCopy {
24872457
pub fn reset_watchman(&mut self) -> Result<(), SnapshotError> {
2488-
self.wc
2489-
.tree_state_mut()
2490-
.map_err(|err| SnapshotError::Other {
2491-
message: "Failed to read the working copy state".to_string(),
2492-
err: err.into(),
2493-
})?
2494-
.reset_watchman();
2458+
self.wc.tree_state_mut()?.reset_watchman();
24952459
self.tree_state_dirty = true;
24962460
Ok(())
24972461
}

lib/src/working_copy.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ pub enum SnapshotError {
181181
/// Checking path with ignore patterns failed.
182182
#[error(transparent)]
183183
GitIgnoreError(#[from] GitIgnoreError),
184+
/// Failed to load the working copy state.
185+
#[error(transparent)]
186+
WorkingCopyStateError(#[from] WorkingCopyStateError),
184187
/// Some other error happened while snapshotting the working copy.
185188
#[error("{message}")]
186189
Other {
@@ -301,6 +304,9 @@ pub enum CheckoutError {
301304
/// Reading or writing from the commit backend failed.
302305
#[error("Internal backend error")]
303306
InternalBackendError(#[from] BackendError),
307+
/// Failed to load the working copy state.
308+
#[error(transparent)]
309+
WorkingCopyStateError(#[from] WorkingCopyStateError),
304310
/// Some other error happened while checking out the working copy.
305311
#[error("{message}")]
306312
Other {
@@ -325,7 +331,10 @@ pub enum ResetError {
325331
/// Reading or writing from the commit backend failed.
326332
#[error("Internal error")]
327333
InternalBackendError(#[from] BackendError),
328-
/// Some other error happened while checking out the working copy.
334+
/// Failed to load the working copy state.
335+
#[error(transparent)]
336+
WorkingCopyStateError(#[from] WorkingCopyStateError),
337+
/// Some other error happened while resetting the working copy.
329338
#[error("{message}")]
330339
Other {
331340
/// Error message.

lib/src/workspace.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -430,12 +430,7 @@ impl Workspace {
430430
old_tree_id: Option<&MergedTreeId>,
431431
commit: &Commit,
432432
) -> Result<CheckoutStats, CheckoutError> {
433-
let mut locked_ws =
434-
self.start_working_copy_mutation()
435-
.map_err(|err| CheckoutError::Other {
436-
message: "Failed to start editing the working copy state".to_string(),
437-
err: err.into(),
438-
})?;
433+
let mut locked_ws = self.start_working_copy_mutation()?;
439434
// Check if the current working-copy commit has changed on disk compared to what
440435
// the caller expected. It's safe to check out another commit
441436
// regardless, but it's probably not what the caller wanted, so we let

0 commit comments

Comments
 (0)