11//! Create graphs in SVG format (Scalable Vector Graphics).
22
3+ use itertools:: enumerate;
4+
35use gleisbau:: graph:: CommitInfo ;
46use gleisbau:: graph:: GitGraph ;
7+ use gleisbau:: layout:: get_deviate_index;
58use gleisbau:: settings:: Settings ;
69use svg:: node:: element:: path:: Data ;
710use svg:: node:: element:: { Circle , Group , Line , Path , Text , Title } ;
811use svg:: Document ;
912
1013/// Creates a SVG visual representation of a graph.
1114pub fn print_svg ( graph : & GitGraph , settings : & Settings ) -> Result < String , String > {
15+ let tracks = graph. tracks . lock ( ) . unwrap ( ) ;
16+ let layout = & graph. layout ;
1217 let mut document = Document :: new ( ) ;
1318
14- let max_idx = graph . commits . len ( ) ;
19+ let max_idx = tracks . commits . len ( ) ;
1520 let mut widest_summary = 0.0 ;
1621 let mut widest_branch_names = 0.0 ;
1722
1823 if settings. debug {
19- for branch in & graph . all_branches {
24+ for ( branch_inx , branch) in enumerate ( & tracks . all_branches ) {
2025 if let ( Some ( start) , Some ( end) ) = branch. range {
26+ let branch_visual = layout. track_visual ( branch_inx) . unwrap ( ) ;
2127 document = document. add ( bold_line (
2228 start,
23- branch . visual . column . unwrap ( ) ,
29+ branch_visual . column . unwrap ( ) ,
2430 end,
25- branch . visual . column . unwrap ( ) ,
31+ branch_visual . column . unwrap ( ) ,
2632 "cyan" ,
2733 ) ) ;
2834 }
@@ -31,7 +37,7 @@ pub fn print_svg(graph: &GitGraph, settings: &Settings) -> Result<String, String
3137
3238 let max_column = find_max_column ( graph) ;
3339
34- for ( idx, info) in graph . commits . iter ( ) . enumerate ( ) {
40+ for ( idx, info) in tracks . commits . iter ( ) . enumerate ( ) {
3541 document = document. add ( draw_commit ( info, graph, idx) ) ;
3642
3743 let commit = graph. repository . find_commit ( info. oid ) . unwrap ( ) ;
@@ -40,10 +46,12 @@ pub fn print_svg(graph: &GitGraph, settings: &Settings) -> Result<String, String
4046 document = document. add ( draw_summary ( idx, max_column, commit_summary) ) ;
4147
4248 if let Some ( trace) = info. branch_trace {
43- let branch = & graph. all_branches [ trace] ;
49+ let branch_visual = layout
50+ . track_visual ( trace)
51+ . expect ( "Branch should have a layout" ) ;
4452
4553 if let Some ( ( branches, width) ) =
46- draw_branches ( idx, branch . visual . column . unwrap ( ) , info, graph)
54+ draw_branches ( idx, branch_visual . column . unwrap ( ) , info, graph)
4755 {
4856 document = document. add ( branches) ;
4957
@@ -91,57 +99,64 @@ fn set_document_size(
9199}
92100
93101fn find_max_column ( graph : & GitGraph ) -> usize {
94- graph
102+ let tracks = graph. tracks . lock ( ) . unwrap ( ) ;
103+ let layout = & graph. layout ;
104+ tracks
95105 . commits
96106 . iter ( )
97107 . filter_map ( |info| {
98108 info. branch_trace
99- . and_then ( |trace| graph. all_branches [ trace] . visual . column )
109+ . and_then ( |trace| layout. track_visual ( trace) )
110+ . and_then ( |visual| visual. column )
100111 } )
101112 . max ( )
102113 . unwrap_or ( 0 )
103114}
104115
116+ // index is graph.commits[index]
105117fn draw_commit ( info : & CommitInfo , graph : & GitGraph , index : usize ) -> Group {
118+ let tracks = graph. tracks . lock ( ) . unwrap ( ) ;
119+ let layout = & graph. layout ;
106120 let mut group = Group :: new ( ) ;
107121
108122 if let Some ( trace) = info. branch_trace {
109- let branch = & graph. all_branches [ trace] ;
110- let branch_color = & branch . visual . svg_color ;
123+ let branch_visual = graph. layout . track_visual ( trace) . unwrap ( ) ;
124+ let branch_color = & branch_visual . svg_color ;
111125
112126 for p in 0 ..2 {
113127 let parent = info. parents [ p] ;
114128 let Some ( par_oid) = parent else {
115129 continue ;
116130 } ;
117- let Some ( par_idx) = graph . indices . get ( & par_oid) else {
118- // Parent is outside scope of graph .indices
131+ let Some ( par_idx) = tracks . indices . get ( & par_oid) else {
132+ // Parent is outside scope of tracks .indices
119133 // so draw a vertical line to the bottom
120- let idx_bottom = graph . commits . len ( ) ;
134+ let idx_bottom = tracks . commits . len ( ) ;
121135 group = group. add ( line (
122136 index,
123- branch . visual . column . unwrap ( ) ,
137+ branch_visual . column . unwrap ( ) ,
124138 idx_bottom,
125- branch . visual . column . unwrap ( ) ,
139+ branch_visual . column . unwrap ( ) ,
126140 branch_color,
127141 ) ) ;
128142 continue ;
129143 } ;
130- let par_info = & graph. commits [ * par_idx] ;
131- let par_branch = & graph. all_branches [ par_info. branch_trace . unwrap ( ) ] ;
144+ let par_info = & tracks. commits [ * par_idx] ;
145+ let par_branch_idx = par_info. branch_trace . unwrap ( ) ;
146+ let par_branch_visual = layout. track_visual ( par_branch_idx) . unwrap ( ) ;
132147
133148 group = group. add ( path (
134149 index,
135- branch . visual . column . unwrap ( ) ,
150+ branch_visual . column . unwrap ( ) ,
136151 * par_idx,
137- par_branch . visual . column . unwrap ( ) ,
138- if branch . visual . column == par_branch . visual . column {
152+ par_branch_visual . column . unwrap ( ) ,
153+ if branch_visual . column == par_branch_visual . column {
139154 index
140155 } else {
141- super :: get_deviate_index ( graph , index, * par_idx)
156+ get_deviate_index ( & tracks , layout , index, * par_idx)
142157 } ,
143158 if info. is_merge {
144- & par_branch . visual . svg_color
159+ & par_branch_visual . svg_color
145160 } else {
146161 branch_color
147162 } ,
@@ -151,7 +166,7 @@ fn draw_commit(info: &CommitInfo, graph: &GitGraph, index: usize) -> Group {
151166 group = group. add (
152167 commit_dot (
153168 index,
154- branch . visual . column . unwrap ( ) ,
169+ branch_visual . column . unwrap ( ) ,
155170 branch_color,
156171 !info. is_merge ,
157172 )
@@ -180,10 +195,12 @@ fn draw_branches(
180195) -> Option < ( Group , f32 ) > {
181196 let ( x, y) = commit_coord ( index, column) ;
182197
183- let mut branch_names = info
184- . branches
198+ let mut branch_names = graph
199+ . labels
200+ . get_labels ( & info. oid )
201+ . unwrap_or ( & vec ! [ ] )
185202 . iter ( )
186- . map ( |b| graph . all_branches [ * b ] . name . clone ( ) )
203+ . map ( |label| label . name . clone ( ) )
187204 . collect :: < Vec < String > > ( ) ;
188205
189206 if graph. head . oid == info. oid {
0 commit comments