brain: Implement review rebasing
d4033ca2536d134da94e45b4cbcfa6dd46037fe2
1 parent
1513d69d
crates/brain/src/backend.rs
+175 -40
| 188 | 188 | fn brain(&self) -> Result<Brain, Error>; |
|
| 189 | 189 | ||
| 190 | 190 | /// Resets and loads the hidden review state for this backend. |
|
| 191 | 191 | fn reset_brain(&self) -> Result<Brain, Error>; |
|
| 192 | 192 | ||
| 193 | + | /// Rebases and loads the hidden review state for this backend. |
|
| 194 | + | fn rebase_brain(&self) -> Result<Brain, Error>; |
|
| 195 | + | ||
| 193 | 196 | /// Returns a repo handle for widening `commit`'s hunk context. |
|
| 194 | 197 | fn repo(&self, commit: &Commit) -> git::Repository; |
|
| 198 | + | ||
| 199 | + | /// Returns each reviewed commit's diff with hunks split for review. |
|
| 200 | + | fn documents(&self) -> Result<Vec<DiffDocument>, Error> { |
|
| 201 | + | self.commits()? |
|
| 202 | + | .iter() |
|
| 203 | + | .map(|commit| Ok(self.commit_diff(commit)?.split_hunks())) |
|
| 204 | + | .collect() |
|
| 205 | + | } |
|
| 195 | 206 | } |
|
| 196 | 207 | ||
| 197 | 208 | impl dyn Backend { |
|
| 198 | 209 | /// Returns a backend for `cwd` bound to the `base..target` range. |
|
| 199 | 210 | /// |
| 210 | 221 | } |
|
| 211 | 222 | } |
|
| 212 | 223 | ||
| 213 | 224 | /// Returns stored review statuses for the repository containing `cwd`. |
|
| 214 | 225 | pub fn statuses(cwd: &Path) -> Result<Vec<BrainStatus>, Error> { |
|
| 215 | - | let repository = if is_jj_workspace(cwd) { |
|
| 216 | - | jj::Repository::new(cwd).git_repository()? |
|
| 217 | - | } else { |
|
| 218 | - | git::Repository::discover(cwd.to_path_buf())? |
|
| 219 | - | }; |
|
| 226 | + | let repository = open_repository(cwd)?; |
|
| 220 | 227 | Brain::statuses(&repository) |
|
| 221 | 228 | } |
|
| 222 | 229 | ||
| 230 | + | /// Opens the Git repository backing the workspace containing `cwd`. |
|
| 231 | + | fn open_repository(cwd: &Path) -> Result<git::Repository, Error> { |
|
| 232 | + | if is_jj_workspace(cwd) { |
|
| 233 | + | Ok(jj::Repository::new(cwd).git_repository()?) |
|
| 234 | + | } else { |
|
| 235 | + | Ok(git::Repository::discover(cwd.to_path_buf())?) |
|
| 236 | + | } |
|
| 237 | + | } |
|
| 238 | + | ||
| 223 | 239 | /// Returns whether `cwd` is inside a Jujutsu workspace. |
|
| 224 | 240 | fn is_jj_workspace(cwd: &Path) -> bool { |
|
| 225 | 241 | cwd.ancestors().any(|path| path.join(JJ_DIR).is_dir()) |
|
| 226 | 242 | } |
|
| 227 | 243 |
| 241 | 257 | comments_ref: Qualified<'static>, |
|
| 242 | 258 | /// Base commit used as the parent of synthetic state commits. |
|
| 243 | 259 | base: Oid, |
|
| 244 | 260 | /// Current synthetic state commit. |
|
| 245 | 261 | commit: Oid, |
|
| 262 | + | /// Patch ids that matched accepted state but stayed pending after rebase. |
|
| 263 | + | rebase_pending: BTreeSet<PatchId>, |
|
| 246 | 264 | } |
|
| 247 | 265 | ||
| 248 | 266 | impl Brain { |
|
| 267 | + | /// Creates a review state handle with no rebase-pending hunks. |
|
| 268 | + | fn new( |
|
| 269 | + | repository: git::Repository, |
|
| 270 | + | state_ref: Qualified<'static>, |
|
| 271 | + | comments_ref: Qualified<'static>, |
|
| 272 | + | base: Oid, |
|
| 273 | + | commit: Oid, |
|
| 274 | + | ) -> Self { |
|
| 275 | + | Self { |
|
| 276 | + | repository, |
|
| 277 | + | state_ref, |
|
| 278 | + | comments_ref, |
|
| 279 | + | base, |
|
| 280 | + | commit, |
|
| 281 | + | rebase_pending: BTreeSet::new(), |
|
| 282 | + | } |
|
| 283 | + | } |
|
| 284 | + | ||
| 249 | 285 | /// Returns the hidden Git ref prefix for `namespace`. |
|
| 250 | 286 | fn prefix_for(namespace: &'static str) -> Result<Qualified<'static>, Error> { |
|
| 251 | 287 | let category = component(BRAIN_REF_CATEGORY)?; |
|
| 252 | 288 | let namespace = component(namespace)?; |
|
| 253 | 289 |
| 296 | 332 | base_state: BrainStatusBaseState::Ok, |
|
| 297 | 333 | progress: ReviewProgress::default(), |
|
| 298 | 334 | relation, |
|
| 299 | 335 | merge, |
|
| 300 | 336 | comments: CommentStatus::None, |
|
| 301 | - | recency: repository.commit_time(branch.head)?, |
|
| 337 | + | recency: branch.time, |
|
| 302 | 338 | }); |
|
| 303 | 339 | } |
|
| 304 | 340 | } |
|
| 305 | 341 | ||
| 306 | 342 | for (review, stored) in stored { |
| 332 | 368 | .map(|(reference, state)| { |
|
| 333 | 369 | let encoded_review = review_from_ref(&prefix, &reference)?; |
|
| 334 | 370 | let review = crate::comments::decode_component(&encoded_review); |
|
| 335 | 371 | let base = repository.parent(state)?; |
|
| 336 | 372 | let comments_ref = Brain::ref_for_encoded(COMMENTS_REF_NAMESPACE, &encoded_review)?; |
|
| 337 | - | let brain = Brain { |
|
| 338 | - | repository: repository.clone(), |
|
| 339 | - | state_ref: reference, |
|
| 340 | - | comments_ref, |
|
| 341 | - | base, |
|
| 342 | - | commit: state, |
|
| 343 | - | }; |
|
| 373 | + | let brain = Brain::new(repository.clone(), reference, comments_ref, base, state); |
|
| 344 | 374 | let comments = brain.comment_status()?; |
|
| 345 | 375 | let (head, progress) = brain.summary(&review)?; |
|
| 346 | 376 | let recency = repository.commit_time(head)?; |
|
| 347 | 377 | ||
| 348 | 378 | Ok(( |
| 399 | 429 | ||
| 400 | 430 | Ok(prefix.join(review)) |
|
| 401 | 431 | } |
|
| 402 | 432 | ||
| 403 | 433 | /// Returns the target commit and accepted-hunk progress for this review. |
|
| 434 | + | /// |
|
| 435 | + | /// All commits' hunk patches are hashed by one `git patch-id` invocation. |
|
| 404 | 436 | fn summary(&self, review: &str) -> Result<(Oid, ReviewProgress), Error> { |
|
| 405 | 437 | let base = Rev::from(self.base); |
|
| 406 | 438 | let target = Rev::try_from(review) |
|
| 407 | 439 | .map_err(|error| Error::Usage(format!("target revision is invalid: {error}")))?; |
|
| 408 | 440 | let head = self.repository.commit_id(&target)?; |
|
| 409 | - | let mut original = Vec::new(); |
|
| 441 | + | let mut patches = Vec::new(); |
|
| 410 | 442 | ||
| 411 | 443 | for (id, _) in self.repository.commits(&base, &target)? { |
|
| 412 | 444 | let bytes = self.repository.commit_diff(id)?; |
|
| 413 | 445 | let document = DiffDocument::decode_bytes(&bytes)?.split_hunks(); |
|
| 414 | - | original.extend(self.patch_ids(&document)?.into_values()); |
|
| 446 | + | for (_, patch) in hunk_identity_patches(&document)? { |
|
| 447 | + | patches.push(patch); |
|
| 448 | + | } |
|
| 415 | 449 | } |
|
| 416 | 450 | ||
| 451 | + | let original = self.repository.patch_ids(&patches)?; |
|
| 417 | 452 | let accepted_ids = self.accepted_patch_ids()?; |
|
| 418 | 453 | let accepted = original |
|
| 419 | 454 | .iter() |
|
| 420 | 455 | .filter(|patch_id| accepted_ids.contains(*patch_id)) |
|
| 421 | 456 | .count(); |
| 440 | 475 | ||
| 441 | 476 | /// Returns per-hunk patch ids from a decoded review document. |
|
| 442 | 477 | /// |
|
| 443 | 478 | /// All hunk patches are hashed by one `git patch-id` invocation. |
|
| 444 | 479 | fn patch_ids(&self, document: &DiffDocument) -> Result<BTreeMap<HunkId, PatchId>, Error> { |
|
| 445 | - | let mut ids = Vec::new(); |
|
| 446 | - | let mut patches = Vec::new(); |
|
| 447 | - | ||
| 448 | - | for file in &document.files { |
|
| 449 | - | for hunk in &file.hunks { |
|
| 450 | - | ids.push(hunk.id); |
|
| 451 | - | patches.push(DiffDocument::for_hunks(file, [hunk.clone()]).to_unified_string()?); |
|
| 452 | - | } |
|
| 453 | - | } |
|
| 480 | + | let (ids, patches): (Vec<_>, Vec<_>) = hunk_identity_patches(document)?.into_iter().unzip(); |
|
| 454 | 481 | let patch_ids = self.repository.patch_ids(&patches)?; |
|
| 455 | 482 | ||
| 456 | 483 | Ok(ids.into_iter().zip(patch_ids).collect()) |
|
| 457 | 484 | } |
|
| 458 | 485 |
| 472 | 499 | }); |
|
| 473 | 500 | } |
|
| 474 | 501 | } |
|
| 475 | 502 | None => Brain::write_fresh_state(&repository, base, &state_ref)?, |
|
| 476 | 503 | }; |
|
| 477 | - | Ok(Self { |
|
| 478 | - | repository, |
|
| 479 | - | state_ref, |
|
| 480 | - | comments_ref, |
|
| 481 | - | base, |
|
| 482 | - | commit, |
|
| 483 | - | }) |
|
| 504 | + | Ok(Self::new(repository, state_ref, comments_ref, base, commit)) |
|
| 484 | 505 | } |
|
| 485 | 506 | ||
| 486 | 507 | /// Replaces the hidden review state for one review and returns it. |
|
| 487 | 508 | fn reset(repository: git::Repository, base: Oid, review: &str) -> Result<Self, Error> { |
|
| 488 | 509 | let state_ref = Brain::ref_for(STATE_REF_NAMESPACE, review)?; |
|
| 489 | 510 | let comments_ref = Brain::ref_for(COMMENTS_REF_NAMESPACE, review)?; |
|
| 490 | 511 | let commit = Brain::write_fresh_state(&repository, base, &state_ref)?; |
|
| 491 | 512 | ||
| 492 | - | Ok(Self { |
|
| 493 | - | repository, |
|
| 494 | - | state_ref, |
|
| 495 | - | comments_ref, |
|
| 496 | - | base, |
|
| 497 | - | commit, |
|
| 498 | - | }) |
|
| 513 | + | Ok(Self::new(repository, state_ref, comments_ref, base, commit)) |
|
| 514 | + | } |
|
| 515 | + | ||
| 516 | + | /// Carries accepted hunks from stored state onto the current base. |
|
| 517 | + | fn rebase( |
|
| 518 | + | repository: git::Repository, |
|
| 519 | + | base: Oid, |
|
| 520 | + | review: &str, |
|
| 521 | + | documents: &[DiffDocument], |
|
| 522 | + | ) -> Result<Self, Error> { |
|
| 523 | + | let state_ref = Brain::ref_for(STATE_REF_NAMESPACE, review)?; |
|
| 524 | + | let comments_ref = Brain::ref_for(COMMENTS_REF_NAMESPACE, review)?; |
|
| 525 | + | let accepted = match repository.optional_ref(&state_ref)? { |
|
| 526 | + | Some(existing) => { |
|
| 527 | + | let parent = repository.parent(existing)?; |
|
| 528 | + | if parent == base { |
|
| 529 | + | return Ok(Self::new( |
|
| 530 | + | repository, |
|
| 531 | + | state_ref, |
|
| 532 | + | comments_ref, |
|
| 533 | + | base, |
|
| 534 | + | existing, |
|
| 535 | + | )); |
|
| 536 | + | } |
|
| 537 | + | let stored = Self::new( |
|
| 538 | + | repository.clone(), |
|
| 539 | + | state_ref.clone(), |
|
| 540 | + | comments_ref.clone(), |
|
| 541 | + | parent, |
|
| 542 | + | existing, |
|
| 543 | + | ); |
|
| 544 | + | stored.accepted_patch_ids()? |
|
| 545 | + | } |
|
| 546 | + | None => BTreeSet::new(), |
|
| 547 | + | }; |
|
| 548 | + | let commit = Brain::write_fresh_state(&repository, base, &state_ref)?; |
|
| 549 | + | let mut brain = Self::new(repository, state_ref, comments_ref, base, commit); |
|
| 550 | + | brain.rebase_pending = brain.rebase_accepted_patches(documents, &accepted)?; |
|
| 551 | + | ||
| 552 | + | Ok(brain) |
|
| 499 | 553 | } |
|
| 500 | 554 | ||
| 501 | 555 | /// Writes an empty accepted-state commit at `base`. |
|
| 502 | 556 | fn write_fresh_state( |
|
| 503 | 557 | repository: &git::Repository, |
| 523 | 577 | Ok(patch_id) |
|
| 524 | 578 | } |
|
| 525 | 579 | ||
| 526 | 580 | /// Computes the stable Git patch id for one hunk of `file`. |
|
| 527 | 581 | pub fn hunk_patch_id(&self, file: &FileDiff, hunk: &Hunk) -> Result<PatchId, Error> { |
|
| 528 | - | let document = DiffDocument::for_hunks(file, [hunk.clone()]); |
|
| 529 | - | let patch = document.to_unified_string()?; |
|
| 582 | + | let patch = hunk_identity_patch(file, hunk)?; |
|
| 530 | 583 | self.patch_id(&patch) |
|
| 531 | 584 | } |
|
| 532 | 585 | ||
| 586 | + | /// Returns whether `patch_id` needs review after a review rebase. |
|
| 587 | + | pub fn is_rebase_pending(&self, patch_id: &PatchId) -> bool { |
|
| 588 | + | self.rebase_pending.contains(patch_id) |
|
| 589 | + | } |
|
| 590 | + | ||
| 533 | 591 | /// Reads the review's persisted comments blob, if any. |
|
| 534 | 592 | pub fn read_comments(&self) -> Result<Option<String>, Error> { |
|
| 535 | 593 | let Some(blob) = self.repository.optional_ref(&self.comments_ref)? else { |
|
| 536 | 594 | return Ok(None); |
|
| 537 | 595 | }; |
| 561 | 619 | pub fn reverse_patch(&mut self, patch: &str) -> Result<(), Error> { |
|
| 562 | 620 | let tree = self.repository.reverse_patch_to_tree(self.commit, patch)?; |
|
| 563 | 621 | self.advance_to_tree(tree) |
|
| 564 | 622 | } |
|
| 565 | 623 | ||
| 624 | + | /// Carries matching accepted patches into this review state. |
|
| 625 | + | fn rebase_accepted_patches( |
|
| 626 | + | &mut self, |
|
| 627 | + | documents: &[DiffDocument], |
|
| 628 | + | accepted: &BTreeSet<PatchId>, |
|
| 629 | + | ) -> Result<BTreeSet<PatchId>, Error> { |
|
| 630 | + | let mut pending = BTreeSet::new(); |
|
| 631 | + | ||
| 632 | + | for document in documents { |
|
| 633 | + | let patch_ids = self.patch_ids(document)?; |
|
| 634 | + | for file in &document.files { |
|
| 635 | + | for hunk in &file.hunks { |
|
| 636 | + | let Some(patch_id) = patch_ids.get(&hunk.id) else { |
|
| 637 | + | continue; |
|
| 638 | + | }; |
|
| 639 | + | if !accepted.contains(patch_id) { |
|
| 640 | + | continue; |
|
| 641 | + | } |
|
| 642 | + | let patch = |
|
| 643 | + | DiffDocument::for_hunks(file, [hunk.clone()]).to_unified_string()?; |
|
| 644 | + | match self.repository.apply_patch_to_tree(self.commit, &patch) { |
|
| 645 | + | Ok(tree) => self.advance_to_tree(tree)?, |
|
| 646 | + | Err(rustik_repo::RepoError::Git(_)) => { |
|
| 647 | + | pending.insert(patch_id.clone()); |
|
| 648 | + | } |
|
| 649 | + | Err(error) => return Err(error.into()), |
|
| 650 | + | } |
|
| 651 | + | } |
|
| 652 | + | } |
|
| 653 | + | } |
|
| 654 | + | ||
| 655 | + | Ok(pending) |
|
| 656 | + | } |
|
| 657 | + | ||
| 566 | 658 | /// Persists `tree` as the current accepted review state. |
|
| 567 | 659 | fn advance_to_tree(&mut self, tree: Oid) -> Result<(), Error> { |
|
| 568 | 660 | let commit = self.repository.commit( |
|
| 569 | 661 | tree, |
|
| 570 | 662 | self.base, |
| 643 | 735 | ||
| 644 | 736 | Ok(accepted_hunks_from_patch_ids(original, &residual_ids)) |
|
| 645 | 737 | } |
|
| 646 | 738 | } |
|
| 647 | 739 | ||
| 740 | + | /// Returns the patch text used to identify `hunk` across base movement. |
|
| 741 | + | fn hunk_identity_patch(file: &FileDiff, hunk: &Hunk) -> Result<String, Error> { |
|
| 742 | + | Ok(DiffDocument::for_hunks(file, [hunk.identity()]).to_unified_string()?) |
|
| 743 | + | } |
|
| 744 | + | ||
| 745 | + | /// Returns each hunk's id and identity patch text, in document order. |
|
| 746 | + | fn hunk_identity_patches(document: &DiffDocument) -> Result<Vec<(HunkId, String)>, Error> { |
|
| 747 | + | let mut patches = Vec::new(); |
|
| 748 | + | for file in &document.files { |
|
| 749 | + | for hunk in &file.hunks { |
|
| 750 | + | patches.push((hunk.id, hunk_identity_patch(file, hunk)?)); |
|
| 751 | + | } |
|
| 752 | + | } |
|
| 753 | + | Ok(patches) |
|
| 754 | + | } |
|
| 755 | + | ||
| 648 | 756 | /// Returns original hunks whose patch ids are absent from the residual set. |
|
| 649 | 757 | fn accepted_hunks_from_patch_ids<P: Ord>( |
|
| 650 | 758 | original: &BTreeMap<HunkId, P>, |
|
| 651 | 759 | residual: &BTreeSet<P>, |
|
| 652 | 760 | ) -> BTreeSet<HunkId> { |
| 752 | 860 | fn reset_brain(&self) -> Result<Brain, Error> { |
|
| 753 | 861 | let base = self.repository.commit_id(&self.base)?; |
|
| 754 | 862 | Brain::reset(self.repository.clone(), base, self.target.as_str()) |
|
| 755 | 863 | } |
|
| 756 | 864 | ||
| 865 | + | fn rebase_brain(&self) -> Result<Brain, Error> { |
|
| 866 | + | let base = self.repository.commit_id(&self.base)?; |
|
| 867 | + | let documents = self.documents()?; |
|
| 868 | + | Brain::rebase( |
|
| 869 | + | self.repository.clone(), |
|
| 870 | + | base, |
|
| 871 | + | self.target.as_str(), |
|
| 872 | + | &documents, |
|
| 873 | + | ) |
|
| 874 | + | } |
|
| 875 | + | ||
| 757 | 876 | fn repo(&self, commit: &Commit) -> git::Repository { |
|
| 758 | 877 | self.repository.with_selector_args(commit.selectors()) |
|
| 759 | 878 | } |
|
| 760 | 879 | } |
|
| 761 | 880 |
| 805 | 924 | fn reset_brain(&self) -> Result<Brain, Error> { |
|
| 806 | 925 | let base = self.jj.resolve(&self.base)?; |
|
| 807 | 926 | Brain::reset(self.repository.clone(), base, self.target.as_str()) |
|
| 808 | 927 | } |
|
| 809 | 928 | ||
| 929 | + | fn rebase_brain(&self) -> Result<Brain, Error> { |
|
| 930 | + | let base = self.jj.resolve(&self.base)?; |
|
| 931 | + | let documents = self.documents()?; |
|
| 932 | + | Brain::rebase( |
|
| 933 | + | self.repository.clone(), |
|
| 934 | + | base, |
|
| 935 | + | self.target.as_str(), |
|
| 936 | + | &documents, |
|
| 937 | + | ) |
|
| 938 | + | } |
|
| 939 | + | ||
| 810 | 940 | fn repo(&self, commit: &Commit) -> git::Repository { |
|
| 811 | 941 | self.repository.with_selector_args(commit.selectors()) |
|
| 812 | 942 | } |
|
| 813 | 943 | } |
|
| 814 | 944 |
| 840 | 970 | let residual = BTreeSet::new(); |
|
| 841 | 971 | let accepted = accepted_hunks_from_patch_ids(&original, &residual); |
|
| 842 | 972 | ||
| 843 | 973 | assert_eq!(accepted, hunk_set([0, 1])); |
|
| 844 | 974 | } |
|
| 975 | + | ||
| 976 | + | #[test] |
|
| 977 | + | fn hunk_identity_patch_omits_context_lines() { |
|
| 978 | + | let document = DiffDocument::decode_str( |
|
| 979 | + | r#" |
f
+0 -0
crates/brain/src/decorate.rs
+16 -3
| 3 | 3 | //! The diff viewer is review-agnostic; this decorator feeds it per-hunk |
|
| 4 | 4 | //! markers. A [`Decorations`] value is an immutable snapshot of the review |
|
| 5 | 5 | //! state, rebuilt and reinstalled by the session whenever acceptance or |
|
| 6 | 6 | //! comments change, so the viewer never observes state mutating underneath |
|
| 7 | 7 | //! it. Accepted hunks are dimmed and marked with a check mark; commented |
|
| 8 | - | //! hunks show a comment marker and count. |
|
| 8 | + | //! hunks show a comment marker and count; hunks that need review after a |
|
| 9 | + | //! rebase show a rebase marker. |
|
| 9 | 10 | ||
| 10 | 11 | use std::collections::{BTreeMap, BTreeSet}; |
|
| 11 | 12 | ||
| 12 | 13 | use rustik_diff::{ |
|
| 13 | 14 | DiffDecorator, DiffPalette, FileDecoration, FileId, HunkDecoration, HunkId, ReviewProgress, |
| 20 | 21 | ||
| 21 | 22 | /// Check mark drawn beside accepted hunks and fully accepted files: โ. |
|
| 22 | 23 | const ACCEPTED_MARK: char = 'โ'; |
|
| 23 | 24 | /// Marker drawn in the corner of a hunk that has comments: ๐ฉ. |
|
| 24 | 25 | const COMMENT_MARK: char = '๐ฉ'; |
|
| 26 | + | /// Marker drawn beside a hunk that was pending after a review rebase. |
|
| 27 | + | const REBASE_PENDING_MARK: char = 'โป'; |
|
| 25 | 28 | ||
| 26 | 29 | /// Immutable decoration snapshot of one commit's review state. |
|
| 27 | 30 | #[derive(Clone, Debug)] |
|
| 28 | 31 | pub struct Decorations { |
|
| 29 | 32 | /// Hunks accepted at snapshot time. |
|
| 30 | 33 | accepted: BTreeSet<HunkId>, |
|
| 34 | + | /// Pending hunks that were accepted before a review rebase. |
|
| 35 | + | rebase_pending: BTreeSet<HunkId>, |
|
| 31 | 36 | /// Per-hunk comment counts at snapshot time. |
|
| 32 | 37 | comments: BTreeMap<HunkId, usize>, |
|
| 33 | 38 | /// Original hunk ids for each file in the commit. |
|
| 34 | 39 | file_hunks: BTreeMap<FileId, Vec<HunkId>>, |
|
| 35 | 40 | /// Diff palette the markers are drawn with. |
| 44 | 49 | file_hunks: &BTreeMap<FileId, Vec<HunkId>>, |
|
| 45 | 50 | palette: DiffPalette, |
|
| 46 | 51 | ) -> Self { |
|
| 47 | 52 | Self { |
|
| 48 | 53 | accepted: review.accepted().clone(), |
|
| 54 | + | rebase_pending: review.rebase_pending().clone(), |
|
| 49 | 55 | comments: comments.counts(), |
|
| 50 | 56 | file_hunks: file_hunks.clone(), |
|
| 51 | 57 | palette, |
|
| 52 | 58 | } |
|
| 53 | 59 | } |
| 81 | 87 | decoration |
|
| 82 | 88 | } |
|
| 83 | 89 | ||
| 84 | 90 | fn hunk(&self, _file: FileId, hunk: HunkId) -> HunkDecoration { |
|
| 85 | 91 | let accepted = self.accepted.contains(&hunk); |
|
| 92 | + | let rebase_pending = self.rebase_pending.contains(&hunk); |
|
| 86 | 93 | let comments = self.comments.get(&hunk).copied().unwrap_or_default(); |
|
| 87 | - | if !accepted && comments == 0 { |
|
| 94 | + | if !accepted && !rebase_pending && comments == 0 { |
|
| 88 | 95 | return HunkDecoration::default(); |
|
| 89 | 96 | } |
|
| 90 | 97 | ||
| 91 | 98 | let mut corner = Line::blank(); |
|
| 92 | 99 | if accepted { |
|
| 93 | 100 | corner = corner.item(Label::from_char(ACCEPTED_MARK).fg(self.palette.positive_fg)); |
|
| 94 | 101 | } |
|
| 95 | - | if comments > 0 { |
|
| 102 | + | if rebase_pending { |
|
| 96 | 103 | if accepted { |
|
| 97 | 104 | corner = corner.item(Label::space()); |
|
| 98 | 105 | } |
|
| 106 | + | corner = corner.item(Label::from_char(REBASE_PENDING_MARK).fg(colors::rgb::ORANGE)); |
|
| 107 | + | } |
|
| 108 | + | if comments > 0 { |
|
| 109 | + | if accepted || rebase_pending { |
|
| 110 | + | corner = corner.item(Label::space()); |
|
| 111 | + | } |
|
| 99 | 112 | corner = |
|
| 100 | 113 | corner.item(Label::new(&format!("{comments} {COMMENT_MARK}")).fg(colors::rgb::DIM)); |
|
| 101 | 114 | } |
|
| 102 | 115 | ||
| 103 | 116 | HunkDecoration { |
crates/brain/src/main.rs
+31 -8
| 37 | 37 | ||
| 38 | 38 | /// Terminal backend used by the interactive viewer. |
|
| 39 | 39 | type BrainTerminal = Terminal<tui_termion::TermionBackend<tui_termion::term::TerminalScreen>>; |
|
| 40 | 40 | ||
| 41 | 41 | /// Command-line usage text. |
|
| 42 | - | const USAGE: &str = "usage: brain [status]\n brain [--reset] diff <from> <to>"; |
|
| 42 | + | const USAGE: &str = "usage: brain [status]\n brain [--reset|--rebase] diff <from> <to>"; |
|
| 43 | 43 | /// Default output width used when terminal width is unknown. |
|
| 44 | 44 | const DEFAULT_STATUS_WIDTH: usize = 140; |
|
| 45 | 45 | /// Marker shown for approved review state: โ. |
|
| 46 | 46 | const APPROVED_STATUS_ICON: char = 'โ'; |
|
| 47 | 47 | /// Marker shown for in-review state: โ. |
| 74 | 74 | "unexpected argument: {arg:?}\n{USAGE}" |
|
| 75 | 75 | ))); |
|
| 76 | 76 | } |
|
| 77 | 77 | match arg { |
|
| 78 | 78 | Long("reset") => state = ReviewStateLoad::Fresh, |
|
| 79 | + | Long("rebase") => state = ReviewStateLoad::Rebase, |
|
| 79 | 80 | Value(command_name) if command_name == "status" => { |
|
| 80 | 81 | command = Some(Command::Status); |
|
| 81 | 82 | } |
|
| 82 | 83 | Value(command_name) if command_name == "diff" => { |
|
| 83 | 84 | let from = parse_revision("from", parser.value()?)?; |
| 90 | 91 | return Err(Error::Usage(format!("unknown command: {value}\n{USAGE}"))); |
|
| 91 | 92 | } |
|
| 92 | 93 | _ => return Err(Error::Usage(USAGE.to_owned())), |
|
| 93 | 94 | } |
|
| 94 | 95 | } |
|
| 95 | - | if state == ReviewStateLoad::Fresh && !matches!(command, Some(Command::Diff { .. })) { |
|
| 96 | + | if state != ReviewStateLoad::Existing && !matches!(command, Some(Command::Diff { .. })) { |
|
| 96 | 97 | return Err(Error::Usage(USAGE.to_owned())); |
|
| 97 | 98 | } |
|
| 98 | 99 | ||
| 99 | 100 | Ok(Self { |
|
| 100 | 101 | state, |
| 122 | 123 | enum ReviewStateLoad { |
|
| 123 | 124 | /// Load existing state, prompting if it is stale. |
|
| 124 | 125 | Existing, |
|
| 125 | 126 | /// Replace existing state before starting the review. |
|
| 126 | 127 | Fresh, |
|
| 128 | + | /// Carry accepted hunks from stale state onto the current review base. |
|
| 129 | + | Rebase, |
|
| 127 | 130 | } |
|
| 128 | 131 | ||
| 129 | 132 | /// Reviewer choice after stale state is found. |
|
| 130 | 133 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
|
| 131 | 134 | enum StaleReviewStateAction { |
|
| 135 | + | /// Carry accepted hunks onto the current review base. |
|
| 136 | + | Rebase, |
|
| 132 | 137 | /// Replace the stored state with empty state at the current base. |
|
| 133 | 138 | Reset, |
|
| 134 | 139 | /// Exit without changing stored state. |
|
| 135 | 140 | Quit, |
|
| 136 | 141 | } |
| 145 | 150 | Dialog::error() |
|
| 146 | 151 | .body([ |
|
| 147 | 152 | Line::new("The existing review is stale."), |
|
| 148 | 153 | Line::new("Its original base commit no longer matches this branch."), |
|
| 149 | 154 | Line::blank(), |
|
| 150 | - | Line::new("Resetting starts this review over on the current branch."), |
|
| 151 | - | Line::new("Accepted hunks are cleared. Existing comments are kept."), |
|
| 155 | + | Line::new("Rebasing carries matching accepted hunks forward."), |
|
| 156 | + | Line::new("Changed or new hunks are left for review."), |
|
| 152 | 157 | ]) |
|
| 153 | 158 | .actions( |
|
| 154 | 159 | ActionList::new() |
|
| 155 | - | .action("r", "reset state") |
|
| 156 | - | .action("q", "quit"), |
|
| 160 | + | .action("โต", "rebase") |
|
| 161 | + | .action("x", "reset") |
|
| 162 | + | .action("esc", "quit"), |
|
| 157 | 163 | ) |
|
| 158 | 164 | .max_size(Size::new(78, 10)) |
|
| 159 | 165 | } |
|
| 160 | 166 | ||
| 161 | 167 | /// Draws the prompt. |
| 203 | 209 | let cursor = terminal.set_cursor_visible(true); |
|
| 204 | 210 | let action = action?; |
|
| 205 | 211 | cursor?; |
|
| 206 | 212 | ||
| 207 | 213 | match action { |
|
| 214 | + | StaleReviewStateAction::Rebase => backend.rebase_brain()?, |
|
| 208 | 215 | StaleReviewStateAction::Reset => backend.reset_brain()?, |
|
| 209 | 216 | StaleReviewStateAction::Quit => return Ok(()), |
|
| 210 | 217 | } |
|
| 211 | 218 | } |
|
| 212 | 219 | Err(error) => return Err(error), |
|
| 213 | 220 | }, |
|
| 214 | 221 | ReviewStateLoad::Fresh => backend.reset_brain()?, |
|
| 222 | + | ReviewStateLoad::Rebase => backend.rebase_brain()?, |
|
| 215 | 223 | }; |
|
| 216 | 224 | let persisted = brain.read_comments()?; |
|
| 217 | 225 | let comments = ReviewComments::parse(persisted.as_deref().unwrap_or_default())?; |
|
| 218 | 226 | ||
| 219 | 227 | let assets = Builtins::load().map_err(|error| Error::Highlight(error.to_string()))?; |
| 425 | 433 | terminal.set_cursor_visible(false)?; |
|
| 426 | 434 | prompt.draw(terminal)?; |
|
| 427 | 435 | ||
| 428 | 436 | while let Some(event) = events.next_input() { |
|
| 429 | 437 | match event? { |
|
| 430 | - | Event::Key(Key::Char('r')) => return Ok(StaleReviewStateAction::Reset), |
|
| 431 | - | Event::Key(Key::Char('q') | Key::Esc | Key::Ctrl('c')) => { |
|
| 438 | + | Event::Key(Key::Enter) => return Ok(StaleReviewStateAction::Rebase), |
|
| 439 | + | Event::Key(Key::Char('x')) => return Ok(StaleReviewStateAction::Reset), |
|
| 440 | + | Event::Key(Key::Esc | Key::Ctrl('c')) => { |
|
| 432 | 441 | return Ok(StaleReviewStateAction::Quit); |
|
| 433 | 442 | } |
|
| 434 | 443 | Event::Resize(_) => prompt.draw(terminal)?, |
|
| 435 | 444 | _ => {} |
|
| 436 | 445 | } |
| 469 | 478 | let parsed = Args::parse(args(&["--reset", "diff", "main", "HEAD"])).unwrap(); |
|
| 470 | 479 | ||
| 471 | 480 | assert_eq!(parsed.state, ReviewStateLoad::Fresh); |
|
| 472 | 481 | } |
|
| 473 | 482 | ||
| 483 | + | #[test] |
|
| 484 | + | fn parses_rebase_flag_before_diff_command() { |
|
| 485 | + | let parsed = Args::parse(args(&["--rebase", "diff", "main", "HEAD"])).unwrap(); |
|
| 486 | + | ||
| 487 | + | assert_eq!(parsed.state, ReviewStateLoad::Rebase); |
|
| 488 | + | } |
|
| 489 | + | ||
| 474 | 490 | #[test] |
|
| 475 | 491 | fn parses_status_command() { |
|
| 476 | 492 | let parsed = Args::parse(args(&["status"])).unwrap(); |
|
| 477 | 493 | ||
| 478 | 494 | assert_eq!(parsed.command, Command::Status); |
| 489 | 505 | fn rejects_reset_without_diff_command() { |
|
| 490 | 506 | let error = Args::parse(args(&["--reset"])).unwrap_err(); |
|
| 491 | 507 | ||
| 492 | 508 | assert!(error.to_string().contains("usage: brain")); |
|
| 493 | 509 | } |
|
| 510 | + | ||
| 511 | + | #[test] |
|
| 512 | + | fn rejects_rebase_without_diff_command() { |
|
| 513 | + | let error = Args::parse(args(&["--rebase"])).unwrap_err(); |
|
| 514 | + | ||
| 515 | + | assert!(error.to_string().contains("usage: brain")); |
|
| 516 | + | } |
|
| 494 | 517 | } |
crates/brain/src/review.rs
+13 -0
| 22 | 22 | /// Per-hunk acceptance state for one commit. |
|
| 23 | 23 | #[derive(Clone, Debug, Default)] |
|
| 24 | 24 | pub struct Review { |
|
| 25 | 25 | /// Hunks the reviewer has accepted. |
|
| 26 | 26 | accepted: BTreeSet<HunkId>, |
|
| 27 | + | /// Pending hunks that were accepted before a review rebase. |
|
| 28 | + | rebase_pending: BTreeSet<HunkId>, |
|
| 27 | 29 | } |
|
| 28 | 30 | ||
| 29 | 31 | impl Review { |
|
| 30 | 32 | /// Creates empty review state with no accepted hunks. |
|
| 31 | 33 | pub fn new() -> Self { |
| 40 | 42 | /// Returns the set of accepted hunks. |
|
| 41 | 43 | pub fn accepted(&self) -> &BTreeSet<HunkId> { |
|
| 42 | 44 | &self.accepted |
|
| 43 | 45 | } |
|
| 44 | 46 | ||
| 47 | + | /// Returns the hunks that need review after a review rebase. |
|
| 48 | + | pub fn rebase_pending(&self) -> &BTreeSet<HunkId> { |
|
| 49 | + | &self.rebase_pending |
|
| 50 | + | } |
|
| 51 | + | ||
| 52 | + | /// Marks `hunk` as needing review after a review rebase. |
|
| 53 | + | pub fn mark_rebase_pending(&mut self, hunk: HunkId) { |
|
| 54 | + | self.rebase_pending.insert(hunk); |
|
| 55 | + | } |
|
| 56 | + | ||
| 45 | 57 | /// Replaces the accepted hunk set. |
|
| 46 | 58 | pub fn replace(&mut self, accepted: BTreeSet<HunkId>) { |
|
| 59 | + | self.rebase_pending.retain(|hunk| !accepted.contains(hunk)); |
|
| 47 | 60 | self.accepted = accepted; |
|
| 48 | 61 | } |
|
| 49 | 62 | ||
| 50 | 63 | /// Returns whether every hunk in `hunks` has been accepted. |
|
| 51 | 64 | pub fn accepts_all(&self, hunks: impl IntoIterator<Item = HunkId>) -> bool { |
crates/brain/src/session/commit.rs
+5 -1
| 65 | 65 | let viewer = DiffViewer::new(document, options); |
|
| 66 | 66 | let files = viewer.document().files.clone(); |
|
| 67 | 67 | let heading = format!("{} {}", commit.id.short(), commit.summary); |
|
| 68 | 68 | let mut hunk_patches = BTreeMap::new(); |
|
| 69 | 69 | let mut patch_ids = BTreeMap::new(); |
|
| 70 | + | let mut review = Review::new(); |
|
| 70 | 71 | let mut targets = BTreeMap::new(); |
|
| 71 | 72 | let mut file_hunks = BTreeMap::new(); |
|
| 72 | 73 | ||
| 73 | 74 | for file in &files { |
|
| 74 | 75 | let path = file.header.display_path().map(|path| path.to_path_buf()); |
| 85 | 86 | heading: heading.clone(), |
|
| 86 | 87 | path: path.clone(), |
|
| 87 | 88 | patch_id: patch_id.clone(), |
|
| 88 | 89 | }, |
|
| 89 | 90 | ); |
|
| 91 | + | if brain.is_rebase_pending(&patch_id) { |
|
| 92 | + | review.mark_rebase_pending(hunk.id); |
|
| 93 | + | } |
|
| 90 | 94 | patch_ids.insert(hunk.id, patch_id); |
|
| 91 | 95 | hunk_patches.insert(hunk.id, patch); |
|
| 92 | 96 | } |
|
| 93 | 97 | } |
|
| 94 | 98 | let comments = Comments::new(store, targets); |
|
| 95 | 99 | let mut loaded = Self { |
|
| 96 | 100 | viewer, |
|
| 97 | - | review: Review::new(), |
|
| 101 | + | review, |
|
| 98 | 102 | comments, |
|
| 99 | 103 | hunk_patches, |
|
| 100 | 104 | patch_ids, |
|
| 101 | 105 | files, |
|
| 102 | 106 | file_hunks, |
a.rs
+0 -0
crates/diff/src/document.rs
+27 -0
| 529 | 529 | ||
| 530 | 530 | /// Returns old-side and new-side body sizes. |
|
| 531 | 531 | fn sizes(&self) -> HunkSizes { |
|
| 532 | 532 | HunkSizes::from_lines(&self.lines) |
|
| 533 | 533 | } |
|
| 534 | + | ||
| 535 | + | /// Returns a copy identifying the change independently of its context. |
|
| 536 | + | /// |
|
| 537 | + | /// Context lines are dropped, sizes are recomputed, and the header text is |
|
| 538 | + | /// cleared, so the same change produces the same patch text even when its |
|
| 539 | + | /// surrounding context or position drifts. |
|
| 540 | + | pub fn identity(&self) -> Self { |
|
| 541 | + | let lines: Vec<_> = self |
|
| 542 | + | .lines |
|
| 543 | + | .iter() |
|
| 544 | + | .filter(|line| !matches!(line.kind, DiffLineKind::Context)) |
|
| 545 | + | .cloned() |
|
| 546 | + | .collect(); |
|
| 547 | + | let sizes = HunkSizes::from_lines(&lines); |
|
| 548 | + | ||
| 549 | + | Self { |
|
| 550 | + | id: self.id, |
|
| 551 | + | header: HunkHeader { |
|
| 552 | + | old_line_no: self.header.old_line_no, |
|
| 553 | + | old_size: sizes.old, |
|
| 554 | + | new_line_no: self.header.new_line_no, |
|
| 555 | + | new_size: sizes.new, |
|
| 556 | + | text: String::new(), |
|
| 557 | + | }, |
|
| 558 | + | lines, |
|
| 559 | + | } |
|
| 560 | + | } |
|
| 534 | 561 | } |
|
| 535 | 562 | ||
| 536 | 563 | /// Old-side and new-side line counts for a hunk body. |
|
| 537 | 564 | #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] |
|
| 538 | 565 | struct HunkSizes { |
a.txt
+0 -0
a
+0 -0
crates/repo/src/git.rs
+22 -5
| 266 | 266 | }) |
|
| 267 | 267 | .collect() |
|
| 268 | 268 | } |
|
| 269 | 269 | ||
| 270 | 270 | /// Returns local branches, sorted by branch name. |
|
| 271 | + | /// |
|
| 272 | + | /// Tip commit times come from the same `for-each-ref` invocation, so |
|
| 273 | + | /// callers can order branches without a subprocess per branch. |
|
| 271 | 274 | fn branches(&self) -> Result<Vec<Branch>, RepoError> { |
|
| 272 | 275 | let bytes = self.git( |
|
| 273 | 276 | "for-each-ref", |
|
| 274 | - | &["--format=%(refname:short)%00%(objectname)", "refs/heads"], |
|
| 277 | + | &[ |
|
| 278 | + | "--format=%(refname:short)%00%(objectname)%00%(committerdate:unix)", |
|
| 279 | + | "refs/heads", |
|
| 280 | + | ], |
|
| 275 | 281 | )?; |
|
| 276 | 282 | let text = utf8(bytes)?; |
|
| 277 | 283 | text.lines() |
|
| 278 | 284 | .map(|line| { |
|
| 279 | - | let (name, head) = line |
|
| 280 | - | .split_once('\0') |
|
| 281 | - | .ok_or_else(|| RepoError::Git(format!("invalid branch line: {line}")))?; |
|
| 285 | + | let mut fields = line.split('\0'); |
|
| 286 | + | let (Some(name), Some(head), Some(seconds)) = |
|
| 287 | + | (fields.next(), fields.next(), fields.next()) |
|
| 288 | + | else { |
|
| 289 | + | return Err(RepoError::Git(format!("invalid branch line: {line}"))); |
|
| 290 | + | }; |
|
| 282 | 291 | let head = head |
|
| 283 | 292 | .parse() |
|
| 284 | 293 | .map_err(|_| RepoError::Git(format!("invalid branch head: {head}")))?; |
|
| 294 | + | let seconds = seconds |
|
| 295 | + | .parse() |
|
| 296 | + | .map_err(|_| RepoError::Git(format!("invalid branch time: {seconds}")))?; |
|
| 285 | 297 | ||
| 286 | 298 | Ok(Branch { |
|
| 287 | 299 | name: name.to_owned(), |
|
| 288 | 300 | head, |
|
| 301 | + | time: CommitTime { seconds }, |
|
| 289 | 302 | }) |
|
| 290 | 303 | }) |
|
| 291 | 304 | .collect() |
|
| 292 | 305 | } |
|
| 293 | 306 |
| 307 | 320 | fn trunk(&self, branches: &[Branch]) -> Result<Option<Branch>, RepoError> { |
|
| 308 | 321 | let default = self.default_branch()?; |
|
| 309 | 322 | Ok(default |
|
| 310 | 323 | .as_deref() |
|
| 311 | 324 | .and_then(|name| branches.iter().find(|branch| branch.name == name)) |
|
| 312 | - | .or_else(|| branches.iter().find(|branch| branch.name == "master")) |
|
| 325 | + | .or_else(|| { |
|
| 326 | + | branches |
|
| 327 | + | .iter() |
|
| 328 | + | .find(|branch| branch.name == refs::name::str::MASTER) |
|
| 329 | + | }) |
|
| 313 | 330 | .cloned()) |
|
| 314 | 331 | } |
|
| 315 | 332 | ||
| 316 | 333 | /// Returns ahead and behind counts for `target` relative to `base`. |
|
| 317 | 334 | fn ahead_behind(&self, base: &Rev, target: &Rev) -> Result<AheadBehind, RepoError> { |
crates/repo/src/lib.rs
+2 -0
| 133 | 133 | pub struct Branch { |
|
| 134 | 134 | /// Short branch name. |
|
| 135 | 135 | pub name: String, |
|
| 136 | 136 | /// Commit at the branch tip. |
|
| 137 | 137 | pub head: Oid, |
|
| 138 | + | /// Committer timestamp of the commit at the branch tip. |
|
| 139 | + | pub time: CommitTime, |
|
| 138 | 140 | } |
|
| 139 | 141 | ||
| 140 | 142 | /// Commit timestamp in seconds since the Unix epoch. |
|
| 141 | 143 | #[derive(Clone, Copy, Debug, Default, Eq, Ord, PartialEq, PartialOrd)] |
|
| 142 | 144 | pub struct CommitTime { |