op_heads_store: let caller create initial operation

It makes the APIs much simpler if we don't have to pass in information
about the initial operation when we create the `OpHeadsStore`. It also
makes the alternative `OpHeadsStore` implementations simpler since we
move some logic into a shared location (`ReadonlyRepo::init()`).

This effectively undoes ec07104126. Maybe some further refactoring
made it possible to move it back as I'm doing in this commit?
This commit is contained in:
Martin von Zweigbergk 2023-02-27 21:38:50 -08:00 committed by Martin von Zweigbergk
parent bab13e1982
commit fd6c7f3bb3
3 changed files with 23 additions and 52 deletions

View file

@ -35,9 +35,7 @@ use crate::index::{
use crate::index_store::IndexStore;
use crate::local_backend::LocalBackend;
use crate::op_heads_store::{self, OpHeadResolutionError, OpHeadsStore};
use crate::op_store::{
BranchTarget, OpStore, OperationId, OperationMetadata, RefTarget, WorkspaceId,
};
use crate::op_store::{BranchTarget, OpStore, OperationId, RefTarget, WorkspaceId};
use crate::operation::Operation;
use crate::refs::merge_ref_targets;
use crate::rewrite::DescendantRebaser;
@ -103,16 +101,10 @@ impl ReadonlyRepo {
|store_path| Box::new(SimpleOpStore::init(store_path))
}
pub fn default_op_heads_store_factory() -> impl FnOnce(
&Path,
&Arc<dyn OpStore>,
&op_store::View,
OperationMetadata,
) -> (Box<dyn OpHeadsStore>, Operation) {
|store_path, op_store, view, operation_metadata| {
let (store, op) =
SimpleOpHeadsStore::init(store_path, op_store, view, operation_metadata);
(Box::new(store), op)
pub fn default_op_heads_store_factory() -> impl FnOnce(&Path) -> Box<dyn OpHeadsStore> {
|store_path| {
let store = SimpleOpHeadsStore::init(store_path);
Box::new(store)
}
}
@ -121,12 +113,7 @@ impl ReadonlyRepo {
repo_path: &Path,
backend_factory: impl FnOnce(&Path) -> Box<dyn Backend>,
op_store_factory: impl FnOnce(&Path) -> Box<dyn OpStore>,
op_heads_store_factory: impl FnOnce(
&Path,
&Arc<dyn OpStore>,
&op_store::View,
OperationMetadata,
) -> (Box<dyn OpHeadsStore>, Operation),
op_heads_store_factory: impl FnOnce(&Path) -> Box<dyn OpHeadsStore>,
) -> Result<Arc<ReadonlyRepo>, PathError> {
let repo_path = repo_path.canonicalize().context(repo_path)?;
@ -143,7 +130,7 @@ impl ReadonlyRepo {
let op_store = op_store_factory(&op_store_path);
let op_store_type_path = op_store_path.join("type");
fs::write(&op_store_type_path, op_store.name()).context(&op_store_type_path)?;
let op_store = Arc::from(op_store);
let op_store: Arc<dyn OpStore> = Arc::from(op_store);
let mut root_view = op_store::View::default();
root_view.head_ids.insert(store.root_commit_id().clone());
@ -155,8 +142,16 @@ impl ReadonlyRepo {
fs::create_dir(&op_heads_path).context(&op_heads_path)?;
let operation_metadata =
crate::transaction::create_op_metadata(user_settings, "initialize repo".to_string());
let (op_heads_store, init_op) =
op_heads_store_factory(&op_heads_path, &op_store, &root_view, operation_metadata);
let root_view_id = op_store.write_view(&root_view).unwrap();
let init_operation = op_store::Operation {
view_id: root_view_id,
parents: vec![],
metadata: operation_metadata,
};
let init_operation_id = op_store.write_operation(&init_operation).unwrap();
let init_operation = Operation::new(op_store.clone(), init_operation_id, init_operation);
let op_heads_store = op_heads_store_factory(&op_heads_path);
op_heads_store.add_op_head(init_operation.id());
let op_heads_type_path = op_heads_path.join("type");
fs::write(&op_heads_type_path, op_heads_store.name()).context(&op_heads_type_path)?;
let op_heads_store = Arc::from(op_heads_store);
@ -171,7 +166,7 @@ impl ReadonlyRepo {
store,
op_store,
op_heads_store,
operation: init_op,
operation: init_operation,
settings: repo_settings,
index_store,
index: OnceCell::new(),

View file

@ -15,12 +15,10 @@
use std::fmt::{Debug, Formatter};
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use crate::lock::FileLock;
use crate::op_heads_store::{OpHeadsStore, OpHeadsStoreLock};
use crate::op_store;
use crate::op_store::{OpStore, OperationId, OperationMetadata};
use crate::op_store::OperationId;
use crate::operation::Operation;
pub struct SimpleOpHeadsStore {
@ -36,26 +34,10 @@ impl Debug for SimpleOpHeadsStore {
}
impl SimpleOpHeadsStore {
pub fn init(
dir: &Path,
op_store: &Arc<dyn OpStore>,
root_view: &op_store::View,
operation_metadata: OperationMetadata,
) -> (Self, Operation) {
let root_view_id = op_store.write_view(root_view).unwrap();
let init_operation = op_store::Operation {
view_id: root_view_id,
parents: vec![],
metadata: operation_metadata,
};
let init_operation_id = op_store.write_operation(&init_operation).unwrap();
let init_operation = Operation::new(op_store.clone(), init_operation_id, init_operation);
pub fn init(dir: &Path) -> Self {
let op_heads_dir = dir.join("heads");
fs::create_dir(&op_heads_dir).unwrap();
let op_heads_store = Self { dir: op_heads_dir };
op_heads_store.add_op_head(init_operation.id());
(op_heads_store, init_operation)
Self { dir: op_heads_dir }
}
pub fn load(dir: &Path) -> Self {

View file

@ -23,8 +23,7 @@ use crate::backend::Backend;
use crate::git_backend::GitBackend;
use crate::local_backend::LocalBackend;
use crate::op_heads_store::OpHeadsStore;
use crate::op_store::{self, OpStore, OperationMetadata, WorkspaceId};
use crate::operation::Operation;
use crate::op_store::{OpStore, WorkspaceId};
use crate::repo::{
CheckOutCommitError, IoResultExt, PathError, ReadonlyRepo, Repo, RepoLoader, StoreFactories,
StoreLoadError,
@ -161,12 +160,7 @@ impl Workspace {
workspace_root: &Path,
backend_factory: impl FnOnce(&Path) -> Box<dyn Backend>,
op_store_factory: impl FnOnce(&Path) -> Box<dyn OpStore>,
op_heads_store_factory: impl FnOnce(
&Path,
&Arc<dyn OpStore>,
&op_store::View,
OperationMetadata,
) -> (Box<dyn OpHeadsStore>, Operation),
op_heads_store_factory: impl FnOnce(&Path) -> Box<dyn OpHeadsStore>,
) -> Result<(Self, Arc<ReadonlyRepo>), WorkspaceInitError> {
let jj_dir = create_jj_dir(workspace_root)?;
let repo_dir = jj_dir.join("repo");