diff --git a/lib/src/default_revset_engine.rs b/lib/src/default_revset_engine.rs index c57281cacfa..15544be0093 100644 --- a/lib/src/default_revset_engine.rs +++ b/lib/src/default_revset_engine.rs @@ -34,8 +34,9 @@ use crate::matchers::{EverythingMatcher, Matcher, PrefixMatcher, Visit}; use crate::repo_path::RepoPath; use crate::revset::{ ChangeIdIndex, ResolvedExpression, ResolvedPredicateExpression, Revset, RevsetEvaluationError, - RevsetFilterPredicate, RevsetGraphEdge, GENERATION_RANGE_FULL, + RevsetFilterPredicate, GENERATION_RANGE_FULL, }; +use crate::revset_graph::RevsetGraphEdge; use crate::rewrite; use crate::store::Store; diff --git a/lib/src/default_revset_graph_iterator.rs b/lib/src/default_revset_graph_iterator.rs index fe483a53d1f..4cdcea3661c 100644 --- a/lib/src/default_revset_graph_iterator.rs +++ b/lib/src/default_revset_graph_iterator.rs @@ -19,7 +19,7 @@ use std::collections::{BTreeMap, HashSet}; use crate::backend::CommitId; use crate::default_index_store::{IndexEntry, IndexPosition}; -use crate::revset::{RevsetGraphEdge, RevsetGraphEdgeType}; +use crate::revset_graph::{RevsetGraphEdge, RevsetGraphEdgeType}; /// Given an iterator over some set of revisions, yields the same revisions with /// associated edge types. diff --git a/lib/src/lib.rs b/lib/src/lib.rs index a69a03f067a..059d749cc55 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -52,6 +52,7 @@ pub mod refs; pub mod repo; pub mod repo_path; pub mod revset; +pub mod revset_graph; pub mod rewrite; pub mod settings; pub mod simple_op_heads_store; diff --git a/lib/src/revset.rs b/lib/src/revset.rs index 9d8881a5514..33043f67db9 100644 --- a/lib/src/revset.rs +++ b/lib/src/revset.rs @@ -39,6 +39,7 @@ use crate::index::{HexPrefix, PrefixResolution}; use crate::op_store::WorkspaceId; use crate::repo::Repo; use crate::repo_path::{FsPathParseError, RepoPath}; +use crate::revset_graph::RevsetGraphEdge; use crate::store::Store; /// Error occurred during symbol resolution. @@ -2158,40 +2159,6 @@ pub trait ChangeIdIndex: Send + Sync { fn shortest_unique_prefix_len(&self, change_id: &ChangeId) -> usize; } -#[derive(Debug, PartialEq, Eq, Clone, Hash)] -pub struct RevsetGraphEdge { - pub target: CommitId, - pub edge_type: RevsetGraphEdgeType, -} - -impl RevsetGraphEdge { - pub fn missing(target: CommitId) -> Self { - Self { - target, - edge_type: RevsetGraphEdgeType::Missing, - } - } - pub fn direct(target: CommitId) -> Self { - Self { - target, - edge_type: RevsetGraphEdgeType::Direct, - } - } - pub fn indirect(target: CommitId) -> Self { - Self { - target, - edge_type: RevsetGraphEdgeType::Indirect, - } - } -} - -#[derive(Debug, PartialEq, Eq, Clone, Hash)] -pub enum RevsetGraphEdgeType { - Missing, - Direct, - Indirect, -} - pub trait RevsetIteratorExt<'index, I> { fn commits(self, store: &Arc) -> RevsetCommitIterator; fn reversed(self) -> ReverseRevsetIterator; @@ -2247,46 +2214,6 @@ pub struct RevsetWorkspaceContext<'a> { pub workspace_root: &'a Path, } -pub struct ReverseRevsetGraphIterator { - items: Vec<(CommitId, Vec)>, -} - -impl ReverseRevsetGraphIterator { - pub fn new<'revset>( - input: Box)> + 'revset>, - ) -> Self { - let mut entries = vec![]; - let mut reverse_edges: HashMap> = HashMap::new(); - for (commit_id, edges) in input { - for RevsetGraphEdge { target, edge_type } in edges { - reverse_edges - .entry(target) - .or_default() - .push(RevsetGraphEdge { - target: commit_id.clone(), - edge_type, - }) - } - entries.push(commit_id); - } - - let mut items = vec![]; - for commit_id in entries.into_iter() { - let edges = reverse_edges.get(&commit_id).cloned().unwrap_or_default(); - items.push((commit_id, edges)); - } - Self { items } - } -} - -impl Iterator for ReverseRevsetGraphIterator { - type Item = (CommitId, Vec); - - fn next(&mut self) -> Option { - self.items.pop() - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/lib/src/revset_graph.rs b/lib/src/revset_graph.rs new file mode 100644 index 00000000000..7e4134db130 --- /dev/null +++ b/lib/src/revset_graph.rs @@ -0,0 +1,95 @@ +// Copyright 2021-2023 The Jujutsu Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#![allow(missing_docs)] + +use std::collections::HashMap; + +use crate::backend::CommitId; + +#[derive(Debug, PartialEq, Eq, Clone, Hash)] +pub struct RevsetGraphEdge { + pub target: CommitId, + pub edge_type: RevsetGraphEdgeType, +} + +impl RevsetGraphEdge { + pub fn missing(target: CommitId) -> Self { + Self { + target, + edge_type: RevsetGraphEdgeType::Missing, + } + } + + pub fn direct(target: CommitId) -> Self { + Self { + target, + edge_type: RevsetGraphEdgeType::Direct, + } + } + + pub fn indirect(target: CommitId) -> Self { + Self { + target, + edge_type: RevsetGraphEdgeType::Indirect, + } + } +} + +#[derive(Debug, PartialEq, Eq, Clone, Hash)] +pub enum RevsetGraphEdgeType { + Missing, + Direct, + Indirect, +} + +pub struct ReverseRevsetGraphIterator { + items: Vec<(CommitId, Vec)>, +} + +impl ReverseRevsetGraphIterator { + pub fn new<'revset>( + input: Box)> + 'revset>, + ) -> Self { + let mut entries = vec![]; + let mut reverse_edges: HashMap> = HashMap::new(); + for (commit_id, edges) in input { + for RevsetGraphEdge { target, edge_type } in edges { + reverse_edges + .entry(target) + .or_default() + .push(RevsetGraphEdge { + target: commit_id.clone(), + edge_type, + }) + } + entries.push(commit_id); + } + + let mut items = vec![]; + for commit_id in entries.into_iter() { + let edges = reverse_edges.get(&commit_id).cloned().unwrap_or_default(); + items.push((commit_id, edges)); + } + Self { items } + } +} + +impl Iterator for ReverseRevsetGraphIterator { + type Item = (CommitId, Vec); + + fn next(&mut self) -> Option { + self.items.pop() + } +} diff --git a/lib/tests/test_default_revset_graph_iterator.rs b/lib/tests/test_default_revset_graph_iterator.rs index 0d0f31fba37..02ceb431af7 100644 --- a/lib/tests/test_default_revset_graph_iterator.rs +++ b/lib/tests/test_default_revset_graph_iterator.rs @@ -17,7 +17,8 @@ use jj_lib::commit::Commit; use jj_lib::default_index_store::ReadonlyIndexWrapper; use jj_lib::default_revset_engine::{evaluate, RevsetImpl}; use jj_lib::repo::{ReadonlyRepo, Repo as _}; -use jj_lib::revset::{ResolvedExpression, RevsetGraphEdge}; +use jj_lib::revset::ResolvedExpression; +use jj_lib::revset_graph::RevsetGraphEdge; use test_case::test_case; use testutils::{CommitGraphBuilder, TestRepo}; diff --git a/lib/tests/test_revset.rs b/lib/tests/test_revset.rs index 053782d4279..cc5935f33ad 100644 --- a/lib/tests/test_revset.rs +++ b/lib/tests/test_revset.rs @@ -30,10 +30,10 @@ use jj_lib::op_store::{BranchTarget, RefTarget, WorkspaceId}; use jj_lib::repo::Repo; use jj_lib::repo_path::RepoPath; use jj_lib::revset::{ - optimize, parse, DefaultSymbolResolver, ReverseRevsetGraphIterator, Revset, RevsetAliasesMap, - RevsetExpression, RevsetFilterPredicate, RevsetGraphEdge, RevsetResolutionError, - RevsetWorkspaceContext, SymbolResolver as _, + optimize, parse, DefaultSymbolResolver, Revset, RevsetAliasesMap, RevsetExpression, + RevsetFilterPredicate, RevsetResolutionError, RevsetWorkspaceContext, SymbolResolver as _, }; +use jj_lib::revset_graph::{ReverseRevsetGraphIterator, RevsetGraphEdge}; use jj_lib::settings::GitSettings; use jj_lib::tree::merge_trees; use jj_lib::workspace::Workspace; diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 0365470c596..00908983cc7 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -41,9 +41,9 @@ use jj_lib::op_store::WorkspaceId; use jj_lib::repo::{ReadonlyRepo, Repo}; use jj_lib::repo_path::RepoPath; use jj_lib::revset::{ - ReverseRevsetGraphIterator, RevsetAliasesMap, RevsetExpression, RevsetFilterPredicate, - RevsetGraphEdge, RevsetGraphEdgeType, RevsetIteratorExt, + RevsetAliasesMap, RevsetExpression, RevsetFilterPredicate, RevsetIteratorExt, }; +use jj_lib::revset_graph::{ReverseRevsetGraphIterator, RevsetGraphEdge, RevsetGraphEdgeType}; use jj_lib::rewrite::{back_out_commit, merge_commit_trees, rebase_commit, DescendantRebaser}; use jj_lib::settings::UserSettings; use jj_lib::tree::{merge_trees, Tree};