From 3a65c1d2ab56e8833218d24b2ce84ef1a384920e Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sun, 18 Apr 2021 17:10:17 -0700 Subject: [PATCH] revsets: add intersection operator --- lib/src/revset.pest | 3 ++- lib/src/revset.rs | 58 ++++++++++++++++++++++++++++++++++++++++ lib/tests/test_revset.rs | 49 +++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 1 deletion(-) diff --git a/lib/src/revset.pest b/lib/src/revset.pest index 4f507edfa..e6395a134 100644 --- a/lib/src/revset.pest +++ b/lib/src/revset.pest @@ -21,8 +21,9 @@ ancestors = { "*:" } prefix_operator = _{ parents | ancestors } union = { "|" } +intersection = { "&" } difference = { "-" } -infix_operator = _{ union| difference } +infix_operator = _{ union| intersection | difference } function_name = @{ (ASCII_ALPHANUMERIC | "_")+ } // The grammar accepts a string literal or an expression for function diff --git a/lib/src/revset.rs b/lib/src/revset.rs index a79116b6d..c9df8131d 100644 --- a/lib/src/revset.rs +++ b/lib/src/revset.rs @@ -108,6 +108,7 @@ pub enum RevsetExpression { base_expression: Box, }, Union(Box, Box), + Intersection(Box, Box), Difference(Box, Box), } @@ -135,6 +136,10 @@ fn parse_infix_expression_rule( Rule::union => { expression1 = RevsetExpression::Union(Box::new(expression1), Box::new(expression2)) } + Rule::intersection => { + expression1 = + RevsetExpression::Intersection(Box::new(expression1), Box::new(expression2)) + } Rule::difference => { expression1 = RevsetExpression::Difference(Box::new(expression1), Box::new(expression2)) @@ -440,6 +445,54 @@ impl<'revset, 'repo> Iterator for UnionRevsetIterator<'revset, 'repo> { } } +struct IntersectionRevset<'revset, 'repo: 'revset> { + set1: Box + 'revset>, + set2: Box + 'revset>, +} + +impl<'repo> Revset<'repo> for IntersectionRevset<'_, 'repo> { + fn iter<'revset>(&'revset self) -> Box> + 'revset> { + Box::new(IntersectionRevsetIterator { + iter1: self.set1.iter().peekable(), + iter2: self.set2.iter().peekable(), + }) + } +} + +struct IntersectionRevsetIterator<'revset, 'repo> { + iter1: Peekable> + 'revset>>, + iter2: Peekable> + 'revset>>, +} + +impl<'revset, 'repo> Iterator for IntersectionRevsetIterator<'revset, 'repo> { + type Item = IndexEntry<'repo>; + + fn next(&mut self) -> Option { + loop { + match (self.iter1.peek(), self.iter2.peek()) { + (None, _) => { + return None; + } + (_, None) => { + return None; + } + (Some(entry1), Some(entry2)) => match entry1.position().cmp(&entry2.position()) { + Ordering::Less => { + self.iter2.next(); + } + Ordering::Equal => { + self.iter1.next(); + return self.iter2.next(); + } + Ordering::Greater => { + self.iter1.next(); + } + }, + } + } + } +} + struct DifferenceRevset<'revset, 'repo: 'revset> { // The minuend (what to subtract from) set1: Box + 'revset>, @@ -559,6 +612,11 @@ pub fn evaluate_expression<'revset, 'repo: 'revset>( let set2 = evaluate_expression(repo, expression2.as_ref())?; Ok(Box::new(UnionRevset { set1, set2 })) } + RevsetExpression::Intersection(expression1, expression2) => { + let set1 = evaluate_expression(repo, expression1.as_ref())?; + let set2 = evaluate_expression(repo, expression2.as_ref())?; + Ok(Box::new(IntersectionRevset { set1, set2 })) + } RevsetExpression::Difference(expression1, expression2) => { let set1 = evaluate_expression(repo, expression1.as_ref())?; let set2 = evaluate_expression(repo, expression2.as_ref())?; diff --git a/lib/tests/test_revset.rs b/lib/tests/test_revset.rs index 6c7b3cc4c..3e7869997 100644 --- a/lib/tests/test_revset.rs +++ b/lib/tests/test_revset.rs @@ -695,6 +695,55 @@ fn test_evaluate_expression_union(use_git: bool) { tx.discard(); } +#[test_case(false ; "local store")] +#[test_case(true ; "git store")] +fn test_evaluate_expression_intersection(use_git: bool) { + let settings = testutils::user_settings(); + let (_temp_dir, repo) = testutils::init_repo(&settings, use_git); + + let mut tx = repo.start_transaction("test"); + let mut_repo = tx.mut_repo(); + + let root_commit = repo.store().root_commit(); + let commit1 = testutils::create_random_commit(&settings, &repo).write_to_repo(mut_repo); + let commit2 = testutils::create_random_commit(&settings, &repo) + .set_parents(vec![commit1.id().clone()]) + .write_to_repo(mut_repo); + let commit3 = testutils::create_random_commit(&settings, &repo) + .set_parents(vec![commit2.id().clone()]) + .write_to_repo(mut_repo); + let commit4 = testutils::create_random_commit(&settings, &repo) + .set_parents(vec![commit3.id().clone()]) + .write_to_repo(mut_repo); + let commit5 = testutils::create_random_commit(&settings, &repo) + .set_parents(vec![commit2.id().clone()]) + .write_to_repo(mut_repo); + + // Intersection between ancestors + assert_eq!( + resolve_commit_ids( + mut_repo.as_repo_ref(), + &format!("*:{} & *:{}", commit4.id().hex(), commit5.id().hex()) + ), + vec![ + commit2.id().clone(), + commit1.id().clone(), + root_commit.id().clone() + ] + ); + + // Intersection of disjoint sets + assert_eq!( + resolve_commit_ids( + mut_repo.as_repo_ref(), + &format!("{} & {}", commit4.id().hex(), commit2.id().hex()) + ), + vec![] + ); + + tx.discard(); +} + #[test_case(false ; "local store")] #[test_case(true ; "git store")] fn test_evaluate_expression_difference(use_git: bool) {