Skip to content

Commit d3c90e2

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 735a27d commit d3c90e2

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
@@ -2391,13 +2391,7 @@ impl LockedWorkingCopy for LockedLocalWorkingCopy {
23912391
&mut self,
23922392
options: &SnapshotOptions,
23932393
) -> Result<(MergedTreeId, SnapshotStats), SnapshotError> {
2394-
let tree_state = self
2395-
.wc
2396-
.tree_state_mut()
2397-
.map_err(|err| SnapshotError::Other {
2398-
message: "Failed to read the working copy state".to_string(),
2399-
err: err.into(),
2400-
})?;
2394+
let tree_state = self.wc.tree_state_mut()?;
24012395
let (is_dirty, stats) = tree_state.snapshot(options)?;
24022396
self.tree_state_dirty |= is_dirty;
24032397
Ok((tree_state.current_tree_id().clone(), stats))
@@ -2411,13 +2405,7 @@ impl LockedWorkingCopy for LockedLocalWorkingCopy {
24112405
// TODO: Write a "pending_checkout" file with the new TreeId so we can
24122406
// continue an interrupted update if we find such a file.
24132407
let new_tree = commit.tree()?;
2414-
let tree_state = self
2415-
.wc
2416-
.tree_state_mut()
2417-
.map_err(|err| CheckoutError::Other {
2418-
message: "Failed to load the working copy state".to_string(),
2419-
err: err.into(),
2420-
})?;
2408+
let tree_state = self.wc.tree_state_mut()?;
24212409
if tree_state.tree_id != *commit.tree_id() {
24222410
let stats = tree_state.check_out(&new_tree, options)?;
24232411
self.tree_state_dirty = true;
@@ -2433,28 +2421,14 @@ impl LockedWorkingCopy for LockedLocalWorkingCopy {
24332421

24342422
fn reset(&mut self, commit: &Commit) -> Result<(), ResetError> {
24352423
let new_tree = commit.tree()?;
2436-
self.wc
2437-
.tree_state_mut()
2438-
.map_err(|err| ResetError::Other {
2439-
message: "Failed to read the working copy state".to_string(),
2440-
err: err.into(),
2441-
})?
2442-
.reset(&new_tree)
2443-
.block_on()?;
2424+
self.wc.tree_state_mut()?.reset(&new_tree).block_on()?;
24442425
self.tree_state_dirty = true;
24452426
Ok(())
24462427
}
24472428

24482429
fn recover(&mut self, commit: &Commit) -> Result<(), ResetError> {
24492430
let new_tree = commit.tree()?;
2450-
self.wc
2451-
.tree_state_mut()
2452-
.map_err(|err| ResetError::Other {
2453-
message: "Failed to read the working copy state".to_string(),
2454-
err: err.into(),
2455-
})?
2456-
.recover(&new_tree)
2457-
.block_on()?;
2431+
self.wc.tree_state_mut()?.recover(&new_tree).block_on()?;
24582432
self.tree_state_dirty = true;
24592433
Ok(())
24602434
}
@@ -2472,11 +2446,7 @@ impl LockedWorkingCopy for LockedLocalWorkingCopy {
24722446
// continue an interrupted update if we find such a file.
24732447
let stats = self
24742448
.wc
2475-
.tree_state_mut()
2476-
.map_err(|err| CheckoutError::Other {
2477-
message: "Failed to load the working copy state".to_string(),
2478-
err: err.into(),
2479-
})?
2449+
.tree_state_mut()?
24802450
.set_sparse_patterns(new_sparse_patterns, options)?;
24812451
self.tree_state_dirty = true;
24822452
Ok(stats)
@@ -2511,13 +2481,7 @@ impl LockedWorkingCopy for LockedLocalWorkingCopy {
25112481

25122482
impl LockedLocalWorkingCopy {
25132483
pub fn reset_watchman(&mut self) -> Result<(), SnapshotError> {
2514-
self.wc
2515-
.tree_state_mut()
2516-
.map_err(|err| SnapshotError::Other {
2517-
message: "Failed to read the working copy state".to_string(),
2518-
err: err.into(),
2519-
})?
2520-
.reset_watchman();
2484+
self.wc.tree_state_mut()?.reset_watchman();
25212485
self.tree_state_dirty = true;
25222486
Ok(())
25232487
}

lib/src/working_copy.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ pub enum SnapshotError {
187187
/// Checking path with ignore patterns failed.
188188
#[error(transparent)]
189189
GitIgnoreError(#[from] GitIgnoreError),
190+
/// Failed to load the working copy state.
191+
#[error(transparent)]
192+
WorkingCopyStateError(#[from] WorkingCopyStateError),
190193
/// Some other error happened while snapshotting the working copy.
191194
#[error("{message}")]
192195
Other {
@@ -326,6 +329,9 @@ pub enum CheckoutError {
326329
/// Reading or writing from the commit backend failed.
327330
#[error("Internal backend error")]
328331
InternalBackendError(#[from] BackendError),
332+
/// Failed to load the working copy state.
333+
#[error(transparent)]
334+
WorkingCopyStateError(#[from] WorkingCopyStateError),
329335
/// Some other error happened while checking out the working copy.
330336
#[error("{message}")]
331337
Other {
@@ -350,7 +356,10 @@ pub enum ResetError {
350356
/// Reading or writing from the commit backend failed.
351357
#[error("Internal error")]
352358
InternalBackendError(#[from] BackendError),
353-
/// Some other error happened while checking out the working copy.
359+
/// Failed to load the working copy state.
360+
#[error(transparent)]
361+
WorkingCopyStateError(#[from] WorkingCopyStateError),
362+
/// Some other error happened while resetting the working copy.
354363
#[error("{message}")]
355364
Other {
356365
/// Error message.

lib/src/workspace.rs

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

0 commit comments

Comments
 (0)