From 97b48635f68382c326d27057f238b9092ac27e89 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Wed, 13 Oct 2021 23:42:25 -0700 Subject: [PATCH] repo: move Backend initialization to Store This is just a cleanup. Now only `Store` knows how to tell which backend to use. --- lib/src/repo.rs | 34 ++++++++++------------------------ lib/src/store.rs | 26 ++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/lib/src/repo.rs b/lib/src/repo.rs index 0c72e2a17..6a935060d 100644 --- a/lib/src/repo.rs +++ b/lib/src/repo.rs @@ -16,20 +16,18 @@ use std::collections::{HashMap, HashSet}; use std::fmt::{Debug, Formatter}; use std::fs; use std::fs::File; -use std::io::{Read, Write}; +use std::io::Read; use std::path::{Path, PathBuf}; use std::sync::{Arc, Mutex, MutexGuard}; use thiserror::Error; -use crate::backend::{Backend, BackendError, CommitId}; +use crate::backend::{BackendError, CommitId}; use crate::commit::Commit; use crate::commit_builder::{new_change_id, signature, CommitBuilder}; use crate::dag_walk::topo_order_reverse; -use crate::git_backend::GitBackend; use crate::index::{IndexRef, MutableIndex, ReadonlyIndex}; use crate::index_store::IndexStore; -use crate::local_backend::LocalBackend; use crate::op_heads_store::OpHeadsStore; use crate::op_store::{BranchTarget, OpStore, OperationId, RefTarget}; use crate::operation::Operation; @@ -141,8 +139,8 @@ impl ReadonlyRepo { wc_path: PathBuf, ) -> Result, RepoInitError> { let repo_path = ReadonlyRepo::init_repo_dir(&wc_path)?; - let backend = Box::new(LocalBackend::init(repo_path.join("store"))); - Ok(ReadonlyRepo::init(settings, repo_path, wc_path, backend)) + let store = Store::init_local(repo_path.join("store")); + Ok(ReadonlyRepo::init(settings, repo_path, wc_path, store)) } /// Initializes a repo with a new Git backend in .jj/git/ (bare Git repo) @@ -151,30 +149,19 @@ impl ReadonlyRepo { wc_path: PathBuf, ) -> Result, RepoInitError> { let repo_path = ReadonlyRepo::init_repo_dir(&wc_path)?; - let store_path = repo_path.join("store"); - let git_repo_path = store_path.join("git"); - git2::Repository::init_bare(&git_repo_path).unwrap(); - let mut git_target_file = File::create(store_path.join("git_target")).unwrap(); - git_target_file.write_all(b"git").unwrap(); - let backend = Box::new(GitBackend::load(&git_repo_path)); - Ok(ReadonlyRepo::init(settings, repo_path, wc_path, backend)) + let store = Store::init_internal_git(repo_path.join("store")); + Ok(ReadonlyRepo::init(settings, repo_path, wc_path, store)) } /// Initializes a repo with an existing Git backend at the specified path pub fn init_external_git( settings: &UserSettings, wc_path: PathBuf, - git_store_path: PathBuf, + git_repo_path: PathBuf, ) -> Result, RepoInitError> { let repo_path = ReadonlyRepo::init_repo_dir(&wc_path)?; - let store_path = repo_path.join("store"); - let git_repo_path = fs::canonicalize(git_store_path).unwrap(); - let mut git_target_file = File::create(store_path.join("git_target")).unwrap(); - git_target_file - .write_all(git_repo_path.to_str().unwrap().as_bytes()) - .unwrap(); - let backend = Box::new(GitBackend::load(&git_repo_path)); - Ok(ReadonlyRepo::init(settings, repo_path, wc_path, backend)) + let store = Store::init_external_git(repo_path.join("store"), git_repo_path); + Ok(ReadonlyRepo::init(settings, repo_path, wc_path, store)) } fn init_repo_dir(wc_path: &Path) -> Result { @@ -197,10 +184,9 @@ impl ReadonlyRepo { user_settings: &UserSettings, repo_path: PathBuf, wc_path: PathBuf, - backend: Box, + store: Arc, ) -> Arc { let repo_settings = user_settings.with_repo(&repo_path).unwrap(); - let store = Store::new(backend); let working_copy = WorkingCopy::init( store.clone(), diff --git a/lib/src/store.rs b/lib/src/store.rs index 6e0a49494..b0ecfe682 100644 --- a/lib/src/store.rs +++ b/lib/src/store.rs @@ -13,8 +13,9 @@ // limitations under the License. use std::collections::HashMap; +use std::fs; use std::fs::File; -use std::io::Read; +use std::io::{Read, Write}; use std::path::PathBuf; use std::sync::{Arc, RwLock}; @@ -41,7 +42,7 @@ pub struct Store { } impl Store { - pub fn new(backend: Box) -> Arc { + fn new(backend: Box) -> Arc { let root_commit_id = CommitId(vec![0; backend.hash_length()]); Arc::new(Store { backend, @@ -51,6 +52,27 @@ impl Store { }) } + pub fn init_local(store_path: PathBuf) -> Arc { + Store::new(Box::new(LocalBackend::init(store_path))) + } + + pub fn init_internal_git(store_path: PathBuf) -> Arc { + let git_repo_path = store_path.join("git"); + git2::Repository::init_bare(&git_repo_path).unwrap(); + let mut git_target_file = File::create(store_path.join("git_target")).unwrap(); + git_target_file.write_all(b"git").unwrap(); + Store::new(Box::new(GitBackend::load(&git_repo_path))) + } + + pub fn init_external_git(store_path: PathBuf, git_repo_path: PathBuf) -> Arc { + let git_repo_path = fs::canonicalize(git_repo_path).unwrap(); + let mut git_target_file = File::create(store_path.join("git_target")).unwrap(); + git_target_file + .write_all(git_repo_path.to_str().unwrap().as_bytes()) + .unwrap(); + Store::new(Box::new(GitBackend::load(&git_repo_path))) + } + pub fn load_store(store_path: PathBuf) -> Arc { let backend: Box; let git_target_path = store_path.join("git_target");