brain: Decode commits before showing history
d686f085d119942ed04bdfe6c4183a2bae8d3d96
History status needs every commit's decoded document. Load all lazy commit slots through the session's canonical path and reconcile their review state before recomputing status. This prevents Tab from treating unopened commits as an internal decoding failure after a review change invalidates the cached status. Assisted-by: Codex:gpt-5.6-sol
1 parent
5d5d2b2d
crates/brain/src/backend.rs
+92 -103
| 243 | 243 | } |
|
| 244 | 244 | ||
| 245 | 245 | let mut reconciled = Vec::with_capacity(commits.len()); |
|
| 246 | 246 | for commit in commits { |
|
| 247 | 247 | let (progress, accepted) = match commit.hunks { |
|
| 248 | + | Some(hunks) if hunks.is_empty() => ( |
|
| 249 | + | // A hunkless binary or metadata-only change has no |
|
| 250 | + | // acceptance action, so its sole review unit is done. |
|
| 251 | + | ReviewProgress { |
|
| 252 | + | accepted: 1, |
|
| 253 | + | total: 1, |
|
| 254 | + | }, |
|
| 255 | + | BTreeSet::new(), |
|
| 256 | + | ), |
|
| 248 | 257 | Some(hunks) => { |
|
| 249 | - | let accepted = hunks |
|
| 250 | - | .iter() |
|
| 251 | - | .filter(|(_, identity)| accepted_state.contains(identity)) |
|
| 252 | - | .map(|(id, _)| *id) |
|
| 253 | - | .collect::<BTreeSet<_>>(); |
|
| 254 | - | ( |
|
| 255 | - | ReviewProgress { |
|
| 256 | - | accepted: accepted |
|
| 257 | - | .iter() |
|
| 258 | - | .filter_map(|id| hunks.get(id)) |
|
| 259 | - | .map(|identity| identity.lines as usize) |
|
| 260 | - | .sum(), |
|
| 261 | - | total: hunks.values().map(|identity| identity.lines as usize).sum(), |
|
| 262 | - | }, |
|
| 263 | - | accepted, |
|
| 264 | - | ) |
|
| 258 | + | let mut progress = ReviewProgress::default(); |
|
| 259 | + | let mut accepted = BTreeSet::new(); |
|
| 260 | + | for (id, identity) in hunks { |
|
| 261 | + | let lines = identity.lines as usize; |
|
| 262 | + | progress.total += lines; |
|
| 263 | + | if accepted_state.contains(identity) { |
|
| 264 | + | progress.accepted += lines; |
|
| 265 | + | accepted.insert(*id); |
|
| 266 | + | } |
|
| 267 | + | } |
|
| 268 | + | (progress, accepted) |
|
| 265 | 269 | } |
|
| 266 | 270 | None => { |
|
| 267 | 271 | let progress = totals.get(&commit.commit).map_or_else( |
|
| 268 | 272 | ReviewProgress::default, |
|
| 269 | 273 | |(accepted, total)| ReviewProgress { |
| 296 | 300 | pub struct CommitStatus { |
|
| 297 | 301 | /// Accepted-line progress for the commit. |
|
| 298 | 302 | pub progress: ReviewProgress, |
|
| 299 | 303 | /// Diff statistics from hunks that remain unaccepted. |
|
| 300 | 304 | pub stats: DiffStat, |
|
| 301 | - | /// Exact accepted-state identity per hunk. |
|
| 302 | - | pub identities: BTreeMap<HunkId, AcceptedHunk>, |
|
| 305 | + | } |
|
| 306 | + | ||
| 307 | + | impl HistoryStatus { |
|
| 308 | + | /// Calculates history status from identities cached by the session. |
|
| 309 | + | pub(crate) fn for_commits( |
|
| 310 | + | brain: &Brain, |
|
| 311 | + | commits: &[(Oid, &DiffDocument, &BTreeMap<HunkId, AcceptedHunk>)], |
|
| 312 | + | ) -> Self { |
|
| 313 | + | let statuses = brain.commit_statuses(commits); |
|
| 314 | + | let mut progress = ReviewProgress::default(); |
|
| 315 | + | let mut stats = DiffStat::default(); |
|
| 316 | + | for status in statuses.values() { |
|
| 317 | + | progress.accepted += status.progress.accepted; |
|
| 318 | + | progress.total += status.progress.total; |
|
| 319 | + | stats.files_changed += status.stats.files_changed; |
|
| 320 | + | stats.insertions += status.stats.insertions; |
|
| 321 | + | stats.deletions += status.stats.deletions; |
|
| 322 | + | } |
|
| 323 | + | Self { |
|
| 324 | + | progress, |
|
| 325 | + | stats, |
|
| 326 | + | commits: statuses, |
|
| 327 | + | } |
|
| 328 | + | } |
|
| 303 | 329 | } |
|
| 304 | 330 | /// Stored status for one review identity. |
|
| 305 | 331 | #[derive(Clone, Debug, Eq, PartialEq)] |
|
| 306 | 332 | pub struct BrainStatus { |
|
| 307 | 333 | /// Head name displayed to the user. |
| 414 | 440 | ||
| 415 | 441 | Some(Self::new(base, target)) |
|
| 416 | 442 | } |
|
| 417 | 443 | } |
|
| 418 | 444 | ||
| 445 | + | /// Returns the user-facing label for `review`. |
|
| 446 | + | fn range_label(review: &ReviewIdentity) -> String { |
|
| 447 | + | format!("{} .. {}", review.base.as_str(), review.target.as_str()) |
|
| 448 | + | } |
|
| 449 | + | ||
| 419 | 450 | /// Encodes exact accepted-hunk state in a synthetic commit message. |
|
| 420 | 451 | fn encode_state(accepted: &Accepted) -> String { |
|
| 421 | 452 | let mut message = format!("{SYNTHETIC_BRAIN_MESSAGE}\n\n{STATE_SECTION}\n"); |
|
| 422 | 453 | for hunk in accepted { |
|
| 423 | 454 | message.push_str(&format!( |
| 502 | 533 | /// Returns the user-facing revision range under review. |
|
| 503 | 534 | fn range_label(&self) -> String; |
|
| 504 | 535 | /// Returns the reviewed commits in `base..target`, oldest first. |
|
| 505 | 536 | fn commits(&self) -> Result<Vec<Commit>, Error>; |
|
| 506 | 537 | ||
| 507 | - | /// Resolves the target commit of the reviewed range. |
|
| 508 | - | fn target(&self) -> Result<Oid, Error>; |
|
| 509 | - | ||
| 510 | - | /// Returns global and per-commit progress and remaining statistics. |
|
| 511 | - | fn history_status( |
|
| 512 | - | &self, |
|
| 513 | - | brain: &Brain, |
|
| 514 | - | commits: &[Oid], |
|
| 515 | - | documents: &[&DiffDocument], |
|
| 516 | - | ) -> Result<HistoryStatus, Error> { |
|
| 517 | - | let statuses = brain.commit_statuses(commits, documents)?; |
|
| 518 | - | let progress = statuses |
|
| 519 | - | .values() |
|
| 520 | - | .fold(ReviewProgress::default(), |mut progress, status| { |
|
| 521 | - | progress.accepted += status.progress.accepted; |
|
| 522 | - | progress.total += status.progress.total; |
|
| 523 | - | progress |
|
| 524 | - | }); |
|
| 525 | - | Ok(HistoryStatus { |
|
| 526 | - | progress, |
|
| 527 | - | stats: brain.remaining_stats(self.target()?)?, |
|
| 528 | - | commits: statuses, |
|
| 529 | - | }) |
|
| 530 | - | } |
|
| 531 | - | ||
| 532 | 538 | /// Returns `commit`'s header. |
|
| 533 | 539 | fn commit_header(&self, commit: Oid) -> Result<CommitHeader, Error>; |
|
| 534 | 540 | ||
| 535 | 541 | /// Returns `commit`'s file diffs against its parent. |
|
| 536 | 542 | fn file_diffs(&self, commit: Oid) -> Result<Vec<FileDiff>, Error>; |
| 852 | 858 | summaries.push(SummaryCommit { commit, identities }); |
|
| 853 | 859 | } |
|
| 854 | 860 | ||
| 855 | 861 | Ok(summaries) |
|
| 856 | 862 | } |
|
| 857 | - | ||
| 858 | - | /// Returns review progress and remaining statistics for each commit. |
|
| 863 | + | /// Returns review progress and remaining statistics for cached commit identities. |
|
| 859 | 864 | fn commit_statuses( |
|
| 860 | 865 | &self, |
|
| 861 | - | commits: &[Oid], |
|
| 862 | - | documents: &[&DiffDocument], |
|
| 863 | - | ) -> Result<BTreeMap<Oid, CommitStatus>, Error> { |
|
| 864 | - | let identities = commits |
|
| 865 | - | .iter() |
|
| 866 | - | .zip(documents) |
|
| 867 | - | .map(|(commit, document)| self.hunk_identities(document, *commit)) |
|
| 868 | - | .collect::<Result<Vec<_>, _>>()?; |
|
| 866 | + | commits: &[(Oid, &DiffDocument, &BTreeMap<HunkId, AcceptedHunk>)], |
|
| 867 | + | ) -> BTreeMap<Oid, CommitStatus> { |
|
| 869 | 868 | let hunks = commits |
|
| 870 | 869 | .iter() |
|
| 871 | - | .zip(&identities) |
|
| 872 | - | .map(|(commit, identities)| CommitHunks { |
|
| 870 | + | .map(|(commit, _, identities)| CommitHunks { |
|
| 873 | 871 | commit: *commit, |
|
| 874 | 872 | hunks: Some(identities), |
|
| 875 | 873 | }) |
|
| 876 | 874 | .collect::<Vec<_>>(); |
|
| 877 | 875 | let reconciled = self.reconcile(&hunks); |
|
| 878 | - | let mut statuses = BTreeMap::new(); |
|
| 879 | - | for (((commit, document), reconciled), identities) in commits |
|
| 876 | + | commits |
|
| 880 | 877 | .iter() |
|
| 881 | - | .zip(documents) |
|
| 882 | 878 | .zip(reconciled) |
|
| 883 | - | .zip(identities) |
|
| 884 | - | { |
|
| 885 | - | let stats = commit_remaining_stats(document, &reconciled.accepted); |
|
| 886 | - | statuses.insert( |
|
| 887 | - | *commit, |
|
| 888 | - | CommitStatus { |
|
| 889 | - | progress: reconciled.progress, |
|
| 890 | - | stats, |
|
| 891 | - | identities, |
|
| 892 | - | }, |
|
| 893 | - | ); |
|
| 894 | - | } |
|
| 895 | - | Ok(statuses) |
|
| 896 | - | } |
|
| 897 | - | ||
| 898 | - | /// Returns the diff statistics still needed to reach `target`. |
|
| 899 | - | fn remaining_stats(&self, target: Oid) -> Result<DiffStat, Error> { |
|
| 900 | - | let document = DiffDocument::decode_str(&self.repository.diff(self.commit, target)?)?; |
|
| 901 | - | let stats = document.stats(); |
|
| 902 | - | Ok(DiffStat { |
|
| 903 | - | files_changed: stats.files_changed, |
|
| 904 | - | insertions: stats.insertions, |
|
| 905 | - | deletions: stats.deletions, |
|
| 906 | - | }) |
|
| 879 | + | .map(|((commit, document, _), reconciled)| { |
|
| 880 | + | ( |
|
| 881 | + | *commit, |
|
| 882 | + | CommitStatus { |
|
| 883 | + | progress: reconciled.progress, |
|
| 884 | + | stats: commit_remaining_stats(document, &reconciled.accepted), |
|
| 885 | + | }, |
|
| 886 | + | ) |
|
| 887 | + | }) |
|
| 888 | + | .collect() |
|
| 907 | 889 | } |
|
| 908 | 890 | ||
| 909 | 891 | /// Returns exact accepted-state identities in document order. |
|
| 910 | 892 | /// |
|
| 911 | 893 | /// `document` must be `commit`'s own split-hunk diff: the identities carry |
| 1226 | 1208 | pub fn reconcile(&self, commits: &[CommitHunks<'_>]) -> Vec<Reconciled> { |
|
| 1227 | 1209 | Reconciled::for_commits(&self.accepted, commits) |
|
| 1228 | 1210 | } |
|
| 1229 | 1211 | } |
|
| 1230 | 1212 | ||
| 1231 | - | /// Returns unaccepted hunk statistics while retaining hunkless file sections. |
|
| 1232 | - | fn commit_remaining_stats(document: &DiffDocument, accepted: &BTreeSet<HunkId>) -> DiffStat { |
|
| 1213 | + | /// Returns statistics for the unaccepted hunks in `document`. |
|
| 1214 | + | pub fn commit_remaining_stats(document: &DiffDocument, accepted: &BTreeSet<HunkId>) -> DiffStat { |
|
| 1233 | 1215 | let mut stats = DiffStat::default(); |
|
| 1234 | 1216 | for file in &document.files { |
|
| 1235 | - | let mut remaining = file.hunks.is_empty(); |
|
| 1217 | + | let mut remaining = false; |
|
| 1236 | 1218 | for hunk in &file.hunks { |
|
| 1237 | 1219 | if accepted.contains(&hunk.id) { |
|
| 1238 | 1220 | continue; |
|
| 1239 | 1221 | } |
|
| 1240 | 1222 | remaining = true; |
| 1327 | 1309 | } |
|
| 1328 | 1310 | } |
|
| 1329 | 1311 | ||
| 1330 | 1312 | impl Backend for GitBackend { |
|
| 1331 | 1313 | fn range_label(&self) -> String { |
|
| 1332 | - | format!( |
|
| 1333 | - | "{} .. {}", |
|
| 1334 | - | self.review.base.as_str(), |
|
| 1335 | - | self.review.target.as_str() |
|
| 1336 | - | ) |
|
| 1337 | - | } |
|
| 1338 | - | ||
| 1339 | - | fn target(&self) -> Result<Oid, Error> { |
|
| 1340 | - | Ok(self.repository.commit_id(&self.review.target)?) |
|
| 1314 | + | range_label(&self.review) |
|
| 1341 | 1315 | } |
|
| 1342 | 1316 | ||
| 1343 | 1317 | fn commits(&self) -> Result<Vec<Commit>, Error> { |
|
| 1344 | 1318 | let pairs = self |
|
| 1345 | 1319 | .repository |
| 1419 | 1393 | } |
|
| 1420 | 1394 | } |
|
| 1421 | 1395 | ||
| 1422 | 1396 | impl Backend for JjBackend { |
|
| 1423 | 1397 | fn range_label(&self) -> String { |
|
| 1424 | - | format!( |
|
| 1425 | - | "{} .. {}", |
|
| 1426 | - | self.review.base.as_str(), |
|
| 1427 | - | self.review.target.as_str() |
|
| 1428 | - | ) |
|
| 1429 | - | } |
|
| 1430 | - | ||
| 1431 | - | fn target(&self) -> Result<Oid, Error> { |
|
| 1432 | - | Ok(self.jj.resolve(&self.review.target)?) |
|
| 1398 | + | range_label(&self.review) |
|
| 1433 | 1399 | } |
|
| 1434 | 1400 | ||
| 1435 | 1401 | fn commits(&self) -> Result<Vec<Commit>, Error> { |
|
| 1436 | 1402 | let commits = self.jj.commits(&self.review.base, &self.review.target)?; |
|
| 1437 | 1403 | let bookmarks = self.jj.bookmarks()?; |
| 1604 | 1570 | let full = Reconciled::for_commits(&Accepted::from([first, second]), &commits); |
|
| 1605 | 1571 | assert!(full[0].progress.is_complete()); |
|
| 1606 | 1572 | assert!(full[0].accepted.is_empty()); |
|
| 1607 | 1573 | } |
|
| 1608 | 1574 | ||
| 1575 | + | #[test] |
|
| 1576 | + | fn hunkless_commit_is_complete_without_accepted_identity() { |
|
| 1577 | + | let commit = "1111111111111111111111111111111111111111".parse().unwrap(); |
|
| 1578 | + | let identities = BTreeMap::new(); |
|
| 1579 | + | let reconciled = Reconciled::for_commits( |
|
| 1580 | + | &Accepted::new(), |
|
| 1581 | + | &[CommitHunks { |
|
| 1582 | + | commit, |
|
| 1583 | + | hunks: Some(&identities), |
|
| 1584 | + | }], |
|
| 1585 | + | ); |
|
| 1586 | + | ||
| 1587 | + | assert!(reconciled[0].progress.is_complete()); |
|
| 1588 | + | assert_eq!( |
|
| 1589 | + | reconciled[0].progress, |
|
| 1590 | + | ReviewProgress { |
|
| 1591 | + | accepted: 1, |
|
| 1592 | + | total: 1, |
|
| 1593 | + | } |
|
| 1594 | + | ); |
|
| 1595 | + | assert!(reconciled[0].accepted.is_empty()); |
|
| 1596 | + | } |
|
| 1597 | + | ||
| 1609 | 1598 | #[test] |
|
| 1610 | 1599 | fn rebase_remaps_within_a_commit_before_across_commits() { |
|
| 1611 | 1600 | let moved = accepted_hunk("1111111111111111111111111111111111111111", "same", 0, 1); |
|
| 1612 | 1601 | // The commit still exists, but gained a hunk, so its stored identity no |
|
| 1613 | 1602 | // longer matches exactly. |
f
+0 -0
empty
+0 -0
crates/brain/src/session.rs
+30 -35
| 115 | 115 | let range = backend.range_label(); |
|
| 116 | 116 | let (mut slots, history_status) = if commits.len() > 1 { |
|
| 117 | 117 | let mut reviewed = Vec::with_capacity(commits.len()); |
|
| 118 | 118 | for commit in commits { |
|
| 119 | 119 | let document = backend.diff_document(commit.id)?.split_hunks(); |
|
| 120 | - | if document.files.iter().any(|file| !file.hunks.is_empty()) { |
|
| 121 | - | reviewed.push((commit, document)); |
|
| 122 | - | } |
|
| 120 | + | let identities = brain.hunk_identities(&document, commit.id)?; |
|
| 121 | + | reviewed.push((commit, document, identities)); |
|
| 123 | 122 | } |
|
| 124 | 123 | if reviewed.is_empty() { |
|
| 125 | 124 | return Err(Error::Empty); |
|
| 126 | 125 | } |
|
| 127 | - | let commit_ids = reviewed |
|
| 128 | - | .iter() |
|
| 129 | - | .map(|(commit, _)| commit.id) |
|
| 130 | - | .collect::<Vec<_>>(); |
|
| 131 | - | let documents = reviewed |
|
| 126 | + | let status_inputs = reviewed |
|
| 132 | 127 | .iter() |
|
| 133 | - | .map(|(_, document)| document) |
|
| 128 | + | .map(|(commit, document, identities)| (commit.id, document, identities)) |
|
| 134 | 129 | .collect::<Vec<_>>(); |
|
| 135 | - | let mut history = backend.history_status(&brain, &commit_ids, &documents)?; |
|
| 130 | + | let mut history = HistoryStatus::for_commits(&brain, &status_inputs); |
|
| 136 | 131 | let slots = reviewed |
|
| 137 | 132 | .into_iter() |
|
| 138 | - | .map(|(commit, document)| { |
|
| 133 | + | .map(|(commit, document, identities)| { |
|
| 139 | 134 | let status = history.commits.entry(commit.id).or_default(); |
|
| 140 | 135 | let progress = status.progress; |
|
| 141 | 136 | let remaining_stats = status.stats; |
|
| 142 | - | let identities = mem::take(&mut status.identities); |
|
| 143 | 137 | CommitSlot { |
|
| 144 | 138 | commit, |
|
| 145 | 139 | progress, |
|
| 146 | 140 | remaining_stats, |
|
| 147 | 141 | document: Some(document), |
| 278 | 272 | None => { |
|
| 279 | 273 | let document = match slot.document.take() { |
|
| 280 | 274 | Some(document) => document, |
|
| 281 | 275 | None => self.backend.diff_document(slot.commit.id)?.split_hunks(), |
|
| 282 | 276 | }; |
|
| 283 | - | if !document.files.iter().any(|file| !file.hunks.is_empty()) { |
|
| 284 | - | return Err(Error::Empty); |
|
| 285 | - | } |
|
| 286 | 277 | CommitReview::load( |
|
| 287 | 278 | &slot.commit, |
|
| 288 | 279 | document, |
|
| 289 | 280 | slot.identities.take(), |
|
| 290 | 281 | self.options.clone(), |
| 511 | 502 | let loaded = self.current()?; |
|
| 512 | 503 | let Some(file) = loaded.viewer.focused_file() else { |
|
| 513 | 504 | loaded.viewer.apply(DiffAction::NextHunk, area); |
|
| 514 | 505 | return Ok(()); |
|
| 515 | 506 | }; |
|
| 516 | - | let Some(file) = loaded.files.iter().find(|candidate| candidate.id == file) else { |
|
| 507 | + | let Some(file) = loaded |
|
| 508 | + | .document |
|
| 509 | + | .files |
|
| 510 | + | .iter() |
|
| 511 | + | .find(|candidate| candidate.id == file) |
|
| 512 | + | else { |
|
| 517 | 513 | return Ok(()); |
|
| 518 | 514 | }; |
|
| 519 | 515 | let hunk_ids = file.hunks.iter().map(|hunk| hunk.id).collect::<Vec<_>>(); |
|
| 520 | 516 | let accepted = loaded.review.accepts_all(hunk_ids.iter().copied()); |
|
| 521 | 517 | let hunks = hunk_ids |
| 562 | 558 | let mut patches = Vec::new(); |
|
| 563 | 559 | for (index, slot) in self.commits.iter().enumerate() { |
|
| 564 | 560 | let Some(loaded) = &slot.loaded else { |
|
| 565 | 561 | continue; |
|
| 566 | 562 | }; |
|
| 567 | - | for file in &loaded.files { |
|
| 563 | + | for file in &loaded.document.files { |
|
| 568 | 564 | let hunks = file |
|
| 569 | 565 | .hunks |
|
| 570 | 566 | .iter() |
|
| 571 | 567 | .filter(|hunk| { |
|
| 572 | 568 | loaded.review.is_accepted(hunk.id) && !rejected.contains(&(index, hunk.id)) |
| 604 | 600 | self.parked = None; |
|
| 605 | 601 | let mut options = self.options.clone(); |
|
| 606 | 602 | options.file_tree = FileTreeMode::Hidden; |
|
| 607 | 603 | ||
| 608 | 604 | let loaded = self.current()?; |
|
| 609 | - | let Some((file, focused)) = loaded.files.iter().find_map(|file| { |
|
| 605 | + | let Some((file, focused)) = loaded.document.files.iter().find_map(|file| { |
|
| 610 | 606 | let focused = file.hunks.iter().find(|candidate| candidate.id == hunk)?; |
|
| 611 | 607 | Some((file, focused)) |
|
| 612 | 608 | }) else { |
|
| 613 | 609 | return Ok(()); |
|
| 614 | 610 | }; |
| 622 | 618 | self.restart_highlighter()?; |
|
| 623 | 619 | ||
| 624 | 620 | Ok(()) |
|
| 625 | 621 | } |
|
| 626 | 622 | ||
| 627 | - | /// Loads and caches global progress and remaining statistics for history. |
|
| 623 | + | /// Caches global progress and remaining statistics without loading commits. |
|
| 628 | 624 | fn ensure_history_status(&mut self) -> Result<(), Error> { |
|
| 629 | 625 | if self.history_status.is_none() { |
|
| 630 | - | let commits = self |
|
| 631 | - | .commits |
|
| 632 | - | .iter() |
|
| 633 | - | .map(|slot| slot.commit.id) |
|
| 634 | - | .collect::<Vec<_>>(); |
|
| 635 | - | let documents = self |
|
| 626 | + | self.reconcile_state(); |
|
| 627 | + | let status_inputs = self |
|
| 636 | 628 | .commits |
|
| 637 | 629 | .iter() |
|
| 638 | 630 | .map(|slot| { |
|
| 639 | - | slot.loaded |
|
| 631 | + | let identities = slot |
|
| 632 | + | .identities |
|
| 640 | 633 | .as_ref() |
|
| 641 | - | .map(|loaded| loaded.viewer.document()) |
|
| 642 | - | .ok_or_else(|| Error::Git("history commit was not decoded".to_owned())) |
|
| 634 | + | .or_else(|| slot.loaded.as_ref().map(|loaded| &loaded.identities)); |
|
| 635 | + | let identities = identities.ok_or_else(|| { |
|
| 636 | + | Error::Git("commit slot has no cached identities".to_owned()) |
|
| 637 | + | })?; |
|
| 638 | + | Ok((slot.commit.id, slot.document()?, identities)) |
|
| 643 | 639 | }) |
|
| 644 | - | .collect::<Result<Vec<_>, _>>()?; |
|
| 645 | - | let status = self |
|
| 646 | - | .backend |
|
| 647 | - | .history_status(&self.brain, &commits, &documents)?; |
|
| 640 | + | .collect::<Result<Vec<_>, Error>>()?; |
|
| 641 | + | let status = HistoryStatus::for_commits(&self.brain, &status_inputs); |
|
| 648 | 642 | for slot in &mut self.commits { |
|
| 649 | 643 | if let Some(commit) = status.commits.get(&slot.commit.id) { |
|
| 650 | 644 | slot.progress = commit.progress; |
|
| 651 | 645 | slot.remaining_stats = commit.stats; |
|
| 652 | 646 | } |
| 896 | 890 | Ok(()) |
|
| 897 | 891 | } |
|
| 898 | 892 | ||
| 899 | 893 | /// Rebuilds the current commit's files to match accepted-hunk visibility. |
|
| 900 | 894 | /// |
|
| 901 | - | /// The original decoded files are kept in `CommitReview::files`; the viewer |
|
| 902 | - | /// receives a filtered copy when accepted hunks are hidden. |
|
| 895 | + | /// The original decoded document is kept in `CommitReview::document`; the |
|
| 896 | + | /// viewer receives a filtered copy when accepted hunks are hidden. |
|
| 903 | 897 | fn refresh_visibility(&mut self) -> Result<(), Error> { |
|
| 904 | 898 | let visibility = self.visibility; |
|
| 905 | 899 | let area = viewer_area(self.terminal.area()); |
|
| 906 | 900 | let loaded = self.current()?; |
|
| 907 | 901 | ||
| 908 | 902 | let files: Vec<FileDiff> = if visibility.show_accepted { |
|
| 909 | - | loaded.files.clone() |
|
| 903 | + | loaded.document.files.clone() |
|
| 910 | 904 | } else { |
|
| 911 | 905 | loaded |
|
| 906 | + | .document |
|
| 912 | 907 | .files |
|
| 913 | 908 | .iter() |
|
| 914 | 909 | .map(|file| { |
|
| 915 | 910 | let mut file = file.clone(); |
|
| 916 | 911 | file.hunks |
crates/brain/src/session/commit.rs
+28 -30
| 1 | 1 | //! Per-commit review state for the session. |
|
| 2 | 2 | ||
| 3 | 3 | use std::collections::BTreeMap; |
|
| 4 | 4 | ||
| 5 | 5 | use rustik_diff::{ |
|
| 6 | - | ContextUpdate, DiffDocument, DiffOptions, DiffViewer, Encode as _, FileDiff, FileId, FileStats, |
|
| 7 | - | HunkId, ReviewProgress, |
|
| 6 | + | ContextUpdate, DiffDocument, DiffOptions, DiffViewer, Encode as _, FileId, FileStats, HunkId, |
|
| 7 | + | ReviewProgress, |
|
| 8 | 8 | }; |
|
| 9 | 9 | use rustik_repo::DiffStat; |
|
| 10 | 10 | use rustik_tui::Rect; |
|
| 11 | 11 | ||
| 12 | - | use crate::backend::{AcceptedHunk, Brain, Commit}; |
|
| 12 | + | use crate::backend::{AcceptedHunk, Brain, Commit, commit_remaining_stats}; |
|
| 13 | 13 | use crate::comments::{CommentTarget, Comments, ReviewComments}; |
|
| 14 | 14 | use crate::decorate::Decorations; |
|
| 15 | 15 | use crate::error::Error; |
|
| 16 | 16 | use crate::review::Review; |
|
| 17 | 17 |
| 31 | 31 | pub(super) identities: Option<BTreeMap<HunkId, AcceptedHunk>>, |
|
| 32 | 32 | /// Decoded diff state, populated when the commit is first displayed. |
|
| 33 | 33 | pub(super) loaded: Option<CommitReview>, |
|
| 34 | 34 | } |
|
| 35 | 35 | ||
| 36 | + | impl CommitSlot { |
|
| 37 | + | /// Returns the original, unfiltered diff document in either slot state. |
|
| 38 | + | pub(super) fn document(&self) -> Result<&DiffDocument, Error> { |
|
| 39 | + | self.document |
|
| 40 | + | .as_ref() |
|
| 41 | + | .or_else(|| self.loaded.as_ref().map(|loaded| &loaded.document)) |
|
| 42 | + | .ok_or_else(|| Error::Git("commit slot has no diff document".to_owned())) |
|
| 43 | + | } |
|
| 44 | + | } |
|
| 45 | + | ||
| 36 | 46 | /// Decoded review state for one commit: its viewer, accepted hunks, and the |
|
| 37 | - | /// full file diffs used to restore hidden hunks. |
|
| 47 | + | /// full document used to restore hidden hunks. |
|
| 38 | 48 | /// |
|
| 39 | 49 | /// Each commit keeps its own viewer so its highlight and word-diff caches |
|
| 40 | 50 | /// persist across commit switches, and its own [`Review`] because decoded hunk |
|
| 41 | 51 | /// ids restart per commit. |
|
| 42 | 52 | pub(super) struct CommitReview { |
| 48 | 58 | pub(super) comments: Comments, |
|
| 49 | 59 | /// Single-hunk patch text keyed by hunk id. |
|
| 50 | 60 | pub(super) hunk_patches: BTreeMap<HunkId, String>, |
|
| 51 | 61 | /// Exact accepted-state identity of each original hunk. |
|
| 52 | 62 | pub(super) identities: BTreeMap<HunkId, AcceptedHunk>, |
|
| 53 | - | /// Full, unfiltered file diffs used to restore hidden hunks. |
|
| 54 | - | pub(super) files: Vec<FileDiff>, |
|
| 63 | + | /// Full, unfiltered diff document used to restore hidden hunks. |
|
| 64 | + | pub(super) document: DiffDocument, |
|
| 55 | 65 | /// Original hunk ids per file, used for decoration snapshots. |
|
| 56 | 66 | pub(super) file_hunks: BTreeMap<FileId, Vec<HunkId>>, |
|
| 57 | 67 | /// Original diff stats per hunk, used for file-level remaining diffstats. |
|
| 58 | 68 | pub(super) hunk_stats: BTreeMap<HunkId, FileStats>, |
|
| 59 | 69 | } |
|
| 60 | 70 | ||
| 61 | 71 | impl CommitReview { |
|
| 62 | 72 | /// Returns the combined diffstat of files that still need review. |
|
| 63 | 73 | pub(super) fn remaining_stats(&self) -> DiffStat { |
|
| 64 | - | let mut total = DiffStat::default(); |
|
| 65 | - | ||
| 66 | - | for hunks in self.file_hunks.values() { |
|
| 67 | - | let mut remaining = hunks.is_empty(); |
|
| 68 | - | for hunk in hunks { |
|
| 69 | - | if self.review.is_accepted(*hunk) { |
|
| 70 | - | continue; |
|
| 71 | - | } |
|
| 72 | - | remaining = true; |
|
| 73 | - | if let Some(stats) = self.hunk_stats.get(hunk) { |
|
| 74 | - | total.insertions += stats.additions; |
|
| 75 | - | total.deletions += stats.deletions; |
|
| 76 | - | } |
|
| 77 | - | } |
|
| 78 | - | total.files_changed += usize::from(remaining); |
|
| 79 | - | } |
|
| 80 | - | total |
|
| 74 | + | commit_remaining_stats(&self.document, self.review.accepted()) |
|
| 81 | 75 | } |
|
| 82 | 76 | ||
| 83 | 77 | /// Builds one commit's review state from its split diff document. |
|
| 84 | 78 | /// |
|
| 85 | 79 | /// Single-hunk patches and their patch ids are computed here, once, so |
| 91 | 85 | identities: Option<BTreeMap<HunkId, AcceptedHunk>>, |
|
| 92 | 86 | options: DiffOptions, |
|
| 93 | 87 | brain: &Brain, |
|
| 94 | 88 | store: &ReviewComments, |
|
| 95 | 89 | ) -> Result<Self, Error> { |
|
| 96 | - | let mut viewer = DiffViewer::new(document, options); |
|
| 90 | + | let mut viewer = DiffViewer::new(document.clone(), options); |
|
| 97 | 91 | viewer.load_file_sources(|oid| brain.read_blob(oid))?; |
|
| 98 | - | let files = viewer.document().files.clone(); |
|
| 99 | 92 | let identities = match identities { |
|
| 100 | 93 | Some(identities) => identities, |
|
| 101 | - | None => brain.hunk_identities(viewer.document(), commit.id)?, |
|
| 94 | + | None => brain.hunk_identities(&document, commit.id)?, |
|
| 102 | 95 | }; |
|
| 103 | 96 | let heading = format!("{} {}", commit.id.short(), commit.summary); |
|
| 104 | 97 | let mut hunk_patches = BTreeMap::new(); |
|
| 105 | 98 | let review = Review::new(); |
|
| 106 | 99 | let mut targets = BTreeMap::new(); |
|
| 107 | 100 | let mut file_hunks = BTreeMap::new(); |
|
| 108 | 101 | let mut hunk_stats = BTreeMap::new(); |
|
| 109 | 102 | ||
| 110 | - | for file in &files { |
|
| 103 | + | for file in &document.files { |
|
| 111 | 104 | let path = file.header.display_path().map(|path| path.to_path_buf()); |
|
| 112 | 105 | file_hunks.insert( |
|
| 113 | 106 | file.id, |
|
| 114 | 107 | file.hunks.iter().map(|hunk| hunk.id).collect::<Vec<_>>(), |
|
| 115 | 108 | ); |
| 139 | 132 | viewer, |
|
| 140 | 133 | review, |
|
| 141 | 134 | comments, |
|
| 142 | 135 | hunk_patches, |
|
| 143 | 136 | identities, |
|
| 144 | - | files, |
|
| 137 | + | document, |
|
| 145 | 138 | file_hunks, |
|
| 146 | 139 | hunk_stats, |
|
| 147 | 140 | }; |
|
| 148 | 141 | loaded.refresh_decorations(); |
|
| 149 | 142 |
| 170 | 163 | mut update: ContextUpdate, |
|
| 171 | 164 | brain: &Brain, |
|
| 172 | 165 | area: Rect, |
|
| 173 | 166 | ) -> Result<(), Error> { |
|
| 174 | 167 | let mut hunk_ids = BTreeMap::<_, Vec<_>>::new(); |
|
| 175 | - | for file in &self.files { |
|
| 168 | + | for file in &self.document.files { |
|
| 176 | 169 | for hunk in &file.hunks { |
|
| 177 | 170 | if let Some(identity) = self.identities.get(&hunk.id) { |
|
| 178 | 171 | hunk_ids |
|
| 179 | 172 | .entry((file.id, identity.patch_id.clone())) |
|
| 180 | 173 | .or_default() |
| 192 | 185 | file.hunks[index].id = id; |
|
| 193 | 186 | } |
|
| 194 | 187 | } |
|
| 195 | 188 | } |
|
| 196 | 189 | for replacement in update.files() { |
|
| 197 | - | if let Some(file) = self.files.iter_mut().find(|file| file.id == replacement.id) { |
|
| 190 | + | if let Some(file) = self |
|
| 191 | + | .document |
|
| 192 | + | .files |
|
| 193 | + | .iter_mut() |
|
| 194 | + | .find(|file| file.id == replacement.id) |
|
| 195 | + | { |
|
| 198 | 196 | *file = replacement.clone(); |
|
| 199 | 197 | } |
|
| 200 | 198 | } |
|
| 201 | 199 | self.viewer.apply_context_update(update, area); |
|
| 202 | 200 | Ok(()) |
a.rs
+0 -0