History

feat: `gix fetch --open-negotiation-graph[=limit]`

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.

Sebastian Thiel committed 3 years ago

Commit 452ed6b1 parent 096838f4 files · +95 −13Browse files

Unified · Split

4 files
  1. gitoxide-core/src/repository/fetch.rs +75 −0
  2. gitoxide-core/src/repository/revision/list.rs +14 −13
  3. src/plumbing/main.rs +2 −0
  4. src/plumbing/options/mod.rs +4 −0
gitoxide-core/src/repository/fetch.rs+75−0
@@ -11,6 +11,7 @@ pub struct Options { · 10 unmodified lines
11 pub shallow: gix::remote::fetch::Shallow,11 pub shallow: gix::remote::fetch::Shallow,
12 pub handshake_info: bool,12 pub handshake_info: bool,
13 pub negotiation_info: bool,13 pub negotiation_info: bool,
14 pub open_negotiation_graph: Option<std::path::PathBuf>,
14}15}
15 16
16pub const PROGRESS_RANGE: std::ops::RangeInclusive<u8> = 1..=3;17pub 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
18pub(crate) mod function {19pub(crate) mod function {
19 use anyhow::bail;20 use anyhow::bail;
20 use gix::{prelude::ObjectIdExt, refspec::match_group::validate::Fix, remote::fetch::Status};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 use super::Options;28 use super::Options;
23 use crate::OutputFormat;29 use crate::OutputFormat;
@@ -33,6 +39,7 @@ pub(crate) mod function { · 9 unmodified lines
33 remote,39 remote,
34 handshake_info,40 handshake_info,
35 negotiation_info,41 negotiation_info,
42 open_negotiation_graph,
36 shallow,43 shallow,
37 ref_specs,44 ref_specs,
38 }: Options,45 }: Options,
@@ -82,6 +89,11 @@ pub(crate) mod function { · 43 unmodified lines
82 if negotiation_info {89 if negotiation_info {
83 print_negotiate_info(&mut out, negotiate.as_ref())?;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 Ok::<_, anyhow::Error>(())97 Ok::<_, anyhow::Error>(())
86 }98 }
87 Status::Change {99 Status::Change {
@@ -99,6 +111,9 @@ pub(crate) mod function { · 11 unmodified lines
99 if negotiation_info {111 if negotiation_info {
100 print_negotiate_info(&mut out, Some(&negotiate))?;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 Ok(())117 Ok(())
103 }118 }
104 }?;119 }?;
@@ -108,6 +123,66 @@ pub(crate) mod function { · 3 unmodified lines
108 Ok(())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 fn print_negotiate_info(186 fn print_negotiate_info(
112 mut out: impl std::io::Write,187 mut out: impl std::io::Write,
113 negotiate: Option<&gix::remote::fetch::outcome::Negotiate>,188 negotiate: Option<&gix::remote::fetch::outcome::Negotiate>,
gitoxide-core/src/repository/revision/list.rs+14−13
@@ -17,8 +17,8 @@ pub const PROGRESS_RANGE: std::ops::RangeInclusive<u8> = 0..=2; · 16 unmodified lines
17 17
18pub(crate) mod function {18pub(crate) mod function {
19 use anyhow::{bail, Context};19 use anyhow::{bail, Context};
20 use gix::hashtable::HashMap;
20 use gix::traverse::commit::Sorting;21 use gix::traverse::commit::Sorting;
21 use std::collections::HashMap;
22 22
23 use gix::Progress;23 use gix::Progress;
24 use layout::backends::svg::SVGWriter;24 use layout::backends::svg::SVGWriter;
@@ -63,7 +63,7 @@ pub(crate) mod function { · 38 unmodified lines
63 Format::Svg { path } => (63 Format::Svg { path } => (
64 layout::topo::layout::VisualGraph::new(Orientation::TopToBottom),64 layout::topo::layout::VisualGraph::new(Orientation::TopToBottom),
65 path,65 path,
66 HashMap::new(),66 HashMap::default(),
67 )67 )
68 .into(),68 .into(),
69 Format::Text => None,69 Format::Text => None,
@@ -79,14 +79,10 @@ pub(crate) mod function { · 9 unmodified lines
79 let commit = commit?;79 let commit = commit?;
80 match vg.as_mut() {80 match vg.as_mut() {
81 Some((vg, _path, map)) => {81 Some((vg, _path, map)) => {
82 let pt = Point::new(100., 30.);
83 let source = match map.get(&commit.id) {82 let source = match map.get(&commit.id) {
84 Some(handle) => *handle,83 Some(handle) => *handle,
85 None => {84 None => {
86 let name = commit.id().shorten_or_id().to_string();85 let handle = vg.add_node(new_node(commit.id()));
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));
90 map.insert(commit.id, handle);86 map.insert(commit.id, handle);
91 handle87 handle
92 }88 }
@@ -96,10 +92,7 @@ pub(crate) mod function { · 3 unmodified lines
96 let dest = match map.get(parent_id.as_ref()) {92 let dest = match map.get(parent_id.as_ref()) {
97 Some(handle) => *handle,93 Some(handle) => *handle,
98 None => {94 None => {
99 let name = parent_id.shorten_or_id().to_string();95 let dest = vg.add_node(new_node(parent_id));
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));
103 map.insert(parent_id.detach(), dest);96 map.insert(parent_id.detach(), dest);
104 dest97 dest
105 }98 }
@@ -127,7 +120,7 @@ pub(crate) mod function { · 21 unmodified lines
127 progress.show_throughput(start);120 progress.show_throughput(start);
128 if let Some((mut vg, path, _)) = vg {121 if let Some((mut vg, path, _)) = vg {
129 let start = std::time::Instant::now();122 let start = std::time::Instant::now();
130 progress.set_name("computing graph");123 progress.set_name("layout graph");
131 progress.info(format!("writing {path:?}…"));124 progress.info(format!("writing {path:?}…"));
132 let mut svg = SVGWriter::new();125 let mut svg = SVGWriter::new();
133 vg.do_it(false, false, false, &mut svg);126 vg.do_it(false, false, false, &mut svg);
@@ -135,6 +128,14 @@ pub(crate) mod function { · 1 unmodified line
135 open::that(path)?;128 open::that(path)?;
136 progress.show_throughput(start);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}
src/plumbing/main.rs+2−0
@@ -190,6 +190,7 @@ pub fn main() -> Result<()> { · 189 unmodified lines
190 dry_run,190 dry_run,
191 handshake_info,191 handshake_info,
192 negotiation_info,192 negotiation_info,
193 open_negotiation_graph,
193 remote,194 remote,
194 shallow,195 shallow,
195 ref_spec,196 ref_spec,
@@ -200,6 +201,7 @@ pub fn main() -> Result<()> { · 4 unmodified lines
200 remote,201 remote,
201 handshake_info,202 handshake_info,
202 negotiation_info,203 negotiation_info,
204 open_negotiation_graph,
203 shallow: shallow.into(),205 shallow: shallow.into(),
204 ref_specs: ref_spec,206 ref_specs: ref_spec,
205 };207 };
src/plumbing/options/mod.rs+4−0
@@ -159,6 +159,10 @@ pub mod fetch { · 158 unmodified lines
159 #[clap(long, short = 's')]159 #[clap(long, short = 's')]
160 pub negotiation_info: bool,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 #[clap(flatten)]166 #[clap(flatten)]
163 pub shallow: ShallowOptions,167 pub shallow: ShallowOptions,
164 168

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t