Open the negotiation graph as SVG, after optionally specifying a limit as rendering/layouting can be very slow. It's useful to see how the negotiation algorithm is reasoning about each commit.
Unified · Split
| @@ -11,6 +11,7 @@ pub struct Options { · 10 unmodified lines | ||
| 11 | 11 | pub shallow: gix::remote::fetch::Shallow, |
| 12 | 12 | pub handshake_info: bool, |
| 13 | 13 | pub negotiation_info: bool, |
| 14 | + pub open_negotiation_graph: Option<std::path::PathBuf>, | |
| 14 | 15 | } |
| 15 | 16 | |
| 16 | 17 | pub const PROGRESS_RANGE: std::ops::RangeInclusive<u8> = 1..=3; |
| @@ -18,6 +19,11 @@ pub const PROGRESS_RANGE: std::ops::RangeInclusive<u8> = 1..=3; · 1 unmodified line | ||
| 18 | 19 | pub(crate) mod function { |
| 19 | 20 | use anyhow::bail; |
| 20 | 21 | use gix::{prelude::ObjectIdExt, refspec::match_group::validate::Fix, remote::fetch::Status}; |
| 22 | + use layout::backends::svg::SVGWriter; | |
| 23 | + use layout::core::base::Orientation; | |
| 24 | + use layout::core::geometry::Point; | |
| 25 | + use layout::core::style::StyleAttr; | |
| 26 | + use layout::std_shapes::shapes::{Arrow, Element, ShapeKind}; | |
| 21 | 27 | |
| 22 | 28 | use super::Options; |
| 23 | 29 | use crate::OutputFormat; |
| @@ -33,6 +39,7 @@ pub(crate) mod function { · 9 unmodified lines | ||
| 33 | 39 | remote, |
| 34 | 40 | handshake_info, |
| 35 | 41 | negotiation_info, |
| 42 | + open_negotiation_graph, | |
| 36 | 43 | shallow, |
| 37 | 44 | ref_specs, |
| 38 | 45 | }: Options, |
| @@ -82,6 +89,11 @@ pub(crate) mod function { · 43 unmodified lines | ||
| 82 | 89 | if negotiation_info { |
| 83 | 90 | print_negotiate_info(&mut out, negotiate.as_ref())?; |
| 84 | 91 | } |
| 92 | + if let Some((negotiate, path)) = | |
| 93 | + open_negotiation_graph.and_then(|path| negotiate.as_ref().map(|n| (n, path))) | |
| 94 | + { | |
| 95 | + render_graph(&repo, &negotiate.graph, &path, progress)?; | |
| 96 | + } | |
| 85 | 97 | Ok::<_, anyhow::Error>(()) |
| 86 | 98 | } |
| 87 | 99 | Status::Change { |
| @@ -99,6 +111,9 @@ pub(crate) mod function { · 11 unmodified lines | ||
| 99 | 111 | if negotiation_info { |
| 100 | 112 | print_negotiate_info(&mut out, Some(&negotiate))?; |
| 101 | 113 | } |
| 114 | + if let Some(path) = open_negotiation_graph { | |
| 115 | + render_graph(&repo, &negotiate.graph, &path, progress)?; | |
| 116 | + } | |
| 102 | 117 | Ok(()) |
| 103 | 118 | } |
| 104 | 119 | }?; |
| @@ -108,6 +123,66 @@ pub(crate) mod function { · 3 unmodified lines | ||
| 108 | 123 | Ok(()) |
| 109 | 124 | } |
| 110 | 125 | |
| 126 | + fn render_graph( | |
| 127 | + repo: &gix::Repository, | |
| 128 | + graph: &gix::negotiate::IdMap, | |
| 129 | + path: &std::path::Path, | |
| 130 | + mut progress: impl gix::Progress, | |
| 131 | + ) -> anyhow::Result<()> { | |
| 132 | + progress.init(Some(graph.len()), gix::progress::count("commits")); | |
| 133 | + progress.set_name("building graph"); | |
| 134 | + | |
| 135 | + let mut map = gix::hashtable::HashMap::default(); | |
| 136 | + let mut vg = layout::topo::layout::VisualGraph::new(Orientation::TopToBottom); | |
| 137 | + | |
| 138 | + for (id, commit) in graph.iter().inspect(|_| progress.inc()) { | |
| 139 | + let source = match map.get(id) { | |
| 140 | + Some(handle) => *handle, | |
| 141 | + None => { | |
| 142 | + let handle = vg.add_node(new_node(id.attach(repo), commit.data.flags)); | |
| 143 | + map.insert(*id, handle); | |
| 144 | + handle | |
| 145 | + } | |
| 146 | + }; | |
| 147 | + | |
| 148 | + for parent_id in &commit.parents { | |
| 149 | + let dest = match map.get(parent_id) { | |
| 150 | + Some(handle) => *handle, | |
| 151 | + None => { | |
| 152 | + let flags = match graph.get(parent_id) { | |
| 153 | + Some(c) => c.data.flags, | |
| 154 | + None => continue, | |
| 155 | + }; | |
| 156 | + let dest = vg.add_node(new_node(parent_id.attach(repo), flags)); | |
| 157 | + map.insert(*parent_id, dest); | |
| 158 | + dest | |
| 159 | + } | |
| 160 | + }; | |
| 161 | + let arrow = Arrow::simple(""); | |
| 162 | + vg.add_edge(arrow, source, dest); | |
| 163 | + } | |
| 164 | + } | |
| 165 | + | |
| 166 | + let start = std::time::Instant::now(); | |
| 167 | + progress.set_name("layout graph"); | |
| 168 | + progress.info(format!("writing {path:?}…")); | |
| 169 | + let mut svg = SVGWriter::new(); | |
| 170 | + vg.do_it(false, false, false, &mut svg); | |
| 171 | + std::fs::write(path, svg.finalize().as_bytes())?; | |
| 172 | + open::that(path)?; | |
| 173 | + progress.show_throughput(start); | |
| 174 | + | |
| 175 | + return Ok(()); | |
| 176 | + | |
| 177 | + fn new_node(id: gix::Id<'_>, flags: gix::negotiate::Flags) -> Element { | |
| 178 | + let pt = Point::new(250., 50.); | |
| 179 | + let name = format!("{}\n\n{flags:?}", id.shorten_or_id()); | |
| 180 | + let shape = ShapeKind::new_box(name.as_str()); | |
| 181 | + let style = StyleAttr::simple(); | |
| 182 | + Element::create(shape, style, Orientation::LeftToRight, pt) | |
| 183 | + } | |
| 184 | + } | |
| 185 | + | |
| 111 | 186 | fn print_negotiate_info( |
| 112 | 187 | mut out: impl std::io::Write, |
| 113 | 188 | negotiate: Option<&gix::remote::fetch::outcome::Negotiate>, |
| @@ -17,8 +17,8 @@ pub const PROGRESS_RANGE: std::ops::RangeInclusive<u8> = 0..=2; · 16 unmodified lines | ||
| 17 | 17 | |
| 18 | 18 | pub(crate) mod function { |
| 19 | 19 | use anyhow::{bail, Context}; |
| 20 | + use gix::hashtable::HashMap; | |
| 20 | 21 | use gix::traverse::commit::Sorting; |
| 21 | − use std::collections::HashMap; | |
| 22 | 22 | |
| 23 | 23 | use gix::Progress; |
| 24 | 24 | use layout::backends::svg::SVGWriter; |
| @@ -63,7 +63,7 @@ pub(crate) mod function { · 38 unmodified lines | ||
| 63 | 63 | Format::Svg { path } => ( |
| 64 | 64 | layout::topo::layout::VisualGraph::new(Orientation::TopToBottom), |
| 65 | 65 | path, |
| 66 | − HashMap::new(), | |
| 66 | + HashMap::default(), | |
| 67 | 67 | ) |
| 68 | 68 | .into(), |
| 69 | 69 | Format::Text => None, |
| @@ -79,14 +79,10 @@ pub(crate) mod function { · 9 unmodified lines | ||
| 79 | 79 | let commit = commit?; |
| 80 | 80 | match vg.as_mut() { |
| 81 | 81 | Some((vg, _path, map)) => { |
| 82 | − let pt = Point::new(100., 30.); | |
| 83 | 82 | let source = match map.get(&commit.id) { |
| 84 | 83 | Some(handle) => *handle, |
| 85 | 84 | None => { |
| 86 | − let name = commit.id().shorten_or_id().to_string(); | |
| 87 | − let shape = ShapeKind::new_box(name.as_str()); | |
| 88 | − let style = StyleAttr::simple(); | |
| 89 | − let handle = vg.add_node(Element::create(shape, style, Orientation::LeftToRight, pt)); | |
| 85 | + let handle = vg.add_node(new_node(commit.id())); | |
| 90 | 86 | map.insert(commit.id, handle); |
| 91 | 87 | handle |
| 92 | 88 | } |
| @@ -96,10 +92,7 @@ pub(crate) mod function { · 3 unmodified lines | ||
| 96 | 92 | let dest = match map.get(parent_id.as_ref()) { |
| 97 | 93 | Some(handle) => *handle, |
| 98 | 94 | None => { |
| 99 | − let name = parent_id.shorten_or_id().to_string(); | |
| 100 | − let shape = ShapeKind::new_box(name.as_str()); | |
| 101 | − let style = StyleAttr::simple(); | |
| 102 | − let dest = vg.add_node(Element::create(shape, style, Orientation::LeftToRight, pt)); | |
| 95 | + let dest = vg.add_node(new_node(parent_id)); | |
| 103 | 96 | map.insert(parent_id.detach(), dest); |
| 104 | 97 | dest |
| 105 | 98 | } |
| @@ -127,7 +120,7 @@ pub(crate) mod function { · 21 unmodified lines | ||
| 127 | 120 | progress.show_throughput(start); |
| 128 | 121 | if let Some((mut vg, path, _)) = vg { |
| 129 | 122 | let start = std::time::Instant::now(); |
| 130 | − progress.set_name("computing graph"); | |
| 123 | + progress.set_name("layout graph"); | |
| 131 | 124 | progress.info(format!("writing {path:?}…")); |
| 132 | 125 | let mut svg = SVGWriter::new(); |
| 133 | 126 | vg.do_it(false, false, false, &mut svg); |
| @@ -135,6 +128,14 @@ pub(crate) mod function { · 1 unmodified line | ||
| 135 | 128 | open::that(path)?; |
| 136 | 129 | progress.show_throughput(start); |
| 137 | 130 | } |
| 138 | − Ok(()) | |
| 131 | + return Ok(()); | |
| 132 | + | |
| 133 | + fn new_node(id: gix::Id<'_>) -> Element { | |
| 134 | + let pt = Point::new(100., 30.); | |
| 135 | + let name = id.shorten_or_id().to_string(); | |
| 136 | + let shape = ShapeKind::new_box(name.as_str()); | |
| 137 | + let style = StyleAttr::simple(); | |
| 138 | + Element::create(shape, style, Orientation::LeftToRight, pt) | |
| 139 | + } | |
| 139 | 140 | } |
| 140 | 141 | } |
| @@ -190,6 +190,7 @@ pub fn main() -> Result<()> { · 189 unmodified lines | ||
| 190 | 190 | dry_run, |
| 191 | 191 | handshake_info, |
| 192 | 192 | negotiation_info, |
| 193 | + open_negotiation_graph, | |
| 193 | 194 | remote, |
| 194 | 195 | shallow, |
| 195 | 196 | ref_spec, |
| @@ -200,6 +201,7 @@ pub fn main() -> Result<()> { · 4 unmodified lines | ||
| 200 | 201 | remote, |
| 201 | 202 | handshake_info, |
| 202 | 203 | negotiation_info, |
| 204 | + open_negotiation_graph, | |
| 203 | 205 | shallow: shallow.into(), |
| 204 | 206 | ref_specs: ref_spec, |
| 205 | 207 | }; |
| @@ -159,6 +159,10 @@ pub mod fetch { · 158 unmodified lines | ||
| 159 | 159 | #[clap(long, short = 's')] |
| 160 | 160 | pub negotiation_info: bool, |
| 161 | 161 | |
| 162 | + /// Open the commit graph used for negotiation and write an SVG file to PATH. | |
| 163 | + #[clap(long, value_name = "PATH", short = 'g')] | |
| 164 | + pub open_negotiation_graph: Option<std::path::PathBuf>, | |
| 165 | + | |
| 162 | 166 | #[clap(flatten)] |
| 163 | 167 | pub shallow: ShallowOptions, |
| 164 | 168 | |