Skip to content

Commit

Permalink
working_copy: allow load_working_copy() to return error
Browse files Browse the repository at this point in the history
It's reasonable for a `WorkingCopy` implementation to want to return
an error. `LocalWorkingCopyFactory` doesn't because it loads all data
lazily. The VFS-based one at Google wants to be able to return an
error, however.
  • Loading branch information
martinvonz committed Apr 19, 2024
1 parent f58e69f commit 8bb92fa
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
6 changes: 3 additions & 3 deletions cli/examples/custom-working-copy/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,12 @@ impl WorkingCopyFactory for ConflictsWorkingCopyFactory {
store: Arc<Store>,
working_copy_path: PathBuf,
state_path: PathBuf,
) -> Box<dyn WorkingCopy> {
Box::new(ConflictsWorkingCopy::load(
) -> Result<Box<dyn WorkingCopy>, WorkingCopyStateError> {
Ok(Box::new(ConflictsWorkingCopy::load(
store,
working_copy_path,
state_path,
))
)))
}
}

Expand Down
1 change: 1 addition & 0 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1504,6 +1504,7 @@ jj git init --colocate",
err @ SignInitError::UnknownBackend(_),
)) => user_error(err),
WorkspaceLoadError::StoreLoadError(err) => internal_error(err),
WorkspaceLoadError::WorkingCopyState(err) => internal_error(err),
WorkspaceLoadError::NonUnicodePath | WorkspaceLoadError::Path(_) => user_error(err),
}
}
Expand Down
8 changes: 6 additions & 2 deletions lib/src/local_working_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1706,8 +1706,12 @@ impl WorkingCopyFactory for LocalWorkingCopyFactory {
store: Arc<Store>,
working_copy_path: PathBuf,
state_path: PathBuf,
) -> Box<dyn WorkingCopy> {
Box::new(LocalWorkingCopy::load(store, working_copy_path, state_path))
) -> Result<Box<dyn WorkingCopy>, WorkingCopyStateError> {
Ok(Box::new(LocalWorkingCopy::load(
store,
working_copy_path,
state_path,
)))
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/working_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub trait WorkingCopyFactory {
store: Arc<Store>,
working_copy_path: PathBuf,
state_path: PathBuf,
) -> Box<dyn WorkingCopy>;
) -> Result<Box<dyn WorkingCopy>, WorkingCopyStateError>;
}

/// A working copy that's being modified.
Expand Down
9 changes: 6 additions & 3 deletions lib/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ pub enum WorkspaceLoadError {
#[error("Repo path could not be interpreted as Unicode text")]
NonUnicodePath,
#[error(transparent)]
WorkingCopyState(#[from] WorkingCopyStateError),
#[error(transparent)]
Path(#[from] PathError),
}

Expand Down Expand Up @@ -503,13 +505,14 @@ impl WorkspaceLoader {
&self,
store: &Arc<Store>,
working_copy_factories: &WorkingCopyFactories,
) -> Result<Box<dyn WorkingCopy>, StoreLoadError> {
) -> Result<Box<dyn WorkingCopy>, WorkspaceLoadError> {
let working_copy_factory = self.get_working_copy_factory(working_copy_factories)?;
Ok(working_copy_factory.load_working_copy(
let working_copy = working_copy_factory.load_working_copy(
store.clone(),
self.workspace_root.to_owned(),
self.working_copy_state_path.to_owned(),
))
)?;
Ok(working_copy)
}
}

Expand Down

0 comments on commit 8bb92fa

Please sign in to comment.