brain: Asynchronous hunk acceptance
10d4bec06d14a32003f8dfe74fed0e55623310f6
1 parent
da3ee72c
crates/brain/src/backend.rs
+7 -2
| 187 | 187 | }); |
|
| 188 | 188 | } |
|
| 189 | 189 | } |
|
| 190 | 190 | ||
| 191 | 191 | /// Authoritative accepted-hunk state for one review. |
|
| 192 | - | type Accepted = BTreeSet<AcceptedHunk>; |
|
| 192 | + | pub(crate) type Accepted = BTreeSet<AcceptedHunk>; |
|
| 193 | 193 | ||
| 194 | 194 | /// Persisted state for one resolved target of a selector-keyed review. |
|
| 195 | 195 | #[derive(Clone, Debug, Eq, PartialEq)] |
|
| 196 | 196 | struct PersistedState { |
|
| 197 | 197 | /// Target commit whose hunks the accepted identities describe. |
| 689 | 689 | /// Target commit whose hunks the accepted identities describe. |
|
| 690 | 690 | target: Oid, |
|
| 691 | 691 | /// Current synthetic state commit. |
|
| 692 | 692 | commit: Oid, |
|
| 693 | 693 | /// Exact hunks accepted by the reviewer. |
|
| 694 | - | accepted: Accepted, |
|
| 694 | + | pub(crate) accepted: Accepted, |
|
| 695 | 695 | } |
|
| 696 | 696 | ||
| 697 | 697 | impl Brain { |
|
| 698 | 698 | /// Creates a review state handle. |
|
| 699 | 699 | fn new( |
| 1194 | 1194 | let blob = self.repository.write_blob(text.as_bytes())?; |
|
| 1195 | 1195 | self.repository.update_ref(&self.comments_ref, blob)?; |
|
| 1196 | 1196 | Ok(()) |
|
| 1197 | 1197 | } |
|
| 1198 | 1198 | ||
| 1199 | + | /// Rolls back accepted-hunk state without writing it to the repository. |
|
| 1200 | + | pub(crate) fn rollback(&mut self, accepted: Accepted) { |
|
| 1201 | + | self.accepted = accepted; |
|
| 1202 | + | } |
|
| 1203 | + | ||
| 1199 | 1204 | /// Marks hunks accepted and updates the derived tree. |
|
| 1200 | 1205 | pub fn accept( |
|
| 1201 | 1206 | &mut self, |
|
| 1202 | 1207 | hunks: impl IntoIterator<Item = AcceptedHunk>, |
|
| 1203 | 1208 | files: impl IntoIterator<Item = FilePatches>, |
f
+0 -0
new.rs
+0 -0
image.png
+0 -0
new.txt
+0 -0
crates/brain/src/session.rs
+249 -94
| 2 | 2 | ||
| 3 | 3 | use std::collections::BTreeSet; |
|
| 4 | 4 | use std::io; |
|
| 5 | 5 | use std::mem; |
|
| 6 | 6 | use std::sync::mpsc::{self, Receiver, Sender}; |
|
| 7 | + | use std::thread::{self, JoinHandle}; |
|
| 7 | 8 | ||
| 8 | 9 | use rustik_diff::{ |
|
| 9 | - | ContextChange, DiffAction, DiffDocument, DiffFocus, DiffOptions, DiffViewer, FileDiff, |
|
| 10 | - | FileTreeMode, HighlightOutput, HighlightRequest, Highlighter, ReviewProgress, ViewChange, |
|
| 11 | - | spawn_highlighter, |
|
| 10 | + | ContextChange, DiffAction, DiffDocument, DiffFocus, DiffOptions, DiffState, DiffViewer, |
|
| 11 | + | FileDiff, FileTreeMode, HighlightOutput, HighlightRequest, Highlighter, ReviewProgress, |
|
| 12 | + | ViewChange, spawn_highlighter, |
|
| 12 | 13 | }; |
|
| 13 | 14 | use rustik_repo::DiffStat; |
|
| 14 | 15 | use rustik_tui::{ |
|
| 15 | 16 | Backend as TuiBackend, Constraint, Control, Event, Key, Line, List, ListState, Mouse, |
|
| 16 | 17 | MouseButton, Rect, ScrollView, Style, Table, TableOptions, Terminal, |
|
| 17 | 18 | }; |
|
| 18 | 19 | use rustik_tui_termion as tui_termion; |
|
| 19 | 20 | ||
| 20 | - | use crate::backend::{Backend, Brain, Commit, CommitHunks, FilePatches, HistoryStatus}; |
|
| 21 | + | use crate::backend::{Accepted, Backend, Brain, Commit, CommitHunks, FilePatches, HistoryStatus}; |
|
| 21 | 22 | use crate::comments::ReviewComments; |
|
| 22 | 23 | use crate::error::Error; |
|
| 23 | 24 | use crate::review::{FileVisibility, HunkVisibility, Visibility}; |
|
| 24 | 25 | ||
| 25 | 26 | mod commit; |
| 182 | 183 | let mut session = Session { |
|
| 183 | 184 | terminal, |
|
| 184 | 185 | commits: slots, |
|
| 185 | 186 | range, |
|
| 186 | 187 | history_status, |
|
| 187 | - | accepted_hunks: Vec::new(), |
|
| 188 | + | accepted: Vec::new(), |
|
| 189 | + | pending: None, |
|
| 188 | 190 | current: 0, |
|
| 189 | 191 | hunk_visibility: HunkVisibility::default(), |
|
| 190 | 192 | mode, |
|
| 191 | 193 | parked: None, |
|
| 192 | 194 | list: ListState::new(), |
| 230 | 232 | } |
|
| 231 | 233 | } |
|
| 232 | 234 | Ok(()) |
|
| 233 | 235 | } |
|
| 234 | 236 | ||
| 235 | - | /// Returns the control flow for a terminal interrupt in the active mode. |
|
| 236 | - | fn terminal_interrupt(context: ActionContext, event: &Event<()>) -> Control { |
|
| 237 | - | if context != ActionContext::HunkEdit && matches!(event, Event::Key(Key::Ctrl('c'))) { |
|
| 238 | - | Control::Quit |
|
| 239 | - | } else { |
|
| 240 | - | Control::Continue |
|
| 241 | - | } |
|
| 237 | + | /// Commit-local hunk location used by acceptance and undo state. |
|
| 238 | + | type HunkLocation = (usize, rustik_diff::HunkId); |
|
| 239 | + | ||
| 240 | + | /// Viewer state around one optimistic acceptance. |
|
| 241 | + | struct NavigationRollback { |
|
| 242 | + | /// Commit whose viewer changed. |
|
| 243 | + | commit: usize, |
|
| 244 | + | /// Viewer state before the optimistic change. |
|
| 245 | + | before: DiffState, |
|
| 246 | + | /// Viewer state after automatic navigation. |
|
| 247 | + | after: DiffState, |
|
| 248 | + | } |
|
| 249 | + | ||
| 250 | + | /// One background acceptance update and its rollback state. |
|
| 251 | + | struct PendingAcceptance { |
|
| 252 | + | /// Accepted identities restored if persistence fails. |
|
| 253 | + | rollback: Accepted, |
|
| 254 | + | /// Undo stack installed after successful persistence. |
|
| 255 | + | undo: Vec<HunkLocation>, |
|
| 256 | + | /// Navigation restored if the user has not moved again. |
|
| 257 | + | navigation: Option<NavigationRollback>, |
|
| 258 | + | /// Background repository update. |
|
| 259 | + | worker: JoinHandle<Result<Brain, String>>, |
|
| 242 | 260 | } |
|
| 243 | 261 | ||
| 244 | 262 | /// Interactive terminal session state. |
|
| 245 | 263 | struct Session<'a, T: TuiBackend<Error = io::Error>> { |
|
| 246 | 264 | /// Terminal frame target. |
| 250 | 268 | /// User-facing revision range under review. |
|
| 251 | 269 | range: String, |
|
| 252 | 270 | /// Lazily cached global progress and remaining statistics for history. |
|
| 253 | 271 | history_status: Option<HistoryStatus>, |
|
| 254 | 272 | /// Hunk acceptances available to undo, in acceptance order. |
|
| 255 | - | accepted_hunks: Vec<(usize, rustik_diff::HunkId)>, |
|
| 273 | + | accepted: Vec<HunkLocation>, |
|
| 274 | + | /// Acceptance update currently writing to the repository. |
|
| 275 | + | pending: Option<PendingAcceptance>, |
|
| 256 | 276 | /// Index of the commit currently displayed. |
|
| 257 | 277 | current: usize, |
|
| 258 | 278 | /// Set of hunks visible across commit viewers. |
|
| 259 | 279 | hunk_visibility: HunkVisibility, |
|
| 260 | 280 | /// Active view: the commit list, the diff, or a hunk's comment box. |
| 321 | 341 | fn update(&mut self, event: TerminalEvent) -> Result<Update, Error> { |
|
| 322 | 342 | match event { |
|
| 323 | 343 | TerminalEvent::Input(event) => { |
|
| 324 | 344 | self.error = None; |
|
| 325 | 345 | let event = event?; |
|
| 326 | - | if terminal_interrupt(self.action_context(), &event) == Control::Quit { |
|
| 327 | - | return Ok(Update::Quit); |
|
| 346 | + | if self.action_context() != ActionContext::HunkEdit |
|
| 347 | + | && matches!(event, Event::Key(Key::Ctrl('c'))) |
|
| 348 | + | { |
|
| 349 | + | return self.try_quit(); |
|
| 328 | 350 | } |
|
| 329 | 351 | let resize = if matches!(event, Event::Resize(_)) { |
|
| 330 | 352 | Resize::Changed |
|
| 331 | 353 | } else { |
|
| 332 | 354 | Resize::Stable |
| 347 | 369 | } |
|
| 348 | 370 | let area = viewer_area(self.terminal.area()); |
|
| 349 | 371 | self.handle_key(event, area)? |
|
| 350 | 372 | } |
|
| 351 | 373 | }; |
|
| 352 | - | Ok(Update::from_control(control, resize)) |
|
| 374 | + | let update = Update::from_control(control, resize); |
|
| 375 | + | if update == Update::Quit { |
|
| 376 | + | self.try_quit() |
|
| 377 | + | } else { |
|
| 378 | + | Ok(update) |
|
| 379 | + | } |
|
| 353 | 380 | } |
|
| 354 | 381 | TerminalEvent::Highlight(generation, HighlightOutput::Highlights(results)) => { |
|
| 355 | 382 | if generation == self.generation |
|
| 356 | 383 | && self |
|
| 357 | 384 | .active_viewer() |
| 383 | 410 | Update::Redraw |
|
| 384 | 411 | } else { |
|
| 385 | 412 | Update::Continue |
|
| 386 | 413 | }) |
|
| 387 | 414 | } |
|
| 415 | + | TerminalEvent::Persistence(worker) => { |
|
| 416 | + | let current = self |
|
| 417 | + | .pending |
|
| 418 | + | .as_ref() |
|
| 419 | + | .is_some_and(|pending| pending.worker.thread().id() == worker); |
|
| 420 | + | if current { |
|
| 421 | + | self.sync_acceptance()?; |
|
| 422 | + | Ok(Update::Redraw) |
|
| 423 | + | } else { |
|
| 424 | + | Ok(Update::Continue) |
|
| 425 | + | } |
|
| 426 | + | } |
|
| 388 | 427 | } |
|
| 389 | 428 | } |
|
| 390 | 429 | ||
| 430 | + | /// Waits for pending acceptance before leaving the session. |
|
| 431 | + | fn try_quit(&mut self) -> Result<Update, Error> { |
|
| 432 | + | self.sync_acceptance()?; |
|
| 433 | + | Ok(if self.error.is_some() { |
|
| 434 | + | Update::Redraw |
|
| 435 | + | } else { |
|
| 436 | + | Update::Quit |
|
| 437 | + | }) |
|
| 438 | + | } |
|
| 439 | + | ||
| 391 | 440 | /// Dispatches one input event, handling review keys before the viewer. |
|
| 392 | 441 | fn handle_key(&mut self, event: Event<()>, area: Rect) -> Result<Control, Error> { |
|
| 442 | + | if self.pending.is_some() && matches!(&event, Event::Key(Key::Char('a' | 'A' | 'u'))) { |
|
| 443 | + | self.sync_acceptance()?; |
|
| 444 | + | if self.error.is_some() { |
|
| 445 | + | return Ok(Control::Redraw); |
|
| 446 | + | } |
|
| 447 | + | } |
|
| 393 | 448 | match event { |
|
| 394 | 449 | Event::Key(Key::Char('a')) => { |
|
| 395 | 450 | self.toggle_focused_hunk(area)?; |
|
| 396 | 451 | Ok(Control::Redraw) |
|
| 397 | 452 | } |
| 502 | 557 | Ok(Control::Continue) |
|
| 503 | 558 | } |
|
| 504 | 559 | } |
|
| 505 | 560 | ||
| 506 | 561 | /// Toggles acceptance of the focused hunk. |
|
| 507 | - | /// |
|
| 508 | - | /// Acceptance updates exact persisted identity state before reconciliation. |
|
| 509 | 562 | fn toggle_focused_hunk(&mut self, area: Rect) -> Result<(), Error> { |
|
| 563 | + | let current = self.current; |
|
| 564 | + | let before = self.current()?.viewer.state().clone(); |
|
| 510 | 565 | let loaded = self.current()?; |
|
| 511 | 566 | let Some(DiffFocus::Hunk(hunk)) = loaded.viewer.state().focus else { |
|
| 512 | 567 | return Ok(()); |
|
| 513 | 568 | }; |
|
| 514 | 569 | if !loaded.identities.contains_key(&hunk) { |
|
| 515 | 570 | return Ok(()); |
|
| 516 | 571 | } |
|
| 517 | 572 | let accepted = loaded.review.is_accepted(hunk); |
|
| 518 | 573 | if accepted { |
|
| 519 | - | self.reject_hunks(BTreeSet::from([(self.current, hunk)]))?; |
|
| 520 | - | self.accepted_hunks |
|
| 521 | - | .retain(|accepted| *accepted != (self.current, hunk)); |
|
| 574 | + | self.reject_hunks(BTreeSet::from([(current, hunk)]))?; |
|
| 522 | 575 | } else { |
|
| 523 | - | self.accept_hunks(BTreeSet::from([(self.current, hunk)]))?; |
|
| 524 | - | self.accepted_hunks.push((self.current, hunk)); |
|
| 576 | + | self.accept_hunks(BTreeSet::from([(current, hunk)]))?; |
|
| 525 | 577 | } |
|
| 526 | - | self.reconcile_reviews()?; |
|
| 527 | 578 | if !accepted && matches!(self.hunk_visibility, HunkVisibility::Pending) { |
|
| 528 | 579 | self.current()?.viewer.apply(DiffAction::NextHunk, area); |
|
| 529 | 580 | } |
|
| 581 | + | self.record_navigation_rollback(current, before)?; |
|
| 530 | 582 | ||
| 531 | 583 | Ok(()) |
|
| 532 | 584 | } |
|
| 585 | + | ||
| 533 | 586 | /// Rejects the most recently accepted hunk that remains accepted. |
|
| 534 | 587 | fn undo_last_hunk_accept(&mut self) -> Result<(), Error> { |
|
| 535 | - | while let Some((index, hunk)) = self.accepted_hunks.pop() { |
|
| 588 | + | while let Some(&(index, hunk)) = self.accepted.last() { |
|
| 536 | 589 | let accepted = self.load(index)?.review.is_accepted(hunk); |
|
| 537 | 590 | if accepted { |
|
| 591 | + | let before = self.load(index)?.viewer.state().clone(); |
|
| 538 | 592 | self.reject_hunks(BTreeSet::from([(index, hunk)]))?; |
|
| 539 | - | self.reconcile_reviews()?; |
|
| 593 | + | self.record_navigation_rollback(index, before)?; |
|
| 540 | 594 | break; |
|
| 541 | 595 | } |
|
| 596 | + | self.accepted.pop(); |
|
| 542 | 597 | } |
|
| 543 | 598 | Ok(()) |
|
| 544 | 599 | } |
|
| 545 | 600 | ||
| 546 | 601 | /// Persists acceptance or rejection of every hunk in the focused file. |
|
| 547 | 602 | fn toggle_focused_file(&mut self, area: Rect) -> Result<(), Error> { |
|
| 548 | 603 | let current = self.current; |
|
| 604 | + | let before = self.current()?.viewer.state().clone(); |
|
| 549 | 605 | let loaded = self.current()?; |
|
| 550 | 606 | let Some(file) = loaded.viewer.focused_file() else { |
|
| 551 | 607 | loaded.viewer.apply(DiffAction::NextHunk, area); |
|
| 552 | 608 | return Ok(()); |
|
| 553 | 609 | }; |
| 560 | 616 | return Ok(()); |
|
| 561 | 617 | }; |
|
| 562 | 618 | let hunk_ids = file.hunks.iter().map(|hunk| hunk.id).collect::<Vec<_>>(); |
|
| 563 | 619 | let accepted = loaded.review.accepts_all(hunk_ids.iter().copied()); |
|
| 564 | 620 | let hunks = hunk_ids |
|
| 565 | - | .iter() |
|
| 566 | - | .copied() |
|
| 621 | + | .into_iter() |
|
| 567 | 622 | .filter(|hunk| loaded.review.is_accepted(*hunk) == accepted) |
|
| 568 | 623 | .filter(|hunk| loaded.identities.contains_key(hunk)) |
|
| 569 | 624 | .map(|hunk| (current, hunk)) |
|
| 570 | 625 | .collect::<BTreeSet<_>>(); |
|
| 571 | 626 | if accepted { |
|
| 572 | - | let rejected = hunk_ids |
|
| 573 | - | .into_iter() |
|
| 574 | - | .map(|hunk| (current, hunk)) |
|
| 575 | - | .collect::<BTreeSet<_>>(); |
|
| 576 | - | self.reject_hunks(rejected.clone())?; |
|
| 577 | - | self.accepted_hunks |
|
| 578 | - | .retain(|accepted| !rejected.contains(accepted)); |
|
| 627 | + | self.reject_hunks(hunks)?; |
|
| 579 | 628 | } else { |
|
| 580 | - | self.accept_hunks(hunks.clone())?; |
|
| 581 | - | self.accepted_hunks.extend(hunks); |
|
| 629 | + | self.accept_hunks(hunks)?; |
|
| 582 | 630 | } |
|
| 583 | - | self.reconcile_reviews()?; |
|
| 631 | + | self.record_navigation_rollback(current, before)?; |
|
| 584 | 632 | Ok(()) |
|
| 585 | 633 | } |
|
| 586 | 634 | ||
| 587 | - | /// Rebuilds accepted state including the selected hunks. |
|
| 588 | - | fn accept_hunks( |
|
| 589 | - | &mut self, |
|
| 590 | - | newly_accepted: BTreeSet<(usize, rustik_diff::HunkId)>, |
|
| 591 | - | ) -> Result<(), Error> { |
|
| 592 | - | for index in 0..self.commits.len() { |
|
| 593 | - | self.load(index)?; |
|
| 594 | - | } |
|
| 595 | - | self.reconcile_reviews()?; |
|
| 596 | - | let identities = newly_accepted |
|
| 635 | + | /// Optimistically accepts the selected hunks and starts persistence. |
|
| 636 | + | fn accept_hunks(&mut self, hunks: BTreeSet<HunkLocation>) -> Result<(), Error> { |
|
| 637 | + | let identities = hunks |
|
| 597 | 638 | .iter() |
|
| 598 | 639 | .filter_map(|(index, hunk)| { |
|
| 599 | 640 | self.commits[*index] |
|
| 600 | 641 | .loaded |
|
| 601 | - | .as_ref()? |
|
| 602 | - | .identities |
|
| 603 | - | .get(hunk) |
|
| 642 | + | .as_ref() |
|
| 643 | + | .and_then(|loaded| loaded.identities.get(hunk)) |
|
| 604 | 644 | .cloned() |
|
| 605 | 645 | }) |
|
| 606 | 646 | .collect::<Vec<_>>(); |
|
| 607 | - | let mut patches = Vec::new(); |
|
| 608 | - | for (index, slot) in self.commits.iter().enumerate() { |
|
| 609 | - | let Some(loaded) = &slot.loaded else { |
|
| 610 | - | continue; |
|
| 611 | - | }; |
|
| 612 | - | for file in &loaded.document.files { |
|
| 613 | - | let hunks = file |
|
| 614 | - | .hunks |
|
| 615 | - | .iter() |
|
| 616 | - | .filter(|hunk| { |
|
| 617 | - | loaded.review.is_accepted(hunk.id) |
|
| 618 | - | || newly_accepted.contains(&(index, hunk.id)) |
|
| 619 | - | }) |
|
| 647 | + | ||
| 648 | + | let mut accepted = self.brain.accepted.clone(); |
|
| 649 | + | accepted.extend(identities.iter().cloned()); |
|
| 650 | + | ||
| 651 | + | let mut undo = self.accepted.clone(); |
|
| 652 | + | undo.extend(hunks); |
|
| 653 | + | ||
| 654 | + | self.persist_acceptance(accepted, undo, move |brain, patches| { |
|
| 655 | + | brain.accept(identities, patches) |
|
| 656 | + | }) |
|
| 657 | + | } |
|
| 658 | + | ||
| 659 | + | /// Optimistically rejects the selected hunks and starts persistence. |
|
| 660 | + | fn reject_hunks(&mut self, hunks: BTreeSet<HunkLocation>) -> Result<(), Error> { |
|
| 661 | + | let identities = hunks |
|
| 662 | + | .iter() |
|
| 663 | + | .filter_map(|(index, hunk)| { |
|
| 664 | + | self.commits[*index] |
|
| 665 | + | .loaded |
|
| 666 | + | .as_ref() |
|
| 667 | + | .and_then(|loaded| loaded.identities.get(hunk)) |
|
| 620 | 668 | .cloned() |
|
| 621 | - | .collect::<Vec<_>>(); |
|
| 622 | - | if !hunks.is_empty() { |
|
| 623 | - | patches.push(FilePatches::new(file, hunks)?); |
|
| 624 | - | } |
|
| 625 | - | } |
|
| 669 | + | }) |
|
| 670 | + | .collect::<BTreeSet<_>>(); |
|
| 671 | + | ||
| 672 | + | let mut accepted = self.brain.accepted.clone(); |
|
| 673 | + | accepted.retain(|identity| !identities.contains(identity)); |
|
| 674 | + | ||
| 675 | + | let mut undo = self.accepted.clone(); |
|
| 676 | + | undo.retain(|hunk| !hunks.contains(hunk)); |
|
| 677 | + | ||
| 678 | + | self.persist_acceptance(accepted, undo, move |brain, patches| { |
|
| 679 | + | brain.reject(&identities, patches) |
|
| 680 | + | }) |
|
| 681 | + | } |
|
| 682 | + | ||
| 683 | + | /// Records automatic navigation for the pending rollback. |
|
| 684 | + | fn record_navigation_rollback( |
|
| 685 | + | &mut self, |
|
| 686 | + | commit: usize, |
|
| 687 | + | before: DiffState, |
|
| 688 | + | ) -> Result<(), Error> { |
|
| 689 | + | let after = self.load(commit)?.viewer.state().clone(); |
|
| 690 | + | if let Some(pending) = &mut self.pending { |
|
| 691 | + | pending.navigation = Some(NavigationRollback { |
|
| 692 | + | commit, |
|
| 693 | + | before, |
|
| 694 | + | after, |
|
| 695 | + | }); |
|
| 626 | 696 | } |
|
| 627 | - | self.brain.accept(identities, patches) |
|
| 697 | + | Ok(()) |
|
| 628 | 698 | } |
|
| 629 | 699 | ||
| 630 | - | /// Rebuilds accepted state without the selected hunks. |
|
| 631 | - | fn reject_hunks( |
|
| 700 | + | /// Shows optimistic state, prepares its projection, and starts persistence. |
|
| 701 | + | fn persist_acceptance( |
|
| 632 | 702 | &mut self, |
|
| 633 | - | rejected: BTreeSet<(usize, rustik_diff::HunkId)>, |
|
| 703 | + | accepted: Accepted, |
|
| 704 | + | undo: Vec<HunkLocation>, |
|
| 705 | + | persist: impl FnOnce(&mut Brain, Vec<FilePatches>) -> Result<(), Error> + Send + 'static, |
|
| 634 | 706 | ) -> Result<(), Error> { |
|
| 635 | - | for index in 0..self.commits.len() { |
|
| 636 | - | self.load(index)?; |
|
| 707 | + | debug_assert!(self.pending.is_none()); |
|
| 708 | + | ||
| 709 | + | let rollback = self.brain.accepted.clone(); |
|
| 710 | + | let mut brain = self.brain.clone(); |
|
| 711 | + | self.brain.rollback(accepted); |
|
| 712 | + | ||
| 713 | + | if let Err(error) = self.reconcile_reviews() { |
|
| 714 | + | self.brain.rollback(rollback); |
|
| 715 | + | self.reconcile_state(); |
|
| 716 | + | return Err(error); |
|
| 637 | 717 | } |
|
| 638 | - | self.reconcile_reviews()?; |
|
| 639 | - | let mut rejected_identities = BTreeSet::new(); |
|
| 640 | - | for (index, hunk) in &rejected { |
|
| 641 | - | let Some(loaded) = &self.commits[*index].loaded else { |
|
| 642 | - | continue; |
|
| 643 | - | }; |
|
| 644 | - | if let Some(identity) = loaded.identities.get(hunk) { |
|
| 645 | - | rejected_identities.insert(identity.clone()); |
|
| 646 | - | } |
|
| 718 | + | if let Err(error) = self.draw_frame() { |
|
| 719 | + | self.brain.rollback(rollback); |
|
| 720 | + | self.reconcile_state(); |
|
| 721 | + | return Err(error.into()); |
|
| 647 | 722 | } |
|
| 723 | + | ||
| 648 | 724 | let mut patches = Vec::new(); |
|
| 649 | - | for (index, slot) in self.commits.iter().enumerate() { |
|
| 650 | - | let Some(loaded) = &slot.loaded else { |
|
| 651 | - | continue; |
|
| 725 | + | for slot in &self.commits { |
|
| 726 | + | let Some(identities) = slot |
|
| 727 | + | .identities |
|
| 728 | + | .as_ref() |
|
| 729 | + | .or_else(|| slot.loaded.as_ref().map(|loaded| &loaded.identities)) |
|
| 730 | + | else { |
|
| 731 | + | self.brain.rollback(rollback); |
|
| 732 | + | self.reconcile_reviews()?; |
|
| 733 | + | return Err(Error::Git( |
|
| 734 | + | "commit slot has no cached identities".to_owned(), |
|
| 735 | + | )); |
|
| 736 | + | }; |
|
| 737 | + | let document = match slot.document() { |
|
| 738 | + | Ok(document) => document, |
|
| 739 | + | Err(error) => { |
|
| 740 | + | self.brain.rollback(rollback); |
|
| 741 | + | self.reconcile_reviews()?; |
|
| 742 | + | return Err(error); |
|
| 743 | + | } |
|
| 652 | 744 | }; |
|
| 653 | - | for file in &loaded.document.files { |
|
| 745 | + | for file in &document.files { |
|
| 654 | 746 | let hunks = file |
|
| 655 | 747 | .hunks |
|
| 656 | 748 | .iter() |
|
| 657 | 749 | .filter(|hunk| { |
|
| 658 | - | loaded.review.is_accepted(hunk.id) && !rejected.contains(&(index, hunk.id)) |
|
| 750 | + | identities |
|
| 751 | + | .get(&hunk.id) |
|
| 752 | + | .is_some_and(|identity| self.brain.accepted.contains(identity)) |
|
| 659 | 753 | }) |
|
| 660 | 754 | .cloned() |
|
| 661 | 755 | .collect::<Vec<_>>(); |
|
| 662 | 756 | if !hunks.is_empty() { |
|
| 663 | - | patches.push(FilePatches::new(file, hunks)?); |
|
| 757 | + | let patch = match FilePatches::new(file, hunks) { |
|
| 758 | + | Ok(patch) => patch, |
|
| 759 | + | Err(error) => { |
|
| 760 | + | self.brain.rollback(rollback); |
|
| 761 | + | self.reconcile_reviews()?; |
|
| 762 | + | return Err(error); |
|
| 763 | + | } |
|
| 764 | + | }; |
|
| 765 | + | patches.push(patch); |
|
| 766 | + | } |
|
| 767 | + | } |
|
| 768 | + | } |
|
| 769 | + | ||
| 770 | + | let events = self.events_tx.clone(); |
|
| 771 | + | let worker = thread::spawn(move || { |
|
| 772 | + | let result = persist(&mut brain, patches) |
|
| 773 | + | .map(|()| brain) |
|
| 774 | + | .map_err(|error| error.to_string()); |
|
| 775 | + | let _ = events.send(TerminalEvent::Persistence(thread::current().id())); |
|
| 776 | + | result |
|
| 777 | + | }); |
|
| 778 | + | self.pending = Some(PendingAcceptance { |
|
| 779 | + | rollback, |
|
| 780 | + | undo, |
|
| 781 | + | navigation: None, |
|
| 782 | + | worker, |
|
| 783 | + | }); |
|
| 784 | + | Ok(()) |
|
| 785 | + | } |
|
| 786 | + | ||
| 787 | + | /// Applies a completed acceptance or restores its optimistic state. |
|
| 788 | + | fn sync_acceptance(&mut self) -> Result<(), Error> { |
|
| 789 | + | let Some(pending) = self.pending.take() else { |
|
| 790 | + | return Ok(()); |
|
| 791 | + | }; |
|
| 792 | + | let PendingAcceptance { |
|
| 793 | + | rollback, |
|
| 794 | + | undo, |
|
| 795 | + | navigation, |
|
| 796 | + | worker, |
|
| 797 | + | } = pending; |
|
| 798 | + | let result = worker |
|
| 799 | + | .join() |
|
| 800 | + | .unwrap_or_else(|_| Err("acceptance update stopped unexpectedly".to_owned())); |
|
| 801 | + | ||
| 802 | + | match result { |
|
| 803 | + | Ok(brain) => { |
|
| 804 | + | self.brain = brain; |
|
| 805 | + | self.accepted = undo; |
|
| 806 | + | } |
|
| 807 | + | Err(message) => { |
|
| 808 | + | let navigation = navigation.and_then(|navigation| { |
|
| 809 | + | let loaded = self.commits.get(navigation.commit)?.loaded.as_ref()?; |
|
| 810 | + | (loaded.viewer.state() == &navigation.after) |
|
| 811 | + | .then_some((navigation.commit, navigation.before)) |
|
| 812 | + | }); |
|
| 813 | + | self.brain.rollback(rollback); |
|
| 814 | + | self.reconcile_reviews()?; |
|
| 815 | + | ||
| 816 | + | if let Some((commit, state)) = navigation { |
|
| 817 | + | self.load(commit)?.viewer.set_state(state); |
|
| 664 | 818 | } |
|
| 819 | + | self.error = Some(message.replace('\n', " · ")); |
|
| 665 | 820 | } |
|
| 666 | 821 | } |
|
| 667 | - | self.brain.reject(&rejected_identities, patches) |
|
| 822 | + | Ok(()) |
|
| 668 | 823 | } |
|
| 669 | 824 | ||
| 670 | 825 | /// Opens the hunk view for the focused hunk to author a comment. |
|
| 671 | 826 | fn enter_hunk_view(&mut self, area: Rect) -> Result<(), Error> { |
|
| 672 | 827 | if !matches!( |
| 679 | 834 | return Ok(()); |
|
| 680 | 835 | }; |
|
| 681 | 836 | // Resume a parked draft on the same hunk instead of discarding it. |
|
| 682 | 837 | if let Some(mut parked) = self.parked.take_if(|parked| parked.hunk == hunk) { |
|
| 683 | 838 | if !parked.is_editing() { |
|
| 684 | - | parked.start_new(); |
|
| 839 | + | parked.enter(); |
|
| 685 | 840 | } |
|
| 686 | 841 | self.mode = Mode::Hunk(parked); |
|
| 687 | 842 | self.restart_highlighter()?; |
|
| 688 | 843 | return Ok(()); |
|
| 689 | 844 | } |
| 701 | 856 | let document = DiffDocument::for_hunks(file, [focused.clone()]); |
|
| 702 | 857 | let mut viewer = DiffViewer::new(document, options); |
|
| 703 | 858 | viewer.load_file_sources(|oid| self.brain.read_blob(oid))?; |
|
| 704 | 859 | ||
| 705 | 860 | let mut view = HunkView::new(hunk, viewer, self.comments.author().to_owned()); |
|
| 706 | - | view.start_new(); |
|
| 861 | + | view.enter(); |
|
| 707 | 862 | self.mode = Mode::Hunk(Box::new(view)); |
|
| 708 | 863 | self.restart_highlighter()?; |
|
| 709 | 864 | ||
| 710 | 865 | Ok(()) |
|
| 711 | 866 | } |
| 787 | 942 | Event::Key(Key::Tab) => { |
|
| 788 | 943 | self.next_tab()?; |
|
| 789 | 944 | return Ok(Control::Redraw); |
|
| 790 | 945 | } |
|
| 791 | 946 | Event::Key(Key::Char('c') | Key::Enter) => { |
|
| 792 | - | view.start_new(); |
|
| 947 | + | view.enter(); |
|
| 793 | 948 | return Ok(Control::Redraw); |
|
| 794 | 949 | } |
|
| 795 | 950 | Event::Key(Key::Char('n')) => { |
|
| 796 | 951 | view.select_next(comment_count); |
|
| 797 | 952 | return Ok(Control::Redraw); |
a.rs
+0 -0
crates/brain/src/session/event.rs
+2 -0
| 109 | 109 | pub(super) enum TerminalEvent { |
|
| 110 | 110 | /// Terminal input event. |
|
| 111 | 111 | Input(io::Result<Event<()>>), |
|
| 112 | 112 | /// Background highlighter output, tagged with its generation. |
|
| 113 | 113 | Highlight(u64, HighlightOutput), |
|
| 114 | + | /// Completion signal from one acceptance persistence thread. |
|
| 115 | + | Persistence(thread::ThreadId), |
|
| 114 | 116 | } |
|
| 115 | 117 | ||
| 116 | 118 | impl TerminalEvent { |
|
| 117 | 119 | /// Receives one channel event, propagating input failures. |
|
| 118 | 120 | pub(super) fn receive<E>(event: Result<Self, E>) -> io::Result<Option<Self>> { |
crates/brain/src/session/hunk.rs
+1 -2
| 73 | 73 | pub(super) fn is_editing(&self) -> bool { |
|
| 74 | 74 | matches!(self.mode, HunkMode::Editing { .. }) |
|
| 75 | 75 | } |
|
| 76 | 76 | ||
| 77 | 77 | /// Starts a new comment draft. |
|
| 78 | - | pub(super) fn start_new(&mut self) { |
|
| 78 | + | pub(super) fn enter(&mut self) { |
|
| 79 | 79 | self.mode = HunkMode::Editing { |
|
| 80 | 80 | selected: self.selected(), |
|
| 81 | 81 | target: CommentEdit::Add, |
|
| 82 | 82 | input: TextInput::new(), |
|
| 83 | 83 | }; |
| 105 | 105 | return None; |
|
| 106 | 106 | }; |
|
| 107 | 107 | if input.is_empty() { |
|
| 108 | 108 | return None; |
|
| 109 | 109 | } |
|
| 110 | - | ||
| 111 | 110 | Some((target.clone(), input.value().to_owned())) |
|
| 112 | 111 | } |
|
| 113 | 112 | ||
| 114 | 113 | /// Applies an edit key to the draft comment. |
|
| 115 | 114 | pub(super) fn input(&mut self, key: rustik_tui::Key) -> bool { |
a.txt
+0 -0