revset: extract graph-related types to separate module

I'm going to add a topo-grouping iterator adapter, and the revset module is
already big enough to split.
This commit is contained in:
Yuya Nishihara 2023-07-17 20:43:33 +09:00
parent c255ff1eaf
commit e2f9ed439e
8 changed files with 107 additions and 82 deletions

View file

@ -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;

View file

@ -19,7 +19,7 @@ use std::collections::{BTreeMap, HashSet};
use crate::backend::CommitId;
use crate::default_index_store::{CompositeIndex, IndexEntry, IndexPosition};
use crate::revset::{RevsetGraphEdge, RevsetGraphEdgeType};
use crate::revset_graph::{RevsetGraphEdge, RevsetGraphEdgeType};
/// Like `RevsetGraphEdge`, but stores `IndexPosition` instead.
///

View file

@ -53,6 +53,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;

View file

@ -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, Copy, Hash)]
pub enum RevsetGraphEdgeType {
Missing,
Direct,
Indirect,
}
pub trait RevsetIteratorExt<'index, I> {
fn commits(self, store: &Arc<Store>) -> RevsetCommitIterator<I>;
fn reversed(self) -> ReverseRevsetIterator;
@ -2247,46 +2214,6 @@ pub struct RevsetWorkspaceContext<'a> {
pub workspace_root: &'a Path,
}
pub struct ReverseRevsetGraphIterator {
items: Vec<(CommitId, Vec<RevsetGraphEdge>)>,
}
impl ReverseRevsetGraphIterator {
pub fn new<'revset>(
input: Box<dyn Iterator<Item = (CommitId, Vec<RevsetGraphEdge>)> + 'revset>,
) -> Self {
let mut entries = vec![];
let mut reverse_edges: HashMap<CommitId, Vec<RevsetGraphEdge>> = 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<RevsetGraphEdge>);
fn next(&mut self) -> Option<Self::Item> {
self.items.pop()
}
}
#[cfg(test)]
mod tests {
use super::*;

95
lib/src/revset_graph.rs Normal file
View file

@ -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, Copy, Hash)]
pub enum RevsetGraphEdgeType {
Missing,
Direct,
Indirect,
}
pub struct ReverseRevsetGraphIterator {
items: Vec<(CommitId, Vec<RevsetGraphEdge>)>,
}
impl ReverseRevsetGraphIterator {
pub fn new<'revset>(
input: Box<dyn Iterator<Item = (CommitId, Vec<RevsetGraphEdge>)> + 'revset>,
) -> Self {
let mut entries = vec![];
let mut reverse_edges: HashMap<CommitId, Vec<RevsetGraphEdge>> = 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<RevsetGraphEdge>);
fn next(&mut self) -> Option<Self::Item> {
self.items.pop()
}
}

View file

@ -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};

View file

@ -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;

View file

@ -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};