From e9ddfdd8bc7beb227385eb7a858224044e18ced0 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sat, 13 Mar 2021 09:12:05 -0800 Subject: [PATCH] Repo: repurpose ReadonlyRepo::loader() to return loader for existing repo It's sometimes useful to create a `RepoLoader` given an existing `ReadonlyRepo`. We already do that in `ReadonlyRepo::reload()`. This patch repurposes `ReadonlyRepo::reload()` for that. --- lib/src/repo.rs | 29 ++++++++++++++--------------- lib/tests/test_load_repo.rs | 6 +++--- src/commands.rs | 4 ++-- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/lib/src/repo.rs b/lib/src/repo.rs index 86503eac9..7e6057350 100644 --- a/lib/src/repo.rs +++ b/lib/src/repo.rs @@ -256,14 +256,21 @@ impl ReadonlyRepo { user_settings: &UserSettings, wc_path: PathBuf, ) -> Result, RepoLoadError> { - ReadonlyRepo::loader(user_settings, wc_path)?.load_at_head() + RepoLoader::init(user_settings, wc_path)?.load_at_head() } pub fn loader( - user_settings: &UserSettings, - wc_path: PathBuf, - ) -> Result { - RepoLoader::init(user_settings, wc_path) + &self + ) -> RepoLoader { + RepoLoader { + wc_path: self.wc_path.clone(), + repo_path: self.repo_path.clone(), + repo_settings: self.settings.clone(), + store: self.store.clone(), + op_store: self.op_store.clone(), + op_heads_store: self.op_heads_store.clone(), + index_store: self.index_store.clone(), + } } pub fn as_repo_ref(&self) -> RepoRef { @@ -356,15 +363,7 @@ impl ReadonlyRepo { } pub fn reload(&mut self) { - let repo_loader = RepoLoader { - wc_path: self.working_copy_path().clone(), - repo_path: self.repo_path.clone(), - repo_settings: self.settings.clone(), - store: self.store.clone(), - op_store: self.op_store.clone(), - op_heads_store: self.op_heads_store.clone(), - index_store: self.index_store.clone(), - }; + let repo_loader = self.loader(); let operation = self .op_heads_store .get_single_op_head(&repo_loader) @@ -404,7 +403,7 @@ pub struct RepoLoader { } impl RepoLoader { - fn init(user_settings: &UserSettings, wc_path: PathBuf) -> Result { + pub fn init(user_settings: &UserSettings, wc_path: PathBuf) -> Result { let repo_path = wc_path.join(".jj"); // TODO: Check if ancestor directory has a .jj/ if !repo_path.is_dir() { diff --git a/lib/tests/test_load_repo.rs b/lib/tests/test_load_repo.rs index 52b2cac96..339f016af 100644 --- a/lib/tests/test_load_repo.rs +++ b/lib/tests/test_load_repo.rs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use jujube_lib::repo::{ReadonlyRepo, RepoLoadError}; +use jujube_lib::repo::{ReadonlyRepo, RepoLoadError, RepoLoader}; use jujube_lib::testutils; use std::sync::Arc; use test_case::test_case; @@ -44,13 +44,13 @@ fn test_load_at_operation(use_git: bool) { // If we load the repo at head, we should not see the commit since it was // removed - let loader = ReadonlyRepo::loader(&settings, repo.working_copy_path().clone()).unwrap(); + let loader = RepoLoader::init(&settings, repo.working_copy_path().clone()).unwrap(); let head_repo = loader.load_at_head().unwrap(); assert!(!head_repo.view().heads().contains(commit.id())); // If we load the repo at the previous operation, we should see the commit since // it has not been removed yet - let loader = ReadonlyRepo::loader(&settings, repo.working_copy_path().clone()).unwrap(); + let loader = RepoLoader::init(&settings, repo.working_copy_path().clone()).unwrap(); let old_repo = loader.load_at(&op).unwrap(); assert!(old_repo.view().heads().contains(commit.id())); } diff --git a/src/commands.rs b/src/commands.rs index 55fcddb43..c2edcadb1 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -41,7 +41,7 @@ use jujube_lib::files; use jujube_lib::files::DiffLine; use jujube_lib::git; use jujube_lib::op_store::{OpStore, OpStoreError, OperationId}; -use jujube_lib::repo::{ReadonlyRepo, RepoLoadError}; +use jujube_lib::repo::{ReadonlyRepo, RepoLoadError, RepoLoader}; use jujube_lib::repo_path::RepoPath; use jujube_lib::rewrite::{back_out_commit, merge_commit_trees, rebase_commit}; use jujube_lib::settings::UserSettings; @@ -94,7 +94,7 @@ impl From for CommandError { fn get_repo(ui: &Ui, matches: &ArgMatches) -> Result, CommandError> { let wc_path_str = matches.value_of("repository").unwrap(); let wc_path = ui.cwd().join(wc_path_str); - let loader = ReadonlyRepo::loader(ui.settings(), wc_path)?; + let loader = RepoLoader::init(ui.settings(), wc_path)?; if let Some(op_str) = matches.value_of("at_op") { let op = resolve_single_op_from_store(loader.op_store(), op_str)?; Ok(loader.load_at(&op)?)