From 504a198cecab3217b46d0dd13ae8e969f1b0a19f Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Fri, 6 Oct 2023 16:26:49 +0200 Subject: [PATCH] Update `radicle-surf` to v0.17 This commit introduces the `FileStats` struct, that adds the insertions and deletions for each file to `DiffContent`. Signed-off-by: Sebastian Martinez --- Cargo.lock | 4 +-- radicle-cli/Cargo.toml | 2 +- radicle-cli/src/git/pretty_diff.rs | 23 ++++------------ radicle-cli/src/git/unified_diff.rs | 18 +++++++++++-- radicle-httpd/Cargo.toml | 2 +- radicle-httpd/src/api/v1/projects.rs | 40 ++++++++++++++++++++++++++++ 6 files changed, 65 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fb4c7681a..c761e0c8e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2031,9 +2031,9 @@ checksum = "db20136bbc9ae63f3fec8e5a6c369f4902fac2244501b5dfc6d668e43475aaa4" [[package]] name = "radicle-surf" -version = "0.16.0" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfc319f555a7cbf938ba4eecb06bd6edcdccce339b20a6b8d8a3af3e92e9f7b7" +checksum = "c8af2d4285e9f326da30ba25e2642b0644ec4b8e36c7243380b01dfb1a15e239" dependencies = [ "anyhow", "base64 0.13.1", diff --git a/radicle-cli/Cargo.toml b/radicle-cli/Cargo.toml index 30cf7af47..6e589b4a3 100644 --- a/radicle-cli/Cargo.toml +++ b/radicle-cli/Cargo.toml @@ -22,7 +22,7 @@ nonempty = { version = "0.8" } # N.b. this is required to use macros, even though it's re-exported # through radicle radicle-git-ext = { version = "0.6.0", features = ["serde"] } -radicle-surf = { version = "0.16.0" } +radicle-surf = { version = "0.17.0" } serde = { version = "1.0" } serde_json = { version = "1" } serde_yaml = { version = "0.8" } diff --git a/radicle-cli/src/git/pretty_diff.rs b/radicle-cli/src/git/pretty_diff.rs index 82f04ec1a..6c298e413 100644 --- a/radicle-cli/src/git/pretty_diff.rs +++ b/radicle-cli/src/git/pretty_diff.rs @@ -165,26 +165,13 @@ impl ToPretty for DiffContent { Some((f.old.oid, f.new_path.clone())), ), }; - let mut header = header.pretty(hi, &(), repo); - let mut additions = 0; - let mut deletions = 0; - match self { - DiffContent::Plain { hunks, .. } => { - for h in hunks.iter() { - for l in &h.lines { - match l { - Modification::Addition(_) => additions += 1, - Modification::Deletion(_) => deletions += 1, - _ => {} - } - } - } - } - DiffContent::Empty => {} - DiffContent::Binary => {} - } + let (additions, deletions) = if let Some(stats) = self.stats() { + (stats.additions, stats.deletions) + } else { + (0, 0) + }; if deletions > 0 { header.push(term::label(format!(" -{deletions}")).fg(theme.color("negative.light"))); } diff --git a/radicle-cli/src/git/unified_diff.rs b/radicle-cli/src/git/unified_diff.rs index 1abb2455e..997f74a67 100644 --- a/radicle-cli/src/git/unified_diff.rs +++ b/radicle-cli/src/git/unified_diff.rs @@ -3,6 +3,7 @@ use std::fmt; use std::io; use std::path::PathBuf; +use radicle_surf::diff::FileStats; use thiserror::Error; use radicle::git; @@ -203,8 +204,17 @@ impl Encode for Diff { impl Decode for DiffContent { fn decode(r: &mut impl io::BufRead) -> Result { let mut hunks = Vec::default(); - - while let Some(h) = Hunk::<_>::try_decode(r)? { + let mut additions = 0; + let mut deletions = 0; + + while let Some(h) = Hunk::try_decode(r)? { + for l in &h.lines { + match l { + Modification::Addition(_) => additions += 1, + Modification::Deletion(_) => deletions += 1, + _ => {} + } + } hunks.push(h); } @@ -214,6 +224,10 @@ impl Decode for DiffContent { // TODO: Handle case for binary. Ok(DiffContent::Plain { hunks: Hunks::from(hunks), + stats: FileStats { + additions, + deletions, + }, // TODO: Properly handle EndOfLine field eof: diff::EofNewLine::NoneMissing, }) diff --git a/radicle-httpd/Cargo.toml b/radicle-httpd/Cargo.toml index 72786e66f..48ca85f0e 100644 --- a/radicle-httpd/Cargo.toml +++ b/radicle-httpd/Cargo.toml @@ -27,7 +27,7 @@ hyper = { version = "0.14.17", default-features = false } lexopt = { version = "0.2.1" } lru = { version = "0.11.0" } nonempty = { version = "0.8.1", features = ["serialize"] } -radicle-surf = { version = "0.16.0", default-features = false, features = ["serde"] } +radicle-surf = { version = "0.17.0", default-features = false, features = ["serde"] } serde = { version = "1", features = ["derive"] } serde_json = { version = "1", features = ["preserve_order"] } thiserror = { version = "1" } diff --git a/radicle-httpd/src/api/v1/projects.rs b/radicle-httpd/src/api/v1/projects.rs index f9ee9d7d5..c9c05959b 100644 --- a/radicle-httpd/src/api/v1/projects.rs +++ b/radicle-httpd/src/api/v1/projects.rs @@ -1187,6 +1187,10 @@ mod routes { }, }, ], + "stats": { + "additions": 1, + "deletions": 0, + }, "eof": "noneMissing", }, "new": { @@ -1218,6 +1222,10 @@ mod routes { }, } ], + "stats": { + "additions": 1, + "deletions": 0, + }, "eof": "noneMissing", }, "new": { @@ -1251,6 +1259,10 @@ mod routes { }, }, ], + "stats": { + "additions": 0, + "deletions": 1, + }, "eof": "noneMissing", }, "old": { @@ -1316,6 +1328,10 @@ mod routes { }, }, ], + "stats": { + "additions": 1, + "deletions": 0, + }, "eof": "noneMissing", }, "new": { @@ -1349,6 +1365,10 @@ mod routes { }, }, ], + "stats": { + "additions": 0, + "deletions": 1, + }, "eof": "noneMissing", }, "old": { @@ -1412,6 +1432,10 @@ mod routes { }, } ], + "stats": { + "additions": 1, + "deletions": 0, + }, "eof": "noneMissing", }, "new": { @@ -1497,6 +1521,10 @@ mod routes { }, }, ], + "stats": { + "additions": 1, + "deletions": 0, + }, "eof": "noneMissing", }, "new": { @@ -1528,6 +1556,10 @@ mod routes { }, } ], + "stats": { + "additions": 1, + "deletions": 0, + }, "eof": "noneMissing", }, "new": { @@ -1561,6 +1593,10 @@ mod routes { }, }, ], + "stats": { + "additions": 0, + "deletions": 1, + }, "eof": "noneMissing", }, "old": { @@ -1892,6 +1928,10 @@ mod routes { }, }, ], + "stats": { + "additions": 1, + "deletions": 0, + }, "eof": "noneMissing", }, "new": {