.cargo/
crates/
brain/
src/
session/
commit.rs
9.2 KiB
event.rs
6.2 KiB
hunk.rs
15.6 KiB
status.rs
37.3 KiB
worker.rs
8.2 KiB
backend.rs
112.6 KiB
comments.rs
26.2 KiB
decorate.rs
6.7 KiB
error.rs
2.8 KiB
main.rs
33.3 KiB
review.rs
2.3 KiB
session.rs
80.1 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/commit.rs
raw
| 1 | //! Per-commit review state for the session. |
| 2 | |
| 3 | use std::collections::BTreeMap; |
| 4 | |
| 5 | use rustik_diff::{ |
| 6 | ContextUpdate, DiffDocument, DiffOptions, DiffViewer, FileId, FileStats, HunkId, ReviewProgress, |
| 7 | }; |
| 8 | use rustik_repo::DiffStat; |
| 9 | use rustik_tui::Rect; |
| 10 | |
| 11 | use crate::backend::{AcceptedHunk, Brain, Commit, CommitReviewState, commit_remaining_stats}; |
| 12 | use crate::comments::{CommentTarget, Comments, ReviewComments}; |
| 13 | use crate::decorate::Decorations; |
| 14 | use crate::error::Error; |
| 15 | use crate::review::Review; |
| 16 | |
| 17 | /// One commit in the reviewed range: header data plus lazily decoded state. |
| 18 | pub(super) struct CommitSlot { |
| 19 | /// Commit under review. |
| 20 | pub(super) commit: Commit, |
| 21 | /// Accepted-line progress for this commit. |
| 22 | /// |
| 23 | /// Persisted line totals provide progress before the diff is decoded. |
| 24 | pub(super) progress: ReviewProgress, |
| 25 | /// Total number of hunks in this commit. |
| 26 | pub(super) total_hunks: usize, |
| 27 | /// Number of comments attached to hunks in this commit. |
| 28 | pub(super) comment_count: usize, |
| 29 | /// Final review state of this commit. |
| 30 | pub(super) state: CommitReviewState, |
| 31 | /// Remaining diff statistics for this commit. |
| 32 | pub(super) remaining_stats: DiffStat, |
| 33 | /// Split diff document retained from history progress calculation. |
| 34 | pub(super) document: Option<DiffDocument>, |
| 35 | /// Exact hunk identities retained from history progress calculation. |
| 36 | pub(super) identities: Option<BTreeMap<HunkId, AcceptedHunk>>, |
| 37 | /// Decoded diff state, populated when the commit is first displayed. |
| 38 | pub(super) loaded: Option<CommitReview>, |
| 39 | } |
| 40 | |
| 41 | impl CommitSlot { |
| 42 | /// Returns the original, unfiltered diff document in either slot state. |
| 43 | pub(super) fn document(&self) -> Result<&DiffDocument, Error> { |
| 44 | self.document |
| 45 | .as_ref() |
| 46 | .or_else(|| self.loaded.as_ref().map(|loaded| &loaded.document)) |
| 47 | .ok_or_else(|| Error::Git("commit slot has no diff document".to_owned())) |
| 48 | } |
| 49 | |
| 50 | /// Returns exact hunk identities in either slot state. |
| 51 | pub(super) fn identities(&self) -> Result<&BTreeMap<HunkId, AcceptedHunk>, Error> { |
| 52 | self.identities |
| 53 | .as_ref() |
| 54 | .or_else(|| self.loaded.as_ref().map(|loaded| &loaded.identities)) |
| 55 | .ok_or_else(|| Error::Git("commit slot has no cached identities".to_owned())) |
| 56 | } |
| 57 | |
| 58 | /// Returns the number of canonical hunks cached for this commit. |
| 59 | pub(super) fn hunk_count(&self) -> usize { |
| 60 | self.identities.as_ref().map_or_else( |
| 61 | || { |
| 62 | self.loaded |
| 63 | .as_ref() |
| 64 | .map_or(0, |loaded| loaded.identities.len()) |
| 65 | }, |
| 66 | BTreeMap::len, |
| 67 | ) |
| 68 | } |
| 69 | |
| 70 | /// Returns the remaining diff statistics in either slot state. |
| 71 | pub(super) fn remaining_stats(&self) -> DiffStat { |
| 72 | self.loaded |
| 73 | .as_ref() |
| 74 | .map_or(self.remaining_stats, CommitReview::remaining_stats) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /// Decoded review state for one commit: its viewer, accepted hunks, and the |
| 79 | /// full document used to restore hidden hunks. |
| 80 | /// |
| 81 | /// Each commit keeps its own viewer so its highlight and word-diff caches |
| 82 | /// persist across commit switches, and its own [`Review`] because decoded hunk |
| 83 | /// ids restart per commit. |
| 84 | pub(super) struct CommitReview { |
| 85 | /// Viewer rendering this commit's diff, owning its caches. |
| 86 | pub(super) viewer: DiffViewer, |
| 87 | /// Accepted-hunk state projected from exact persisted identities. |
| 88 | pub(super) review: Review, |
| 89 | /// Per-hunk comment view for this commit. |
| 90 | pub(super) comments: Comments, |
| 91 | /// Exact accepted-state identity, including patch id, of each original hunk. |
| 92 | pub(super) identities: BTreeMap<HunkId, AcceptedHunk>, |
| 93 | /// Full, unfiltered diff document used to restore hidden hunks. |
| 94 | pub(super) document: DiffDocument, |
| 95 | /// Original hunk ids per file, used for decoration snapshots. |
| 96 | pub(super) file_hunks: BTreeMap<FileId, Vec<HunkId>>, |
| 97 | /// Original diff stats per hunk, used for file-level remaining diffstats. |
| 98 | pub(super) hunk_stats: BTreeMap<HunkId, FileStats>, |
| 99 | } |
| 100 | |
| 101 | impl CommitReview { |
| 102 | /// Returns the combined diffstat of files that still need review. |
| 103 | pub(super) fn remaining_stats(&self) -> DiffStat { |
| 104 | commit_remaining_stats(&self.document, self.review.accepted()) |
| 105 | } |
| 106 | |
| 107 | /// Returns whether this commit's active file is expanded to its full source. |
| 108 | pub(super) fn is_full_file(&self) -> bool { |
| 109 | self.viewer.is_full_file() |
| 110 | } |
| 111 | |
| 112 | /// Builds one commit's review state from its split diff document. |
| 113 | /// |
| 114 | /// Hunk identities are computed here once, so later reconciliations only |
| 115 | /// hash residual hunks and comment lookups can match on stable patch ids. |
| 116 | pub(super) fn load( |
| 117 | commit: &Commit, |
| 118 | document: DiffDocument, |
| 119 | identities: Option<BTreeMap<HunkId, AcceptedHunk>>, |
| 120 | options: DiffOptions, |
| 121 | brain: &Brain, |
| 122 | store: &ReviewComments, |
| 123 | ) -> Result<Self, Error> { |
| 124 | let mut viewer = DiffViewer::new(document.clone(), options); |
| 125 | viewer.load_file_sources(|oid| brain.read_blob(oid))?; |
| 126 | let identities = match identities { |
| 127 | Some(identities) => identities, |
| 128 | None => brain.hunk_identities(&document, commit.id)?, |
| 129 | }; |
| 130 | let heading = format!("{} {}", commit.id.short(), commit.summary); |
| 131 | let review = Review::new(); |
| 132 | let mut targets = BTreeMap::new(); |
| 133 | let mut file_hunks = BTreeMap::new(); |
| 134 | let mut hunk_stats = BTreeMap::new(); |
| 135 | |
| 136 | for file in &document.files { |
| 137 | let path = file.header.display_path().map(|path| path.to_path_buf()); |
| 138 | file_hunks.insert( |
| 139 | file.id, |
| 140 | file.hunks.iter().map(|hunk| hunk.id).collect::<Vec<_>>(), |
| 141 | ); |
| 142 | for hunk in &file.hunks { |
| 143 | hunk_stats.insert(hunk.id, hunk.stats()); |
| 144 | // `hunk_identities` covers every hunk of the same document, so |
| 145 | // a miss is a bug: the hunk could never be accepted or |
| 146 | // commented on, which is worse left silent. |
| 147 | let identity = identities |
| 148 | .get(&hunk.id) |
| 149 | .ok_or_else(|| Error::Git(format!("hunk {:?} has no identity", hunk.id)))?; |
| 150 | targets.insert( |
| 151 | hunk.id, |
| 152 | CommentTarget { |
| 153 | commit: commit.id, |
| 154 | heading: heading.clone(), |
| 155 | path: path.clone(), |
| 156 | patch_id: identity.patch_id.clone(), |
| 157 | ordinal: identity.ordinal, |
| 158 | }, |
| 159 | ); |
| 160 | } |
| 161 | } |
| 162 | let comments = Comments::new(store, targets); |
| 163 | let mut loaded = Self { |
| 164 | viewer, |
| 165 | review, |
| 166 | comments, |
| 167 | identities, |
| 168 | document, |
| 169 | file_hunks, |
| 170 | hunk_stats, |
| 171 | }; |
| 172 | loaded.refresh_decorations(); |
| 173 | |
| 174 | Ok(loaded) |
| 175 | } |
| 176 | |
| 177 | /// Reinstalls a fresh decoration snapshot on the viewer. |
| 178 | /// |
| 179 | /// Decorations are immutable snapshots, so this must run after every |
| 180 | /// change to acceptance or comment state for the viewer to observe it. |
| 181 | pub(super) fn refresh_decorations(&mut self) { |
| 182 | self.viewer.set_decorator(Decorations::snapshot( |
| 183 | &self.review, |
| 184 | &self.comments, |
| 185 | &self.file_hunks, |
| 186 | &self.hunk_stats, |
| 187 | self.viewer.options().diff_palette, |
| 188 | )); |
| 189 | } |
| 190 | |
| 191 | /// Applies a context refetch while preserving review hunk identities. |
| 192 | pub(super) fn apply_context_update( |
| 193 | &mut self, |
| 194 | mut update: ContextUpdate, |
| 195 | brain: &Brain, |
| 196 | area: Rect, |
| 197 | ) -> Result<(), Error> { |
| 198 | let mut hunk_ids = BTreeMap::<_, Vec<_>>::new(); |
| 199 | for file in &self.document.files { |
| 200 | for hunk in &file.hunks { |
| 201 | if let Some(identity) = self.identities.get(&hunk.id) { |
| 202 | hunk_ids |
| 203 | .entry((file.id, identity.patch_id.clone())) |
| 204 | .or_default() |
| 205 | .push(hunk.id); |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | for ids in hunk_ids.values_mut() { |
| 210 | ids.reverse(); |
| 211 | } |
| 212 | for file in update.files_mut() { |
| 213 | for index in 0..file.hunks.len() { |
| 214 | let patch_id = brain.hunk_patch_id(file, &file.hunks[index])?; |
| 215 | if let Some(id) = hunk_ids.get_mut(&(file.id, patch_id)).and_then(Vec::pop) { |
| 216 | file.hunks[index].id = id; |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | self.viewer.apply_context_update(update, area); |
| 221 | Ok(()) |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | #[cfg(test)] |
| 226 | mod tests { |
| 227 | use super::*; |
| 228 | use rustik_diff::Decode as _; |
| 229 | |
| 230 | #[test] |
| 231 | fn review_hunks_split_by_change_group() { |
| 232 | let document = DiffDocument::decode_str( |
| 233 | "\ |
| 234 | diff --git a/a.rs b/a.rs |
| 235 | --- a/a.rs |
| 236 | +++ b/a.rs |
| 237 | @@ -1,4 +1,5 @@ |
| 238 | -old |
| 239 | +new |
| 240 | +extra |
| 241 | context |
| 242 | -before |
| 243 | +after |
| 244 | tail |
| 245 | ", |
| 246 | ) |
| 247 | .unwrap(); |
| 248 | |
| 249 | let document = document.split_hunks(); |
| 250 | |
| 251 | assert_eq!(document.files[0].hunks.len(), 2); |
| 252 | assert_eq!(document.files[0].hunks[0].id, HunkId::new(0)); |
| 253 | assert_eq!(document.files[0].hunks[1].id, HunkId::new(1)); |
| 254 | assert_eq!(document.files[0].hunks[0].header.old_line_no, 1); |
| 255 | assert_eq!(document.files[0].hunks[1].header.old_line_no, 2); |
| 256 | } |
| 257 | } |