git-loom 0.18.0

A Git CLI tool that weaves together multiple feature branches into integration branches
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
use crate::core::repo::{
    CommitInfo, ContextCommit, FileChange, RemoteStatus, RepoInfo, UpstreamInfo,
};
use crate::core::shortid::IdAllocator;
use colored::{Color, Colorize};
use std::collections::{HashMap, HashSet};
use std::fmt::Write;
use terminal_size::{Width, terminal_size};

// ── Theme ────────────────────────────────────────────────────────────────

/// Color palette for graph output. Use [`Theme::dark`] or [`Theme::light`].
pub struct Theme {
    /// Graph structure: lines, connectors, dots on the integration line.
    pub graph: Color,
    /// Branch names in brackets (local and remote).
    pub branch: Color,
    /// Labels like (upstream) and (common base).
    pub label: Color,
    /// Dimmed secondary text: messages, dates, file changes, "no changes".
    pub dim: Color,
    /// Commit message text.
    pub message: Color,
    /// Short ID prefix (also underlined in rendering).
    pub shortid: Color,
    /// Staged (index) file status: green, matching git convention.
    pub staged: Color,
    /// Unstaged (worktree) file status: red, matching git convention.
    pub unstaged: Color,
    /// Untracked file marker and path color.
    pub untracked: Color,
    /// Branch remote tracking ref exists and is in sync.
    pub remote_synced: Color,
    /// Branch has unpushed commits ahead of its remote.
    pub remote_ahead: Color,
    /// Branch remote tracking ref is gone (deleted on remote).
    pub remote_gone: Color,
    /// Conflicted file status: bold red, matching git convention.
    pub conflict: Color,
    /// Rotating colors for commit dots on feature branches.
    pub branch_dots: &'static [Color],
}

impl Theme {
    /// Dark terminal background theme (default).
    pub fn dark() -> Self {
        Theme {
            graph: Color::BrightBlack,
            branch: Color::Green,
            label: Color::Cyan,
            dim: Color::AnsiColor(240),
            message: Color::AnsiColor(248),
            shortid: Color::Blue,
            staged: Color::Green,
            unstaged: Color::Red,
            untracked: Color::Magenta,
            remote_synced: Color::Green,
            remote_ahead: Color::Yellow,
            remote_gone: Color::Red,
            conflict: Color::Red,
            branch_dots: BRANCH_DOTS,
        }
    }

    /// Light terminal background theme.
    pub fn light() -> Self {
        Theme {
            graph: Color::AnsiColor(248),
            branch: Color::Green,
            label: Color::Blue,
            dim: Color::AnsiColor(248),
            message: Color::AnsiColor(243),
            shortid: Color::Blue,
            staged: Color::Green,
            unstaged: Color::Red,
            untracked: Color::Magenta,
            remote_synced: Color::Green,
            remote_ahead: Color::Yellow,
            remote_gone: Color::Red,
            conflict: Color::Red,
            branch_dots: BRANCH_DOTS,
        }
    }
}

/// Rotating colors for commit dots on feature branches (shared by all themes).
const BRANCH_DOTS: &[Color] = &[
    Color::Yellow,
    Color::Cyan,
    Color::Magenta,
    Color::Blue,
    Color::Red,
    Color::Green,
];

/// Number of untracked files above which the display switches to multi-column layout.
const UNTRACKED_MULTICOLUMN_THRESHOLD: usize = 5;

/// Width of the graph prefix ("│   ") in visible columns.
const GRAPH_PREFIX_WIDTH: usize = 4;

// ── Data types ──────────────────────────────────────────────────────────

/// Display configuration for the status graph renderer.
pub struct RenderOpts {
    /// Terminal column width, used for multi-column untracked layout.
    /// `None` means non-TTY (e.g. pipe) — fall back to single-column.
    pub terminal_width: Option<u16>,
    /// Color theme for the graph output.
    pub theme: Theme,
    /// CWD prefix relative to repo root (empty string if at root).
    pub cwd_prefix: String,
}

/// A logical section in the rendered status output. Sections are built from
/// RepoInfo and rendered top-to-bottom with UTF-8 box-drawing characters.
enum Section {
    /// Working tree status (always present, may contain zero changes).
    WorkingChanges(Vec<FileChange>),
    /// A feature branch: its name(s) and the commits it owns.
    /// Multiple names occur when several branches point to the same tip commit.
    /// Each name is paired with its remote tracking status (None = never pushed).
    Branch {
        names: Vec<(String, Option<RemoteStatus>)>,
        commits: Vec<CommitInfo>,
    },
    /// Commits on the integration line that don't belong to any feature branch.
    Loose(Vec<CommitInfo>),
    /// The upstream tracking branch / common base marker at the bottom of the status.
    Upstream(UpstreamInfo),
    /// Context commits before the base (display-only, dimmed).
    Context(Vec<ContextCommit>),
}

// ── Public API ──────────────────────────────────────────────────────────

/// Build sections from repo data and render them as a UTF-8 graph string.
pub fn render(info: RepoInfo, ids: &IdAllocator, opts: &RenderOpts) -> String {
    let sections = build_sections(info);
    render_sections(&sections, ids, opts)
}

/// Detect terminal width and build render options for the given theme.
pub fn default_render_opts(theme: Theme, cwd_prefix: String) -> RenderOpts {
    RenderOpts {
        terminal_width: terminal_size().map(|(Width(w), _)| w),
        theme,
        cwd_prefix,
    }
}

/// Convert a repo-relative path to CWD-relative for display.
fn display_path(repo_path: &str, cwd_prefix: &str) -> String {
    crate::core::repo::cwd_relative_path(repo_path, cwd_prefix)
}

// ── Section building ────────────────────────────────────────────────────

/// Group commits into sections: working changes, feature branches, loose
/// commits, and the upstream marker. Commits are assigned to a branch when
/// they follow a branch tip in topological order.
fn build_sections(info: RepoInfo) -> Vec<Section> {
    // Build a set of branch tip OIDs for quick lookup.
    let branch_tip_set: HashSet<git2::Oid> = info.branches.iter().map(|b| b.tip_oid).collect();

    // Group branches by tip OID to handle co-located branches (multiple
    // branch names pointing to the same commit).
    let mut tip_to_names: HashMap<git2::Oid, Vec<(String, Option<RemoteStatus>)>> = HashMap::new();
    for b in &info.branches {
        tip_to_names
            .entry(b.tip_oid)
            .or_default()
            .push((b.name.clone(), b.remote.clone()));
    }

    // Build a parent lookup from the commit list so we can walk ancestry chains.
    let parent_map: HashMap<git2::Oid, Option<git2::Oid>> =
        info.commits.iter().map(|c| (c.oid, c.parent_oid)).collect();

    // For each unique branch tip, compute the set of commits belonging to it.
    // Walk from the branch tip along parent links, stopping at:
    //   - A commit not in our range (outside upstream..HEAD)
    //   - Another branch's tip (stacked-branch boundary)
    // Use the first name in the group as the canonical name for commit assignment.
    let mut commit_to_branch: HashMap<git2::Oid, String> = HashMap::new();
    let mut seen_tips: HashSet<git2::Oid> = HashSet::new();
    for b in &info.branches {
        if !seen_tips.insert(b.tip_oid) {
            continue; // Already processed commits for this tip
        }
        let canonical_name = tip_to_names[&b.tip_oid][0].0.clone();
        let mut current = Some(b.tip_oid);
        let mut is_tip = true;
        while let Some(oid) = current {
            if !parent_map.contains_key(&oid) {
                break; // outside our commit range
            }
            // Stop at another branch's tip (but not at our own tip)
            if !is_tip && branch_tip_set.contains(&oid) {
                break;
            }
            is_tip = false;
            commit_to_branch.insert(oid, canonical_name.clone());
            current = parent_map.get(&oid).and_then(|p| *p);
        }
    }

    // Map canonical name → all names for that branch group.
    // Reverse so the newest (alphabetically last) branch appears on top.
    let mut canonical_to_names: HashMap<String, Vec<(String, Option<RemoteStatus>)>> =
        HashMap::new();
    for branches in tip_to_names.values() {
        let mut reversed = branches.clone();
        reversed.reverse();
        canonical_to_names.insert(branches[0].0.clone(), reversed);
    }

    let mut sections: Vec<Section> = Vec::new();

    sections.push(Section::WorkingChanges(info.working_changes));

    // Separate commits into loose and branch groups, preserving topo order within each.
    let mut loose_commits: Vec<CommitInfo> = Vec::new();
    let mut branch_sections: Vec<Section> = Vec::new();

    let mut commits = info.commits.into_iter().peekable();
    while let Some(commit) = commits.next() {
        if let Some(branch_name) = commit_to_branch.get(&commit.oid) {
            let name = branch_name.clone();
            let names = canonical_to_names
                .get(&name)
                .cloned()
                .unwrap_or_else(|| vec![(name.clone(), None)]);
            let mut branch_commits = vec![commit];

            // Collect subsequent commits that belong to the same branch.
            while let Some(next) = commits.peek() {
                if commit_to_branch.get(&next.oid) == Some(&name) {
                    branch_commits.push(commits.next().unwrap());
                } else {
                    break;
                }
            }
            branch_sections.push(Section::Branch {
                names,
                commits: branch_commits,
            });
        } else {
            loose_commits.push(commit);
            while let Some(next) = commits.peek() {
                if commit_to_branch.contains_key(&next.oid) {
                    break;
                }
                loose_commits.push(commits.next().unwrap());
            }
        }
    }

    // Add empty sections for branches at the merge-base (no commits in range).
    let represented: HashSet<&String> = commit_to_branch.values().collect();
    for branches in canonical_to_names.values() {
        // canonical key is the pre-reversal first name; check if any name is represented
        if !branches.iter().any(|(n, _)| represented.contains(n)) {
            branch_sections.push(Section::Branch {
                names: branches.clone(),
                commits: vec![],
            });
        }
    }

    // Loose commits first, then feature branches.
    if !loose_commits.is_empty() {
        sections.push(Section::Loose(loose_commits));
    }
    sections.extend(branch_sections);

    sections.push(Section::Upstream(info.upstream));

    if !info.context_commits.is_empty() {
        sections.push(Section::Context(info.context_commits));
    }

    sections
}

/// Check if the next branch section is stacked on top of the current one.
/// Two branches are stacked if the first commit of the next branch is a parent
/// of the last commit of the current branch.
fn is_stacked_with_next(sections: &[Section], idx: usize) -> bool {
    let Section::Branch { commits, .. } = &sections[idx] else {
        return false;
    };
    let Some(Section::Branch {
        commits: next_commits,
        ..
    }) = sections.get(idx + 1)
    else {
        return false;
    };
    let Some(last) = commits.last() else {
        return false;
    };
    let Some(next_first) = next_commits.first() else {
        return false;
    };
    last.parent_oid == Some(next_first.oid)
}

// ── Rendering ───────────────────────────────────────────────────────────

/// Render sections as a UTF-8 graph. Stacked branches (where the last commit
/// of a branch is a parent of the first commit of the next) are connected
/// with `││` and `│├─`, while independent branches get `├╯` then `│╭─`.
fn render_sections(sections: &[Section], ids: &IdAllocator, opts: &RenderOpts) -> String {
    let mut out = String::new();
    let last_idx = sections.len() - 1;
    let mut branch_color_idx: usize = 0;

    for (idx, section) in sections.iter().enumerate() {
        match section {
            Section::WorkingChanges(changes) => {
                render_working_changes(&mut out, changes, ids, opts);
            }
            Section::Branch { names, commits } => {
                let dot_color =
                    opts.theme.branch_dots[branch_color_idx % opts.theme.branch_dots.len()];
                branch_color_idx += 1;

                let prev_stacked = idx > 0 && is_stacked_with_next(sections, idx - 1);
                let next_stacked = is_stacked_with_next(sections, idx);

                render_branch(
                    &mut out,
                    names,
                    commits,
                    dot_color,
                    prev_stacked,
                    next_stacked,
                    idx < last_idx,
                    ids,
                    &opts.theme,
                    &opts.cwd_prefix,
                );
            }
            Section::Loose(commits) => {
                render_loose(
                    &mut out,
                    commits,
                    idx < last_idx,
                    ids,
                    &opts.theme,
                    &opts.cwd_prefix,
                );
            }
            Section::Upstream(info) => {
                render_upstream(&mut out, info, &opts.theme);
            }
            Section::Context(commits) => {
                render_context(&mut out, commits, &opts.theme);
            }
        }
    }

    out
}

fn render_working_changes(
    out: &mut String,
    changes: &[FileChange],
    ids: &IdAllocator,
    opts: &RenderOpts,
) {
    let theme = &opts.theme;
    writeln!(
        out,
        "{} {} {}{}{}",
        "╭─".color(theme.graph),
        ids.get_unstaged().color(theme.shortid).underline(),
        "[".color(theme.dim),
        "local changes".color(theme.label),
        "]".color(theme.dim)
    )
    .unwrap();

    let conflicted: Vec<&FileChange> = changes
        .iter()
        .filter(|f| f.index == '!' && f.worktree == '!')
        .collect();
    let tracked: Vec<&FileChange> = changes
        .iter()
        .filter(|f| f.index != '!' && !(f.index == '?' && f.worktree == '?'))
        .collect();
    let untracked: Vec<&FileChange> = changes
        .iter()
        .filter(|f| f.index == '?' && f.worktree == '?')
        .collect();

    if conflicted.is_empty() && tracked.is_empty() && untracked.is_empty() {
        writeln!(
            out,
            "{}   {}",
            "".color(theme.graph),
            "no changes".color(theme.dim)
        )
        .unwrap();
    } else {
        for change in &conflicted {
            writeln!(
                out,
                "{}   {} {} {}",
                "".color(theme.graph),
                ids.get_file(&change.path).color(theme.shortid).underline(),
                "!!".color(theme.conflict).bold(),
                display_path(&change.path, &opts.cwd_prefix)
                    .color(theme.conflict)
                    .bold()
            )
            .unwrap();
        }
        for change in &tracked {
            writeln!(
                out,
                "{}   {} {}{} {}",
                "".color(theme.graph),
                ids.get_file(&change.path).color(theme.shortid).underline(),
                change.index.to_string().color(theme.staged),
                change.worktree.to_string().color(theme.unstaged),
                display_path(&change.path, &opts.cwd_prefix)
            )
            .unwrap();
        }
        if !untracked.is_empty() {
            render_untracked(out, &untracked, ids, opts);
        }
    }

    writeln!(out, "{}", "".color(theme.graph)).unwrap();
}

fn render_untracked(
    out: &mut String,
    untracked: &[&FileChange],
    ids: &IdAllocator,
    opts: &RenderOpts,
) {
    if untracked.len() > UNTRACKED_MULTICOLUMN_THRESHOLD
        && let Some(width) = opts.terminal_width
    {
        render_untracked_multicolumn(out, untracked, ids, width, &opts.theme, &opts.cwd_prefix);
        return;
    }
    render_untracked_single_column(out, untracked, ids, &opts.theme, &opts.cwd_prefix);
}

fn render_untracked_single_column(
    out: &mut String,
    untracked: &[&FileChange],
    ids: &IdAllocator,
    theme: &Theme,
    cwd_prefix: &str,
) {
    for change in untracked {
        writeln!(
            out,
            "{}   {} {} {}",
            "".color(theme.graph),
            ids.get_file(&change.path).color(theme.shortid).underline(),
            "".color(theme.untracked),
            display_path(&change.path, cwd_prefix)
        )
        .unwrap();
    }
}

/// Render untracked files in a multi-column grid (top-to-bottom, left-to-right).
fn render_untracked_multicolumn(
    out: &mut String,
    untracked: &[&FileChange],
    ids: &IdAllocator,
    term_width: u16,
    theme: &Theme,
    cwd_prefix: &str,
) {
    let available = (term_width as usize).saturating_sub(GRAPH_PREFIX_WIDTH);
    let separator = ""; // 5 display columns
    let separator_width: usize = 5;

    // Compute plain-text width of each entry: "{sid}  ⁕ {path}"
    let entry_widths: Vec<usize> = untracked
        .iter()
        .map(|f| {
            let sid = ids.get_file(&f.path);
            let disp = display_path(&f.path, cwd_prefix);
            sid.len() + 1 + 2 + 1 + disp.len()
        })
        .collect();

    let max_entry_width = entry_widths.iter().copied().max().unwrap_or(1);
    let col_slot = max_entry_width + separator_width;

    let num_cols = (available / col_slot).max(1).min(untracked.len());
    let num_rows = untracked.len().div_ceil(num_cols);

    for row in 0..num_rows {
        write!(out, "{}   ", "".color(theme.graph)).unwrap();
        for col in 0..num_cols {
            let idx = col * num_rows + row;
            if idx >= untracked.len() {
                break;
            }
            let f = untracked[idx];
            let sid = ids.get_file(&f.path);
            let disp = display_path(&f.path, cwd_prefix);

            write!(
                out,
                "{} {} {}",
                sid.color(theme.shortid).underline(),
                "".color(theme.untracked),
                disp
            )
            .unwrap();

            // Pad entry to max_entry_width, then add pipe separator
            let next_idx = (col + 1) * num_rows + row;
            if col + 1 < num_cols && next_idx < untracked.len() {
                let padding = max_entry_width.saturating_sub(entry_widths[idx]);
                write!(out, "{}{}", " ".repeat(padding), separator.color(theme.dim)).unwrap();
            }
        }
        writeln!(out).unwrap();
    }
}

#[allow(clippy::too_many_arguments)]
fn render_branch(
    out: &mut String,
    names: &[(String, Option<RemoteStatus>)],
    commits: &[CommitInfo],
    dot_color: Color,
    prev_stacked: bool,
    next_stacked: bool,
    more_sections: bool,
    ids: &IdAllocator,
    theme: &Theme,
    cwd_prefix: &str,
) {
    for (i, (name, remote)) in names.iter().enumerate() {
        let branch_id = ids.get_branch(name);
        let connector = if i == 0 && !prev_stacked {
            "│╭─"
        } else {
            "│├─"
        };
        let remote_indicator = match remote {
            Some(RemoteStatus::Synced) => format!(" {}", "".color(theme.remote_synced)),
            Some(RemoteStatus::Ahead) => format!(" {}", "".color(theme.remote_ahead)),
            Some(RemoteStatus::Gone) => format!(" {}", "".color(theme.remote_gone)),
            None => String::new(),
        };
        writeln!(
            out,
            "{} {} {}{}{}{}",
            connector.color(theme.graph),
            branch_id.color(theme.shortid).underline(),
            "[".color(theme.dim),
            name.color(theme.branch).bold(),
            "]".color(theme.dim),
            remote_indicator,
        )
        .unwrap();
    }

    for commit in commits {
        let sid = ids.get_commit(commit.oid);
        let rest: String = commit.short_id.chars().skip(sid.len()).collect();
        writeln!(
            out,
            "{}{}    {}{} {}",
            "".color(theme.graph),
            "".color(dot_color),
            sid.color(theme.shortid).underline(),
            rest.color(theme.dim),
            commit.message
        )
        .unwrap();
        for (i, file) in commit.files.iter().enumerate() {
            let file_sid = format!("{}:{}", sid, i);
            writeln!(
                out,
                "{}{}      {} {}{} {}",
                "".color(theme.graph),
                "".color(dot_color),
                file_sid.color(theme.shortid).underline(),
                file.index.to_string().color(theme.staged),
                file.worktree.to_string().color(theme.unstaged),
                display_path(&file.path, cwd_prefix)
            )
            .unwrap();
        }
    }
    if next_stacked {
        writeln!(out, "{}", "││".color(theme.graph)).unwrap();
    } else {
        writeln!(out, "{}", "├╯".color(theme.graph)).unwrap();
        if more_sections {
            writeln!(out, "{}", "".color(theme.graph)).unwrap();
        }
    }
}

fn render_loose(
    out: &mut String,
    commits: &[CommitInfo],
    more_sections: bool,
    ids: &IdAllocator,
    theme: &Theme,
    cwd_prefix: &str,
) {
    for commit in commits {
        let sid = ids.get_commit(commit.oid);
        let rest: String = commit.short_id.chars().skip(sid.len()).collect();
        writeln!(
            out,
            "{}    {}{} {}",
            "".color(theme.graph),
            sid.color(theme.shortid).underline(),
            rest.color(theme.dim),
            commit.message
        )
        .unwrap();
        for (i, file) in commit.files.iter().enumerate() {
            let file_sid = format!("{}:{}", sid, i);
            writeln!(
                out,
                "{}       {} {}{} {}",
                "".color(theme.graph),
                file_sid.color(theme.shortid).underline(),
                file.index.to_string().color(theme.staged),
                file.worktree.to_string().color(theme.unstaged),
                display_path(&file.path, cwd_prefix)
            )
            .unwrap();
        }
    }
    if more_sections {
        writeln!(out, "{}", "".color(theme.graph)).unwrap();
    }
}

fn render_upstream(out: &mut String, info: &UpstreamInfo, theme: &Theme) {
    if info.commits_ahead > 0 {
        let count_text = format!(
            "\u{23EB} {} new commit{}",
            info.commits_ahead,
            if info.commits_ahead == 1 { "" } else { "s" }
        )
        .color(theme.message);
        writeln!(
            out,
            "{}{}  {}{}{} {}",
            "".color(theme.graph),
            "".color(theme.graph),
            "[".color(theme.dim),
            info.label.color(theme.branch).bold(),
            "]".color(theme.dim),
            count_text
        )
        .unwrap();
        writeln!(
            out,
            "{} {} {} {} {}",
            "├╯".color(theme.graph),
            info.base_short_id.color(theme.dim),
            "(common base)".color(theme.label),
            info.base_date.color(theme.dim),
            info.base_message.color(theme.message)
        )
        .unwrap();
    } else {
        writeln!(
            out,
            "{} {} {} {}{}{} {}",
            "".color(theme.graph),
            info.base_short_id.color(theme.dim),
            "(upstream)".color(theme.label),
            "[".color(theme.dim),
            info.label.color(theme.branch).bold(),
            "]".color(theme.dim),
            info.base_message.color(theme.message)
        )
        .unwrap();
    }
}

fn render_context(out: &mut String, commits: &[ContextCommit], theme: &Theme) {
    for commit in commits {
        writeln!(
            out,
            "{} {} {} {}",
            "·".color(theme.dim),
            commit.short_hash.color(theme.dim),
            commit.date.color(theme.dim),
            commit.message.color(theme.message),
        )
        .unwrap();
    }
}

#[cfg(test)]
#[path = "graph_test.rs"]
mod tests;