From d9e82970592724f2b0d95e5e23715f8dcb7069be Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Fri, 8 Dec 2023 00:22:48 +0900 Subject: [PATCH] index: add 'static version of evaluate_revset() to ReadonlyIndex We'll probably need a better abstraction, but a separate method is good enough to remove unsafe code from ReadonlyRepo. I'm not sure if this is feasible for the other backends, but I guess there would be less lifetimed variables than DefaultReadonlyIndex. --- lib/src/default_index/readonly.rs | 12 +++++++++++- lib/src/index.rs | 8 ++++++++ lib/tests/test_default_revset_graph_iterator.rs | 8 ++++---- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/src/default_index/readonly.rs b/lib/src/default_index/readonly.rs index e08895ca0..94d5426ee 100644 --- a/lib/src/default_index/readonly.rs +++ b/lib/src/default_index/readonly.rs @@ -30,6 +30,7 @@ use super::entry::{IndexEntry, IndexPosition, SmallIndexPositionsVec}; use super::mutable::DefaultMutableIndex; use super::store::IndexLoadError; use crate::backend::{ChangeId, CommitId, ObjectId}; +use crate::default_revset_engine; use crate::index::{HexPrefix, Index, MutableIndex, PrefixResolution, ReadonlyIndex}; use crate::revset::{ResolvedExpression, Revset, RevsetEvaluationError}; use crate::store::Store; @@ -373,7 +374,7 @@ impl IndexSegment for ReadonlyIndexSegment { } /// Commit index backend which stores data on local disk. -#[derive(Debug)] +#[derive(Clone, Debug)] pub struct DefaultReadonlyIndex(Arc); impl DefaultReadonlyIndex { @@ -440,6 +441,15 @@ impl ReadonlyIndex for DefaultReadonlyIndex { self } + fn evaluate_revset_static( + &self, + expression: &ResolvedExpression, + store: &Arc, + ) -> Result>, RevsetEvaluationError> { + let revset_impl = default_revset_engine::evaluate(expression, store, self.clone())?; + Ok(Box::new(revset_impl)) + } + fn start_modification(&self) -> Box { Box::new(DefaultMutableIndex::incremental(self.0.clone())) } diff --git a/lib/src/index.rs b/lib/src/index.rs index cf8514e61..2f7cfff21 100644 --- a/lib/src/index.rs +++ b/lib/src/index.rs @@ -75,6 +75,14 @@ pub trait ReadonlyIndex: Send + Sync { fn as_index(&self) -> &dyn Index; + // TODO: might be better to split Index::evaluate_revset() to + // Readonly/MutableIndex::evaluate_static(). + fn evaluate_revset_static( + &self, + expression: &ResolvedExpression, + store: &Arc, + ) -> Result>, RevsetEvaluationError>; + fn start_modification(&self) -> Box; } diff --git a/lib/tests/test_default_revset_graph_iterator.rs b/lib/tests/test_default_revset_graph_iterator.rs index cfa502fac..aac8c0abd 100644 --- a/lib/tests/test_default_revset_graph_iterator.rs +++ b/lib/tests/test_default_revset_graph_iterator.rs @@ -22,10 +22,10 @@ use jj_lib::revset_graph::RevsetGraphEdge; use test_case::test_case; use testutils::{CommitGraphBuilder, TestRepo}; -fn revset_for_commits<'index>( - repo: &'index ReadonlyRepo, +fn revset_for_commits( + repo: &ReadonlyRepo, commits: &[&Commit], -) -> RevsetImpl<&'index DefaultReadonlyIndex> { +) -> RevsetImpl { let index = repo .readonly_index() .as_any() @@ -33,7 +33,7 @@ fn revset_for_commits<'index>( .unwrap(); let expression = ResolvedExpression::Commits(commits.iter().map(|commit| commit.id().clone()).collect()); - evaluate(&expression, repo.store(), index).unwrap() + evaluate(&expression, repo.store(), index.clone()).unwrap() } fn direct(commit: &Commit) -> RevsetGraphEdge {