From e7b5f9a90b107c07e84fb9a912e04d3edc9e3021 Mon Sep 17 00:00:00 2001 From: Slack Coder Date: Thu, 24 Aug 2023 09:37:18 -0500 Subject: [PATCH] surf: Open Diff to custom construction Put the Diff type in lines with its associated types by opening it up to construction by crate consumers. The existing associated types have all their fields public. Open construction up by making the `Diff::insert_*` methods public and updating the Diff's stats manually. `Diff::stat` correctness is covered by existing tests. --- radicle-surf/src/diff.rs | 38 ++++++++++++++++++++++++++++++------ radicle-surf/src/diff/git.rs | 1 - 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/radicle-surf/src/diff.rs b/radicle-surf/src/diff.rs index 3275d9e..648ca7a 100644 --- a/radicle-surf/src/diff.rs +++ b/radicle-surf/src/diff.rs @@ -97,17 +97,40 @@ impl Diff { &self.stats } - fn insert_modified(&mut self, path: PathBuf, diff: DiffContent, old: DiffFile, new: DiffFile) { + fn update_stats(&mut self, diff: &DiffContent) { + self.stats.files_changed += 1; + if let DiffContent::Plain { hunks, .. } = diff { + for h in hunks.iter() { + for l in &h.lines { + match l { + Modification::Addition(_) => self.stats.insertions += 1, + Modification::Deletion(_) => self.stats.deletions += 1, + _ => (), + } + } + } + } + } + + pub fn insert_modified( + &mut self, + path: PathBuf, + diff: DiffContent, + old: DiffFile, + new: DiffFile, + ) { + self.update_stats(&diff); let diff = FileDiff::Modified(Modified { path, diff, old, new, }); - self.files.push(diff) + self.files.push(diff); } - fn insert_moved(&mut self, old_path: PathBuf, new_path: PathBuf) { + pub fn insert_moved(&mut self, old_path: PathBuf, new_path: PathBuf) { + self.update_stats(&DiffContent::Empty); let diff = FileDiff::Moved(Moved { old_path, new_path, @@ -116,7 +139,8 @@ impl Diff { self.files.push(diff); } - fn insert_copied(&mut self, old_path: PathBuf, new_path: PathBuf) { + pub fn insert_copied(&mut self, old_path: PathBuf, new_path: PathBuf) { + self.update_stats(&DiffContent::Empty); let diff = FileDiff::Copied(Copied { old_path, new_path, @@ -125,12 +149,14 @@ impl Diff { self.files.push(diff); } - fn insert_added(&mut self, path: PathBuf, diff: DiffContent, new: DiffFile) { + pub fn insert_added(&mut self, path: PathBuf, diff: DiffContent, new: DiffFile) { + self.update_stats(&diff); let diff = FileDiff::Added(Added { path, diff, new }); self.files.push(diff); } - fn insert_deleted(&mut self, path: PathBuf, diff: DiffContent, old: DiffFile) { + pub fn insert_deleted(&mut self, path: PathBuf, diff: DiffContent, old: DiffFile) { + self.update_stats(&diff); let diff = FileDiff::Deleted(Deleted { path, diff, old }); self.files.push(diff); } diff --git a/radicle-surf/src/diff/git.rs b/radicle-surf/src/diff/git.rs index b3f61e0..0a78a89 100644 --- a/radicle-surf/src/diff/git.rs +++ b/radicle-surf/src/diff/git.rs @@ -229,7 +229,6 @@ impl<'a> TryFrom> for Diff { use git2::Delta; let mut diff = Diff::new(); - diff.stats = git_diff.stats()?.into(); for (idx, delta) in git_diff.deltas().enumerate() { match delta.status() {