From 608d211083e2db46f673c797bc684aaf6912afc6 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Wed, 25 Sep 2024 16:51:21 +0200 Subject: [PATCH] Add `repo::diff` tauri command --- src-tauri/bindings/Patch.ts | 2 ++ src-tauri/bindings/Stats.ts | 7 +++++++ src-tauri/src/commands/repos.rs | 20 ++++++++++++++++++++ src-tauri/src/lib.rs | 1 + src-tauri/src/types/cobs.rs | 24 ++++++++++++++++++++++++ src/components/PatchTeaser.svelte | 6 ++++++ src/views/repo/Patches.svelte | 2 +- 7 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src-tauri/bindings/Stats.ts diff --git a/src-tauri/bindings/Patch.ts b/src-tauri/bindings/Patch.ts index 94dfb7077..e70ff8a62 100644 --- a/src-tauri/bindings/Patch.ts +++ b/src-tauri/bindings/Patch.ts @@ -5,6 +5,8 @@ export type Patch = { id: string; author: Author; title: string; + base: string; + head: string; state: | { status: "draft"; diff --git a/src-tauri/bindings/Stats.ts b/src-tauri/bindings/Stats.ts new file mode 100644 index 000000000..fd2d8e6b3 --- /dev/null +++ b/src-tauri/bindings/Stats.ts @@ -0,0 +1,7 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export type Stats = { + files_changed: number; + insertions: number; + deletions: number; +}; diff --git a/src-tauri/src/commands/repos.rs b/src-tauri/src/commands/repos.rs index 93e6dd859..8b11c9a23 100644 --- a/src-tauri/src/commands/repos.rs +++ b/src-tauri/src/commands/repos.rs @@ -1,8 +1,10 @@ use radicle::identity::RepoId; +use radicle::storage::ReadRepository; use radicle::storage::ReadStorage; use crate::error::Error; use crate::types; +use crate::types::cobs; use crate::AppState; /// List all repos. @@ -40,3 +42,21 @@ pub fn repo_by_id( Ok::<_, Error>(repo_info) } + +#[tauri::command] +pub async fn diff( + ctx: tauri::State<'_, AppState>, + rid: RepoId, + base: String, + head: String, +) -> Result { + let (repo, _) = ctx.repo(rid)?; + let repo = radicle_surf::Repository::open(repo.path())?; + let base = repo.commit(base)?; + let commit = repo.commit(head)?; + let diff = repo.diff(base.id, commit.id)?; + + let stats = diff.stats(); + + Ok::<_, Error>(cobs::Stats::new(stats)) +} diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index f7dd10ad3..0a5522255 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -140,6 +140,7 @@ pub fn run() { auth::authenticate, repos::list_repos, repos::repo_by_id, + repos::diff, cobs::list_issues, cobs::issues_by_id, cobs::list_patches, diff --git a/src-tauri/src/types/cobs.rs b/src-tauri/src/types/cobs.rs index 90ad29920..9bd398561 100644 --- a/src-tauri/src/types/cobs.rs +++ b/src-tauri/src/types/cobs.rs @@ -78,6 +78,10 @@ pub struct Patch { id: String, author: Author, title: String, + #[ts(as = "String")] + base: git::Oid, + #[ts(as = "String")] + head: git::Oid, #[ts(type = r#"{ status: 'draft' } | { @@ -104,6 +108,8 @@ impl Patch { author: Author::new(*patch.author().id(), aliases), title: patch.title().to_string(), state: patch.state().clone(), + base: *patch.base(), + head: *patch.head(), assignees: patch .assignees() .map(|did| Author::new(did, aliases)) @@ -458,3 +464,21 @@ impl CobOptions { self.announce.unwrap_or(true) } } + +#[derive(TS, Serialize)] +#[ts(export)] +pub struct Stats { + pub files_changed: usize, + pub insertions: usize, + pub deletions: usize, +} + +impl Stats { + pub fn new(stats: &radicle_surf::diff::Stats) -> Self { + Self { + files_changed: stats.files_changed, + insertions: stats.insertions, + deletions: stats.deletions, + } + } +} diff --git a/src/components/PatchTeaser.svelte b/src/components/PatchTeaser.svelte index 137255061..922b70e09 100644 --- a/src/components/PatchTeaser.svelte +++ b/src/components/PatchTeaser.svelte @@ -1,13 +1,16 @@