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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
use rustc_hash::FxHashMap;
use crate::git::{Commit, CommitHash, Repository};
type CommitPosMap<'a> = FxHashMap<&'a CommitHash, (usize, usize)>;
#[derive(Debug)]
pub struct Graph<'a> {
pub commits: Vec<&'a Commit>,
pub commit_pos_map: CommitPosMap<'a>,
pub edges: Vec<Vec<Edge>>,
pub max_pos_x: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Edge {
pub edge_type: EdgeType,
pub pos_x: usize,
pub associated_line_pos_x: usize,
}
impl Edge {
pub fn new(edge_type: EdgeType, pos_x: usize, line_pos_x: usize) -> Self {
Self {
edge_type,
pos_x,
associated_line_pos_x: line_pos_x,
}
}
}
#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub enum EdgeType {
Vertical, // │
Horizontal, // ─
Up, // ╵
Down, // ╷
Left, // ╴
Right, // ╶
RightTop, // ╮
RightBottom, // ╯
LeftTop, // ╭
LeftBottom, // ╰
}
impl EdgeType {
pub fn is_vertically_related(&self) -> bool {
matches!(self, EdgeType::Vertical | EdgeType::Up | EdgeType::Down)
}
}
pub fn calc_graph(repository: &Repository) -> Graph<'_> {
let commits = repository.all_commits();
let commit_pos_map = calc_commit_positions(&commits, repository);
let (graph_edges, max_pos_x) = calc_edges(&commit_pos_map, &commits, repository);
Graph {
commits,
commit_pos_map,
edges: graph_edges,
max_pos_x,
}
}
fn calc_commit_positions<'a>(
commits: &[&'a Commit],
repository: &'a Repository,
) -> CommitPosMap<'a> {
let mut commit_pos_map: CommitPosMap = FxHashMap::default();
let mut commit_line_state: Vec<Option<&CommitHash>> = Vec::new();
for (pos_y, commit) in commits.iter().enumerate() {
let filtered_children_hash = filtered_children_hash(commit, repository);
if filtered_children_hash.is_empty() {
let pos_x = get_first_vacant_line(&commit_line_state);
add_commit_line(commit, &mut commit_line_state, pos_x);
commit_pos_map.insert(&commit.commit_hash, (pos_x, pos_y));
} else {
let pos_x = update_commit_line(commit, &mut commit_line_state, &filtered_children_hash);
commit_pos_map.insert(&commit.commit_hash, (pos_x, pos_y));
}
}
commit_pos_map
}
fn filtered_children_hash<'a>(
commit: &'a Commit,
repository: &'a Repository,
) -> Vec<&'a CommitHash> {
repository
.children_hash(&commit.commit_hash)
.into_iter()
.filter(|child_hash| {
let child_parents_hash = repository.parents_hash(child_hash);
!child_parents_hash.is_empty() && *child_parents_hash[0] == commit.commit_hash
})
.collect()
}
fn get_first_vacant_line(commit_line_state: &[Option<&CommitHash>]) -> usize {
commit_line_state
.iter()
.position(|c| c.is_none())
.unwrap_or(commit_line_state.len())
}
fn add_commit_line<'a>(
commit: &'a Commit,
commit_line_state: &mut Vec<Option<&'a CommitHash>>,
pos_x: usize,
) {
if commit_line_state.len() <= pos_x {
commit_line_state.push(Some(&commit.commit_hash));
} else {
commit_line_state[pos_x] = Some(&commit.commit_hash);
}
}
fn update_commit_line<'a>(
commit: &'a Commit,
commit_line_state: &mut [Option<&'a CommitHash>],
target_commit_hashes: &[&CommitHash],
) -> usize {
if commit_line_state.is_empty() {
return 0;
}
let mut min_pos_x = commit_line_state.len().saturating_sub(1);
for target_hash in target_commit_hashes {
for (pos_x, commit_hash) in commit_line_state.iter().enumerate() {
if let Some(hash) = commit_hash {
if hash == target_hash {
commit_line_state[pos_x] = None;
if min_pos_x > pos_x {
min_pos_x = pos_x;
}
break;
}
}
}
}
commit_line_state[min_pos_x] = Some(&commit.commit_hash);
min_pos_x
}
#[derive(Debug, Clone)]
struct WrappedEdge<'a> {
edge: Edge,
edge_parent_hash: &'a CommitHash,
}
impl<'a> WrappedEdge<'a> {
fn new(
edge_type: EdgeType,
pos_x: usize,
line_pos_x: usize,
edge_parent_hash: &'a CommitHash,
) -> Self {
Self {
edge: Edge::new(edge_type, pos_x, line_pos_x),
edge_parent_hash,
}
}
}
fn calc_edges(
commit_pos_map: &CommitPosMap,
commits: &[&Commit],
repository: &Repository,
) -> (Vec<Vec<Edge>>, usize) {
let mut max_pos_x = 0;
let mut edges: Vec<Vec<WrappedEdge>> = vec![vec![]; commits.len()];
for commit in commits {
let (pos_x, pos_y) = commit_pos_map[&commit.commit_hash];
let hash = &commit.commit_hash;
for child_hash in repository.children_hash(hash) {
let (child_pos_x, child_pos_y) = commit_pos_map[child_hash];
if pos_x == child_pos_x {
// commit
edges[pos_y].push(WrappedEdge::new(EdgeType::Up, pos_x, pos_x, hash));
for y in ((child_pos_y + 1)..pos_y).rev() {
edges[y].push(WrappedEdge::new(EdgeType::Vertical, pos_x, pos_x, hash));
}
edges[child_pos_y].push(WrappedEdge::new(EdgeType::Down, pos_x, pos_x, hash));
} else {
let child_first_parent_hash = &commits[child_pos_y].parent_commit_hashes[0];
if *child_first_parent_hash == *hash {
// branch
if pos_x < child_pos_x {
edges[pos_y].push(WrappedEdge::new(
EdgeType::Right,
pos_x,
child_pos_x,
hash,
));
for x in (pos_x + 1)..child_pos_x {
edges[pos_y].push(WrappedEdge::new(
EdgeType::Horizontal,
x,
child_pos_x,
hash,
));
}
edges[pos_y].push(WrappedEdge::new(
EdgeType::RightBottom,
child_pos_x,
child_pos_x,
hash,
));
} else {
edges[pos_y].push(WrappedEdge::new(
EdgeType::Left,
pos_x,
child_pos_x,
hash,
));
for x in (child_pos_x + 1)..pos_x {
edges[pos_y].push(WrappedEdge::new(
EdgeType::Horizontal,
x,
child_pos_x,
hash,
));
}
edges[pos_y].push(WrappedEdge::new(
EdgeType::LeftBottom,
child_pos_x,
child_pos_x,
hash,
));
}
for y in ((child_pos_y + 1)..pos_y).rev() {
edges[y].push(WrappedEdge::new(
EdgeType::Vertical,
child_pos_x,
child_pos_x,
hash,
));
}
edges[child_pos_y].push(WrappedEdge::new(
EdgeType::Down,
child_pos_x,
child_pos_x,
hash,
));
} else {
// merge
// skip
}
}
}
if max_pos_x < pos_x {
max_pos_x = pos_x;
}
// draw down edge if has parent but parent not in the graph (when max_count is set)
if !commit.parent_commit_hashes.is_empty()
&& repository.commit(&commit.parent_commit_hashes[0]).is_none()
{
edges[pos_y].push(WrappedEdge::new(EdgeType::Down, pos_x, pos_x, hash));
((pos_y + 1)..commits.len()).for_each(|y| {
edges[y].push(WrappedEdge::new(EdgeType::Vertical, pos_x, pos_x, hash));
});
}
}
for commit in commits {
let (pos_x, pos_y) = commit_pos_map[&commit.commit_hash];
let hash = &commit.commit_hash;
for child_hash in repository.children_hash(hash) {
let (child_pos_x, child_pos_y) = commit_pos_map[child_hash];
if pos_x == child_pos_x {
// commit
// skip
} else {
let child_first_parent_hash = &commits[child_pos_y].parent_commit_hashes[0];
if *child_first_parent_hash == *hash {
// branch
// skip
} else {
// merge
let mut overlap = false;
let mut new_pos_x = pos_x;
let mut skip_judge_overlap = true;
for y in (child_pos_y + 1)..pos_y {
let processing_commit_pos_x =
commit_pos_map.get(&commits[y].commit_hash).unwrap().0;
if processing_commit_pos_x == new_pos_x {
skip_judge_overlap = false;
break;
}
if edges[y]
.iter()
.filter(|e| e.edge.pos_x == pos_x)
.filter(|e| matches!(e.edge.edge_type, EdgeType::Vertical))
.any(|e| e.edge_parent_hash != hash)
{
skip_judge_overlap = false;
break;
}
}
if !skip_judge_overlap {
for y in (child_pos_y + 1)..pos_y {
let processing_commit_pos_x =
commit_pos_map.get(&commits[y].commit_hash).unwrap().0;
if processing_commit_pos_x == new_pos_x {
overlap = true;
if new_pos_x < processing_commit_pos_x + 1 {
new_pos_x = processing_commit_pos_x + 1;
}
}
for edge in &edges[y] {
if edge.edge.pos_x >= new_pos_x
&& edge.edge_parent_hash != hash
&& matches!(edge.edge.edge_type, EdgeType::Vertical)
{
overlap = true;
if new_pos_x < edge.edge.pos_x + 1 {
new_pos_x = edge.edge.pos_x + 1;
}
}
}
}
}
if overlap {
// detour
edges[pos_y].push(WrappedEdge::new(EdgeType::Right, pos_x, pos_x, hash));
for x in (pos_x + 1)..new_pos_x {
edges[pos_y].push(WrappedEdge::new(
EdgeType::Horizontal,
x,
pos_x,
hash,
));
}
edges[pos_y].push(WrappedEdge::new(
EdgeType::RightBottom,
new_pos_x,
pos_x,
hash,
));
for y in ((child_pos_y + 1)..pos_y).rev() {
edges[y].push(WrappedEdge::new(
EdgeType::Vertical,
new_pos_x,
pos_x,
hash,
));
}
edges[child_pos_y].push(WrappedEdge::new(
EdgeType::RightTop,
new_pos_x,
pos_x,
hash,
));
for x in (child_pos_x + 1)..new_pos_x {
edges[child_pos_y].push(WrappedEdge::new(
EdgeType::Horizontal,
x,
pos_x,
hash,
));
}
edges[child_pos_y].push(WrappedEdge::new(
EdgeType::Right,
child_pos_x,
pos_x,
hash,
));
if max_pos_x < new_pos_x {
max_pos_x = new_pos_x;
}
} else {
edges[pos_y].push(WrappedEdge::new(EdgeType::Up, pos_x, pos_x, hash));
for y in ((child_pos_y + 1)..pos_y).rev() {
edges[y].push(WrappedEdge::new(EdgeType::Vertical, pos_x, pos_x, hash));
}
if pos_x < child_pos_x {
edges[child_pos_y].push(WrappedEdge::new(
EdgeType::LeftTop,
pos_x,
pos_x,
hash,
));
for x in (pos_x + 1)..child_pos_x {
edges[child_pos_y].push(WrappedEdge::new(
EdgeType::Horizontal,
x,
pos_x,
hash,
));
}
edges[child_pos_y].push(WrappedEdge::new(
EdgeType::Left,
child_pos_x,
pos_x,
hash,
));
} else {
edges[child_pos_y].push(WrappedEdge::new(
EdgeType::RightTop,
pos_x,
pos_x,
hash,
));
for x in (child_pos_x + 1)..pos_x {
edges[child_pos_y].push(WrappedEdge::new(
EdgeType::Horizontal,
x,
pos_x,
hash,
));
}
edges[child_pos_y].push(WrappedEdge::new(
EdgeType::Right,
child_pos_x,
pos_x,
hash,
));
}
}
}
}
}
if max_pos_x < pos_x {
max_pos_x = pos_x;
}
}
let edges: Vec<Vec<Edge>> = edges
.into_iter()
.map(|es| {
let mut es: Vec<Edge> = es.into_iter().map(|e| e.edge).collect();
es.sort_by_key(|e| (e.associated_line_pos_x, e.pos_x, e.edge_type));
es.dedup();
es
})
.collect();
(edges, max_pos_x)
}