brain: Show reviewed commit header
4f69c52da41f2bb5e0cee71c2ca25a089df1b49f
Attach the reviewed commit identity and summary to each diff document so the viewer renders its standard commit preamble. Assisted-by: Codex:gpt-5.6
1 parent
3dee01ed
crates/brain/src/backend.rs
+38 -11
| 10 | 10 | use std::collections::{BTreeMap, BTreeSet}; |
|
| 11 | 11 | use std::ffi::OsString; |
|
| 12 | 12 | use std::path::Path; |
|
| 13 | 13 | use std::str::FromStr as _; |
|
| 14 | 14 | ||
| 15 | - | use rustik_diff::{Decode as _, DiffDocument, Encode as _, FileDiff, Hunk, HunkId, ReviewProgress}; |
|
| 15 | + | use rustik_diff::{ |
|
| 16 | + | CommitHeader, Decode as _, DiffDocument, Encode as _, FileDiff, Hunk, HunkId, ReviewProgress, |
|
| 17 | + | }; |
|
| 16 | 18 | use rustik_repo::{ |
|
| 17 | 19 | AheadBehind, CommitTime, DiffStat, MergeStatus, Oid, PatchId, ReadRepository, Rev, |
|
| 18 | 20 | WriteRepository, |
|
| 19 | 21 | git::{ |
|
| 20 | 22 | self, |
| 432 | 434 | /// constructed, so listing commits needs no further arguments. |
|
| 433 | 435 | pub trait Backend { |
|
| 434 | 436 | /// Returns the reviewed commits in `base..target`, oldest first. |
|
| 435 | 437 | fn commits(&self) -> Result<Vec<Commit>, Error>; |
|
| 436 | 438 | ||
| 437 | - | /// Returns `commit`'s diff against its parent as a parsed document. |
|
| 438 | - | fn commit_diff(&self, commit: &Commit) -> Result<DiffDocument, Error>; |
|
| 439 | + | /// Returns `commit`'s header. |
|
| 440 | + | fn commit_header(&self, commit: Oid) -> Result<CommitHeader, Error>; |
|
| 441 | + | ||
| 442 | + | /// Returns `commit`'s file diffs against its parent. |
|
| 443 | + | fn file_diffs(&self, commit: Oid) -> Result<Vec<FileDiff>, Error>; |
|
| 444 | + | ||
| 445 | + | /// Returns `commit`'s header and file diffs as one document. |
|
| 446 | + | fn diff_document(&self, commit: Oid) -> Result<DiffDocument, Error> { |
|
| 447 | + | Ok(DiffDocument { |
|
| 448 | + | commit: Some(self.commit_header(commit)?), |
|
| 449 | + | files: self.file_diffs(commit)?, |
|
| 450 | + | }) |
|
| 451 | + | } |
|
| 439 | 452 | ||
| 440 | 453 | /// Loads the hidden review state for this backend. |
|
| 441 | 454 | fn brain(&self) -> Result<Brain, Error>; |
|
| 442 | 455 | ||
| 443 | 456 | /// Resets and loads the hidden review state for this backend. |
| 1141 | 1154 | .repository |
|
| 1142 | 1155 | .commits(&self.review.base, &self.review.target)?; |
|
| 1143 | 1156 | Commit::with_stats(&self.repository, pairs) |
|
| 1144 | 1157 | } |
|
| 1145 | 1158 | ||
| 1146 | - | fn commit_diff(&self, commit: &Commit) -> Result<DiffDocument, Error> { |
|
| 1147 | - | let bytes = self.repository.commit_diff(commit.id)?; |
|
| 1159 | + | fn commit_header(&self, commit: Oid) -> Result<CommitHeader, Error> { |
|
| 1160 | + | let bytes = self.repository.commit_header(commit)?; |
|
| 1148 | 1161 | let document = DiffDocument::decode_bytes(&bytes)?; |
|
| 1149 | - | Ok(document) |
|
| 1162 | + | document |
|
| 1163 | + | .commit |
|
| 1164 | + | .ok_or_else(|| Error::Git(format!("commit {commit} has no header"))) |
|
| 1165 | + | } |
|
| 1166 | + | ||
| 1167 | + | fn file_diffs(&self, commit: Oid) -> Result<Vec<FileDiff>, Error> { |
|
| 1168 | + | let bytes = self.repository.commit_diff(commit)?; |
|
| 1169 | + | Ok(DiffDocument::decode_bytes(&bytes)?.files) |
|
| 1150 | 1170 | } |
|
| 1151 | 1171 | ||
| 1152 | 1172 | fn brain(&self) -> Result<Brain, Error> { |
|
| 1153 | 1173 | let base = self.repository.commit_id(&self.review.base)?; |
|
| 1154 | 1174 | Brain::load(self.repository.clone(), base, &self.review) |
| 1162 | 1182 | fn rebase_brain(&self) -> Result<Brain, Error> { |
|
| 1163 | 1183 | let base = self.repository.commit_id(&self.review.base)?; |
|
| 1164 | 1184 | let commits = self.commits()?; |
|
| 1165 | 1185 | let documents = commits |
|
| 1166 | 1186 | .iter() |
|
| 1167 | - | .map(|commit| Ok(self.commit_diff(commit)?.split_hunks())) |
|
| 1187 | + | .map(|commit| Ok(self.diff_document(commit.id)?.split_hunks())) |
|
| 1168 | 1188 | .collect::<Result<Vec<_>, Error>>()?; |
|
| 1169 | 1189 | Brain::rebase( |
|
| 1170 | 1190 | self.repository.clone(), |
|
| 1171 | 1191 | base, |
|
| 1172 | 1192 | &self.review, |
| 1207 | 1227 | fn commits(&self) -> Result<Vec<Commit>, Error> { |
|
| 1208 | 1228 | let pairs = self.jj.commits(&self.review.base, &self.review.target)?; |
|
| 1209 | 1229 | Commit::with_stats(&self.repository, pairs) |
|
| 1210 | 1230 | } |
|
| 1211 | 1231 | ||
| 1212 | - | fn commit_diff(&self, commit: &Commit) -> Result<DiffDocument, Error> { |
|
| 1213 | - | let bytes = self.jj.commit_diff(commit.id)?; |
|
| 1232 | + | fn commit_header(&self, commit: Oid) -> Result<CommitHeader, Error> { |
|
| 1233 | + | let bytes = self.repository.commit_header(commit)?; |
|
| 1214 | 1234 | let document = DiffDocument::decode_bytes(&bytes)?; |
|
| 1215 | - | Ok(document) |
|
| 1235 | + | document |
|
| 1236 | + | .commit |
|
| 1237 | + | .ok_or_else(|| Error::Git(format!("commit {commit} has no header"))) |
|
| 1238 | + | } |
|
| 1239 | + | ||
| 1240 | + | fn file_diffs(&self, commit: Oid) -> Result<Vec<FileDiff>, Error> { |
|
| 1241 | + | let bytes = self.jj.commit_diff(commit)?; |
|
| 1242 | + | Ok(DiffDocument::decode_bytes(&bytes)?.files) |
|
| 1216 | 1243 | } |
|
| 1217 | 1244 | ||
| 1218 | 1245 | fn brain(&self) -> Result<Brain, Error> { |
|
| 1219 | 1246 | let base = self.jj.resolve(&self.review.base)?; |
|
| 1220 | 1247 | Brain::load(self.repository.clone(), base, &self.review) |
| 1228 | 1255 | fn rebase_brain(&self) -> Result<Brain, Error> { |
|
| 1229 | 1256 | let base = self.jj.resolve(&self.review.base)?; |
|
| 1230 | 1257 | let commits = self.commits()?; |
|
| 1231 | 1258 | let documents = commits |
|
| 1232 | 1259 | .iter() |
|
| 1233 | - | .map(|commit| Ok(self.commit_diff(commit)?.split_hunks())) |
|
| 1260 | + | .map(|commit| Ok(self.diff_document(commit.id)?.split_hunks())) |
|
| 1234 | 1261 | .collect::<Result<Vec<_>, Error>>()?; |
|
| 1235 | 1262 | Brain::rebase( |
|
| 1236 | 1263 | self.repository.clone(), |
|
| 1237 | 1264 | base, |
|
| 1238 | 1265 | &self.review, |
f
+0 -0
crates/brain/src/session.rs
+1 -1
| 207 | 207 | fn load(&mut self, index: usize) -> Result<&mut CommitReview, Error> { |
|
| 208 | 208 | let slot = &mut self.commits[index]; |
|
| 209 | 209 | let loaded = match slot.loaded.take() { |
|
| 210 | 210 | Some(loaded) => loaded, |
|
| 211 | 211 | None => { |
|
| 212 | - | let document = self.backend.commit_diff(&slot.commit)?; |
|
| 212 | + | let document = self.backend.diff_document(slot.commit.id)?; |
|
| 213 | 213 | CommitReview::load( |
|
| 214 | 214 | &slot.commit, |
|
| 215 | 215 | document, |
|
| 216 | 216 | self.options.clone(), |
|
| 217 | 217 | &self.brain, |
crates/diff/src/commit.rs
+13 -13
| 5 | 5 | ||
| 6 | 6 | use crate::{Oid, OidError}; |
|
| 7 | 7 | ||
| 8 | 8 | /// Parsed commit preamble. |
|
| 9 | 9 | #[derive(Clone, Debug, Eq, PartialEq)] |
|
| 10 | - | pub struct Commit { |
|
| 10 | + | pub struct CommitHeader { |
|
| 11 | 11 | /// Object identifier from the `commit <oid>` line. |
|
| 12 | 12 | pub hash: Oid, |
|
| 13 | 13 | /// Ref decoration following the hash (e.g. `(HEAD -> main, origin/main)`). |
|
| 14 | 14 | pub decoration: Option<String>, |
|
| 15 | 15 | /// Signature verification output between the commit hash and headers. |
| 45 | 45 | fn from(_: OidError) -> Self { |
|
| 46 | 46 | Self |
|
| 47 | 47 | } |
|
| 48 | 48 | } |
|
| 49 | 49 | ||
| 50 | - | impl Commit { |
|
| 50 | + | impl CommitHeader { |
|
| 51 | 51 | /// Returns the first header value matching `name` (case-insensitive). |
|
| 52 | 52 | pub fn header(&self, name: &str) -> Option<&str> { |
|
| 53 | 53 | self.headers |
|
| 54 | 54 | .iter() |
|
| 55 | 55 | .find(|header| header.key.eq_ignore_ascii_case(name)) |
|
| 56 | 56 | .map(|header| header.value.as_str()) |
|
| 57 | 57 | } |
|
| 58 | 58 | } |
|
| 59 | 59 | ||
| 60 | - | impl FromStr for Commit { |
|
| 60 | + | impl FromStr for CommitHeader { |
|
| 61 | 61 | type Err = ParseCommitError; |
|
| 62 | 62 | ||
| 63 | 63 | fn from_str(input: &str) -> Result<Self, Self::Err> { |
|
| 64 | 64 | let mut hash = None; |
|
| 65 | 65 | let mut decoration = None; |
| 103 | 103 | message: lines.collect::<Vec<_>>().join("\n"), |
|
| 104 | 104 | }) |
|
| 105 | 105 | } |
|
| 106 | 106 | } |
|
| 107 | 107 | ||
| 108 | - | impl fmt::Display for Commit { |
|
| 108 | + | impl fmt::Display for CommitHeader { |
|
| 109 | 109 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
|
| 110 | 110 | write!(f, "commit {}", self.hash)?; |
|
| 111 | 111 | if let Some(decoration) = &self.decoration { |
|
| 112 | 112 | write!(f, " {decoration}")?; |
|
| 113 | 113 | } |
| 152 | 152 | const SHA1: &str = "1234567890abcdef1234567890abcdef12345678"; |
|
| 153 | 153 | ||
| 154 | 154 | #[test] |
|
| 155 | 155 | fn parses_headers_only() { |
|
| 156 | 156 | let input = format!("commit {SHA1}\nAuthor: A\n\n"); |
|
| 157 | - | let commit = Commit::from_str(&input).unwrap(); |
|
| 157 | + | let commit = CommitHeader::from_str(&input).unwrap(); |
|
| 158 | 158 | ||
| 159 | 159 | assert_eq!(commit.hash.to_string(), SHA1); |
|
| 160 | 160 | assert_eq!(commit.decoration, None); |
|
| 161 | 161 | assert_eq!(commit.header("author"), Some("A")); |
|
| 162 | 162 | assert!(commit.message.is_empty()); |
| 164 | 164 | ||
| 165 | 165 | #[test] |
|
| 166 | 166 | fn parses_sha256_hash() { |
|
| 167 | 167 | let oid = "0".repeat(64); |
|
| 168 | 168 | let input = format!("commit {oid}\n\n"); |
|
| 169 | - | let commit = Commit::from_str(&input).unwrap(); |
|
| 169 | + | let commit = CommitHeader::from_str(&input).unwrap(); |
|
| 170 | 170 | ||
| 171 | 171 | assert_eq!(commit.hash.to_string(), oid); |
|
| 172 | 172 | } |
|
| 173 | 173 | ||
| 174 | 174 | #[test] |
|
| 175 | 175 | fn parses_decoration_after_hash() { |
|
| 176 | 176 | let input = format!("commit {SHA1} (HEAD -> main)\n\n"); |
|
| 177 | - | let commit = Commit::from_str(&input).unwrap(); |
|
| 177 | + | let commit = CommitHeader::from_str(&input).unwrap(); |
|
| 178 | 178 | ||
| 179 | 179 | assert_eq!(commit.hash.to_string(), SHA1); |
|
| 180 | 180 | assert_eq!(commit.decoration.as_deref(), Some("(HEAD -> main)")); |
|
| 181 | 181 | } |
|
| 182 | 182 | ||
| 183 | 183 | #[test] |
|
| 184 | 184 | fn parses_signature_before_headers() { |
|
| 185 | 185 | let input = |
|
| 186 | 186 | format!("commit {SHA1}\nGood signature\nNo principal matched.\nAuthor: A\nDate: B\n\n"); |
|
| 187 | - | let commit = Commit::from_str(&input).unwrap(); |
|
| 187 | + | let commit = CommitHeader::from_str(&input).unwrap(); |
|
| 188 | 188 | ||
| 189 | 189 | assert_eq!( |
|
| 190 | 190 | commit.signature, |
|
| 191 | 191 | ["Good signature", "No principal matched."] |
|
| 192 | 192 | ); |
| 196 | 196 | } |
|
| 197 | 197 | ||
| 198 | 198 | #[test] |
|
| 199 | 199 | fn parses_message_with_indent_preserved() { |
|
| 200 | 200 | let input = format!("commit {SHA1}\nAuthor: A\n\n Subject\n\n Body\n"); |
|
| 201 | - | let commit = Commit::from_str(&input).unwrap(); |
|
| 201 | + | let commit = CommitHeader::from_str(&input).unwrap(); |
|
| 202 | 202 | ||
| 203 | 203 | assert_eq!(commit.message, " Subject\n\n Body\n"); |
|
| 204 | 204 | } |
|
| 205 | 205 | ||
| 206 | 206 | #[test] |
|
| 207 | 207 | fn rejects_input_without_commit_line() { |
|
| 208 | 208 | assert_eq!( |
|
| 209 | - | Commit::from_str("Author: A\nDate: B\n"), |
|
| 209 | + | CommitHeader::from_str("Author: A\nDate: B\n"), |
|
| 210 | 210 | Err(ParseCommitError), |
|
| 211 | 211 | ); |
|
| 212 | - | assert_eq!(Commit::from_str(""), Err(ParseCommitError)); |
|
| 212 | + | assert_eq!(CommitHeader::from_str(""), Err(ParseCommitError)); |
|
| 213 | 213 | } |
|
| 214 | 214 | ||
| 215 | 215 | #[test] |
|
| 216 | 216 | fn rejects_non_hex_hash() { |
|
| 217 | 217 | assert_eq!( |
|
| 218 | - | Commit::from_str("commit not-hex\n\n"), |
|
| 218 | + | CommitHeader::from_str("commit not-hex\n\n"), |
|
| 219 | 219 | Err(ParseCommitError), |
|
| 220 | 220 | ); |
|
| 221 | 221 | } |
|
| 222 | 222 | ||
| 223 | 223 | #[test] |
| 230 | 230 | " Subject", |
|
| 231 | 231 | "", |
|
| 232 | 232 | " Body", |
|
| 233 | 233 | "", |
|
| 234 | 234 | ]; |
|
| 235 | - | let commit: Commit = lines.join("\n").parse().unwrap(); |
|
| 235 | + | let commit: CommitHeader = lines.join("\n").parse().unwrap(); |
|
| 236 | 236 | let expected: String = lines.iter().map(|line| format!("{line}\n")).collect(); |
|
| 237 | 237 | ||
| 238 | 238 | assert_eq!(commit.to_string(), expected); |
|
| 239 | 239 | } |
|
| 240 | 240 | } |
crates/diff/src/document.rs
+1 -1
| 70 | 70 | ||
| 71 | 71 | /// A complete unified diff document. |
|
| 72 | 72 | #[derive(Clone, Debug, Default, Eq, PartialEq)] |
|
| 73 | 73 | pub struct DiffDocument { |
|
| 74 | 74 | /// Commit preamble before the first `diff --git` section, if present. |
|
| 75 | - | pub commit: Option<crate::Commit>, |
|
| 75 | + | pub commit: Option<crate::CommitHeader>, |
|
| 76 | 76 | /// File diffs in input order. |
|
| 77 | 77 | pub files: Vec<FileDiff>, |
|
| 78 | 78 | } |
|
| 79 | 79 | ||
| 80 | 80 | /// A single file section in a unified diff. |
a.txt
+0 -0
a
+0 -0
crates/diff/src/lib.rs
+1 -1
| 28 | 28 | pub mod decode; |
|
| 29 | 29 | pub mod encode; |
|
| 30 | 30 | ||
| 31 | 31 | #[cfg(feature = "builtins")] |
|
| 32 | 32 | pub use builtins::Builtins; |
|
| 33 | - | pub use commit::{Commit, Header, ParseCommitError}; |
|
| 33 | + | pub use commit::{CommitHeader, Header, ParseCommitError}; |
|
| 34 | 34 | pub use context::ContextChange; |
|
| 35 | 35 | pub use decode::Decode; |
|
| 36 | 36 | pub use decorate::{DiffDecorator, FileDecoration, HunkDecoration, ReviewProgress}; |
|
| 37 | 37 | pub use document::{ |
|
| 38 | 38 | DecodeError, DiffDocument, DiffLine, DiffLineKind, FileDiff, FileHeader, FileId, FileKind, |
crates/diff/src/viewer.rs
+4 -4
| 21 | 21 | use crate::context::{self, ContextChange, ContextState}; |
|
| 22 | 22 | use crate::decorate::{DiffDecorator, FileDecoration, HunkDecoration, ReviewProgress}; |
|
| 23 | 23 | use crate::highlight::{DiffHighlighter, HighlightKeyInput, HighlightResult}; |
|
| 24 | 24 | use crate::word; |
|
| 25 | 25 | use crate::{ |
|
| 26 | - | Commit, Decode, DecodeError, DiffDocument, DiffLine, DiffLineKind, DiffSide, FileDiff, FileId, |
|
| 27 | - | FileKind, FileMode, FileStats, HighlightKey, HighlightRequest, HighlightSource, Hunk, HunkId, |
|
| 28 | - | Oid, RowId, |
|
| 26 | + | CommitHeader, Decode, DecodeError, DiffDocument, DiffLine, DiffLineKind, DiffSide, FileDiff, |
|
| 27 | + | FileId, FileKind, FileMode, FileStats, HighlightKey, HighlightRequest, HighlightSource, Hunk, |
|
| 28 | + | HunkId, Oid, RowId, |
|
| 29 | 29 | }; |
|
| 30 | 30 | ||
| 31 | 31 | pub use tree::{FileTree, FileTreeMode, FileTreeTarget}; |
|
| 32 | 32 | ||
| 33 | 33 | /// Visible and lookahead row ranges that should be highlighted. |
| 1739 | 1739 | } |
|
| 1740 | 1740 | self.index = RowIndex { files, rows }; |
|
| 1741 | 1741 | } |
|
| 1742 | 1742 | ||
| 1743 | 1743 | /// Lays out the commit preamble for the given body width. |
|
| 1744 | - | fn preamble(commit: Option<&Commit>, cols: usize) -> Vec<Line> { |
|
| 1744 | + | fn preamble(commit: Option<&CommitHeader>, cols: usize) -> Vec<Line> { |
|
| 1745 | 1745 | let Some(commit) = commit else { |
|
| 1746 | 1746 | return Vec::new(); |
|
| 1747 | 1747 | }; |
|
| 1748 | 1748 | // Normalize the message body first, then trim only outer empty lines. |
|
| 1749 | 1749 | let message_lines = commit |
a.rs
+0 -0
a.rs
+0 -0
b.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
b.rs
+0 -0
crates/diff/tests/golden/sticky_header_before_next_file_64x5.txt
+0 -0
src/a.rs
+0 -0
src/b.rs
+0 -0
README.md
+0 -0
src/app/a.rs
+0 -0
src/lib/b.rs
+0 -0
README.md
+0 -0
a.rs
+0 -0
b.rs
+0 -0
assets/css/style.css
+0 -0
assets/css/style.css
+0 -0
first.rs
+0 -0
second.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
b.rs
+0 -0
a.rs\n",
+0 -0
a\n\
+0 -0
crates/repo/src/git.rs
+6 -0
| 186 | 186 | } else { |
|
| 187 | 187 | Err(RepoError::git(&output)) |
|
| 188 | 188 | } |
|
| 189 | 189 | } |
|
| 190 | 190 | ||
| 191 | + | /// Returns the `git log`-style header for `commit`. |
|
| 192 | + | pub fn commit_header(&self, commit: Oid) -> Result<Vec<u8>, RepoError> { |
|
| 193 | + | let commit = commit.to_string(); |
|
| 194 | + | self.git("show", &["-s", "--format=medium", &commit]) |
|
| 195 | + | } |
|
| 196 | + | ||
| 191 | 197 | /// Runs a Git subcommand with optional stdin, index, and environment overrides. |
|
| 192 | 198 | fn run(&self, subcommand: &str, args: &[&str], opts: Opts<'_>) -> Result<Output, RepoError> { |
|
| 193 | 199 | let mut command = self.command(subcommand); |
|
| 194 | 200 | command.args(args); |
|
| 195 | 201 |