brain: Key review state by range
2e4e691897e6d8b2c36642660cb4b80bae9f1d4e
Brain stored review state under refs derived only from the target selector. Different ranges like HEAD^^^^..HEAD and HEAD^..HEAD both resolved to refs/brain/state/HEAD, so resetting one range wrote a synthetic state commit whose parent matched only that range's base. Opening the other range then compared the stored parent with its own base and reported the review as stale again. Store review state under a range identity that includes both base and target selectors. Legacy target-only refs are ignored rather than decoded, since no compatibility with existing review state is needed. Reviews whose selectors no longer resolve are skipped in status instead of failing the whole listing, the base selector is resolved once per stored review, and when several reviews match a branch the most recent one attaches to its row. Assisted-by: Codex:gpt-5.5
1 parent
665d9d28
crates/brain/src/backend.rs
+166 -103
| 152 | 152 | ||
| 153 | 153 | /// Stored review state before it is joined to local branch rows. |
|
| 154 | 154 | struct StoredReview { |
|
| 155 | 155 | /// Base commit recorded by the review state. |
|
| 156 | 156 | base: Oid, |
|
| 157 | + | /// Current resolution of the base selector, for staleness checks. |
|
| 158 | + | current_base: Oid, |
|
| 157 | 159 | /// Accepted-hunk progress across the review. |
|
| 158 | 160 | progress: ReviewProgress, |
|
| 159 | 161 | /// Comment storage status for this review. |
|
| 160 | 162 | comments: CommentStatus, |
|
| 161 | 163 | /// Head commit timestamp used to order status rows. |
|
| 162 | 164 | recency: CommitTime, |
|
| 165 | + | /// State commit timestamp used to choose between matching reviews. |
|
| 166 | + | updated: CommitTime, |
|
| 167 | + | } |
|
| 168 | + | ||
| 169 | + | /// Persistent identity for one review, keyed by its range selectors. |
|
| 170 | + | #[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)] |
|
| 171 | + | struct ReviewIdentity { |
|
| 172 | + | /// Base revision selector for the reviewed range. |
|
| 173 | + | base: Rev, |
|
| 174 | + | /// Target revision selector for the reviewed range. |
|
| 175 | + | target: Rev, |
|
| 176 | + | } |
|
| 177 | + | ||
| 178 | + | impl ReviewIdentity { |
|
| 179 | + | /// Creates an identity for a `base..target` review range. |
|
| 180 | + | fn new(base: Rev, target: Rev) -> Self { |
|
| 181 | + | Self { base, target } |
|
| 182 | + | } |
|
| 183 | + | ||
| 184 | + | /// Returns the table label for this review identity. |
|
| 185 | + | fn label(&self) -> String { |
|
| 186 | + | format!("{}..{}", self.base.as_str(), self.target.as_str()) |
|
| 187 | + | } |
|
| 188 | + | ||
| 189 | + | /// Decodes a stored review identity from a state ref. |
|
| 190 | + | /// |
|
| 191 | + | /// Returns `None` for refs that do not use the `range/<base>/<target>` |
|
| 192 | + | /// layout, such as state written before range-keyed identities existed. |
|
| 193 | + | fn from_ref(prefix: &Qualified<'_>, reference: &Qualified<'_>) -> Option<Self> { |
|
| 194 | + | let review = reference.strip_prefix(prefix)?; |
|
| 195 | + | let (base, target) = review.as_str().strip_prefix("range/")?.split_once('/')?; |
|
| 196 | + | if base.is_empty() || target.is_empty() || target.contains('/') { |
|
| 197 | + | return None; |
|
| 198 | + | } |
|
| 199 | + | let base = Rev::try_from(crate::comments::decode_component(base).as_str()).ok()?; |
|
| 200 | + | let target = Rev::try_from(crate::comments::decode_component(target).as_str()).ok()?; |
|
| 201 | + | ||
| 202 | + | Some(Self::new(base, target)) |
|
| 203 | + | } |
|
| 163 | 204 | } |
|
| 164 | 205 | ||
| 165 | 206 | /// Commit hunk identity data used to summarize stored review progress. |
|
| 166 | 207 | struct SummaryCommit { |
|
| 167 | 208 | /// Commit being summarized. |
| 296 | 337 | let namespace = component(namespace)?; |
|
| 297 | 338 | ||
| 298 | 339 | Ok(Qualified::from_components(category, namespace, [])) |
|
| 299 | 340 | } |
|
| 300 | 341 | ||
| 301 | - | /// Returns the hidden Git ref for `review` under `namespace`. |
|
| 302 | - | fn ref_for(namespace: &'static str, review: &str) -> Result<Qualified<'static>, Error> { |
|
| 342 | + | /// Returns the hidden Git ref for `identity` under `namespace`. |
|
| 343 | + | fn ref_for_identity( |
|
| 344 | + | namespace: &'static str, |
|
| 345 | + | identity: &ReviewIdentity, |
|
| 346 | + | ) -> Result<Qualified<'static>, Error> { |
|
| 303 | 347 | let prefix = Brain::prefix_for(namespace)?; |
|
| 304 | - | let review = component(crate::comments::encode_component(review))?; |
|
| 348 | + | let range = component("range")?; |
|
| 349 | + | let base = component(crate::comments::encode_component(identity.base.as_str()))?; |
|
| 350 | + | let target = component(crate::comments::encode_component(identity.target.as_str()))?; |
|
| 305 | 351 | ||
| 306 | - | Ok(prefix.join(review)) |
|
| 352 | + | Ok(prefix.join(range).join(base).join(target)) |
|
| 307 | 353 | } |
|
| 308 | 354 | ||
| 309 | 355 | /// Returns stored review statuses for all brain state refs. |
|
| 310 | 356 | fn statuses(repository: &git::Repository) -> Result<Vec<BrainStatus>, Error> { |
|
| 311 | 357 | let branches = repository.branches()?; |
| 321 | 367 | (AheadBehind::default(), MergeStatus::Clean) |
|
| 322 | 368 | } else { |
|
| 323 | 369 | relation_to_trunk(repository, &trunk_rev, &Rev::from(branch.head))? |
|
| 324 | 370 | }; |
|
| 325 | 371 | ||
| 326 | - | if let Some(review) = stored.remove(&branch.name) { |
|
| 372 | + | let matched = stored |
|
| 373 | + | .iter() |
|
| 374 | + | .filter(|(identity, stored)| { |
|
| 375 | + | identity.target.as_str() == branch.name && stored.current_base == trunk.head |
|
| 376 | + | }) |
|
| 377 | + | .max_by_key(|(_, stored)| stored.updated) |
|
| 378 | + | .map(|(review, _)| review.clone()); |
|
| 379 | + | ||
| 380 | + | if let Some(review) = matched.and_then(|review| stored.remove(&review)) { |
|
| 327 | 381 | statuses.push(Brain::status_for_stored( |
|
| 328 | 382 | branch.name.clone(), |
|
| 329 | 383 | BrainStatusHead::Branch, |
|
| 330 | 384 | trunk.head, |
|
| 331 | 385 | relation, |
| 345 | 399 | recency: branch.time, |
|
| 346 | 400 | }); |
|
| 347 | 401 | } |
|
| 348 | 402 | } |
|
| 349 | 403 | ||
| 350 | - | for (review, stored) in stored { |
|
| 351 | - | let target = Rev::try_from(review.as_str()) |
|
| 352 | - | .map_err(|error| Error::Usage(format!("target revision is invalid: {error}")))?; |
|
| 353 | - | let (relation, merge) = relation_to_trunk(repository, &trunk_rev, &target)?; |
|
| 404 | + | for (identity, stored) in stored { |
|
| 405 | + | let (relation, merge) = relation_to_trunk(repository, &trunk_rev, &identity.target)?; |
|
| 354 | 406 | statuses.push(Brain::status_for_stored( |
|
| 355 | - | review, |
|
| 407 | + | identity.label(), |
|
| 356 | 408 | BrainStatusHead::Stored, |
|
| 357 | - | trunk.head, |
|
| 409 | + | stored.current_base, |
|
| 358 | 410 | relation, |
|
| 359 | 411 | merge, |
|
| 360 | 412 | stored, |
|
| 361 | 413 | )); |
|
| 362 | 414 | } |
| 366 | 418 | } |
|
| 367 | 419 | ||
| 368 | 420 | /// Returns stored review state keyed by decoded review identity. |
|
| 369 | 421 | fn stored_reviews( |
|
| 370 | 422 | repository: &git::Repository, |
|
| 371 | - | ) -> Result<BTreeMap<String, StoredReview>, Error> { |
|
| 423 | + | ) -> Result<BTreeMap<ReviewIdentity, StoredReview>, Error> { |
|
| 372 | 424 | let prefix = Brain::prefix_for(STATE_REF_NAMESPACE)?; |
|
| 373 | - | repository |
|
| 374 | - | .refs(&prefix)? |
|
| 375 | - | .into_iter() |
|
| 376 | - | .map(|(reference, state)| { |
|
| 377 | - | let encoded_review = review_from_ref(&prefix, &reference)?; |
|
| 378 | - | let review = crate::comments::decode_component(&encoded_review); |
|
| 379 | - | let base = repository.parent(state)?; |
|
| 380 | - | let comments_ref = Brain::ref_for_encoded(COMMENTS_REF_NAMESPACE, &encoded_review)?; |
|
| 381 | - | let brain = Brain::new(repository.clone(), reference, comments_ref, base, state); |
|
| 382 | - | let comments = brain.comment_status()?; |
|
| 383 | - | let (head, progress) = brain.summary(&review)?; |
|
| 384 | - | let recency = repository.commit_time(head)?; |
|
| 385 | - | ||
| 386 | - | Ok(( |
|
| 387 | - | review, |
|
| 388 | - | StoredReview { |
|
| 389 | - | base, |
|
| 390 | - | progress, |
|
| 391 | - | comments, |
|
| 392 | - | recency, |
|
| 393 | - | }, |
|
| 394 | - | )) |
|
| 395 | - | }) |
|
| 396 | - | .collect() |
|
| 425 | + | let mut reviews = BTreeMap::new(); |
|
| 426 | + | ||
| 427 | + | for (reference, state) in repository.refs(&prefix)? { |
|
| 428 | + | let Some(identity) = ReviewIdentity::from_ref(&prefix, &reference) else { |
|
| 429 | + | continue; |
|
| 430 | + | }; |
|
| 431 | + | // A review whose selectors no longer resolve (deleted branch, |
|
| 432 | + | // rewritten history) is skipped rather than failing the listing. |
|
| 433 | + | let Ok(current_base) = repository.commit_id(&identity.base) else { |
|
| 434 | + | continue; |
|
| 435 | + | }; |
|
| 436 | + | if repository.commit_id(&identity.target).is_err() { |
|
| 437 | + | continue; |
|
| 438 | + | } |
|
| 439 | + | let base = repository.parent(state)?; |
|
| 440 | + | let comments_ref = Brain::ref_for_identity(COMMENTS_REF_NAMESPACE, &identity)?; |
|
| 441 | + | let brain = Brain::new(repository.clone(), reference, comments_ref, base, state); |
|
| 442 | + | let comments = brain.comment_status()?; |
|
| 443 | + | let (head, progress) = brain.summary(&identity)?; |
|
| 444 | + | let recency = repository.commit_time(head)?; |
|
| 445 | + | let updated = repository.commit_time(state)?; |
|
| 446 | + | ||
| 447 | + | reviews.insert( |
|
| 448 | + | identity, |
|
| 449 | + | StoredReview { |
|
| 450 | + | base, |
|
| 451 | + | current_base, |
|
| 452 | + | progress, |
|
| 453 | + | comments, |
|
| 454 | + | recency, |
|
| 455 | + | updated, |
|
| 456 | + | }, |
|
| 457 | + | ); |
|
| 458 | + | } |
|
| 459 | + | Ok(reviews) |
|
| 397 | 460 | } |
|
| 398 | 461 | ||
| 399 | 462 | /// Returns status for one stored review and branch relation. |
|
| 400 | 463 | fn status_for_stored( |
|
| 401 | 464 | head: String, |
| 428 | 491 | Err(_) => CommentStatus::Invalid, |
|
| 429 | 492 | }; |
|
| 430 | 493 | Ok(status) |
|
| 431 | 494 | } |
|
| 432 | 495 | ||
| 433 | - | /// Returns the hidden Git ref for already encoded `review` under `namespace`. |
|
| 434 | - | fn ref_for_encoded(namespace: &'static str, review: &str) -> Result<Qualified<'static>, Error> { |
|
| 435 | - | let prefix = Brain::prefix_for(namespace)?; |
|
| 436 | - | let review = component(review.to_owned())?; |
|
| 437 | - | ||
| 438 | - | Ok(prefix.join(review)) |
|
| 439 | - | } |
|
| 440 | - | ||
| 441 | 496 | /// Returns the target commit and accepted-hunk progress for this review. |
|
| 442 | 497 | /// |
|
| 443 | 498 | /// Progress is derived by the same tree reconciliation used by the |
|
| 444 | 499 | /// interactive review, so it can handle multi-commit ranges whose accepted |
|
| 445 | 500 | /// tree diff no longer has the same hunk shape as the original commits. |
|
| 446 | - | fn summary(&self, review: &str) -> Result<(Oid, ReviewProgress), Error> { |
|
| 501 | + | fn summary(&self, identity: &ReviewIdentity) -> Result<(Oid, ReviewProgress), Error> { |
|
| 447 | 502 | let base = Rev::from(self.base); |
|
| 448 | - | let target = Rev::try_from(review) |
|
| 449 | - | .map_err(|error| Error::Usage(format!("target revision is invalid: {error}")))?; |
|
| 450 | - | let head = self.repository.commit_id(&target)?; |
|
| 451 | - | let commits = self.summary_commits(&base, &target)?; |
|
| 503 | + | let target = &identity.target; |
|
| 504 | + | let head = self.repository.commit_id(target)?; |
|
| 505 | + | let commits = self.summary_commits(&base, target)?; |
|
| 452 | 506 | let hunks = commits |
|
| 453 | 507 | .iter() |
|
| 454 | 508 | .map(|summary| CommitHunks { |
|
| 455 | 509 | commit: &summary.commit, |
|
| 456 | 510 | hunks: Some(&summary.patch_ids), |
| 508 | 562 | ||
| 509 | 563 | Ok(ids.into_iter().zip(patch_ids).collect()) |
|
| 510 | 564 | } |
|
| 511 | 565 | ||
| 512 | 566 | /// Loads or initializes the hidden review state for one review. |
|
| 513 | - | fn load(repository: git::Repository, base: Oid, review: &str) -> Result<Self, Error> { |
|
| 514 | - | let state_ref = Brain::ref_for(STATE_REF_NAMESPACE, review)?; |
|
| 515 | - | let comments_ref = Brain::ref_for(COMMENTS_REF_NAMESPACE, review)?; |
|
| 567 | + | fn load( |
|
| 568 | + | repository: git::Repository, |
|
| 569 | + | base: Oid, |
|
| 570 | + | identity: &ReviewIdentity, |
|
| 571 | + | ) -> Result<Self, Error> { |
|
| 572 | + | let state_ref = Brain::ref_for_identity(STATE_REF_NAMESPACE, identity)?; |
|
| 573 | + | let comments_ref = Brain::ref_for_identity(COMMENTS_REF_NAMESPACE, identity)?; |
|
| 516 | 574 | let commit = match repository.optional_ref(&state_ref)? { |
|
| 517 | 575 | Some(existing) => { |
|
| 518 | 576 | let parent = repository.parent(existing)?; |
|
| 519 | 577 | if parent == base { |
|
| 520 | 578 | existing |
| 529 | 587 | }; |
|
| 530 | 588 | Ok(Self::new(repository, state_ref, comments_ref, base, commit)) |
|
| 531 | 589 | } |
|
| 532 | 590 | ||
| 533 | 591 | /// Replaces the hidden review state for one review and returns it. |
|
| 534 | - | fn reset(repository: git::Repository, base: Oid, review: &str) -> Result<Self, Error> { |
|
| 535 | - | let state_ref = Brain::ref_for(STATE_REF_NAMESPACE, review)?; |
|
| 536 | - | let comments_ref = Brain::ref_for(COMMENTS_REF_NAMESPACE, review)?; |
|
| 592 | + | fn reset( |
|
| 593 | + | repository: git::Repository, |
|
| 594 | + | base: Oid, |
|
| 595 | + | identity: &ReviewIdentity, |
|
| 596 | + | ) -> Result<Self, Error> { |
|
| 597 | + | let state_ref = Brain::ref_for_identity(STATE_REF_NAMESPACE, identity)?; |
|
| 598 | + | let comments_ref = Brain::ref_for_identity(COMMENTS_REF_NAMESPACE, identity)?; |
|
| 537 | 599 | let commit = Brain::write_fresh_state(&repository, base, &state_ref)?; |
|
| 538 | 600 | ||
| 539 | 601 | Ok(Self::new(repository, state_ref, comments_ref, base, commit)) |
|
| 540 | 602 | } |
|
| 541 | 603 | ||
| 542 | 604 | /// Carries accepted hunks from stored state onto the current base. |
|
| 543 | 605 | fn rebase( |
|
| 544 | 606 | repository: git::Repository, |
|
| 545 | 607 | base: Oid, |
|
| 546 | - | review: &str, |
|
| 608 | + | identity: &ReviewIdentity, |
|
| 547 | 609 | documents: &[DiffDocument], |
|
| 548 | 610 | ) -> Result<Self, Error> { |
|
| 549 | - | let state_ref = Brain::ref_for(STATE_REF_NAMESPACE, review)?; |
|
| 550 | - | let comments_ref = Brain::ref_for(COMMENTS_REF_NAMESPACE, review)?; |
|
| 611 | + | let state_ref = Brain::ref_for_identity(STATE_REF_NAMESPACE, identity)?; |
|
| 612 | + | let comments_ref = Brain::ref_for_identity(COMMENTS_REF_NAMESPACE, identity)?; |
|
| 551 | 613 | let accepted = match repository.optional_ref(&state_ref)? { |
|
| 552 | 614 | Some(existing) => { |
|
| 553 | 615 | let parent = repository.parent(existing)?; |
|
| 554 | 616 | if parent == base { |
|
| 555 | 617 | return Ok(Self::new( |
| 833 | 895 | .map_err(|error| Error::Git(format!("invalid brain ref component: {error}")))?; |
|
| 834 | 896 | Component::from_refstr(refname) |
|
| 835 | 897 | .ok_or_else(|| Error::Git("invalid brain ref component: contains slash".to_owned())) |
|
| 836 | 898 | } |
|
| 837 | 899 | ||
| 838 | - | /// Returns the encoded review component from a state ref. |
|
| 839 | - | fn review_from_ref(prefix: &Qualified<'_>, reference: &Qualified<'_>) -> Result<String, Error> { |
|
| 840 | - | reference |
|
| 841 | - | .strip_prefix(prefix) |
|
| 842 | - | .filter(|review| !review.is_empty() && !review.contains('/')) |
|
| 843 | - | .map(|review| review.as_str().to_owned()) |
|
| 844 | - | .ok_or_else(|| Error::Git(format!("invalid brain state ref: {reference}"))) |
|
| 845 | - | } |
|
| 846 | - | ||
| 847 | 900 | /// A [`Backend`] backed by the `git` command. |
|
| 848 | 901 | pub struct GitBackend { |
|
| 849 | 902 | /// Git repository operations. |
|
| 850 | 903 | repository: git::Repository, |
|
| 851 | - | /// Base revision, the exclusive lower bound of the range. |
|
| 852 | - | base: Rev, |
|
| 853 | - | /// Target revision, the inclusive upper bound of the range. |
|
| 854 | - | target: Rev, |
|
| 904 | + | /// Reviewed range identity. |
|
| 905 | + | review: ReviewIdentity, |
|
| 855 | 906 | } |
|
| 856 | 907 | ||
| 857 | 908 | impl GitBackend { |
|
| 858 | 909 | /// Creates a Git backend reviewing `base..target` in `cwd`. |
|
| 859 | 910 | fn new(cwd: &Path, base: Rev, target: Rev) -> Result<Self, Error> { |
|
| 860 | 911 | let repository = git::Repository::discover(cwd.to_path_buf())?; |
|
| 861 | 912 | Ok(Self { |
|
| 862 | 913 | repository, |
|
| 863 | - | base, |
|
| 864 | - | target, |
|
| 914 | + | review: ReviewIdentity::new(base, target), |
|
| 865 | 915 | }) |
|
| 866 | 916 | } |
|
| 867 | 917 | } |
|
| 868 | 918 | ||
| 869 | 919 | impl Backend for GitBackend { |
|
| 870 | 920 | fn commits(&self) -> Result<Vec<Commit>, Error> { |
|
| 871 | - | let pairs = self.repository.commits(&self.base, &self.target)?; |
|
| 921 | + | let pairs = self |
|
| 922 | + | .repository |
|
| 923 | + | .commits(&self.review.base, &self.review.target)?; |
|
| 872 | 924 | Commit::with_stats(&self.repository, pairs) |
|
| 873 | 925 | } |
|
| 874 | 926 | ||
| 875 | 927 | fn commit_diff(&self, commit: &Commit) -> Result<DiffDocument, Error> { |
|
| 876 | 928 | let bytes = self.repository.commit_diff(commit.id)?; |
|
| 877 | 929 | let document = DiffDocument::decode_bytes(&bytes)?; |
|
| 878 | 930 | Ok(document) |
|
| 879 | 931 | } |
|
| 880 | 932 | ||
| 881 | 933 | fn brain(&self) -> Result<Brain, Error> { |
|
| 882 | - | let base = self.repository.commit_id(&self.base)?; |
|
| 883 | - | Brain::load(self.repository.clone(), base, self.target.as_str()) |
|
| 934 | + | let base = self.repository.commit_id(&self.review.base)?; |
|
| 935 | + | Brain::load(self.repository.clone(), base, &self.review) |
|
| 884 | 936 | } |
|
| 885 | 937 | ||
| 886 | 938 | fn reset_brain(&self) -> Result<Brain, Error> { |
|
| 887 | - | let base = self.repository.commit_id(&self.base)?; |
|
| 888 | - | Brain::reset(self.repository.clone(), base, self.target.as_str()) |
|
| 939 | + | let base = self.repository.commit_id(&self.review.base)?; |
|
| 940 | + | Brain::reset(self.repository.clone(), base, &self.review) |
|
| 889 | 941 | } |
|
| 890 | 942 | ||
| 891 | 943 | fn rebase_brain(&self) -> Result<Brain, Error> { |
|
| 892 | - | let base = self.repository.commit_id(&self.base)?; |
|
| 944 | + | let base = self.repository.commit_id(&self.review.base)?; |
|
| 893 | 945 | let documents = self.documents()?; |
|
| 894 | - | Brain::rebase( |
|
| 895 | - | self.repository.clone(), |
|
| 896 | - | base, |
|
| 897 | - | self.target.as_str(), |
|
| 898 | - | &documents, |
|
| 899 | - | ) |
|
| 946 | + | Brain::rebase(self.repository.clone(), base, &self.review, &documents) |
|
| 900 | 947 | } |
|
| 901 | 948 | ||
| 902 | 949 | fn repo(&self, commit: &Commit) -> git::Repository { |
|
| 903 | 950 | self.repository.with_selector_args(commit.selectors()) |
|
| 904 | 951 | } |
| 908 | 955 | pub struct JjBackend { |
|
| 909 | 956 | /// Jujutsu workspace handle. |
|
| 910 | 957 | jj: jj::Repository, |
|
| 911 | 958 | /// Embedded Git repository, used for stats and review state. |
|
| 912 | 959 | repository: git::Repository, |
|
| 913 | - | /// Base revision, the exclusive lower bound of the range. |
|
| 914 | - | base: Rev, |
|
| 915 | - | /// Target revision, the inclusive upper bound of the range. |
|
| 916 | - | target: Rev, |
|
| 960 | + | /// Reviewed range identity. |
|
| 961 | + | review: ReviewIdentity, |
|
| 917 | 962 | } |
|
| 918 | 963 | ||
| 919 | 964 | impl JjBackend { |
|
| 920 | 965 | /// Creates a Jujutsu backend reviewing `base..target` in `cwd`. |
|
| 921 | 966 | fn new(cwd: &Path, base: Rev, target: Rev) -> Result<Self, Error> { |
|
| 922 | 967 | let jj = jj::Repository::new(cwd); |
|
| 923 | 968 | let repository = jj.git_repository()?; |
|
| 924 | 969 | Ok(Self { |
|
| 925 | 970 | jj, |
|
| 926 | 971 | repository, |
|
| 927 | - | base, |
|
| 928 | - | target, |
|
| 972 | + | review: ReviewIdentity::new(base, target), |
|
| 929 | 973 | }) |
|
| 930 | 974 | } |
|
| 931 | 975 | } |
|
| 932 | 976 | ||
| 933 | 977 | impl Backend for JjBackend { |
|
| 934 | 978 | fn commits(&self) -> Result<Vec<Commit>, Error> { |
|
| 935 | - | let pairs = self.jj.commits(&self.base, &self.target)?; |
|
| 979 | + | let pairs = self.jj.commits(&self.review.base, &self.review.target)?; |
|
| 936 | 980 | Commit::with_stats(&self.repository, pairs) |
|
| 937 | 981 | } |
|
| 938 | 982 | ||
| 939 | 983 | fn commit_diff(&self, commit: &Commit) -> Result<DiffDocument, Error> { |
|
| 940 | 984 | let bytes = self.jj.commit_diff(commit.id)?; |
|
| 941 | 985 | let document = DiffDocument::decode_bytes(&bytes)?; |
|
| 942 | 986 | Ok(document) |
|
| 943 | 987 | } |
|
| 944 | 988 | ||
| 945 | 989 | fn brain(&self) -> Result<Brain, Error> { |
|
| 946 | - | let base = self.jj.resolve(&self.base)?; |
|
| 947 | - | Brain::load(self.repository.clone(), base, self.target.as_str()) |
|
| 990 | + | let base = self.jj.resolve(&self.review.base)?; |
|
| 991 | + | Brain::load(self.repository.clone(), base, &self.review) |
|
| 948 | 992 | } |
|
| 949 | 993 | ||
| 950 | 994 | fn reset_brain(&self) -> Result<Brain, Error> { |
|
| 951 | - | let base = self.jj.resolve(&self.base)?; |
|
| 952 | - | Brain::reset(self.repository.clone(), base, self.target.as_str()) |
|
| 995 | + | let base = self.jj.resolve(&self.review.base)?; |
|
| 996 | + | Brain::reset(self.repository.clone(), base, &self.review) |
|
| 953 | 997 | } |
|
| 954 | 998 | ||
| 955 | 999 | fn rebase_brain(&self) -> Result<Brain, Error> { |
|
| 956 | - | let base = self.jj.resolve(&self.base)?; |
|
| 1000 | + | let base = self.jj.resolve(&self.review.base)?; |
|
| 957 | 1001 | let documents = self.documents()?; |
|
| 958 | - | Brain::rebase( |
|
| 959 | - | self.repository.clone(), |
|
| 960 | - | base, |
|
| 961 | - | self.target.as_str(), |
|
| 962 | - | &documents, |
|
| 963 | - | ) |
|
| 1002 | + | Brain::rebase(self.repository.clone(), base, &self.review, &documents) |
|
| 964 | 1003 | } |
|
| 965 | 1004 | ||
| 966 | 1005 | fn repo(&self, commit: &Commit) -> git::Repository { |
|
| 967 | 1006 | self.repository.with_selector_args(commit.selectors()) |
|
| 968 | 1007 | } |
| 997 | 1036 | let accepted = accepted_hunks_from_patch_ids(&original, &residual); |
|
| 998 | 1037 | ||
| 999 | 1038 | assert_eq!(accepted, hunk_set([0, 1])); |
|
| 1000 | 1039 | } |
|
| 1001 | 1040 | ||
| 1041 | + | #[test] |
|
| 1042 | + | fn review_identity_roundtrips_through_ref() { |
|
| 1043 | + | let base = Rev::try_from("HEAD^^^^").unwrap(); |
|
| 1044 | + | let target = Rev::try_from("HEAD").unwrap(); |
|
| 1045 | + | let identity = ReviewIdentity::new(base, target); |
|
| 1046 | + | let reference = Brain::ref_for_identity(STATE_REF_NAMESPACE, &identity).unwrap(); |
|
| 1047 | + | let prefix = Brain::prefix_for(STATE_REF_NAMESPACE).unwrap(); |
|
| 1048 | + | let decoded = ReviewIdentity::from_ref(&prefix, &reference).unwrap(); |
|
| 1049 | + | ||
| 1050 | + | assert_eq!(decoded, identity); |
|
| 1051 | + | assert_eq!( |
|
| 1052 | + | reference.as_str(), |
|
| 1053 | + | "refs/brain/state/range/HEAD%5e%5e%5e%5e/HEAD" |
|
| 1054 | + | ); |
|
| 1055 | + | } |
|
| 1056 | + | ||
| 1057 | + | #[test] |
|
| 1058 | + | fn review_identity_ignores_unrecognized_refs() { |
|
| 1059 | + | let prefix = Brain::prefix_for(STATE_REF_NAMESPACE).unwrap(); |
|
| 1060 | + | let legacy = prefix.join(component("HEAD".to_owned()).unwrap()); |
|
| 1061 | + | ||
| 1062 | + | assert_eq!(ReviewIdentity::from_ref(&prefix, &legacy), None); |
|
| 1063 | + | } |
|
| 1064 | + | ||
| 1002 | 1065 | #[test] |
|
| 1003 | 1066 | fn hunk_identity_patch_omits_context_lines() { |
|
| 1004 | 1067 | let document = DiffDocument::decode_str( |
|
| 1005 | 1068 | r#" |
f
+0 -0
crates/repo/src/lib.rs
+1 -1
| 96 | 96 | stat |
|
| 97 | 97 | } |
|
| 98 | 98 | } |
|
| 99 | 99 | ||
| 100 | 100 | /// User-supplied Git revision or Jujutsu revset selector. |
|
| 101 | - | #[derive(Clone, Debug, Eq, PartialEq)] |
|
| 101 | + | #[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)] |
|
| 102 | 102 | pub struct Rev(String); |
|
| 103 | 103 | ||
| 104 | 104 | impl Rev { |
|
| 105 | 105 | /// Returns the selector text. |
|
| 106 | 106 | pub fn as_str(&self) -> &str { |