gitlimes 0.1.0

Fast, low-memory git history CLI: colored log, branches, author stats and a unicode commit graph
Documentation
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//! Turns a `Step` into terminal rows.
//!
//! Lanes live in even cells and the gaps between them in odd cells, so a
//! horizontal connector can run underneath other lanes without ever colliding
//! with them: where a horizontal crosses a vertical the two merge into a cross
//! glyph instead of overwriting it.
//!
//! A commit is drawn as up to four rows, in this order:
//!
//! ```text
//! fold_row        lanes converging into this commit, from above
//! commit_row      the dot
//! branch_row      lanes leaving this commit, below
//! shift_row       lanes sliding left into columns that just freed up
//! ```
//!
//! Folds are drawn *above* the dot because a fork point is only known when the
//! shared parent is reached - which is the same thing `git log --graph` does
//! when it prints `|/` on the line before the commit.

use super::lanes::Step;
use crate::style::{c, LANES, RESET};

pub struct Glyphs {
    pub commit: char,
    pub merge: char,
    pub vert: char,
    pub horiz: char,
    pub cross: char,
    /// A lane moving one column to the left.
    pub slide: char,
    /// Lane to the right bends down-left into the commit column.
    pub fold_left: char,
    /// Lane to the left bends down-right into the commit column.
    pub fold_right: char,
    /// Edge leaves the commit column and turns down to the right.
    pub branch_right: char,
    /// Edge leaves the commit column and turns down to the left.
    pub branch_left: char,
    pub tee_right: char,
    pub tee_left: char,
    pub tee_both: char,
    /// A lane starting here while the connector continues past it.
    pub tee_down: char,
    /// A lane ending here while the connector continues past it.
    pub tee_up: char,
}

pub const UNICODE: Glyphs = Glyphs {
    commit: '\u{25cf}',
    merge: '\u{25cb}',
    vert: '\u{2502}',
    horiz: '\u{2500}',
    cross: '\u{253c}',
    slide: '\u{2571}',
    fold_left: '\u{256f}',
    fold_right: '\u{2570}',
    branch_right: '\u{256e}',
    branch_left: '\u{256d}',
    tee_right: '\u{251c}',
    tee_left: '\u{2524}',
    tee_both: '\u{253c}',
    tee_down: '\u{252c}',
    tee_up: '\u{2534}',
};

pub const ASCII: Glyphs = Glyphs {
    commit: '*',
    merge: 'o',
    vert: '|',
    horiz: '-',
    cross: '+',
    slide: '/',
    fold_left: '/',
    fold_right: '\\',
    branch_right: '\\',
    branch_left: '/',
    tee_right: '+',
    tee_left: '+',
    tee_both: '+',
    tee_down: '+',
    tee_up: '+',
};

/// A row of glyphs plus the lane colour each one should be painted in.
struct Grid<'a> {
    cells: Vec<char>,
    colors: Vec<usize>,
    g: &'a Glyphs,
}

impl<'a> Grid<'a> {
    fn new(lanes: usize, g: &'a Glyphs) -> Grid<'a> {
        let w = if lanes == 0 { 0 } else { lanes * 2 - 1 };
        Grid {
            cells: vec![' '; w],
            colors: vec![0; w],
            g,
        }
    }

    /// Writing a horizontal over a vertical (or the reverse) produces a
    /// crossing rather than clobbering the lane that was already there.
    fn put(&mut self, at: usize, ch: char, color: usize) {
        if at >= self.cells.len() {
            return;
        }
        let existing = self.cells[at];
        let crossing = (existing == self.g.vert && ch == self.g.horiz)
            || (existing == self.g.horiz && ch == self.g.vert);
        self.cells[at] = if existing == ' ' {
            ch
        } else if crossing {
            self.g.cross
        } else {
            ch
        };
        // A crossing keeps the colour of the lane already there, so a lane's
        // colour stays continuous down the page.
        if !crossing {
            self.colors[at] = color;
        }
    }

    fn render(&self, min_width: usize) -> String {
        let mut out = String::new();
        let mut last: Option<usize> = None;
        for (i, ch) in self.cells.iter().enumerate() {
            if *ch == ' ' {
                if last.is_some() {
                    out.push_str(c(RESET));
                    last = None;
                }
                out.push(' ');
                continue;
            }
            let color = self.colors[i];
            if last != Some(color) {
                out.push_str(c(LANES[color % LANES.len()]));
                last = Some(color);
            }
            out.push(*ch);
        }
        if last.is_some() {
            out.push_str(c(RESET));
        }
        for _ in self.cells.len()..min_width {
            out.push(' ');
        }
        out
    }
}

fn cell(lane: usize) -> usize {
    lane * 2
}

fn occupied(row: &[bool], lane: usize) -> bool {
    row.get(lane).copied().unwrap_or(false)
}

/// Falls back to the column index so a lane without a recorded colour still
/// differs from its neighbours.
fn lane_color(map: &[usize], lane: usize) -> usize {
    map.get(lane).copied().unwrap_or(lane)
}

/// Lanes converging into this commit, drawn above the dot. `None` when nothing
/// folds in.
pub fn fold_row(step: &Step, g: &Glyphs, min_width: usize) -> Option<String> {
    if step.closing.is_empty() {
        return None;
    }
    let mut grid = Grid::new(step.width(), g);

    // Lanes that pass straight through this row.
    for j in 0..step.width() {
        if j != step.col && occupied(&step.before, j) && occupied(&step.at, j) {
            grid.put(cell(j), g.vert, lane_color(&step.colors, j));
        }
    }
    connect(&mut grid, g, step, &step.closing, &step.before, true);
    Some(grid.render(min_width))
}

/// The row the commit dot sits on: every open lane draws a vertical, except the
/// commit's own column.
pub fn commit_row(step: &Step, g: &Glyphs, min_width: usize) -> String {
    let mut grid = Grid::new(step.width(), g);
    for j in 0..step.width() {
        if occupied(&step.at, j) {
            grid.put(cell(j), g.vert, lane_color(&step.colors, j));
        }
    }
    let dot = if step.is_merge { g.merge } else { g.commit };
    grid.put(cell(step.col), dot, step.dot_color);
    grid.render(min_width)
}

/// Lanes leaving this commit, drawn below the dot. `None` when nothing branches
/// out.
pub fn branch_row(step: &Step, g: &Glyphs, min_width: usize) -> Option<String> {
    if step.opening.is_empty() {
        return None;
    }
    let mut grid = Grid::new(step.width(), g);

    for j in 0..step.width() {
        if j != step.col && occupied(&step.at, j) && occupied(&step.pre_shift, j) {
            grid.put(cell(j), g.vert, lane_color(&step.colors, j));
        }
    }
    connect(&mut grid, g, step, &step.opening, &step.at, false);
    Some(grid.render(min_width))
}

/// Lanes sliding one column left to close up a freed column. `None` when
/// nothing moves.
pub fn shift_row(step: &Step, g: &Glyphs, min_width: usize) -> Option<String> {
    if step.shifts.is_empty() {
        return None;
    }
    let mut grid = Grid::new(step.width(), g);

    for j in 0..step.width() {
        let moving = step.shifts.iter().any(|(from, _)| *from == j);
        if occupied(&step.pre_shift, j) && !moving {
            grid.put(cell(j), g.vert, lane_color(&step.colors, j));
        }
    }
    // Every move is one column, so each diagonal occupies the gap between the
    // old and new column and can never collide with another.
    for &(from, to) in &step.shifts {
        grid.put(cell(to) + 1, g.slide, lane_color(&step.colors, from));
    }
    Some(grid.render(min_width))
}

/// Draws edges between the commit column and each lane in `targets`, plus the
/// junction that ties them to the commit's own column.
///
/// `existing` says which lanes were already live: an edge reaching a live lane
/// gets a tee (the lane continues past the join) rather than a corner (the lane
/// starts or ends here).
fn connect(
    grid: &mut Grid,
    g: &Glyphs,
    step: &Step,
    targets: &[usize],
    existing: &[bool],
    folding: bool,
) {
    let col = step.col;
    let color = step.dot_color;

    // Every horizontal first. Drawing a corner and then running another
    // target's horizontal over it is how an octopus merge used to lose the
    // connector for every lane except its farthest.
    for &k in targets {
        run(grid, g, col, k, color);
    }

    // Only the lane at the end of a run gets a corner; the connector passes
    // straight through the ones in between, so those get a tee.
    let end_right = targets.iter().copied().filter(|k| *k > col).max();
    let end_left = targets.iter().copied().filter(|k| *k < col).min();

    for &k in targets {
        let right = k > col;
        let at_end = if right {
            Some(k) == end_right
        } else {
            Some(k) == end_left
        };
        let glyph = if occupied(existing, k) && !folding {
            // The lane was already open and keeps going, so the edge joins it.
            if right {
                g.tee_left
            } else {
                g.tee_right
            }
        } else if !at_end {
            // The connector continues past this lane.
            if folding {
                g.tee_up
            } else {
                g.tee_down
            }
        } else if folding {
            if right {
                g.fold_left
            } else {
                g.fold_right
            }
        } else if right {
            g.branch_right
        } else {
            g.branch_left
        };
        grid.put(cell(k), glyph, color);
    }

    let junction = match (end_left.is_some(), end_right.is_some()) {
        (true, true) => g.tee_both,
        (false, true) => g.tee_right,
        (true, false) => g.tee_left,
        (false, false) => g.vert,
    };
    grid.put(cell(col), junction, color);
}

/// Draws the horizontal between two lanes, exclusive of both endpoints.
fn run(grid: &mut Grid, g: &Glyphs, from_lane: usize, to_lane: usize, color: usize) {
    let (lo, hi) = if from_lane < to_lane {
        (cell(from_lane), cell(to_lane))
    } else {
        (cell(to_lane), cell(from_lane))
    };
    for p in (lo + 1)..hi {
        grid.put(p, g.horiz, color);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::graph::lanes::Lanes;

    /// Renders every row of a step in drawing order, trimmed.
    fn rows(step: &Step) -> Vec<String> {
        let mut v = Vec::new();
        if let Some(r) = fold_row(step, &ASCII, 0) {
            v.push(r.trim_end().to_string());
        }
        v.push(commit_row(step, &ASCII, 0).trim_end().to_string());
        if let Some(r) = branch_row(step, &ASCII, 0) {
            v.push(r.trim_end().to_string());
        }
        if let Some(r) = shift_row(step, &ASCII, 0) {
            v.push(r.trim_end().to_string());
        }
        v
    }

    #[test]
    fn linear_history_draws_a_single_column() {
        let mut l = Lanes::new();
        let s = l.advance("a", &["b"]);
        assert_eq!(rows(&s), vec!["*"]);
    }

    #[test]
    fn merge_branches_out_below_the_dot() {
        let mut l = Lanes::new();
        let s = l.advance("m", &["a", "side"]);
        assert_eq!(rows(&s), vec!["o", "+-\\"], "merges get a hollow dot");
    }

    #[test]
    fn fork_folds_in_above_the_dot() {
        // Matches `git log --graph`, which prints the fold before the commit
        // the two lanes converge on.
        let mut l = Lanes::new();
        l.advance("m", &["a", "s"]);
        l.advance("a", &["r"]);
        l.advance("s", &["r"]);
        let s = l.advance("r", &[]);
        assert_eq!(rows(&s), vec!["+-/", "*"]);
    }

    #[test]
    fn a_commit_can_fold_in_and_branch_out_at_once() {
        // The merge commit of a branch that itself had two children: one lane
        // folds in from above and a new one opens below.
        let mut l = Lanes::new();
        l.advance("top", &["m", "b"]); // opens lane 1
        l.advance("b", &["m"]); // lane 1 now also waits for m
        let s = l.advance("m", &["p", "side"]);
        assert_eq!(
            rows(&s),
            vec!["+-/", "o", "+-\\"],
            "fold above, dot, branch below"
        );
    }

    #[test]
    fn a_freed_column_is_closed_up_by_a_diagonal() {
        let mut l = Lanes::new();
        l.advance("m", &["a", "s"]); // lane 0 = a, lane 1 = s
        let s = l.advance("a", &[]); // lane 0 ends, so lane 1 slides into it
        assert_eq!(rows(&s), vec!["* |", " /"]);
    }

    #[test]
    fn horizontal_crossing_a_lane_does_not_erase_it() {
        // A merge reaching past an open middle lane must cross it, not cut it.
        let mut l = Lanes::new();
        l.advance("m", &["a", "s"]); // opens lane 1
        let s = l.advance("a", &["p", "far"]); // opens lane 2, crossing lane 1
        assert_eq!(
            branch_row(&s, &ASCII, 0).unwrap().trim_end(),
            "+-+-\\",
            "lane 1 survives as a crossing"
        );
    }

    #[test]
    fn a_lane_keeps_one_colour_across_a_shift() {
        let mut l = Lanes::new();
        let m = l.advance("m", &["a", "s"]);
        let side_color = m.colors_after[1];
        let s = l.advance("a", &[]); // side lane slides from column 1 to 0
        assert_eq!(s.shifts, vec![(1, 0)]);
        assert_eq!(
            s.colors_after[0], side_color,
            "the branch keeps its colour in its new column"
        );
    }

    #[test]
    fn min_width_pads_so_commit_text_lines_up() {
        let mut l = Lanes::new();
        let s = l.advance("a", &["b"]);
        assert_eq!(commit_row(&s, &ASCII, 5), "*    ");
    }
}

#[cfg(test)]
mod octopus_tests {
    use super::*;
    use crate::graph::lanes::Lanes;

    #[test]
    fn every_opened_lane_gets_its_own_connector() {
        // An octopus merge opens two lanes. Before the fix the horizontal drawn
        // for the farther one overwrote the nearer one's corner, so a lane
        // appeared on the next row with nothing joining it to its commit.
        let mut l = Lanes::new();
        let s = l.advance("m", &["p1", "p2", "p3"]);
        assert_eq!(s.opening, vec![1, 2]);
        assert_eq!(
            branch_row(&s, &ASCII, 0).unwrap().trim_end(),
            "+-+-\\",
            "lane 1 must have a connector, not a bare horizontal"
        );
        let uni = branch_row(&s, &UNICODE, 0).unwrap();
        assert!(
            uni.contains('\u{252c}'),
            "the intermediate lane should get a downward tee: {:?}",
            uni.trim_end()
        );
    }

    #[test]
    fn every_folded_lane_gets_its_own_connector() {
        let mut l = Lanes::new();
        l.advance("m", &["a", "b", "c"]);
        l.advance("a", &["z"]);
        l.advance("b", &["z"]);
        l.advance("c", &["z"]);
        let s = l.advance("z", &[]);
        assert_eq!(s.closing, vec![1, 2]);
        assert_eq!(
            fold_row(&s, &ASCII, 0).unwrap().trim_end(),
            "+-+-/",
            "lane 1 must have a connector, not a bare horizontal"
        );
        let uni = fold_row(&s, &UNICODE, 0).unwrap();
        assert!(
            uni.contains('\u{2534}'),
            "the intermediate lane should get an upward tee: {:?}",
            uni.trim_end()
        );
    }
}