brain: Preserve acceptance after amend
7d722837bf84612d327970c6a4e6939910a98802
Treat a changed target OID as stale review state so the existing rebase flow remaps accepted hunks onto the amended commit. Unchanged hunks keep their acceptance while changed hunks remain pending. Assisted-by: Codex:gpt-5.6
1 parent
72285fee
crates/brain/src/backend.rs
+36 -21
| 1039 | 1039 | let comments_ref = Brain::ref_for_identity(COMMENTS_REF_NAMESPACE, identity)?; |
|
| 1040 | 1040 | let (commit, persisted) = match repository.optional_ref(&state_ref)? { |
|
| 1041 | 1041 | Some(existing) => { |
|
| 1042 | 1042 | let parent = repository.parent(existing)?; |
|
| 1043 | 1043 | if parent != base { |
|
| 1044 | - | return Err(Error::StaleReviewState { |
|
| 1045 | - | stored_base: parent, |
|
| 1046 | - | current_base: base, |
|
| 1047 | - | }); |
|
| 1044 | + | return Err(Error::StaleReviewState); |
|
| 1048 | 1045 | } |
|
| 1049 | 1046 | let message = repository.commit_message(existing)?; |
|
| 1050 | 1047 | let persisted = decode_state(&message).ok_or(Error::InvalidReviewState)?; |
|
| 1048 | + | if persisted.target != target { |
|
| 1049 | + | return Err(Error::StaleReviewState); |
|
| 1050 | + | } |
|
| 1051 | 1051 | (existing, persisted) |
|
| 1052 | 1052 | } |
|
| 1053 | 1053 | None => { |
|
| 1054 | 1054 | let commit = Brain::fresh_state_commit(&repository, base, target)?; |
|
| 1055 | 1055 | repository.update_ref(&state_ref, commit)?; |
| 1695 | 1695 | Some(comments) |
|
| 1696 | 1696 | ); |
|
| 1697 | 1697 | } |
|
| 1698 | 1698 | ||
| 1699 | 1699 | #[test] |
|
| 1700 | - | fn rebase_remaps_acceptance_when_only_target_moves() { |
|
| 1700 | + | fn rebase_keeps_unchanged_hunk_acceptance_after_amend() { |
|
| 1701 | 1701 | let repository = TestRepository::new(); |
|
| 1702 | - | let file = repository.path.join("reviewed.txt"); |
|
| 1703 | - | fs::write(&file, "base\n").unwrap(); |
|
| 1704 | - | repository.git(&["add", "reviewed.txt"]); |
|
| 1702 | + | let reviewed = repository.path.join("reviewed.txt"); |
|
| 1703 | + | let amended = repository.path.join("amended.txt"); |
|
| 1704 | + | fs::write(&reviewed, "base\n").unwrap(); |
|
| 1705 | + | fs::write(&amended, "base\n").unwrap(); |
|
| 1706 | + | repository.git(&["add", "reviewed.txt", "amended.txt"]); |
|
| 1705 | 1707 | repository.git(&["commit", "-q", "-m", "base"]); |
|
| 1706 | 1708 | repository.git(&["switch", "-q", "-c", "topic"]); |
|
| 1707 | - | fs::write(&file, "accepted\n").unwrap(); |
|
| 1709 | + | fs::write(&reviewed, "accepted\n").unwrap(); |
|
| 1710 | + | fs::write(&amended, "first\n").unwrap(); |
|
| 1708 | 1711 | repository.git(&["commit", "-q", "-am", "T1"]); |
|
| 1709 | 1712 | ||
| 1710 | 1713 | let base = Rev::try_from("main").unwrap(); |
|
| 1711 | 1714 | let target = Rev::try_from("topic").unwrap(); |
|
| 1712 | 1715 | let backend = GitBackend::new(&repository.path, base, target).unwrap(); |
|
| 1713 | 1716 | let first = backend.commits().unwrap().pop().unwrap(); |
|
| 1714 | 1717 | let first_document = backend.diff_document(first.id).unwrap().split_hunks(); |
|
| 1715 | 1718 | let mut brain = backend.reset_brain().unwrap(); |
|
| 1716 | - | let first_identity = brain |
|
| 1717 | - | .hunk_identities(&first_document, first.id) |
|
| 1718 | - | .unwrap() |
|
| 1719 | - | .into_values() |
|
| 1720 | - | .next() |
|
| 1719 | + | let first_identities = brain.hunk_identities(&first_document, first.id).unwrap(); |
|
| 1720 | + | let first_file = first_document |
|
| 1721 | + | .files |
|
| 1722 | + | .iter() |
|
| 1723 | + | .find(|file| { |
|
| 1724 | + | file.header |
|
| 1725 | + | .display_path() |
|
| 1726 | + | .is_some_and(|path| path == Path::new("reviewed.txt")) |
|
| 1727 | + | }) |
|
| 1721 | 1728 | .unwrap(); |
|
| 1722 | - | let first_file = &first_document.files[0]; |
|
| 1729 | + | let first_identity = first_identities[&first_file.hunks[0].id].clone(); |
|
| 1723 | 1730 | let first_patches = FilePatches::new(first_file, first_file.hunks.clone()).unwrap(); |
|
| 1724 | 1731 | brain.accept([first_identity], [first_patches]).unwrap(); |
|
| 1725 | 1732 | ||
| 1726 | - | repository.git(&["commit", "-q", "--amend", "-m", "T2"]); |
|
| 1733 | + | fs::write(&amended, "second\n").unwrap(); |
|
| 1734 | + | repository.git(&["commit", "-q", "-am", "T2", "--amend"]); |
|
| 1727 | 1735 | let second = backend.commits().unwrap().pop().unwrap(); |
|
| 1728 | 1736 | let second_document = backend.diff_document(second.id).unwrap().split_hunks(); |
|
| 1729 | - | let second_identity = brain |
|
| 1730 | - | .hunk_identities(&second_document, second.id) |
|
| 1731 | - | .unwrap() |
|
| 1732 | - | .into_values() |
|
| 1733 | - | .next() |
|
| 1737 | + | let second_identities = brain.hunk_identities(&second_document, second.id).unwrap(); |
|
| 1738 | + | let second_file = second_document |
|
| 1739 | + | .files |
|
| 1740 | + | .iter() |
|
| 1741 | + | .find(|file| { |
|
| 1742 | + | file.header |
|
| 1743 | + | .display_path() |
|
| 1744 | + | .is_some_and(|path| path == Path::new("reviewed.txt")) |
|
| 1745 | + | }) |
|
| 1734 | 1746 | .unwrap(); |
|
| 1747 | + | let second_identity = second_identities[&second_file.hunks[0].id].clone(); |
|
| 1748 | + | ||
| 1749 | + | assert!(matches!(backend.brain(), Err(Error::StaleReviewState))); |
|
| 1735 | 1750 | ||
| 1736 | 1751 | let rebased = backend.rebase_brain().unwrap(); |
|
| 1737 | 1752 | ||
| 1738 | 1753 | assert_eq!(rebased.state_ref, brain.state_ref); |
|
| 1739 | 1754 | assert_eq!(rebased.target, second.id); |
f
+0 -0
new.rs
+0 -0
image.png
+0 -0
new.txt
+0 -0
crates/brain/src/error.rs
+6 -15
| 1 | 1 | //! Error handling for the `brain` binary. |
|
| 2 | 2 | ||
| 3 | 3 | use std::io; |
|
| 4 | 4 | ||
| 5 | 5 | use rustik_diff::DecodeError; |
|
| 6 | - | use rustik_repo::{Oid, RepoError}; |
|
| 6 | + | use rustik_repo::RepoError; |
|
| 7 | 7 | ||
| 8 | 8 | /// Top-level error type for the binary. |
|
| 9 | 9 | #[derive(Debug)] |
|
| 10 | 10 | pub enum Error { |
|
| 11 | 11 | /// I/O failed. |
| 14 | 14 | Backend(i32), |
|
| 15 | 15 | /// A Git plumbing command failed, carrying its diagnostic. |
|
| 16 | 16 | Git(String), |
|
| 17 | 17 | /// A hidden-tree merge encountered a content conflict. |
|
| 18 | 18 | MergeConflict(String), |
|
| 19 | - | /// The persisted review state belongs to another base commit. |
|
| 20 | - | StaleReviewState { |
|
| 21 | - | /// Base commit recorded by the persisted state commit. |
|
| 22 | - | stored_base: Oid, |
|
| 23 | - | /// Base commit requested by the current review. |
|
| 24 | - | current_base: Oid, |
|
| 25 | - | }, |
|
| 19 | + | /// The persisted review state belongs to another resolved range. |
|
| 20 | + | StaleReviewState, |
|
| 26 | 21 | /// Persisted review state is not valid. |
|
| 27 | 22 | InvalidReviewState, |
|
| 28 | 23 | /// Diff parsing failed. |
|
| 29 | 24 | Parse(DecodeError), |
|
| 30 | 25 | /// Highlighting asset setup failed, carrying the backend message. |
| 70 | 65 | match self { |
|
| 71 | 66 | Self::Io(error) => write!(f, "{error}"), |
|
| 72 | 67 | Self::Backend(code) => write!(f, "backend command exited with status {code}"), |
|
| 73 | 68 | Self::Git(message) => write!(f, "{message}"), |
|
| 74 | 69 | Self::MergeConflict(message) => write!(f, "merge conflict: {message}"), |
|
| 75 | - | Self::StaleReviewState { |
|
| 76 | - | stored_base, |
|
| 77 | - | current_base, |
|
| 78 | - | } => write!( |
|
| 79 | - | f, |
|
| 80 | - | "stored review state is for base {stored_base}, but current base is {current_base}" |
|
| 81 | - | ), |
|
| 70 | + | Self::StaleReviewState => { |
|
| 71 | + | f.write_str("stored review state is for a different resolved range") |
|
| 72 | + | } |
|
| 82 | 73 | Self::InvalidReviewState => { |
|
| 83 | 74 | f.write_str("stored review state is invalid; reset the review to continue") |
|
| 84 | 75 | } |
|
| 85 | 76 | Self::Parse(error) => write!(f, "{error}"), |
|
| 86 | 77 | Self::Highlight(error) => write!(f, "{error}"), |
crates/brain/src/main.rs
+2 -2
| 178 | 178 | /// Creates the dialog widget for the current frame. |
|
| 179 | 179 | fn dialog(self) -> Dialog { |
|
| 180 | 180 | Dialog::error() |
|
| 181 | 181 | .body([ |
|
| 182 | 182 | Line::new("The existing review is stale."), |
|
| 183 | - | Line::new("Its original base commit no longer matches this branch."), |
|
| 183 | + | Line::new("Its resolved commits no longer match this branch."), |
|
| 184 | 184 | Line::blank(), |
|
| 185 | 185 | Line::new("Rebasing carries matching accepted hunks forward."), |
|
| 186 | 186 | Line::new("Changed or new hunks are left for review."), |
|
| 187 | 187 | ]) |
|
| 188 | 188 | .actions( |
| 234 | 234 | let mut terminal = None; |
|
| 235 | 235 | let mut events = None; |
|
| 236 | 236 | let brain = match state { |
|
| 237 | 237 | ReviewStateLoad::Existing => match backend.brain() { |
|
| 238 | 238 | Ok(brain) => brain, |
|
| 239 | - | Err(Error::StaleReviewState { .. }) => { |
|
| 239 | + | Err(Error::StaleReviewState) => { |
|
| 240 | 240 | let terminal = terminal.get_or_insert(open_terminal()?); |
|
| 241 | 241 | let events = events.get_or_insert_with(session::Events::spawn); |
|
| 242 | 242 | let action = prompt_stale_review_state(terminal, events); |
|
| 243 | 243 | let cursor = terminal.set_cursor_visible(true); |
|
| 244 | 244 | let action = action?; |