diff --git a/src/flux.rs b/src/flux.rs index e8170dac..32778ea4 100644 --- a/src/flux.rs +++ b/src/flux.rs @@ -1,4 +1,5 @@ pub mod event; +pub mod git; pub mod store; pub mod task; pub mod terminal; diff --git a/src/flux/git.rs b/src/flux/git.rs new file mode 100644 index 00000000..22c5d539 --- /dev/null +++ b/src/flux/git.rs @@ -0,0 +1,20 @@ +use radicle::git; +use radicle::git::Oid; + +/// Get the diff stats between two commits. +/// Should match the default output of `git diff --stat` exactly. +pub fn diff_stats( + repo: &git::raw::Repository, + old: &Oid, + new: &Oid, +) -> Result { + let old = repo.find_commit(**old)?; + let new = repo.find_commit(**new)?; + let old_tree = old.tree()?; + let new_tree = new.tree()?; + let mut diff = repo.diff_tree_to_tree(Some(&old_tree), Some(&new_tree), None)?; + let mut find_opts = git::raw::DiffFindOptions::new(); + + diff.find_similar(Some(&mut find_opts))?; + diff.stats() +} diff --git a/src/flux/ui/cob.rs b/src/flux/ui/cob.rs index ceb3d57f..97282385 100644 --- a/src/flux/ui/cob.rs +++ b/src/flux/ui/cob.rs @@ -12,8 +12,9 @@ use radicle::node::notifications::{Notification, NotificationId, NotificationKin use radicle::node::AliasStore; use radicle::patch::{Patch, PatchId, Patches}; use radicle::storage::git::Repository; -use radicle::storage::{ReadRepository, ReadStorage, RefUpdate}; +use radicle::storage::{ReadRepository, ReadStorage, RefUpdate, WriteRepository}; +use super::super::git; use super::theme::style; use super::widget::ToRow; use super::{format, span}; @@ -423,11 +424,9 @@ impl PatchItem { patch: (PatchId, Patch), ) -> Result { let (id, patch) = patch; - let (_, rev) = patch.latest(); - let repo = radicle_surf::Repository::open(repository.path())?; - let base = repo.commit(rev.base())?; - let head = repo.commit(rev.head())?; - let diff = repo.diff(base.id, head.id)?; + let (_, revision) = patch.latest(); + let (from, to) = revision.range(); + let stats = git::diff_stats(repository.raw(), &from, &to)?; Ok(Self { id, @@ -438,10 +437,10 @@ impl PatchItem { alias: profile.aliases().alias(&patch.author().id), you: *patch.author().id == *profile.did(), }, - head: rev.head(), - added: diff.stats().insertions as u16, - removed: diff.stats().deletions as u16, - timestamp: rev.timestamp(), + head: revision.head(), + added: stats.insertions() as u16, + removed: stats.deletions() as u16, + timestamp: patch.updated_at(), }) } }