diff --git a/lib/src/graph.rs b/lib/src/graph.rs index 09450a3fe1..18362a6259 100644 --- a/lib/src/graph.rs +++ b/lib/src/graph.rs @@ -85,24 +85,7 @@ where pub fn new( input: impl Iterator, RevsetEvaluationError>>, ) -> Result { - let mut entries = vec![]; - let mut reverse_edges: HashMap>> = HashMap::new(); - for item in input { - let (node, edges) = item?; - for GraphEdge { target, edge_type } in edges { - reverse_edges.entry(target).or_default().push(GraphEdge { - target: node.clone(), - edge_type, - }); - } - entries.push(node); - } - - let mut items = vec![]; - for node in entries.into_iter() { - let edges = reverse_edges.get(&node).cloned().unwrap_or_default(); - items.push((node, edges)); - } + let items = reverse_graph(input)?; Ok(Self { items }) } } @@ -115,6 +98,31 @@ impl Iterator for ReverseGraphIterator { } } +/// Creates new graph in which edges are reversed. +fn reverse_graph( + input: impl Iterator, E>>, +) -> Result>, E> { + let mut entries = vec![]; + let mut reverse_edges: HashMap>> = HashMap::new(); + for item in input { + let (node, edges) = item?; + for GraphEdge { target, edge_type } in edges { + reverse_edges.entry(target).or_default().push(GraphEdge { + target: node.clone(), + edge_type, + }); + } + entries.push(node); + } + + let mut items = vec![]; + for node in entries.into_iter() { + let edges = reverse_edges.get(&node).cloned().unwrap_or_default(); + items.push((node, edges)); + } + Ok(items) +} + /// Graph iterator adapter to group topological branches. /// /// Basic idea is DFS from the heads. At fork point, the other descendant