Skip to content

Commit

Permalink
workspace: allow extensions to customize the creation of the .jj dire…
Browse files Browse the repository at this point in the history
…ctory

Some specialized filesystems may need custom behavior here, by synthesizing the directory through another mechanism, or by accepting its prior existence in specific circumstances.
  • Loading branch information
torquestomp committed Mar 12, 2024
1 parent 99e8b51 commit f96ff03
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
5 changes: 4 additions & 1 deletion cli/examples/custom-working-copy/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ use jj_lib::working_copy::{
CheckoutError, CheckoutStats, LockedWorkingCopy, ResetError, SnapshotError, SnapshotOptions,
WorkingCopy, WorkingCopyFactory, WorkingCopyStateError,
};
use jj_lib::workspace::{default_working_copy_factories, Workspace, WorkspaceInitError};
use jj_lib::workspace::{
create_jj_dir, default_working_copy_factories, Workspace, WorkspaceInitError,
};

#[derive(clap::Parser, Clone, Debug)]
enum CustomCommand {
Expand All @@ -58,6 +60,7 @@ fn run_custom_command(
Workspace::init_with_factories(
command_helper.settings(),
wc_path,
create_jj_dir,
&backend_initializer,
Signer::from_settings(command_helper.settings())
.map_err(WorkspaceInitError::SignInit)?,
Expand Down
10 changes: 8 additions & 2 deletions lib/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ pub struct Workspace {
working_copy: Box<dyn WorkingCopy>,
}

fn create_jj_dir(workspace_root: &Path) -> Result<PathBuf, WorkspaceInitError> {
/// Default function to create the .jj directory inside an existing repo
/// directory.
///
/// Custom extensions can override this to do alternative initialization.
pub fn create_jj_dir(workspace_root: &Path) -> Result<PathBuf, WorkspaceInitError> {
let jj_dir = workspace_root.join(".jj");
match std::fs::create_dir(&jj_dir).context(&jj_dir) {
Ok(()) => Ok(jj_dir),
Expand Down Expand Up @@ -230,6 +234,7 @@ impl Workspace {
pub fn init_with_factories(
user_settings: &UserSettings,
workspace_root: &Path,
jj_dir_creator: impl FnOnce(&Path) -> Result<PathBuf, WorkspaceInitError>,
backend_initializer: &BackendInitializer,
signer: Signer,
op_store_initializer: &OpStoreInitializer,
Expand All @@ -239,7 +244,7 @@ impl Workspace {
working_copy_factory: &dyn WorkingCopyFactory,
workspace_id: WorkspaceId,
) -> Result<(Self, Arc<ReadonlyRepo>), WorkspaceInitError> {
let jj_dir = create_jj_dir(workspace_root)?;
let jj_dir = jj_dir_creator(workspace_root)?;
(|| {
let repo_dir = jj_dir.join("repo");
std::fs::create_dir(&repo_dir).context(&repo_dir)?;
Expand Down Expand Up @@ -284,6 +289,7 @@ impl Workspace {
Self::init_with_factories(
user_settings,
workspace_root,
create_jj_dir,
backend_initializer,
signer,
ReadonlyRepo::default_op_store_initializer(),
Expand Down

0 comments on commit f96ff03

Please sign in to comment.