brain: Show branch pointers in history

8de202df02c5cf301957bdede52c73ccc2174fed
List local branch names beside each commit whose tip points at that commit.

Assisted-by: Codex:gpt-5.6-sol
Alexis Sellier committed ago 1 parent 1f98c19f
crates/brain/src/backend.rs +17 -2
47 47
    pub id: Oid,
48 48
    /// First line of the commit message.
49 49
    pub summary: String,
50 50
    /// Author name without an email address.
51 51
    pub author: String,
52 +
    /// Local branches whose tips point to this commit.
53 +
    pub branches: Vec<String>,
52 54
    /// Newest author or committer timestamp.
53 55
    pub recency: CommitTime,
54 56
    /// Aggregated diff size, shown in the commit list.
55 57
    pub stats: DiffStat,
56 58
}
66 68
67 69
    /// Joins commit metadata with bulk-fetched diff stats and recency.
68 70
    fn with_stats(
69 71
        repository: &git::Repository,
70 72
        commits: Vec<(Oid, String)>,
73 +
        pointers: impl IntoIterator<Item = (Oid, String)>,
71 74
    ) -> Result<Vec<Self>, Error> {
72 75
        let ids = commits.iter().map(|(id, _)| *id).collect::<Vec<_>>();
73 76
        let stats = repository.commit_stats(&ids)?;
74 77
        let metadata = repository.commit_metadata(&ids)?;
78 +
        let mut branches = BTreeMap::<Oid, Vec<String>>::new();
79 +
        for (head, name) in pointers {
80 +
            branches.entry(head).or_default().push(name);
81 +
        }
75 82
76 83
        Ok(commits
77 84
            .into_iter()
78 85
            .zip(stats)
79 86
            .zip(metadata)
80 87
            .map(|(((id, summary), stats), (author, recency))| Self {
81 88
                id,
82 89
                summary,
83 90
                author,
91 +
                branches: branches.remove(&id).unwrap_or_default(),
84 92
                recency,
85 93
                stats,
86 94
            })
87 95
            .collect())
88 96
    }
775 783
            .into_iter()
776 784
            .map(|(id, summary)| Commit {
777 785
                id,
778 786
                summary,
779 787
                author: String::new(),
788 +
                branches: Vec::new(),
780 789
                recency: CommitTime::default(),
781 790
                stats: DiffStat::default(),
782 791
            });
783 792
        let mut summaries = Vec::with_capacity(commits.len());
784 793
1199 1208
1200 1209
    fn commits(&self) -> Result<Vec<Commit>, Error> {
1201 1210
        let pairs = self
1202 1211
            .repository
1203 1212
            .commits(&self.review.base, &self.review.target)?;
1204 -
        Commit::with_stats(&self.repository, pairs)
1213 +
        let branches = self
1214 +
            .repository
1215 +
            .branches()?
1216 +
            .into_iter()
1217 +
            .map(|branch| (branch.head, branch.name));
1218 +
        Commit::with_stats(&self.repository, pairs, branches)
1205 1219
    }
1206 1220
1207 1221
    fn commit_header(&self, commit: Oid) -> Result<CommitHeader, Error> {
1208 1222
        let bytes = self.repository.commit_header(commit)?;
1209 1223
        let document = DiffDocument::decode_bytes(&bytes)?;
1291 1305
        })
1292 1306
    }
1293 1307
1294 1308
    fn commits(&self) -> Result<Vec<Commit>, Error> {
1295 1309
        let commits = self.jj.commits(&self.review.base, &self.review.target)?;
1296 -
        Commit::with_stats(&self.repository, commits)
1310 +
        let bookmarks = self.jj.bookmarks()?;
1311 +
        Commit::with_stats(&self.repository, commits, bookmarks)
1297 1312
    }
1298 1313
1299 1314
    fn commit_header(&self, commit: Oid) -> Result<CommitHeader, Error> {
1300 1315
        let bytes = self.repository.commit_header(commit)?;
1301 1316
        let document = DiffDocument::decode_bytes(&bytes)?;
f +0 -0
crates/brain/src/session.rs +2 -2
1019 1019
                let now = current_timestamp();
1020 1020
                let last = self.commits.len() - 1;
1021 1021
                self.terminal.draw(|frame| {
1022 1022
                    let (body, chrome_area) = Chrome::areas(frame.area);
1023 1023
                    let cols = ScrollView::body_cols(body);
1024 -
                    let mut author_table = Table::<10, Line>::new(TableOptions {
1024 +
                    let mut author_table = Table::<11, Line>::new(TableOptions {
1025 1025
                        spacing: 2,
1026 1026
                        ..TableOptions::default()
1027 1027
                    });
1028 1028
                    for (index, slot) in self.commits.iter().enumerate() {
1029 1029
                        let graph = match (index, last) {
1037 1037
                        ));
1038 1038
                    }
1039 1039
                    let rows = if author_table.size(Constraint::UNBOUNDED).cols <= cols {
1040 1040
                        author_table.lines(Constraint::max(Size::new(cols, usize::MAX)))
1041 1041
                    } else {
1042 -
                        let mut table = Table::<9, Line>::new(TableOptions {
1042 +
                        let mut table = Table::<10, Line>::new(TableOptions {
1043 1043
                            spacing: 2,
1044 1044
                            ..TableOptions::default()
1045 1045
                        });
1046 1046
                        for (index, slot) in self.commits.iter().enumerate() {
1047 1047
                            let graph = match (index, last) {
crates/brain/src/session/status.rs +56 -15
356 356
pub(super) fn commit_row(
357 357
    slot: &CommitSlot,
358 358
    palette: DiffPalette,
359 359
    graph: &str,
360 360
    now: u64,
361 -
) -> [Line; 9] {
362 -
    let [graph, id, summary, insertions, deletions, files, status, progress, _, age] =
363 -
        commit_row_with_author(slot, palette, graph, now);
361 +
1: ) -> [Line; 10] {
362 +
    let [
363 +
        graph, id, insertions, deletions, files, status, progress, branches, _, age, summary,
364 +
    ] = commit_row_with_author(slot, palette, graph, now);
364 365
365 366
    [
366 -
        graph, id, summary, insertions, deletions, files, status, progress, age,
367 +
        graph, id, insertions, deletions, files, status, progress, branches, age, summary,
368 +
2:         Line::new(Label::new(&format!("+{}", stats.insertions)).fg(palette.positive_fg)),
369 +
        Line::new(Label::new(&format!("-{}", stats.deletions)).fg(palette.negative_fg)),
370 +
        Line::new(Label::new(&file_count(stats.files_changed)).fg(FOOTER_FG)),
371 +
3:         Line::new(slot.commit.summary.as_str()),
372 +
4:         let mut table = Table::<11, Line>::new(TableOptions {
367 373
    ]
368 374
}
369 375
370 376
/// Returns one commit-table row including an author column.
371 377
pub(super) fn commit_row_with_author(
372 378
    slot: &CommitSlot,
373 379
    palette: DiffPalette,
374 380
    graph: &str,
375 381
    now: u64,
376 -
) -> [Line; 10] {
382 +
) -> [Line; 11] {
377 383
    let stats = slot.commit.stats;
378 384
    let short = slot.commit.id.short();
379 385
    let status = if slot.progress.is_complete() {
380 386
        Label::from_char(REVIEWED_MARK).fg(palette.positive_fg)
381 387
    } else {
382 388
        Label::space()
383 389
    };
384 390
    [
385 391
        Line::new(Label::new(graph).fg(colors::fixed::MUTED)),
386 392
        Line::new(Label::new(&short).fg(colors::rgb::ORANGE)),
387 -
        Line::new(slot.commit.summary.as_str()),
388 -
        Line::new(Label::new(&format!("+{}", stats.insertions)).fg(palette.positive_fg)),
393 +
1: ) -> [Line; 10] {
394 +
    let [
395 +
        graph, id, insertions, deletions, files, status, progress, branches, _, age, summary,
396 +
    ] = commit_row_with_author(slot, palette, graph, now);
397 +
398 +
    [
399 +
        graph, id, insertions, deletions, files, status, progress, branches, age, summary,
400 +
2:         Line::new(Label::new(&format!("+{}", stats.insertions)).fg(palette.positive_fg)),
389 401
        Line::new(Label::new(&format!("-{}", stats.deletions)).fg(palette.negative_fg)),
390 402
        Line::new(Label::new(&file_count(stats.files_changed)).fg(FOOTER_FG)),
403 +
3:         Line::new(slot.commit.summary.as_str()),
404 +
4:         let mut table = Table::<11, Line>::new(TableOptions {
391 405
        Line::new(status),
392 406
        progress_bar(slot.progress, palette, FOOTER_FG),
407 +
        Line::new(Label::new(&slot.commit.branches.join(", ")).fg(colors::rgb::TEAL)),
393 408
        Line::new(Label::new(slot.commit.author.as_str()).fg(colors::fixed::MUTED)),
394 409
        Line::new(
395 410
            Label::from(relative_time(
396 411
                slot.commit.recency.seconds.into(),
397 412
                now.into(),
398 413
            ))
399 414
            .fg(colors::fixed::DIM),
400 415
        ),
416 +
1: ) -> [Line; 10] {
417 +
    let [
418 +
        graph, id, insertions, deletions, files, status, progress, branches, _, age, summary,
419 +
    ] = commit_row_with_author(slot, palette, graph, now);
420 +
421 +
    [
422 +
        graph, id, insertions, deletions, files, status, progress, branches, age, summary,
423 +
2:         Line::new(Label::new(&format!("+{}", stats.insertions)).fg(palette.positive_fg)),
424 +
        Line::new(Label::new(&format!("-{}", stats.deletions)).fg(palette.negative_fg)),
425 +
        Line::new(Label::new(&file_count(stats.files_changed)).fg(FOOTER_FG)),
426 +
3:         Line::new(slot.commit.summary.as_str()),
427 +
4:         let mut table = Table::<11, Line>::new(TableOptions {
401 428
    ]
402 429
}
403 430
404 431
/// Renders one stored comment as a bordered card.
405 432
pub(super) fn comment_card_lines(comment: &Comment, selected: bool, cols: usize) -> Vec<Line> {
594 621
        assert_eq!(buffer.get(79, 0).unwrap().glyph(), " ");
595 622
        assert_eq!(buffer.get(79, 0).unwrap().style().bg_color(), FOOTER_BG);
596 623
    }
597 624
598 625
    #[test]
599 -
    fn commit_row_keeps_review_status_at_pre_age_width() {
626 +
    fn commit_row_keeps_status_and_pointers_before_summary() {
600 627
        let now = 2_000_000_000;
601 628
        let recency = CommitTime {
602 629
            seconds: now - 3_600,
603 630
        };
604 631
        let slot = CommitSlot {
605 632
            commit: Commit {
606 633
                id: "0123456789abcdef0123456789abcdef01234567"
607 634
                    .parse()
608 635
                    .unwrap(),
609 -
                summary: "A commit summary".to_owned(),
636 +
                summary: "A commit summary that cannot fit in the remaining width".to_owned(),
610 637
                author: "Alice".to_owned(),
638 +
                branches: vec!["main".to_owned()],
611 639
                recency,
612 640
                stats: DiffStat {
613 641
                    files_changed: 1,
614 642
                    insertions: 2,
615 643
                    deletions: 3,
619 647
                accepted: 1,
620 648
                total: 1,
621 649
            },
622 650
            loaded: None,
623 651
        };
624 -
        let age = relative_time(recency.seconds.into(), now.into());
625 -
        let mut table = Table::<10, Line>::new(TableOptions {
652 +
1: ) -> [Line; 10] {
653 +
    let [
654 +
        graph, id, insertions, deletions, files, status, progress, branches, _, age, summary,
655 +
    ] = commit_row_with_author(slot, palette, graph, now);
656 +
657 +
    [
658 +
        graph, id, insertions, deletions, files, status, progress, branches, age, summary,
659 +
2:         Line::new(Label::new(&format!("+{}", stats.insertions)).fg(palette.positive_fg)),
660 +
        Line::new(Label::new(&format!("-{}", stats.deletions)).fg(palette.negative_fg)),
661 +
        Line::new(Label::new(&file_count(stats.files_changed)).fg(FOOTER_FG)),
662 +
3:         Line::new(slot.commit.summary.as_str()),
663 +
4:         let mut table = Table::<11, Line>::new(TableOptions {
626 664
            spacing: 2,
627 665
            ..TableOptions::default()
628 666
        });
629 667
        table.push(commit_row_with_author(
630 668
            &slot,
631 669
            DiffPalette::default(),
632 670
            "──●",
633 671
            now as u64,
634 672
        ));
635 -
        let pre_age_cols =
636 -
            table.size(Constraint::UNBOUNDED).cols - age.chars().count() - 2;
673 +
        let constrained_cols = table.size(Constraint::UNBOUNDED).cols
674 +
            - slot.commit.summary.chars().count()
675 +
            + 1;
637 676
638 677
        let line = table
639 -
            .lines(Constraint::tight(pre_age_cols))
678 +
            .lines(Constraint::tight(constrained_cols))
640 679
            .pop()
641 680
            .unwrap()
642 681
            .to_string();
643 682
644 683
        assert!(line.contains(REVIEWED_MARK), "line: {line:?}");
684 +
        assert!(line.contains("main"), "line: {line:?}");
685 +
        assert!(!line.contains(&slot.commit.summary), "line: {line:?}");
645 686
646 -
        let mut hidden = Table::<9, Line>::new(TableOptions {
687 +
        let mut hidden = Table::<10, Line>::new(TableOptions {
647 688
            spacing: 2,
648 689
            ..TableOptions::default()
649 690
        });
650 691
        hidden.push(commit_row(&slot, DiffPalette::default(), "──●", now as u64));
651 692
        assert_eq!(
crates/repo/src/jj.rs +26 -0
44 44
            commits.push((id, summary.trim_start().to_owned()));
45 45
        }
46 46
        Ok(commits)
47 47
    }
48 48
49 +
    /// Returns local bookmark names paired with their target commits.
50 +
    pub fn bookmarks(&self) -> Result<Vec<(Oid, String)>, RepoError> {
51 +
        let stdout = self.jj_bytes(&[
52 +
            "log",
53 +
            "--no-graph",
54 +
            "-r",
55 +
            "bookmarks()",
56 +
            "-T",
57 +
            "commit_id ++ \"\\t\" ++ local_bookmarks.map(|bookmark| bookmark.name()).join(\"\\t\") ++ \"\\n\"",
58 +
        ])?;
59 +
        let text = String::from_utf8(stdout).map_err(|e| RepoError::Git(e.to_string()))?;
60 +
        let mut bookmarks = Vec::new();
61 +
        for line in text.lines() {
62 +
            let mut fields = line.split('\t');
63 +
            let id = fields
64 +
                .next()
65 +
                .ok_or_else(|| RepoError::Git("malformed bookmark line from jj log".to_owned()))?
66 +
                .parse()
67 +
                .map_err(|_| RepoError::Git("invalid oid from jj log".to_owned()))?;
68 +
            for name in fields {
69 +
                bookmarks.push((id, name.to_owned()));
70 +
            }
71 +
        }
72 +
        Ok(bookmarks)
73 +
    }
74 +
49 75
    /// Returns `commit`'s diff in unified Git format.
50 76
    pub fn commit_diff(&self, commit: Oid) -> Result<Vec<u8>, RepoError> {
51 77
        let commit = commit.to_string();
52 78
        self.jj_bytes(&["diff", "--git", "-r", &commit])
53 79
    }