brain: Clear comments when resetting review
4a6561db822e8897dd4902a437184d75d944a925
Reset now deletes the review comment ref after replacing accepted state, so --reset starts a wholly empty review. Ordinary loads and rebases continue reading the existing comment blob. Assisted-by: Codex:gpt-5.6-sol
1 parent
84f14f01
crates/brain/src/backend.rs
+70 -7
| 1023 | 1023 | let message = repository.commit_message(existing)?; |
|
| 1024 | 1024 | let persisted = decode_state(&message).ok_or(Error::InvalidReviewState)?; |
|
| 1025 | 1025 | (existing, persisted) |
|
| 1026 | 1026 | } |
|
| 1027 | 1027 | None => { |
|
| 1028 | - | let commit = Brain::write_fresh_state(&repository, base, target, &state_ref)?; |
|
| 1028 | + | let commit = Brain::fresh_state_commit(&repository, base, target)?; |
|
| 1029 | + | repository.update_ref(&state_ref, commit)?; |
|
| 1029 | 1030 | let persisted = PersistedState { |
|
| 1030 | 1031 | target, |
|
| 1031 | 1032 | accepted: Accepted::new(), |
|
| 1032 | 1033 | }; |
|
| 1033 | 1034 | (commit, persisted) |
| 1051 | 1052 | target: Oid, |
|
| 1052 | 1053 | identity: &ReviewIdentity, |
|
| 1053 | 1054 | ) -> Result<Self, Error> { |
|
| 1054 | 1055 | let state_ref = Brain::ref_for_identity(STATE_REF_NAMESPACE, identity)?; |
|
| 1055 | 1056 | let comments_ref = Brain::ref_for_identity(COMMENTS_REF_NAMESPACE, identity)?; |
|
| 1056 | - | let commit = Brain::write_fresh_state(&repository, base, target, &state_ref)?; |
|
| 1057 | + | let commit = Brain::fresh_state_commit(&repository, base, target)?; |
|
| 1058 | + | repository.update_ref_and_delete(&state_ref, commit, &comments_ref)?; |
|
| 1057 | 1059 | ||
| 1058 | 1060 | Ok(Self::new( |
|
| 1059 | 1061 | repository, |
|
| 1060 | 1062 | state_ref, |
|
| 1061 | 1063 | comments_ref, |
| 1095 | 1097 | } |
|
| 1096 | 1098 | persisted.accepted |
|
| 1097 | 1099 | } |
|
| 1098 | 1100 | None => Accepted::new(), |
|
| 1099 | 1101 | }; |
|
| 1100 | - | let commit = Brain::write_fresh_state(&repository, base, target, &state_ref)?; |
|
| 1102 | + | let commit = Brain::fresh_state_commit(&repository, base, target)?; |
|
| 1103 | + | repository.update_ref(&state_ref, commit)?; |
|
| 1101 | 1104 | let mut brain = Self::new( |
|
| 1102 | 1105 | repository, |
|
| 1103 | 1106 | state_ref, |
|
| 1104 | 1107 | comments_ref, |
|
| 1105 | 1108 | base, |
| 1110 | 1113 | brain.remap(commits, documents, &stored)?; |
|
| 1111 | 1114 | ||
| 1112 | 1115 | Ok(brain) |
|
| 1113 | 1116 | } |
|
| 1114 | 1117 | ||
| 1115 | - | /// Writes an empty accepted-state commit for one resolved review range. |
|
| 1116 | - | fn write_fresh_state( |
|
| 1118 | + | /// Creates an empty accepted-state commit for one resolved review range. |
|
| 1119 | + | fn fresh_state_commit( |
|
| 1117 | 1120 | repository: &git::Repository, |
|
| 1118 | 1121 | base: Oid, |
|
| 1119 | 1122 | target: Oid, |
|
| 1120 | - | state_ref: &Qualified<'_>, |
|
| 1121 | 1123 | ) -> Result<Oid, Error> { |
|
| 1122 | 1124 | let tree = repository.tree_id(base)?; |
|
| 1123 | 1125 | let state = PersistedState { |
|
| 1124 | 1126 | target, |
|
| 1125 | 1127 | accepted: Accepted::new(), |
| 1129 | 1131 | base, |
|
| 1130 | 1132 | "brain", |
|
| 1131 | 1133 | "brain@example.invalid", |
|
| 1132 | 1134 | &encode_state(&state), |
|
| 1133 | 1135 | )?; |
|
| 1134 | - | repository.update_ref(state_ref, fresh)?; |
|
| 1135 | 1136 | ||
| 1136 | 1137 | Ok(fresh) |
|
| 1137 | 1138 | } |
|
| 1138 | 1139 | ||
| 1139 | 1140 | /// Computes the stable Git patch id for `patch` text. |
| 1595 | 1596 | fn drop(&mut self) { |
|
| 1596 | 1597 | fs::remove_dir_all(&self.path).unwrap(); |
|
| 1597 | 1598 | } |
|
| 1598 | 1599 | } |
|
| 1599 | 1600 | ||
| 1601 | + | #[test] |
|
| 1602 | + | fn reset_clears_persisted_comments() { |
|
| 1603 | + | let repository = TestRepository::new(); |
|
| 1604 | + | repository.git(&["commit", "-q", "--allow-empty", "-m", "base"]); |
|
| 1605 | + | repository.git(&["switch", "-q", "-c", "topic"]); |
|
| 1606 | + | repository.git(&["commit", "-q", "--allow-empty", "-m", "target"]); |
|
| 1607 | + | let base = Rev::try_from("main").unwrap(); |
|
| 1608 | + | let target = Rev::try_from("topic").unwrap(); |
|
| 1609 | + | let backend = GitBackend::new(repository.path(), base, target).unwrap(); |
|
| 1610 | + | let comments = r#"{"id":1,"author":"A","timestamp":1,"heading":"h","path":null,"patch_id":"p","body":"comment","pending_updates":null}"#; |
|
| 1611 | + | let brain = backend.reset_brain().unwrap(); |
|
| 1612 | + | brain.write_comments(comments).unwrap(); |
|
| 1613 | + | ||
| 1614 | + | let loaded = backend.brain().unwrap(); |
|
| 1615 | + | assert_eq!(loaded.read_comments().unwrap().as_deref(), Some(comments)); |
|
| 1616 | + | ||
| 1617 | + | let rebased = backend.rebase_brain().unwrap(); |
|
| 1618 | + | assert_eq!(rebased.read_comments().unwrap().as_deref(), Some(comments)); |
|
| 1619 | + | ||
| 1620 | + | let reset = backend.reset_brain().unwrap(); |
|
| 1621 | + | assert_eq!(reset.read_comments().unwrap(), None); |
|
| 1622 | + | } |
|
| 1623 | + | ||
| 1624 | + | #[test] |
|
| 1625 | + | fn reset_ref_update_is_atomic_when_comment_ref_is_locked() { |
|
| 1626 | + | let repository = TestRepository::new(); |
|
| 1627 | + | repository.git(&["commit", "-q", "--allow-empty", "-m", "base"]); |
|
| 1628 | + | repository.git(&["switch", "-q", "-c", "topic"]); |
|
| 1629 | + | repository.git(&["commit", "-q", "--allow-empty", "-m", "target"]); |
|
| 1630 | + | let base = Rev::try_from("main").unwrap(); |
|
| 1631 | + | let target = Rev::try_from("topic").unwrap(); |
|
| 1632 | + | let backend = GitBackend::new(repository.path(), base, target).unwrap(); |
|
| 1633 | + | let brain = backend.reset_brain().unwrap(); |
|
| 1634 | + | brain.write_comments("comment").unwrap(); |
|
| 1635 | + | let state = brain |
|
| 1636 | + | .repository |
|
| 1637 | + | .optional_ref(&brain.state_ref) |
|
| 1638 | + | .unwrap() |
|
| 1639 | + | .unwrap(); |
|
| 1640 | + | let comments = brain |
|
| 1641 | + | .repository |
|
| 1642 | + | .optional_ref(&brain.comments_ref) |
|
| 1643 | + | .unwrap() |
|
| 1644 | + | .unwrap(); |
|
| 1645 | + | repository.git(&["commit", "-q", "--allow-empty", "-m", "moved target"]); |
|
| 1646 | + | let lock = repository |
|
| 1647 | + | .path |
|
| 1648 | + | .join(".git") |
|
| 1649 | + | .join(format!("{}.lock", brain.comments_ref.as_str())); |
|
| 1650 | + | fs::write(lock, "").unwrap(); |
|
| 1651 | + | ||
| 1652 | + | assert!(backend.reset_brain().is_err()); |
|
| 1653 | + | assert_eq!( |
|
| 1654 | + | brain.repository.optional_ref(&brain.state_ref).unwrap(), |
|
| 1655 | + | Some(state) |
|
| 1656 | + | ); |
|
| 1657 | + | assert_eq!( |
|
| 1658 | + | brain.repository.optional_ref(&brain.comments_ref).unwrap(), |
|
| 1659 | + | Some(comments) |
|
| 1660 | + | ); |
|
| 1661 | + | } |
|
| 1662 | + | ||
| 1600 | 1663 | #[test] |
|
| 1601 | 1664 | fn rebase_remaps_acceptance_when_only_target_moves() { |
|
| 1602 | 1665 | let repository = TestRepository::new(); |
|
| 1603 | 1666 | let file = repository.path.join("reviewed.txt"); |
|
| 1604 | 1667 | fs::write(&file, "base\n").unwrap(); |
f
+0 -0
new.rs
+0 -0
image.png
+0 -0
new.txt
+0 -0
crates/repo/src/git.rs
+27 -0
| 865 | 865 | } |
|
| 866 | 866 | Oid::try_from(output.stdout.as_slice()) |
|
| 867 | 867 | .map_err(|_| RepoError::Git("invalid oid from commit-tree".to_owned())) |
|
| 868 | 868 | } |
|
| 869 | 869 | ||
| 870 | + | /// Moves `updated` to `commit` and deletes `deleted` in one transaction. |
|
| 871 | + | fn update_ref_and_delete( |
|
| 872 | + | &self, |
|
| 873 | + | updated: &Qualified<'_>, |
|
| 874 | + | commit: Oid, |
|
| 875 | + | deleted: &Qualified<'_>, |
|
| 876 | + | ) -> Result<(), RepoError> { |
|
| 877 | + | let transaction = format!( |
|
| 878 | + | "update {} {}\ndelete {}\n", |
|
| 879 | + | updated.as_str(), |
|
| 880 | + | commit, |
|
| 881 | + | deleted.as_str() |
|
| 882 | + | ); |
|
| 883 | + | let output = self.run( |
|
| 884 | + | "update-ref", |
|
| 885 | + | &["--stdin"], |
|
| 886 | + | Opts { |
|
| 887 | + | stdin: Some(transaction.as_bytes()), |
|
| 888 | + | ..Opts::default() |
|
| 889 | + | }, |
|
| 890 | + | )?; |
|
| 891 | + | if !output.status.success() { |
|
| 892 | + | return Err(RepoError::git(&output)); |
|
| 893 | + | } |
|
| 894 | + | Ok(()) |
|
| 895 | + | } |
|
| 896 | + | ||
| 870 | 897 | /// Moves `reference` to point at `commit`. |
|
| 871 | 898 | fn update_ref(&self, reference: &Qualified<'_>, commit: Oid) -> Result<(), RepoError> { |
|
| 872 | 899 | let commit = commit.to_string(); |
|
| 873 | 900 | let output = self.run( |
|
| 874 | 901 | "update-ref", |
root\nnew file mode"));
+0 -0
file\n"));
+0 -0
file\n"),
+0 -0
file\n"),
+0 -0
a\n--- a/a\n+++ b/a\n@@ -1 +1 @@\n-old\n+new\n".to_owned(),
+0 -0
b\n--- a/b\n+++ b/b\n@@ -0,0 +1 @@\n+line\n".to_owned(),
+0 -0
crates/repo/src/lib.rs
+8 -0
| 257 | 257 | author: &str, |
|
| 258 | 258 | email: &str, |
|
| 259 | 259 | message: &str, |
|
| 260 | 260 | ) -> Result<Oid, RepoError>; |
|
| 261 | 261 | ||
| 262 | + | /// Moves `updated` to `commit` and deletes `deleted` in one transaction. |
|
| 263 | + | fn update_ref_and_delete( |
|
| 264 | + | &self, |
|
| 265 | + | updated: &Qualified<'_>, |
|
| 266 | + | commit: Oid, |
|
| 267 | + | deleted: &Qualified<'_>, |
|
| 268 | + | ) -> Result<(), RepoError>; |
|
| 269 | + | ||
| 262 | 270 | /// Moves `reference` to point at `commit`. |
|
| 263 | 271 | fn update_ref(&self, reference: &Qualified<'_>, commit: Oid) -> Result<(), RepoError>; |
|
| 264 | 272 | } |
|
| 265 | 273 | ||
| 266 | 274 | /// A diff backend a viewer can re-query for wider hunk context. |