|
| 1 | +use std::collections::BTreeMap; |
| 2 | + |
| 3 | +use anyhow::Result; |
| 4 | +use but_graph::{Commit, Graph, Segment}; |
| 5 | +use petgraph::{Direction, graph::NodeIndex}; |
| 6 | + |
| 7 | +use crate::graph_rebase::{Edge, Editor, Step, StepGraph, StepGraphIndex}; |
| 8 | + |
| 9 | +/// Provides an extension for creating an Editor out of the segment graph |
| 10 | +pub trait GraphExt { |
| 11 | + /// Creates an editor. |
| 12 | + fn create_editor(&self) -> Result<Editor>; |
| 13 | +} |
| 14 | + |
| 15 | +impl GraphExt for Graph { |
| 16 | + /// Creates an editor out of the segment graph. |
| 17 | + fn create_editor(&self) -> Result<Editor> { |
| 18 | + let entrypoint = self.lookup_entrypoint()?; |
| 19 | + |
| 20 | + // Commits in this list are ordred such that iterating in reverse will |
| 21 | + // have any relevant parent commits already inserted in the graph. |
| 22 | + let mut commits: Vec<Commit> = Vec::new(); |
| 23 | + // References are ordered from child-most to parent-most |
| 24 | + let mut references: BTreeMap<gix::ObjectId, Vec<gix::refs::FullName>> = BTreeMap::new(); |
| 25 | + |
| 26 | + self.visit_all_segments_including_start_until( |
| 27 | + entrypoint.segment_index, |
| 28 | + Direction::Outgoing, |
| 29 | + |segment| { |
| 30 | + if let Some(refname) = segment.ref_name() |
| 31 | + && let Some(commit) = find_nearest_commit(self, segment) |
| 32 | + { |
| 33 | + references |
| 34 | + .entry(commit.id) |
| 35 | + .and_modify(|rs| rs.push(refname.to_owned())) |
| 36 | + .or_insert_with(|| vec![refname.to_owned()]); |
| 37 | + } |
| 38 | + |
| 39 | + for commit in &segment.commits { |
| 40 | + if !commit.refs.is_empty() { |
| 41 | + let refs = commit |
| 42 | + .refs |
| 43 | + .iter() |
| 44 | + .map(|r| r.ref_name.clone()) |
| 45 | + .collect::<Vec<_>>(); |
| 46 | + if let Some(entry) = references.get_mut(&commit.id) { |
| 47 | + entry.extend(refs); |
| 48 | + } else { |
| 49 | + references.insert(commit.id, refs); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + commits.extend(segment.commits.clone()); |
| 55 | + |
| 56 | + false |
| 57 | + }, |
| 58 | + ); |
| 59 | + |
| 60 | + // When adding child-nodes, this lookup tells us where to find the |
| 61 | + // relevant "parent" to point to. |
| 62 | + let mut steps_for_commits: BTreeMap<gix::ObjectId, NodeIndex<StepGraphIndex>> = |
| 63 | + BTreeMap::new(); |
| 64 | + let mut graph = StepGraph::new(); |
| 65 | + |
| 66 | + while let Some(c) = commits.pop() { |
| 67 | + let mut ni = graph.add_node(Step::Pick { id: c.id }); |
| 68 | + |
| 69 | + for (order, p) in c.parent_ids.iter().enumerate() { |
| 70 | + if let Some(parent_ni) = steps_for_commits.get(p) { |
| 71 | + graph.add_edge(ni, *parent_ni, Edge { order }); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + if let Some(refs) = references.get_mut(&c.id) { |
| 76 | + // We insert in reverse to preserve the child-most to |
| 77 | + // parent-most ordering that the frontend sees in the step graph |
| 78 | + for r in refs.iter().rev() { |
| 79 | + let ref_ni = graph.add_node(Step::Reference { refname: r.clone() }); |
| 80 | + graph.add_edge(ref_ni, ni, Edge { order: 0 }); |
| 81 | + ni = ref_ni; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + steps_for_commits.insert(c.id, ni); |
| 86 | + } |
| 87 | + |
| 88 | + Ok(Editor { |
| 89 | + graph, |
| 90 | + initial_references: references.values().flatten().cloned().collect(), |
| 91 | + }) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/// Find the commit that is nearest to the top of the segment via a first parent |
| 96 | +/// traversal. |
| 97 | +fn find_nearest_commit<'graph>( |
| 98 | + graph: &'graph Graph, |
| 99 | + segment: &'graph Segment, |
| 100 | +) -> Option<&'graph Commit> { |
| 101 | + let mut target = Some(segment); |
| 102 | + while let Some(s) = target { |
| 103 | + if let Some(c) = s.commits.first() { |
| 104 | + return Some(c); |
| 105 | + } |
| 106 | + |
| 107 | + target = graph |
| 108 | + .segments_below_in_order(s.id) |
| 109 | + .next() |
| 110 | + .map(|s| &graph[s.1]); |
| 111 | + } |
| 112 | + |
| 113 | + None |
| 114 | +} |
0 commit comments