-
-
Notifications
You must be signed in to change notification settings - Fork 753
Expand file tree
/
Copy pathgraph_lanes.rs
More file actions
337 lines (307 loc) · 7 KB
/
Copy pathgraph_lanes.rs
File metadata and controls
337 lines (307 loc) · 7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//! Assign SourceTree-style lane glyphs to a newest-first commit list.
use super::graph_log::GraphCommit;
use super::CommitId;
/// One drawn cell in the lane column.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GraphCell {
/// Glyph to render.
pub ch: char,
/// Stable color index for this lane (cycle in the UI theme).
pub color: usize,
}
/// Lane glyphs for a single commit row.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct GraphRow {
/// Cells left-to-right.
pub cells: Vec<GraphCell>,
/// Lane index of this commit's node.
pub commit_lane: usize,
}
/// Compute lane rows for `commits` (must be newest-first, as from
/// [`super::graph_log::get_graph_commits`]).
#[must_use]
pub fn assign_lanes(commits: &[GraphCommit]) -> Vec<GraphRow> {
let mut reserved: Vec<Option<CommitId>> = Vec::new();
let mut colors: Vec<usize> = Vec::new();
let mut next_color = 0_usize;
let mut rows = Vec::with_capacity(commits.len());
for commit in commits {
let matching: Vec<usize> = reserved
.iter()
.enumerate()
.filter_map(|(i, id)| {
(*id == Some(commit.id)).then_some(i)
})
.collect();
let commit_lane = matching.first().copied().unwrap_or_else(|| {
open_lane(
&mut reserved,
&mut colors,
&mut next_color,
commit.id,
)
});
// Ensure colors vec matches reserved length.
while colors.len() < reserved.len() {
colors.push(next_color);
next_color = next_color.wrapping_add(1);
}
let parent_count = commit.parents.len();
let mut cells = draw_cells(
&reserved,
&colors,
commit_lane,
&matching,
parent_count,
);
// Update reserved lanes for following rows.
for &lane in &matching {
if let Some(slot) = reserved.get_mut(lane) {
*slot = None;
}
}
if let Some((first, rest)) = commit.parents.split_first() {
let first_already = reserved
.iter()
.position(|id| *id == Some(*first));
match first_already {
Some(existing) if existing != commit_lane => {
// Merge into an existing lane; free this one.
if let Some(slot) = reserved.get_mut(commit_lane)
{
*slot = None;
}
draw_merge_connector(
&mut cells,
&colors,
commit_lane,
existing,
);
}
_ => {
if let Some(slot) = reserved.get_mut(commit_lane)
{
*slot = Some(*first);
}
}
}
for parent in rest {
if reserved.iter().any(|id| *id == Some(*parent)) {
continue;
}
let lane = open_lane(
&mut reserved,
&mut colors,
&mut next_color,
*parent,
);
draw_fork_connector(
&mut cells,
&colors,
commit_lane,
lane,
);
}
}
while reserved.last() == Some(&None) {
reserved.pop();
colors.pop();
}
while cells.last().is_some_and(|c| c.ch == ' ') {
cells.pop();
}
rows.push(GraphRow {
cells,
commit_lane,
});
}
rows
}
fn open_lane(
reserved: &mut Vec<Option<CommitId>>,
colors: &mut Vec<usize>,
next_color: &mut usize,
id: CommitId,
) -> usize {
if let Some(free) = reserved.iter().position(Option::is_none) {
reserved[free] = Some(id);
free
} else {
let lane = reserved.len();
reserved.push(Some(id));
colors.push(*next_color);
*next_color = next_color.wrapping_add(1);
lane
}
}
fn draw_cells(
reserved: &[Option<CommitId>],
colors: &[usize],
commit_lane: usize,
matching: &[usize],
_parent_count: usize,
) -> Vec<GraphCell> {
let width = reserved.len().max(commit_lane + 1);
let mut cells = Vec::with_capacity(width * 2);
for i in 0..width {
let color = colors.get(i).copied().unwrap_or(0);
let ch = if i == commit_lane {
'●'
} else if matching.contains(&i) {
// Secondary merge into this commit (refined below).
'╯'
} else if reserved.get(i).is_some_and(Option::is_some) {
'│'
} else {
' '
};
cells.push(GraphCell { ch, color });
cells.push(GraphCell {
ch: ' ',
color: 0,
});
}
// Horizontal connectors from secondary matching lanes to commit.
for &lane in matching.iter().skip(1) {
let color = colors.get(lane).copied().unwrap_or(0);
fill_horizontal(&mut cells, colors, commit_lane, lane, color);
let idx = lane * 2;
if let Some(cell) = cells.get_mut(idx) {
cell.ch = if lane > commit_lane { '╯' } else { '╰' };
cell.color = color;
}
}
cells
}
fn draw_merge_connector(
cells: &mut Vec<GraphCell>,
colors: &[usize],
from: usize,
to: usize,
) {
let color = colors.get(to).copied().unwrap_or(0);
ensure_width(cells, to.max(from) + 1);
fill_horizontal(cells, colors, from, to, color);
}
fn draw_fork_connector(
cells: &mut Vec<GraphCell>,
colors: &[usize],
from: usize,
to: usize,
) {
let color = colors.get(to).copied().unwrap_or(0);
ensure_width(cells, to.max(from) + 1);
fill_horizontal(cells, colors, from, to, color);
let idx = to * 2;
if let Some(cell) = cells.get_mut(idx) {
cell.ch = if to > from { '╮' } else { '╭' };
cell.color = color;
}
}
fn ensure_width(cells: &mut Vec<GraphCell>, lanes: usize) {
while cells.len() < lanes * 2 {
cells.push(GraphCell {
ch: ' ',
color: 0,
});
cells.push(GraphCell {
ch: ' ',
color: 0,
});
}
}
fn fill_horizontal(
cells: &mut [GraphCell],
_colors: &[usize],
from: usize,
to: usize,
color: usize,
) {
let left = from.min(to);
let right = from.max(to);
for i in left..=right {
let idx = i * 2;
if idx >= cells.len() {
break;
}
if i == from {
continue;
}
if cells[idx].ch == ' ' || cells[idx].ch == '─' {
cells[idx] = GraphCell { ch: '─', color };
if idx + 1 < cells.len() {
cells[idx + 1] = GraphCell { ch: '─', color };
}
}
}
}
/// Render a row as a plain string (tests / debugging).
#[cfg(test)]
#[must_use]
pub fn row_to_string(row: &GraphRow) -> String {
row.cells.iter().map(|c| c.ch).collect()
}
#[cfg(test)]
mod tests {
use super::*;
use git2::Oid;
fn cid(n: u8) -> CommitId {
let mut bytes = [0_u8; 20];
bytes[19] = n;
CommitId::new(Oid::from_bytes(&bytes).unwrap())
}
#[test]
fn test_linear_history_single_lane() {
let a = cid(1);
let b = cid(2);
let c = cid(3);
let commits = vec![
GraphCommit {
id: a,
parents: vec![b],
},
GraphCommit {
id: b,
parents: vec![c],
},
GraphCommit {
id: c,
parents: vec![],
},
];
let rows = assign_lanes(&commits);
assert_eq!(rows.len(), 3);
assert!(row_to_string(&rows[0]).contains('●'));
assert_eq!(rows[0].commit_lane, 0);
assert_eq!(rows[1].commit_lane, 0);
assert_eq!(rows[2].commit_lane, 0);
}
#[test]
fn test_diverged_branches_use_two_lanes() {
let tip_a = cid(1);
let tip_b = cid(2);
let base = cid(3);
let commits = vec![
GraphCommit {
id: tip_a,
parents: vec![base],
},
GraphCommit {
id: tip_b,
parents: vec![base],
},
GraphCommit {
id: base,
parents: vec![],
},
];
let rows = assign_lanes(&commits);
assert_eq!(rows.len(), 3);
assert!(
rows.iter().any(|r| r.commit_lane >= 1),
"expected a second lane for diverged tips, got {:?}",
rows.iter().map(|r| r.commit_lane).collect::<Vec<_>>()
);
assert!(row_to_string(&rows[2]).contains('●'));
}
}