diff: Support signatures in commits
d8233e9ab7f1ca4e25a96b42e6762ae4ac473afc
1 parent
c9494b9b
crates/diff/src/commit.rs
+28 -0
| 10 | 10 | pub struct Commit { |
|
| 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 | + | /// Signature verification output between the commit hash and headers. |
|
| 16 | + | pub signature: Vec<String>, |
|
| 15 | 17 | /// Headers in input order with original key casing (e.g. `Author`, `Date`). |
|
| 16 | 18 | pub headers: Vec<Header>, |
|
| 17 | 19 | /// Commit message, verbatim and including any source indentation. |
|
| 18 | 20 | pub message: String, |
|
| 19 | 21 | } |
| 59 | 61 | type Err = ParseCommitError; |
|
| 60 | 62 | ||
| 61 | 63 | fn from_str(input: &str) -> Result<Self, Self::Err> { |
|
| 62 | 64 | let mut hash = None; |
|
| 63 | 65 | let mut decoration = None; |
|
| 66 | + | let mut signature = Vec::new(); |
|
| 64 | 67 | let mut headers = Vec::new(); |
|
| 65 | 68 | let mut lines = input.split('\n').peekable(); |
|
| 66 | 69 | ||
| 67 | 70 | while let Some(&line) = lines.peek() { |
|
| 68 | 71 | if line.is_empty() { |
| 82 | 85 | if let Some(header) = Header::parse(line) { |
|
| 83 | 86 | headers.push(header); |
|
| 84 | 87 | lines.next(); |
|
| 85 | 88 | continue; |
|
| 86 | 89 | } |
|
| 90 | + | if hash.is_some() && headers.is_empty() { |
|
| 91 | + | signature.push(line.to_owned()); |
|
| 92 | + | lines.next(); |
|
| 93 | + | continue; |
|
| 94 | + | } |
|
| 87 | 95 | break; |
|
| 88 | 96 | } |
|
| 89 | 97 | ||
| 90 | 98 | Ok(Self { |
|
| 91 | 99 | hash: hash.ok_or(ParseCommitError)?, |
|
| 92 | 100 | decoration, |
|
| 101 | + | signature, |
|
| 93 | 102 | headers, |
|
| 94 | 103 | message: lines.collect::<Vec<_>>().join("\n"), |
|
| 95 | 104 | }) |
|
| 96 | 105 | } |
|
| 97 | 106 | } |
| 101 | 110 | write!(f, "commit {}", self.hash)?; |
|
| 102 | 111 | if let Some(decoration) = &self.decoration { |
|
| 103 | 112 | write!(f, " {decoration}")?; |
|
| 104 | 113 | } |
|
| 105 | 114 | writeln!(f)?; |
|
| 115 | + | for line in &self.signature { |
|
| 116 | + | writeln!(f, "{line}")?; |
|
| 117 | + | } |
|
| 106 | 118 | for header in &self.headers { |
|
| 107 | 119 | writeln!(f, "{}: {}", header.key, header.value)?; |
|
| 108 | 120 | } |
|
| 109 | 121 | if !self.message.is_empty() { |
|
| 110 | 122 | writeln!(f)?; |
| 166 | 178 | ||
| 167 | 179 | assert_eq!(commit.hash.to_string(), SHA1); |
|
| 168 | 180 | assert_eq!(commit.decoration.as_deref(), Some("(HEAD -> main)")); |
|
| 169 | 181 | } |
|
| 170 | 182 | ||
| 183 | + | #[test] |
|
| 184 | + | fn parses_signature_before_headers() { |
|
| 185 | + | let input = |
|
| 186 | + | format!("commit {SHA1}\nGood signature\nNo principal matched.\nAuthor: A\nDate: B\n\n"); |
|
| 187 | + | let commit = Commit::from_str(&input).unwrap(); |
|
| 188 | + | ||
| 189 | + | assert_eq!( |
|
| 190 | + | commit.signature, |
|
| 191 | + | ["Good signature", "No principal matched."] |
|
| 192 | + | ); |
|
| 193 | + | assert_eq!(commit.header("author"), Some("A")); |
|
| 194 | + | assert_eq!(commit.header("date"), Some("B")); |
|
| 195 | + | assert!(commit.message.is_empty()); |
|
| 196 | + | } |
|
| 197 | + | ||
| 171 | 198 | #[test] |
|
| 172 | 199 | fn parses_message_with_indent_preserved() { |
|
| 173 | 200 | let input = format!("commit {SHA1}\nAuthor: A\n\n Subject\n\n Body\n"); |
|
| 174 | 201 | let commit = Commit::from_str(&input).unwrap(); |
|
| 175 | 202 |
| 195 | 222 | ||
| 196 | 223 | #[test] |
|
| 197 | 224 | fn display_matches_decoder_line_encoding() { |
|
| 198 | 225 | let lines = [ |
|
| 199 | 226 | "commit 1234567890abcdef1234567890abcdef12345678", |
|
| 227 | + | "Good signature", |
|
| 200 | 228 | "Author: A", |
|
| 201 | 229 | "", |
|
| 202 | 230 | " Subject", |
|
| 203 | 231 | "", |
|
| 204 | 232 | " Body", |
crates/diff/src/encode.rs
+0 -0
a.txt
+0 -0
b.txt
+0 -0
crates/diff/src/viewer.rs
+8 -0
| 1559 | 1559 | } |
|
| 1560 | 1560 | }; |
|
| 1561 | 1561 | let hash = commit.hash.to_string(); |
|
| 1562 | 1562 | let hash_width = hash.width() + commit.decoration.as_deref().map_or(0, |d| 1 + d.width()); |
|
| 1563 | 1563 | let value_width = std::iter::once(hash_width) |
|
| 1564 | + | .chain(commit.signature.iter().map(|line| line.as_str().width())) |
|
| 1564 | 1565 | .chain(commit.headers.iter().map(|h| h.value.as_str().width())) |
|
| 1565 | 1566 | .max() |
|
| 1566 | 1567 | .unwrap_or_default(); |
|
| 1567 | 1568 | // Hash row is a two-part component (hash plus optional signature marker). |
|
| 1568 | 1569 | let hash_line = Line::new(Label::new(hash.as_str()).fg(colors::rgb::ORANGE)); |
| 1583 | 1584 | }) |
|
| 1584 | 1585 | .row([ |
|
| 1585 | 1586 | Line::new(Label::new("commit").fg(colors::rgb::DIM)), |
|
| 1586 | 1587 | hash_value, |
|
| 1587 | 1588 | ]); |
|
| 1589 | + | for (index, signature) in commit.signature.iter().enumerate() { |
|
| 1590 | + | let key = if index == 0 { "signature" } else { "" }; |
|
| 1591 | + | table.push([ |
|
| 1592 | + | Line::new(Label::new(key).fg(colors::rgb::DIM)), |
|
| 1593 | + | Line::new(signature.as_str()), |
|
| 1594 | + | ]); |
|
| 1595 | + | } |
|
| 1588 | 1596 | // One row per commit header key/value, with date kept dim for softer emphasis. |
|
| 1589 | 1597 | for header in &commit.headers { |
|
| 1590 | 1598 | let key = header.key.to_ascii_lowercase(); |
|
| 1591 | 1599 | let header_value = if header.key.eq_ignore_ascii_case("date") { |
|
| 1592 | 1600 | Line::new(header.value.as_str()).style(Style::new(colors::rgb::DIM)) |
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