.cargo/
crates/
brain/
src/
session/
commit.rs
9.3 KiB
event.rs
6.2 KiB
hunk.rs
15.5 KiB
status.rs
31.7 KiB
worker.rs
8.6 KiB
backend.rs
104.7 KiB
comments.rs
22.1 KiB
decorate.rs
6.7 KiB
error.rs
2.6 KiB
main.rs
29.2 KiB
review.rs
2.3 KiB
session.rs
74.7 KiB
Cargo.toml
552 B
diff/
files/
highlight/
repo/
signals/
tui/
tui-core/
tui-termion/
tui-widgets/
scripts/
src/
.gitignore
30 B
.gitsigners
198 B
AGENTS.md
1.9 KiB
CONTRIBUTING
1.7 KiB
Cargo.lock
5.4 KiB
Cargo.toml
1.9 KiB
LICENSE
1.1 KiB
README.md
245 B
STYLE
3.7 KiB
clippy.toml
213 B
crates/brain/src/session/worker.rs
raw
| 1 | //! Serialized repository I/O for the interactive session. |
| 2 | //! |
| 3 | //! All repository and filesystem I/O initiated by the UI must be represented by |
| 4 | //! a [`Command`] and executed here. Keeping the backend and writable review state |
| 5 | //! in this worker makes blocking the terminal event loop difficult by construction. |
| 6 | |
| 7 | use std::collections::{BTreeMap, BTreeSet}; |
| 8 | use std::sync::mpsc::{self, Sender}; |
| 9 | use std::thread; |
| 10 | |
| 11 | use rustik_diff::{DiffDocument, DiffOptions, DiffViewer, HunkId}; |
| 12 | use rustik_repo::Oid; |
| 13 | |
| 14 | use crate::backend::{AcceptedHunk, Backend, Brain, Commit, FilePatches}; |
| 15 | use crate::comments::ReviewComments; |
| 16 | |
| 17 | use super::commit::{CommitReview, CommitSlot}; |
| 18 | use super::event::TerminalEvent; |
| 19 | use super::{HistoryStatus, reviewed_slots}; |
| 20 | |
| 21 | /// One review-state mutation executed by the repository worker. |
| 22 | pub(super) enum ReviewMutation { |
| 23 | /// Accepts exact hunk identities and projects their patches. |
| 24 | Accept { |
| 25 | /// Identities being accepted. |
| 26 | identities: Vec<AcceptedHunk>, |
| 27 | /// File patches used to update the projected tree. |
| 28 | patches: Vec<FilePatches>, |
| 29 | }, |
| 30 | /// Rejects exact hunk identities and rebuilds their projected files. |
| 31 | Reject { |
| 32 | /// Identities being rejected. |
| 33 | identities: BTreeSet<AcceptedHunk>, |
| 34 | /// File patches used to update the projected tree. |
| 35 | patches: Vec<FilePatches>, |
| 36 | }, |
| 37 | /// Toggles one commit's rejected decision. |
| 38 | ToggleRejected { |
| 39 | /// Commit whose decision changes. |
| 40 | commit: Oid, |
| 41 | }, |
| 42 | /// Toggles one commit's dropped decision. |
| 43 | ToggleDropped { |
| 44 | /// Commit whose decision changes. |
| 45 | commit: Oid, |
| 46 | }, |
| 47 | } |
| 48 | |
| 49 | /// Inputs needed to construct one commit viewer. |
| 50 | pub(super) struct LoadCommit { |
| 51 | /// Session commit index. |
| 52 | pub(super) index: usize, |
| 53 | /// Commit metadata. |
| 54 | pub(super) commit: Commit, |
| 55 | /// Previously cached split document. |
| 56 | pub(super) document: Option<DiffDocument>, |
| 57 | /// Previously cached exact hunk identities. |
| 58 | pub(super) identities: Option<BTreeMap<HunkId, AcceptedHunk>>, |
| 59 | /// Viewer construction options. |
| 60 | pub(super) options: DiffOptions, |
| 61 | /// Review comments used to decorate the viewer. |
| 62 | pub(super) comments: ReviewComments, |
| 63 | } |
| 64 | |
| 65 | /// Blocking operation sent from the UI to the repository worker. |
| 66 | pub(super) enum Command { |
| 67 | /// Loads and constructs one commit viewer. |
| 68 | LoadCommit(Box<LoadCommit>), |
| 69 | /// Loads history documents and aggregate status. |
| 70 | LoadHistory { |
| 71 | /// Commits to load, oldest first. |
| 72 | commits: Vec<Commit>, |
| 73 | /// Review comments used to count annotations per commit. |
| 74 | comments: ReviewComments, |
| 75 | }, |
| 76 | /// Loads file sources into a focused hunk viewer. |
| 77 | LoadSources { |
| 78 | /// Viewer awaiting file contents. |
| 79 | viewer: Box<DiffViewer>, |
| 80 | /// Hunk that owns the viewer. |
| 81 | hunk: HunkId, |
| 82 | }, |
| 83 | /// Persists one review-state mutation. |
| 84 | Mutate(ReviewMutation), |
| 85 | /// Persists the complete encoded comment store. |
| 86 | WriteComments { |
| 87 | /// Encoded JSONL comment data. |
| 88 | encoded: String, |
| 89 | }, |
| 90 | /// Stops the worker after all preceding commands. |
| 91 | Shutdown, |
| 92 | } |
| 93 | |
| 94 | /// Typed result returned to the terminal event loop. |
| 95 | pub(super) enum Output { |
| 96 | /// One commit viewer finished loading. |
| 97 | CommitLoaded { |
| 98 | /// Session commit index. |
| 99 | index: usize, |
| 100 | /// Loaded viewer or its user-facing failure. |
| 101 | result: Box<Result<CommitReview, String>>, |
| 102 | }, |
| 103 | /// History state finished loading. |
| 104 | HistoryLoaded(Box<Result<(Vec<CommitSlot>, HistoryStatus), String>>), |
| 105 | /// Focused hunk sources finished loading. |
| 106 | SourcesLoaded { |
| 107 | /// Hunk that owns the viewer. |
| 108 | hunk: HunkId, |
| 109 | /// Populated viewer or its user-facing failure. |
| 110 | result: Box<Result<DiffViewer, String>>, |
| 111 | }, |
| 112 | /// A state mutation finished persisting. |
| 113 | Persisted(Box<Result<Option<Brain>, String>>), |
| 114 | } |
| 115 | |
| 116 | /// Handle used by the UI to enqueue repository commands. |
| 117 | pub(super) struct Worker { |
| 118 | /// Command sender owned by the terminal session. |
| 119 | commands: Sender<Command>, |
| 120 | /// Worker thread joined only after queued commands have completed. |
| 121 | thread: Option<thread::JoinHandle<()>>, |
| 122 | } |
| 123 | |
| 124 | impl Worker { |
| 125 | /// Starts the serialized repository worker. |
| 126 | pub(super) fn spawn( |
| 127 | backend: Box<dyn Backend>, |
| 128 | brain: Brain, |
| 129 | events: Sender<TerminalEvent>, |
| 130 | ) -> Self { |
| 131 | let (commands, receiver) = mpsc::channel(); |
| 132 | let thread = thread::spawn(move || { |
| 133 | let mut brain = brain; |
| 134 | while let Ok(command) = receiver.recv() { |
| 135 | let output = match command { |
| 136 | Command::LoadCommit(load) => { |
| 137 | let LoadCommit { |
| 138 | index, |
| 139 | commit, |
| 140 | document, |
| 141 | identities, |
| 142 | options, |
| 143 | comments, |
| 144 | } = *load; |
| 145 | let result = (|| { |
| 146 | let document = match document { |
| 147 | Some(document) => document, |
| 148 | None => backend.diff_document(commit.id)?.split_hunks(), |
| 149 | }; |
| 150 | CommitReview::load( |
| 151 | &commit, document, identities, options, &brain, &comments, |
| 152 | ) |
| 153 | })() |
| 154 | .map_err(|error| error.to_string()); |
| 155 | Output::CommitLoaded { |
| 156 | index, |
| 157 | result: Box::new(result), |
| 158 | } |
| 159 | } |
| 160 | Command::LoadHistory { commits, comments } => { |
| 161 | let result = reviewed_slots(commits, &*backend, &brain, &comments) |
| 162 | .map_err(|error| error.to_string()); |
| 163 | Output::HistoryLoaded(Box::new(result)) |
| 164 | } |
| 165 | Command::LoadSources { viewer, hunk } => { |
| 166 | let mut viewer = *viewer; |
| 167 | let result = viewer |
| 168 | .load_file_sources(|oid| brain.read_blob(oid)) |
| 169 | .map(|()| viewer) |
| 170 | .map_err(|error| error.to_string()); |
| 171 | Output::SourcesLoaded { |
| 172 | hunk, |
| 173 | result: Box::new(result), |
| 174 | } |
| 175 | } |
| 176 | Command::Mutate(mutation) => { |
| 177 | let result = match mutation { |
| 178 | ReviewMutation::Accept { |
| 179 | identities, |
| 180 | patches, |
| 181 | } => brain.accept(identities, patches), |
| 182 | ReviewMutation::Reject { |
| 183 | identities, |
| 184 | patches, |
| 185 | } => brain.reject(&identities, patches), |
| 186 | ReviewMutation::ToggleRejected { commit } => { |
| 187 | brain.toggle_commit_rejected(commit) |
| 188 | } |
| 189 | ReviewMutation::ToggleDropped { commit } => { |
| 190 | brain.toggle_commit_dropped(commit) |
| 191 | } |
| 192 | } |
| 193 | .map(|()| Some(brain.clone())) |
| 194 | .map_err(|error| error.to_string()); |
| 195 | Output::Persisted(Box::new(result)) |
| 196 | } |
| 197 | Command::WriteComments { encoded } => { |
| 198 | let result = brain |
| 199 | .write_comments(&encoded) |
| 200 | .map(|()| None) |
| 201 | .map_err(|error| error.to_string()); |
| 202 | Output::Persisted(Box::new(result)) |
| 203 | } |
| 204 | Command::Shutdown => break, |
| 205 | }; |
| 206 | if events |
| 207 | .send(TerminalEvent::Worker(Box::new(output))) |
| 208 | .is_err() |
| 209 | { |
| 210 | break; |
| 211 | } |
| 212 | } |
| 213 | }); |
| 214 | Self { |
| 215 | commands, |
| 216 | thread: Some(thread), |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /// Enqueues one blocking operation without waiting for it. |
| 221 | pub(super) fn send(&self, command: Command) -> Result<(), String> { |
| 222 | self.commands |
| 223 | .send(command) |
| 224 | .map_err(|_| "repository worker stopped unexpectedly".to_owned()) |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | impl Drop for Worker { |
| 229 | fn drop(&mut self) { |
| 230 | let _ = self.commands.send(Command::Shutdown); |
| 231 | if let Some(thread) = self.thread.take() { |
| 232 | let _ = thread.join(); |
| 233 | } |
| 234 | } |
| 235 | } |