repo: inline single-user init_repo_dir()

The function has only one caller since 25b922cd0b and it's pretty
small. Inlining also means we can reuse the joined paths created in
it, so I did that by extracting variables for them.
This commit is contained in:
Martin von Zweigbergk 2022-10-27 06:39:20 -07:00 committed by Martin von Zweigbergk
parent 3cdcf6cf3d
commit 2b64e52b4d

View file

@ -129,35 +129,38 @@ impl Debug for ReadonlyRepo {
} }
impl ReadonlyRepo { impl ReadonlyRepo {
fn init_repo_dir(repo_path: &Path) {
fs::create_dir(repo_path.join("store")).unwrap();
fs::create_dir(repo_path.join("op_store")).unwrap();
fs::create_dir(repo_path.join("op_heads")).unwrap();
fs::create_dir(repo_path.join("index")).unwrap();
}
pub fn init( pub fn init(
user_settings: &UserSettings, user_settings: &UserSettings,
repo_path: &Path, repo_path: &Path,
backend_factory: impl FnOnce(&Path) -> Box<dyn Backend>, backend_factory: impl FnOnce(&Path) -> Box<dyn Backend>,
) -> Arc<ReadonlyRepo> { ) -> Arc<ReadonlyRepo> {
let repo_path = repo_path.canonicalize().unwrap(); let repo_path = repo_path.canonicalize().unwrap();
ReadonlyRepo::init_repo_dir(&repo_path);
let store_path = repo_path.join("store"); let store_path = repo_path.join("store");
fs::create_dir(&store_path).unwrap();
let backend = backend_factory(&store_path); let backend = backend_factory(&store_path);
fs::write(&store_path.join("backend"), backend.name()).unwrap(); fs::write(&store_path.join("backend"), backend.name()).unwrap();
let store = Store::new(backend); let store = Store::new(backend);
let repo_settings = user_settings.with_repo(&repo_path).unwrap(); let repo_settings = user_settings.with_repo(&repo_path).unwrap();
let op_store: Arc<dyn OpStore> = Arc::new(SimpleOpStore::init(repo_path.join("op_store")));
let op_store_path = repo_path.join("op_store");
fs::create_dir(&op_store_path).unwrap();
let op_store: Arc<dyn OpStore> = Arc::new(SimpleOpStore::init(op_store_path));
let mut root_view = op_store::View::default(); let mut root_view = op_store::View::default();
root_view.head_ids.insert(store.root_commit_id().clone()); root_view.head_ids.insert(store.root_commit_id().clone());
root_view root_view
.public_head_ids .public_head_ids
.insert(store.root_commit_id().clone()); .insert(store.root_commit_id().clone());
let (op_heads_store, init_op) =
OpHeadsStore::init(repo_path.join("op_heads"), &op_store, &root_view); let op_heads_path = repo_path.join("op_heads");
fs::create_dir(&op_heads_path).unwrap();
let (op_heads_store, init_op) = OpHeadsStore::init(op_heads_path, &op_store, &root_view);
let op_heads_store = Arc::new(op_heads_store); let op_heads_store = Arc::new(op_heads_store);
let index_store = Arc::new(IndexStore::init(repo_path.join("index")));
let index_path = repo_path.join("index");
fs::create_dir(&index_path).unwrap();
let index_store = Arc::new(IndexStore::init(index_path));
let view = View::new(root_view); let view = View::new(root_view);
Arc::new(ReadonlyRepo { Arc::new(ReadonlyRepo {
repo_path, repo_path,