Skip to content

Commit

Permalink
revset: extract graph-related types to separate module
Browse files Browse the repository at this point in the history
I'm going to add a topo-grouping iterator adapter, and the revset module is
already big enough to split.
  • Loading branch information
yuja committed Jul 19, 2023
1 parent 92ee512 commit 8c2fd0e
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 82 deletions.
3 changes: 2 additions & 1 deletion lib/src/default_revset_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion lib/src/default_revset_graph_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
75 changes: 1 addition & 74 deletions lib/src/revset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<Store>) -> RevsetCommitIterator<I>;
fn reversed(self) -> ReverseRevsetIterator;
Expand Down Expand Up @@ -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::*;
Expand Down
95 changes: 95 additions & 0 deletions lib/src/revset_graph.rs
Original file line number Diff line number Diff line change
@@ -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<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()
}
}
3 changes: 2 additions & 1 deletion lib/tests/test_default_revset_graph_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
6 changes: 3 additions & 3 deletions lib/tests/test_revset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down

0 comments on commit 8c2fd0e

Please sign in to comment.