lib: Improve perf of patch item conversion

This commit is contained in:
Erik Kundt
2024-03-19 00:34:24 +01:00
parent 53cca8870b
commit ac76014a55
3 changed files with 30 additions and 10 deletions
+1
View File
@@ -1,4 +1,5 @@
pub mod event;
pub mod git;
pub mod store;
pub mod task;
pub mod terminal;
+20
View File
@@ -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 <old> <new> --stat` exactly.
pub fn diff_stats(
repo: &git::raw::Repository,
old: &Oid,
new: &Oid,
) -> Result<git::raw::DiffStats, git::raw::Error> {
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()
}
+9 -10
View File
@@ -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<Self, anyhow::Error> {
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(),
})
}
}