brain: Recover reviews after branch rename
8a10dfc9db697a348529d79d31768a982de37741
A renamed target branch makes its stored symbolic selector unresolved, even though the review state retains the target commit. Fall back to persisted range OIDs so status can associate that state and its comments with the renamed branch. Assisted-by: Codex:gpt-5.6-sol
1 parent
bb39ca06
crates/brain/src/backend.rs
+110 -15
| 426 | 426 | ||
| 427 | 427 | /// Stored review state before it is joined to local branch rows. |
|
| 428 | 428 | struct StoredReview { |
|
| 429 | 429 | /// Base commit recorded by the review state. |
|
| 430 | 430 | base: Oid, |
|
| 431 | - | /// Current resolution of the base selector, for staleness checks. |
|
| 431 | + | /// Current base selector resolution, or stored base for an unresolved selector. |
|
| 432 | 432 | current_base: Oid, |
|
| 433 | - | /// Current resolution of the target selector, for branch matching. |
|
| 433 | + | /// Current target selector resolution, or stored target for an unresolved selector. |
|
| 434 | 434 | current_target: Oid, |
|
| 435 | 435 | /// Accepted-line progress across the review. |
|
| 436 | 436 | progress: ReviewProgress, |
|
| 437 | 437 | /// Comment storage status for this review. |
|
| 438 | 438 | comments: CommentStatus, |
| 776 | 776 | }); |
|
| 777 | 777 | } |
|
| 778 | 778 | } |
|
| 779 | 779 | ||
| 780 | 780 | for (identity, stored) in stored { |
|
| 781 | - | let (relation, merge) = relation_to_trunk(repository, &trunk_rev, &identity.target)?; |
|
| 781 | + | let target = Rev::from(stored.current_target); |
|
| 782 | + | let (relation, merge) = relation_to_trunk(repository, &trunk_rev, &target)?; |
|
| 782 | 783 | statuses.push(Brain::status_for_stored( |
|
| 783 | 784 | identity.label(), |
|
| 784 | 785 | BrainStatusHead::Stored, |
|
| 785 | 786 | stored.current_base, |
|
| 786 | 787 | relation, |
| 802 | 803 | ||
| 803 | 804 | for (reference, state) in repository.refs(&prefix)? { |
|
| 804 | 805 | let Some(identity) = ReviewIdentity::from_ref(&prefix, &reference) else { |
|
| 805 | 806 | continue; |
|
| 806 | 807 | }; |
|
| 807 | - | // A review whose selectors no longer resolve (deleted branch, |
|
| 808 | - | // rewritten history) is skipped rather than failing the listing. |
|
| 809 | - | let Ok(current_base) = repository.commit_id(&identity.base) else { |
|
| 810 | - | continue; |
|
| 811 | - | }; |
|
| 812 | - | let Ok(current_target) = repository.commit_id(&identity.target) else { |
|
| 813 | - | continue; |
|
| 814 | - | }; |
|
| 815 | 808 | let base = repository.parent(state)?; |
|
| 816 | 809 | let comments_ref = Brain::ref_for_identity(COMMENTS_REF_NAMESPACE, &identity)?; |
|
| 817 | 810 | let message = repository.commit_message(state)?; |
|
| 818 | 811 | let Some(persisted) = decode_state(&message) else { |
|
| 819 | 812 | continue; |
|
| 820 | 813 | }; |
|
| 821 | - | ||
| 814 | + | // Resolved selectors preserve moving-range semantics. When a selector |
|
| 815 | + | // disappears, use the immutable range only while its target commit exists. |
|
| 816 | + | let current_base = repository.commit_id(&identity.base).unwrap_or(base); |
|
| 817 | + | let Ok(current_target) = repository |
|
| 818 | + | .commit_id(&identity.target) |
|
| 819 | + | .or_else(|_| repository.commit_id(&Rev::from(persisted.target))) |
|
| 820 | + | else { |
|
| 821 | + | continue; |
|
| 822 | + | }; |
|
| 822 | 823 | let brain = Brain::new( |
|
| 823 | 824 | repository.clone(), |
|
| 824 | 825 | reference, |
|
| 825 | 826 | comments_ref, |
|
| 826 | 827 | base, |
|
| 827 | 828 | persisted.target, |
|
| 828 | 829 | state, |
|
| 829 | 830 | persisted.accepted, |
|
| 830 | 831 | ); |
|
| 831 | 832 | let comments = brain.comment_status()?; |
|
| 832 | - | let progress = brain.summary(&identity)?.1; |
|
| 833 | + | let target = Rev::from(current_target); |
|
| 834 | + | let progress = brain.summary(&target)?.1; |
|
| 833 | 835 | let recency = repository.commit_time(current_target)?; |
|
| 834 | 836 | let updated = repository.commit_time(state)?; |
|
| 835 | 837 | ||
| 836 | 838 | reviews.insert( |
|
| 837 | 839 | identity, |
| 884 | 886 | } |
|
| 885 | 887 | ||
| 886 | 888 | /// Returns the target commit and accepted-line progress for this review. |
|
| 887 | 889 | /// |
|
| 888 | 890 | /// Progress is counted from changed lines in exact accepted-hunk identities. |
|
| 889 | - | fn summary(&self, identity: &ReviewIdentity) -> Result<(Oid, ReviewProgress), Error> { |
|
| 891 | + | fn summary(&self, target: &Rev) -> Result<(Oid, ReviewProgress), Error> { |
|
| 890 | 892 | let base = Rev::from(self.base); |
|
| 891 | - | let target = &identity.target; |
|
| 892 | 893 | let head = self.repository.commit_id(target)?; |
|
| 893 | 894 | let commits = self.summary_commits(&base, target)?; |
|
| 894 | 895 | let hunks = commits |
|
| 895 | 896 | .iter() |
|
| 896 | 897 | .map(|summary| CommitHunks { |
| 1749 | 1750 | assert_eq!(statuses[0].head, "main"); |
|
| 1750 | 1751 | assert_eq!(statuses[0].head_kind, BrainStatusHead::Branch); |
|
| 1751 | 1752 | assert_ne!(statuses[0].state, BrainStatusState::NoState); |
|
| 1752 | 1753 | } |
|
| 1753 | 1754 | ||
| 1755 | + | #[test] |
|
| 1756 | + | fn statuses_recover_renamed_symbolic_target_by_persisted_oid() { |
|
| 1757 | + | let fixture = TestRepository::new(); |
|
| 1758 | + | let file = fixture.path.join("reviewed.txt"); |
|
| 1759 | + | fs::write(&file, "base\n").unwrap(); |
|
| 1760 | + | fixture.git(&["add", "reviewed.txt"]); |
|
| 1761 | + | fixture.git(&["commit", "-q", "-m", "base"]); |
|
| 1762 | + | fixture.git(&["switch", "-q", "-c", "feature"]); |
|
| 1763 | + | fs::write(&file, "reviewed\n").unwrap(); |
|
| 1764 | + | fixture.git(&["commit", "-q", "-am", "feature"]); |
|
| 1765 | + | let repository = git::Repository::discover(fixture.path.clone()).unwrap(); |
|
| 1766 | + | let identity = ReviewIdentity::new( |
|
| 1767 | + | Rev::try_from("main").unwrap(), |
|
| 1768 | + | Rev::try_from("feature").unwrap(), |
|
| 1769 | + | ); |
|
| 1770 | + | let base = repository.commit_id(&identity.base).unwrap(); |
|
| 1771 | + | let target = repository.commit_id(&identity.target).unwrap(); |
|
| 1772 | + | let brain = Brain::reset(repository.clone(), base, target, &identity).unwrap(); |
|
| 1773 | + | brain |
|
| 1774 | + | .write_comments( |
|
| 1775 | + | r#"{"id":7,"author":"A","timestamp":1,"heading":"h","path":null,"patch_id":"p","ordinal":0,"body":"retained","pending_updates":null}"#, |
|
| 1776 | + | ) |
|
| 1777 | + | .unwrap(); |
|
| 1778 | + | ||
| 1779 | + | fixture.git(&["branch", "-m", "reviewed-feature"]); |
|
| 1780 | + | let statuses = Brain::statuses(&repository).unwrap(); |
|
| 1781 | + | let reviewed = statuses |
|
| 1782 | + | .iter() |
|
| 1783 | + | .filter(|status| status.state != BrainStatusState::NoState) |
|
| 1784 | + | .collect::<Vec<_>>(); |
|
| 1785 | + | ||
| 1786 | + | assert_eq!(reviewed.len(), 1); |
|
| 1787 | + | assert_eq!(reviewed[0].head, "reviewed-feature"); |
|
| 1788 | + | assert_eq!(reviewed[0].head_kind, BrainStatusHead::Branch); |
|
| 1789 | + | assert_eq!(reviewed[0].state, BrainStatusState::InReview); |
|
| 1790 | + | assert_eq!(reviewed[0].comments, CommentStatus::Count(1)); |
|
| 1791 | + | assert!(reviewed[0].progress.total > 0); |
|
| 1792 | + | } |
|
| 1793 | + | ||
| 1794 | + | #[test] |
|
| 1795 | + | fn statuses_skip_pruned_target_without_hiding_renamed_review() { |
|
| 1796 | + | let fixture = TestRepository::new(); |
|
| 1797 | + | let file = fixture.path.join("reviewed.txt"); |
|
| 1798 | + | fs::write(&file, "base\n").unwrap(); |
|
| 1799 | + | fixture.git(&["add", "reviewed.txt"]); |
|
| 1800 | + | fixture.git(&["commit", "-q", "-m", "base"]); |
|
| 1801 | + | fixture.git(&["switch", "-q", "-c", "feature"]); |
|
| 1802 | + | fs::write(&file, "reviewed\n").unwrap(); |
|
| 1803 | + | fixture.git(&["commit", "-q", "-am", "feature"]); |
|
| 1804 | + | let repository = git::Repository::discover(fixture.path.clone()).unwrap(); |
|
| 1805 | + | let renamed = ReviewIdentity::new( |
|
| 1806 | + | Rev::try_from("main").unwrap(), |
|
| 1807 | + | Rev::try_from("feature").unwrap(), |
|
| 1808 | + | ); |
|
| 1809 | + | let base = repository.commit_id(&renamed.base).unwrap(); |
|
| 1810 | + | let renamed_target = repository.commit_id(&renamed.target).unwrap(); |
|
| 1811 | + | let _renamed_brain = |
|
| 1812 | + | Brain::reset(repository.clone(), base, renamed_target, &renamed).unwrap(); |
|
| 1813 | + | fixture.git(&["branch", "-m", "reviewed-feature"]); |
|
| 1814 | + | ||
| 1815 | + | fixture.git(&["switch", "-q", "main"]); |
|
| 1816 | + | fixture.git(&["switch", "-q", "-c", "abandoned"]); |
|
| 1817 | + | fs::write(&file, "abandoned\n").unwrap(); |
|
| 1818 | + | fixture.git(&["commit", "-q", "-am", "abandoned"]); |
|
| 1819 | + | let abandoned = ReviewIdentity::new( |
|
| 1820 | + | Rev::try_from("main").unwrap(), |
|
| 1821 | + | Rev::try_from("abandoned").unwrap(), |
|
| 1822 | + | ); |
|
| 1823 | + | let abandoned_target = repository.commit_id(&abandoned.target).unwrap(); |
|
| 1824 | + | let _abandoned_brain = |
|
| 1825 | + | Brain::reset(repository.clone(), base, abandoned_target, &abandoned).unwrap(); |
|
| 1826 | + | fixture.git(&["switch", "-q", "main"]); |
|
| 1827 | + | fixture.git(&["branch", "-D", "abandoned"]); |
|
| 1828 | + | fixture.git(&[ |
|
| 1829 | + | "reflog", |
|
| 1830 | + | "expire", |
|
| 1831 | + | "--expire=now", |
|
| 1832 | + | "--expire-unreachable=now", |
|
| 1833 | + | "--all", |
|
| 1834 | + | ]); |
|
| 1835 | + | fixture.git(&["gc", "--prune=now"]); |
|
| 1836 | + | assert!(repository.commit_id(&Rev::from(abandoned_target)).is_err()); |
|
| 1837 | + | ||
| 1838 | + | let statuses = Brain::statuses(&repository).unwrap(); |
|
| 1839 | + | let renamed = statuses |
|
| 1840 | + | .iter() |
|
| 1841 | + | .find(|status| status.head == "reviewed-feature") |
|
| 1842 | + | .unwrap(); |
|
| 1843 | + | ||
| 1844 | + | assert_eq!(statuses.len(), 2); |
|
| 1845 | + | assert_eq!(renamed.head_kind, BrainStatusHead::Branch); |
|
| 1846 | + | assert_ne!(renamed.state, BrainStatusState::NoState); |
|
| 1847 | + | } |
|
| 1848 | + | ||
| 1754 | 1849 | /// Returns an accepted identity for one test commit and hunk. |
|
| 1755 | 1850 | fn accepted_hunk(commit: &str, patch: &str, ordinal: u32, total: u32) -> AcceptedHunk { |
|
| 1756 | 1851 | AcceptedHunk { |
|
| 1757 | 1852 | commit: commit.parse().unwrap(), |
|
| 1758 | 1853 | patch_id: PatchId::from(patch.to_owned()), |
f
+0 -0
new.rs
+0 -0
image.png
+0 -0
new.txt
+0 -0