ok/jj
1
0
Fork 0
forked from mirrors/jj

workspace: support creating shares of custom workspaces

This commit is contained in:
Daniel Ploch 2024-01-25 12:25:02 -05:00 committed by Daniel Ploch
parent cb889f0b45
commit 20cbe77bf5
3 changed files with 35 additions and 14 deletions

View file

@ -614,6 +614,18 @@ impl CommandHelper {
self.for_loaded_repo(ui, workspace, repo) self.for_loaded_repo(ui, workspace, repo)
} }
pub fn get_working_copy_factory(&self) -> Result<&dyn WorkingCopyFactory, CommandError> {
let loader = self.workspace_loader()?;
// We convert StoreLoadError -> WorkspaceLoadError -> CommandError
let factory: Result<_, WorkspaceLoadError> = loader
.get_working_copy_factory(&self.working_copy_factories)
.map_err(|e| e.into());
let factory = factory
.map_err(|err| map_workspace_load_error(err, self.global_args.repository.as_deref()))?;
Ok(factory)
}
#[instrument(skip_all)] #[instrument(skip_all)]
pub fn load_workspace(&self) -> Result<Workspace, CommandError> { pub fn load_workspace(&self) -> Result<Workspace, CommandError> {
let loader = self.workspace_loader()?; let loader = self.workspace_loader()?;

View file

@ -23,7 +23,7 @@ use jj_lib::object_id::ObjectId;
use jj_lib::op_store::WorkspaceId; use jj_lib::op_store::WorkspaceId;
use jj_lib::repo::Repo; use jj_lib::repo::Repo;
use jj_lib::rewrite::merge_commit_trees; use jj_lib::rewrite::merge_commit_trees;
use jj_lib::workspace::{default_working_copy_factory, Workspace}; use jj_lib::workspace::Workspace;
use tracing::instrument; use tracing::instrument;
use crate::cli_util::{ use crate::cli_util::{
@ -148,12 +148,13 @@ fn cmd_workspace_add(
"Workspace named '{name}' already exists" "Workspace named '{name}' already exists"
))); )));
} }
// TODO: How do we create a workspace with a non-default working copy?
let working_copy_factory = command.get_working_copy_factory()?;
let (new_workspace, repo) = Workspace::init_workspace_with_existing_repo( let (new_workspace, repo) = Workspace::init_workspace_with_existing_repo(
command.settings(), command.settings(),
&destination_path, &destination_path,
repo, repo,
&*default_working_copy_factory(), working_copy_factory,
workspace_id, workspace_id,
)?; )?;
writeln!( writeln!(

View file

@ -484,25 +484,33 @@ impl WorkspaceLoader {
Ok(workspace) Ok(workspace)
} }
fn load_working_copy( pub fn get_working_copy_factory<'a>(
&self, &self,
store: &Arc<Store>, working_copy_factories: &'a HashMap<String, Box<dyn WorkingCopyFactory>>,
working_copy_factories: &HashMap<String, Box<dyn WorkingCopyFactory>>, ) -> Result<&'a dyn WorkingCopyFactory, StoreLoadError> {
) -> Result<Box<dyn WorkingCopy>, StoreLoadError> {
// For compatibility with existing repos. TODO: Delete default in 0.17+ // For compatibility with existing repos. TODO: Delete default in 0.17+
let working_copy_type = read_store_type_compat( let working_copy_type = read_store_type_compat(
"working copy", "working copy",
self.working_copy_state_path.join("type"), self.working_copy_state_path.join("type"),
LocalWorkingCopy::name, LocalWorkingCopy::name,
)?; )?;
let working_copy_factory =
working_copy_factories
.get(&working_copy_type)
.ok_or_else(|| StoreLoadError::UnsupportedType {
store: "working copy",
store_type: working_copy_type.to_string(),
})?;
if let Some(factory) = working_copy_factories.get(&working_copy_type) {
Ok(factory.as_ref())
} else {
Err(StoreLoadError::UnsupportedType {
store: "working copy",
store_type: working_copy_type.to_string(),
})
}
}
fn load_working_copy(
&self,
store: &Arc<Store>,
working_copy_factories: &HashMap<String, Box<dyn WorkingCopyFactory>>,
) -> Result<Box<dyn WorkingCopy>, StoreLoadError> {
let working_copy_factory = self.get_working_copy_factory(working_copy_factories)?;
Ok(working_copy_factory.load_working_copy( Ok(working_copy_factory.load_working_copy(
store.clone(), store.clone(),
self.workspace_root.to_owned(), self.workspace_root.to_owned(),