brain: Fix status progress bar

218534ed0152362c9df571bb0ecdb90020d7a9cb
Alexis Sellier committed ago 1 parent 6cf3f799
crates/brain/src/backend.rs +47 -21
160 160
    comments: CommentStatus,
161 161
    /// Head commit timestamp used to order status rows.
162 162
    recency: CommitTime,
163 163
}
164 164
165 +
/// Commit hunk identity data used to summarize stored review progress.
166 +
struct SummaryCommit {
167 +
    /// Commit being summarized.
168 +
    commit: Commit,
169 +
    /// Stable patch id per hunk in the commit.
170 +
    patch_ids: BTreeMap<HunkId, PatchId>,
171 +
}
172 +
165 173
/// Stored comment status for one review identity.
166 174
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
167 175
pub enum CommentStatus {
168 176
    /// No comment blob exists.
169 177
    None,
430 438
        Ok(prefix.join(review))
431 439
    }
432 440
433 441
    /// Returns the target commit and accepted-hunk progress for this review.
434 442
    ///
435 -
    /// All commits' hunk patches are hashed by one `git patch-id` invocation.
443 +
    /// Progress is derived by the same tree reconciliation used by the
444 +
    /// interactive review, so it can handle multi-commit ranges whose accepted
445 +
    /// tree diff no longer has the same hunk shape as the original commits.
436 446
    fn summary(&self, review: &str) -> Result<(Oid, ReviewProgress), Error> {
437 447
        let base = Rev::from(self.base);
438 448
        let target = Rev::try_from(review)
439 449
            .map_err(|error| Error::Usage(format!("target revision is invalid: {error}")))?;
440 450
        let head = self.repository.commit_id(&target)?;
441 -
        let mut patches = Vec::new();
451 +
        let commits = self.summary_commits(&base, &target)?;
452 +
        let hunks = commits
453 +
            .iter()
454 +
            .map(|summary| CommitHunks {
455 +
                commit: &summary.commit,
456 +
                hunks: Some(&summary.patch_ids),
457 +
            })
458 +
            .collect::<Vec<_>>();
459 +
        let reconciled = self.reconcile(&hunks)?;
460 +
        let accepted = reconciled.iter().map(|state| state.accepted.len()).sum();
461 +
        let total = hunks
462 +
            .iter()
463 +
            .filter_map(|commit| commit.hunks)
464 +
            .map(BTreeMap::len)
465 +
            .sum();
466 +
467 +
        Ok((head, ReviewProgress { accepted, total }))
468 +
    }
442 469
443 -
        for (id, _) in self.repository.commits(&base, &target)? {
444 -
            let bytes = self.repository.commit_diff(id)?;
470 +
    /// Returns commits and stable hunk patch ids for status progress.
471 +
    fn summary_commits(&self, base: &Rev, target: &Rev) -> Result<Vec<SummaryCommit>, Error> {
472 +
        let commits = self
473 +
            .repository
474 +
            .commits(base, target)?
475 +
            .into_iter()
476 +
            .map(|(id, summary)| Commit {
477 +
                id,
478 +
                summary,
479 +
                stats: DiffStat::default(),
480 +
            });
481 +
        let mut summaries = Vec::with_capacity(commits.len());
482 +
483 +
        for commit in commits {
484 +
            let bytes = self.repository.commit_diff(commit.id)?;
445 485
            let document = DiffDocument::decode_bytes(&bytes)?.split_hunks();
446 -
            for (_, patch) in hunk_identity_patches(&document)? {
447 -
                patches.push(patch);
448 -
            }
486 +
            let patch_ids = self.patch_ids(&document)?;
487 +
            summaries.push(SummaryCommit { commit, patch_ids });
449 488
        }
450 489
451 -
        let original = self.repository.patch_ids(&patches)?;
452 -
        let accepted_ids = self.accepted_patch_ids()?;
453 -
        let accepted = original
454 -
            .iter()
455 -
            .filter(|patch_id| accepted_ids.contains(*patch_id))
456 -
            .count();
457 -
458 -
        Ok((
459 -
            head,
460 -
            ReviewProgress {
461 -
                accepted,
462 -
                total: original.len(),
463 -
            },
464 -
        ))
490 +
        Ok(summaries)
465 491
    }
466 492
467 493
    /// Returns patch ids present in the stored accepted-state diff.
468 494
    fn accepted_patch_ids(&self) -> Result<BTreeSet<PatchId>, Error> {
469 495
        let patch = self.repository.diff(self.base, self.commit)?;
f +0 -0
crates/brain/src/session/status.rs +1 -6
298 298
pub(crate) fn progress_bar(
299 299
    progress: ReviewProgress,
300 300
    palette: DiffPalette,
301 301
    empty_fg: Color,
302 302
) -> Line {
303 -
    let filled = progress
304 -
        .accepted
305 -
        .saturating_mul(COMMIT_PROGRESS_BAR_WIDTH)
306 -
        .checked_div(progress.total)
307 -
        .unwrap_or(0)
308 -
        .min(COMMIT_PROGRESS_BAR_WIDTH);
303 +
    let filled = progress.filled(COMMIT_PROGRESS_BAR_WIDTH);
309 304
    let empty = COMMIT_PROGRESS_BAR_WIDTH - filled;
310 305
311 306
    Line::blank()
312 307
        .item(Label::new(&COMMIT_PROGRESS_BAR_GLYPH.repeat(filled)).fg(palette.positive_fg))
313 308
        .item(Label::new(&COMMIT_PROGRESS_BAR_GLYPH.repeat(empty)).fg(empty_fg))
crates/diff/src/decorate.rs +14 -0
62 62
impl ReviewProgress {
63 63
    /// Returns whether all reviewable hunks have been accepted.
64 64
    pub fn is_complete(self) -> bool {
65 65
        self.total > 0 && self.accepted >= self.total
66 66
    }
67 +
68 +
    /// Returns the filled cell count for a progress bar of the given width.
69 +
    ///
70 +
    /// Rounds up so any nonzero progress is visible.
71 +
    pub fn filled(self, width: usize) -> usize {
72 +
        if self.total == 0 {
73 +
            0
74 +
        } else {
75 +
            self.accepted
76 +
                .saturating_mul(width)
77 +
                .div_ceil(self.total)
78 +
                .min(width)
79 +
        }
80 +
    }
67 81
}
68 82
69 83
impl Ord for ReviewProgress {
70 84
    fn cmp(&self, other: &Self) -> Ordering {
71 85
        match (self.total == 0, other.total == 0) {
crates/diff/src/viewer.rs +1 -6
2068 2068
        Line::new(Label::new(&" ".repeat(padding))).extend(line.labels().iter().cloned())
2069 2069
    }
2070 2070
2071 2071
    /// Returns a compact file review progress bar.
2072 2072
    fn progress_bar(progress: ReviewProgress, palette: DiffPalette) -> Line {
2073 -
        let filled = progress
2074 -
            .accepted
2075 -
            .saturating_mul(FILE_PROGRESS_BAR_WIDTH)
2076 -
            .checked_div(progress.total)
2077 -
            .unwrap_or(0)
2078 -
            .min(FILE_PROGRESS_BAR_WIDTH);
2073 +
        let filled = progress.filled(FILE_PROGRESS_BAR_WIDTH);
2079 2074
        let empty = FILE_PROGRESS_BAR_WIDTH - filled;
2080 2075
2081 2076
        Line::blank()
2082 2077
            .item(Label::new(&FILE_PROGRESS_BAR_GLYPH.repeat(filled)).fg(palette.positive_fg))
2083 2078
            .item(Label::new(&FILE_PROGRESS_BAR_GLYPH.repeat(empty)).fg(colors::FAINT))
a.rs +0 -0
a.rs +0 -0
b.rs +0 -0
a.rs +0 -0
a.rs +0 -0
a.rs +0 -0
b.rs +0 -0
crates/diff/tests/golden/sticky_header_before_next_file_64x5.txt +0 -0
src/a.rs +0 -0
src/b.rs +0 -0
README.md +0 -0
src/app/a.rs +0 -0
src/lib/b.rs +0 -0
README.md +0 -0
a.rs +0 -0
b.rs +0 -0
assets/css/style.css +0 -0
assets/css/style.css +0 -0
first.rs +0 -0
second.rs +0 -0
a.rs +0 -0
a.rs +0 -0
a.rs +0 -0
a.rs +0 -0
b.rs +0 -0