revset: add new graph iterator function for tests

I'm about to make `Revset::iter()` yield just `CommitId`s, but the
tests in `test_default_revset_graph_iterator.rs` need an `IndexEntry`
iterator so they can pass it into `RevsetGraphIterator::new()`. This
commits prepares for the change by adding a
`RevsetImpl::iter_graph_impl()` that returns `RevsetGraphIterator`,
keeping `InternalRevset` still hidden within the revset engine. We
could instead have made that (and `ToPredicateFn`) visible to tests. I
can't say which is better.
This commit is contained in:
Martin von Zweigbergk 2023-03-22 21:38:23 -07:00 committed by Martin von Zweigbergk
parent c8f387d5b3
commit b5ea79f32e
3 changed files with 39 additions and 20 deletions

View file

@ -733,7 +733,8 @@ impl Index for MutableIndexImpl {
repo: &'index dyn Repo,
expression: &RevsetExpression,
) -> Result<Box<dyn Revset<'index> + 'index>, RevsetError> {
default_revset_engine::evaluate(repo, CompositeIndex(self), expression)
let revset_impl = default_revset_engine::evaluate(repo, CompositeIndex(self), expression)?;
Ok(Box::new(revset_impl))
}
}
@ -1698,6 +1699,10 @@ impl ReadonlyIndexImpl {
}))
}
pub fn as_composite(&self) -> CompositeIndex {
CompositeIndex(self)
}
fn name(&self) -> &str {
&self.name
}
@ -1806,7 +1811,8 @@ impl Index for ReadonlyIndexImpl {
repo: &'index dyn Repo,
expression: &RevsetExpression,
) -> Result<Box<dyn Revset<'index> + 'index>, RevsetError> {
default_revset_engine::evaluate(repo, CompositeIndex(self), expression)
let revset_impl = default_revset_engine::evaluate(repo, CompositeIndex(self), expression)?;
Ok(Box::new(revset_impl))
}
}

View file

@ -51,7 +51,7 @@ trait InternalRevset<'index>: ToPredicateFn<'index> {
fn iter(&self) -> Box<dyn Iterator<Item = IndexEntry<'index>> + '_>;
}
struct RevsetImpl<'index> {
pub struct RevsetImpl<'index> {
inner: Box<dyn InternalRevset<'index> + 'index>,
index: CompositeIndex<'index>,
}
@ -66,6 +66,10 @@ impl<'index> RevsetImpl<'index> {
index,
}
}
pub fn iter_graph_impl(&self) -> RevsetGraphIterator<'_, 'index> {
RevsetGraphIterator::new(self.inner.iter())
}
}
impl<'index> Revset<'index> for RevsetImpl<'index> {
@ -491,9 +495,9 @@ pub fn evaluate<'index>(
repo: &'index dyn Repo,
index: CompositeIndex<'index>,
expression: &RevsetExpression,
) -> Result<Box<dyn Revset<'index> + 'index>, RevsetError> {
) -> Result<RevsetImpl<'index>, RevsetError> {
let internal_revset = internal_evaluate(repo, expression)?;
Ok(Box::new(RevsetImpl::new(internal_revset, index)))
Ok(RevsetImpl::new(internal_revset, index))
}
fn internal_evaluate<'index>(

View file

@ -14,19 +14,22 @@
use itertools::Itertools;
use jujutsu_lib::commit::Commit;
use jujutsu_lib::default_revset_graph_iterator::RevsetGraphIterator;
use jujutsu_lib::default_index_store::ReadonlyIndexImpl;
use jujutsu_lib::default_revset_engine::{evaluate, RevsetImpl};
use jujutsu_lib::repo::Repo;
use jujutsu_lib::revset::{Revset, RevsetExpression, RevsetGraphEdge};
use jujutsu_lib::revset::{RevsetExpression, RevsetGraphEdge};
use test_case::test_case;
use testutils::{CommitGraphBuilder, TestRepo};
fn revset_for_commits<'index>(
repo: &'index dyn Repo,
commits: &[&Commit],
) -> Box<dyn Revset<'index> + 'index> {
RevsetExpression::commits(commits.iter().map(|commit| commit.id().clone()).collect())
.evaluate(repo)
.unwrap()
fn revset_for_commits<'index>(repo: &'index dyn Repo, commits: &[&Commit]) -> RevsetImpl<'index> {
let index = repo
.index()
.as_any()
.downcast_ref::<ReadonlyIndexImpl>()
.unwrap();
let expression =
RevsetExpression::commits(commits.iter().map(|commit| commit.id().clone()).collect());
evaluate(repo, index.as_composite(), &expression).unwrap()
}
fn direct(commit: &Commit) -> RevsetGraphEdge {
@ -66,7 +69,8 @@ fn test_graph_iterator_linearized(skip_transitive_edges: bool) {
let root_commit = repo.store().root_commit();
let revset = revset_for_commits(repo.as_ref(), &[&commit_a, &commit_d]);
let commits = RevsetGraphIterator::new(revset.iter())
let commits = revset
.iter_graph_impl()
.set_skip_transitive_edges(skip_transitive_edges)
.collect_vec();
assert_eq!(commits.len(), 2);
@ -105,7 +109,8 @@ fn test_graph_iterator_virtual_octopus(skip_transitive_edges: bool) {
let root_commit = repo.store().root_commit();
let revset = revset_for_commits(repo.as_ref(), &[&commit_a, &commit_b, &commit_c, &commit_f]);
let commits = RevsetGraphIterator::new(revset.iter())
let commits = revset
.iter_graph_impl()
.set_skip_transitive_edges(skip_transitive_edges)
.collect_vec();
assert_eq!(commits.len(), 4);
@ -155,7 +160,8 @@ fn test_graph_iterator_simple_fork(skip_transitive_edges: bool) {
let root_commit = repo.store().root_commit();
let revset = revset_for_commits(repo.as_ref(), &[&commit_a, &commit_c, &commit_e]);
let commits = RevsetGraphIterator::new(revset.iter())
let commits = revset
.iter_graph_impl()
.set_skip_transitive_edges(skip_transitive_edges)
.collect_vec();
assert_eq!(commits.len(), 3);
@ -195,7 +201,8 @@ fn test_graph_iterator_multiple_missing(skip_transitive_edges: bool) {
let root_commit = repo.store().root_commit();
let revset = revset_for_commits(repo.as_ref(), &[&commit_b, &commit_f]);
let commits = RevsetGraphIterator::new(revset.iter())
let commits = revset
.iter_graph_impl()
.set_skip_transitive_edges(skip_transitive_edges)
.collect_vec();
assert_eq!(commits.len(), 2);
@ -238,7 +245,8 @@ fn test_graph_iterator_edge_to_ancestor(skip_transitive_edges: bool) {
let repo = tx.commit();
let revset = revset_for_commits(repo.as_ref(), &[&commit_c, &commit_d, &commit_f]);
let commits = RevsetGraphIterator::new(revset.iter())
let commits = revset
.iter_graph_impl()
.set_skip_transitive_edges(skip_transitive_edges)
.collect_vec();
assert_eq!(commits.len(), 3);
@ -296,7 +304,8 @@ fn test_graph_iterator_edge_escapes_from_(skip_transitive_edges: bool) {
repo.as_ref(),
&[&commit_a, &commit_d, &commit_g, &commit_h, &commit_j],
);
let commits = RevsetGraphIterator::new(revset.iter())
let commits = revset
.iter_graph_impl()
.set_skip_transitive_edges(skip_transitive_edges)
.collect_vec();
assert_eq!(commits.len(), 5);