|
| 1 | +use but_graph::SegmentMetadata; |
| 2 | +use but_graph::{EntryPoint, Graph, SegmentIndex}; |
| 3 | + |
| 4 | +use termtree::Tree; |
| 5 | + |
| 6 | +use but_graph::projection::StackCommitDebugFlags; |
| 7 | +use std::collections::{BTreeMap, BTreeSet}; |
| 8 | + |
| 9 | +type StringTree = Tree<String>; |
| 10 | + |
| 11 | +/// Visualize `graph` as a tree. |
| 12 | +pub fn graph_workspace(workspace: &but_graph::projection::Workspace<'_>) -> StringTree { |
| 13 | + let commit_flags = workspace |
| 14 | + .graph |
| 15 | + .hard_limit_hit() |
| 16 | + .then_some(StackCommitDebugFlags::HardLimitReached) |
| 17 | + .unwrap_or_default(); |
| 18 | + let mut root = Tree::new(workspace.debug_string()); |
| 19 | + for stack in &workspace.stacks { |
| 20 | + root.push(tree_for_stack(stack, commit_flags)); |
| 21 | + } |
| 22 | + root |
| 23 | +} |
| 24 | + |
| 25 | +fn tree_for_stack( |
| 26 | + stack: &but_graph::projection::Stack, |
| 27 | + commit_flags: StackCommitDebugFlags, |
| 28 | +) -> StringTree { |
| 29 | + let mut root = Tree::new(stack.debug_string()); |
| 30 | + for segment in &stack.segments { |
| 31 | + root.push(tree_for_stack_segment(segment, commit_flags)); |
| 32 | + } |
| 33 | + root |
| 34 | +} |
| 35 | + |
| 36 | +fn tree_for_stack_segment( |
| 37 | + segment: &but_graph::projection::StackSegment, |
| 38 | + commit_flags: StackCommitDebugFlags, |
| 39 | +) -> StringTree { |
| 40 | + let mut root = Tree::new(segment.debug_string()); |
| 41 | + if let Some(outside) = &segment.commits_outside { |
| 42 | + for commit in outside { |
| 43 | + root.push(format!("{}*", commit.debug_string(commit_flags))); |
| 44 | + } |
| 45 | + } |
| 46 | + for commit in &segment.commits_on_remote { |
| 47 | + root.push(commit.debug_string(commit_flags | StackCommitDebugFlags::RemoteOnly)); |
| 48 | + } |
| 49 | + for commit in &segment.commits { |
| 50 | + root.push(commit.debug_string(commit_flags)); |
| 51 | + } |
| 52 | + root |
| 53 | +} |
| 54 | + |
| 55 | +/// Visualize `graph` as a tree. |
| 56 | +pub fn graph_tree(graph: &Graph) -> StringTree { |
| 57 | + let mut root = Tree::new("".to_string()); |
| 58 | + let mut seen = Default::default(); |
| 59 | + for sidx in graph.tip_segments() { |
| 60 | + root.push(recurse_segment(graph, sidx, &mut seen)); |
| 61 | + } |
| 62 | + let missing = graph.num_segments() - seen.len(); |
| 63 | + if missing > 0 { |
| 64 | + let mut missing = Tree::new(format!( |
| 65 | + "ERROR: disconnected {missing} nodes unreachable through base" |
| 66 | + )); |
| 67 | + let mut newly_seen = Default::default(); |
| 68 | + for sidx in graph.segments().filter(|sidx| !seen.contains(sidx)) { |
| 69 | + missing.push(recurse_segment(graph, sidx, &mut newly_seen)); |
| 70 | + } |
| 71 | + root.push(missing); |
| 72 | + seen.extend(newly_seen); |
| 73 | + } |
| 74 | + |
| 75 | + if seen.is_empty() { |
| 76 | + "<UNBORN>".to_string().into() |
| 77 | + } else { |
| 78 | + root |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +fn no_first_commit_on_named_segments(mut ep: EntryPoint<'_>) -> EntryPoint<'_> { |
| 83 | + if ep.segment.ref_name.is_some() && ep.commit_index == Some(0) { |
| 84 | + ep.commit_index = None; |
| 85 | + } |
| 86 | + ep |
| 87 | +} |
| 88 | + |
| 89 | +fn tree_for_commit( |
| 90 | + commit: &but_graph::Commit, |
| 91 | + is_entrypoint: bool, |
| 92 | + is_early_end: bool, |
| 93 | + hard_limit_hit: bool, |
| 94 | +) -> StringTree { |
| 95 | + Graph::commit_debug_string(commit, is_entrypoint, is_early_end, hard_limit_hit).into() |
| 96 | +} |
| 97 | +fn recurse_segment( |
| 98 | + graph: &but_graph::Graph, |
| 99 | + sidx: SegmentIndex, |
| 100 | + seen: &mut BTreeSet<SegmentIndex>, |
| 101 | +) -> StringTree { |
| 102 | + let segment = &graph[sidx]; |
| 103 | + if seen.contains(&sidx) { |
| 104 | + return format!( |
| 105 | + "→:{sidx}:{name}", |
| 106 | + sidx = sidx.index(), |
| 107 | + name = graph[sidx] |
| 108 | + .ref_name |
| 109 | + .as_ref() |
| 110 | + .map(|n| format!( |
| 111 | + " ({}{maybe_sibling})", |
| 112 | + Graph::ref_debug_string(n), |
| 113 | + maybe_sibling = segment |
| 114 | + .sibling_segment_id |
| 115 | + .map_or_else(String::new, |sid| format!(" →:{}:", sid.index())) |
| 116 | + )) |
| 117 | + .unwrap_or_default() |
| 118 | + ) |
| 119 | + .into(); |
| 120 | + } |
| 121 | + seen.insert(sidx); |
| 122 | + let ep = no_first_commit_on_named_segments(graph.lookup_entrypoint().unwrap()); |
| 123 | + let segment_is_entrypoint = ep.segment_index == sidx; |
| 124 | + let mut show_segment_entrypoint = segment_is_entrypoint; |
| 125 | + if segment_is_entrypoint { |
| 126 | + // Reduce noise by preferring ref-based entry-points. |
| 127 | + if segment.ref_name.is_none() && ep.commit_index.is_some() { |
| 128 | + show_segment_entrypoint = false; |
| 129 | + } |
| 130 | + } |
| 131 | + let connected_segments = { |
| 132 | + let mut m = BTreeMap::<_, Vec<_>>::new(); |
| 133 | + let below = graph.segments_below_in_order(sidx).collect::<Vec<_>>(); |
| 134 | + for (cidx, sidx) in below { |
| 135 | + m.entry(cidx).or_default().push(sidx); |
| 136 | + } |
| 137 | + m |
| 138 | + }; |
| 139 | + |
| 140 | + let mut root = Tree::new(format!( |
| 141 | + "{entrypoint}{meta}{arrow}:{id}[{generation}]:{ref_name_and_remote}", |
| 142 | + meta = match segment.metadata { |
| 143 | + None => { |
| 144 | + "" |
| 145 | + } |
| 146 | + Some(SegmentMetadata::Workspace(_)) => { |
| 147 | + "📕" |
| 148 | + } |
| 149 | + Some(SegmentMetadata::Branch(_)) => { |
| 150 | + "📙" |
| 151 | + } |
| 152 | + }, |
| 153 | + id = segment.id.index(), |
| 154 | + generation = segment.generation, |
| 155 | + arrow = if segment.workspace_metadata().is_some() { |
| 156 | + "►►►" |
| 157 | + } else { |
| 158 | + "►" |
| 159 | + }, |
| 160 | + entrypoint = if show_segment_entrypoint { |
| 161 | + if ep.commit.is_none() && ep.commit_index.is_some() { |
| 162 | + "🫱" |
| 163 | + } else { |
| 164 | + "👉" |
| 165 | + } |
| 166 | + } else { |
| 167 | + "" |
| 168 | + }, |
| 169 | + ref_name_and_remote = Graph::ref_and_remote_debug_string( |
| 170 | + segment.ref_name.as_ref(), |
| 171 | + segment.remote_tracking_ref_name.as_ref(), |
| 172 | + segment.sibling_segment_id |
| 173 | + ), |
| 174 | + )); |
| 175 | + for (cidx, commit) in segment.commits.iter().enumerate() { |
| 176 | + let mut commit_tree = tree_for_commit( |
| 177 | + commit, |
| 178 | + segment_is_entrypoint && Some(cidx) == ep.commit_index, |
| 179 | + if cidx + 1 != segment.commits.len() { |
| 180 | + false |
| 181 | + } else { |
| 182 | + graph.is_early_end_of_traversal(sidx) |
| 183 | + }, |
| 184 | + graph.hard_limit_hit(), |
| 185 | + ); |
| 186 | + if let Some(segment_indices) = connected_segments.get(&Some(cidx)) { |
| 187 | + for sidx in segment_indices { |
| 188 | + commit_tree.push(recurse_segment(graph, *sidx, seen)); |
| 189 | + } |
| 190 | + } |
| 191 | + root.push(commit_tree); |
| 192 | + } |
| 193 | + // Get the segments that are directly connected. |
| 194 | + if let Some(segment_indices) = connected_segments.get(&None) { |
| 195 | + for sidx in segment_indices { |
| 196 | + root.push(recurse_segment(graph, *sidx, seen)); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + root |
| 201 | +} |
0 commit comments