Radish alpha
r
rad:z4D5UCArafTzTQpDZNQRuqswh3ury
Radicle desktop app
Radicle
Git
Add `get_diff` tauri command
Merged did:key:z6MkkfM3...sVz5 opened 1 year ago

We get plain text diff for two commits :tada:

check

👉 Workflow runs 👉 Branch on GitHub

7 files changed +64 -2 ebbccccc 6d649aea
modified Cargo.lock
@@ -3880,6 +3880,7 @@ dependencies = [
 "nonempty",
 "radicle-git-ext",
 "radicle-std-ext",
+
 "serde",
 "tar",
 "thiserror",
 "url",
modified crates/radicle-tauri/Cargo.toml
@@ -19,7 +19,7 @@ anyhow = { version = "1.0" }
base64 = { version = "0.22" }
log = { version = "0.4" }
radicle = { git = "https://seed.radicle.xyz/z3gqcJUoA1n9HaHKufZs5FCSGazv5.git" }
-
radicle-surf = { version = "0.22.0" }
+
radicle-surf = { version = "0.22.0", features = ["serde"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0" }
tauri = { version = "2.0", features = ["isolation"] }
modified crates/radicle-tauri/src/commands.rs
@@ -1,5 +1,6 @@
pub mod auth;
pub mod cob;
+
pub mod diff;
pub mod profile;
pub mod repo;
pub mod thread;
added crates/radicle-tauri/src/commands/diff.rs
@@ -0,0 +1,45 @@
+
use radicle::storage::ReadStorage;
+
use radicle_surf as surf;
+

+
use radicle::git;
+
use radicle::identity;
+
use serde::Deserialize;
+
use serde::Serialize;
+

+
use crate::{error, AppState};
+

+
#[derive(Serialize, Deserialize)]
+
pub struct Options {
+
    pub base: git::Oid,
+
    pub head: git::Oid,
+
    pub unified: u32,
+
}
+

+
#[tauri::command]
+
pub async fn get_diff(
+
    ctx: tauri::State<'_, AppState>,
+
    rid: identity::RepoId,
+
    options: Options,
+
) -> Result<surf::diff::Diff, error::Error> {
+
    let repo = ctx.profile.storage.repository(rid)?.backend;
+
    let base = repo.find_commit(*options.base)?;
+
    let head = repo.find_commit(*options.head)?;
+

+
    let mut opts = git::raw::DiffOptions::new();
+
    opts.patience(true)
+
        .minimal(true)
+
        .context_lines(options.unified);
+

+
    let mut find_opts = git::raw::DiffFindOptions::new();
+
    find_opts.exact_match_only(true);
+
    find_opts.all(true);
+

+
    let left = base.tree()?;
+
    let right = head.tree()?;
+

+
    let mut diff = repo.diff_tree_to_tree(Some(&left), Some(&right), Some(&mut opts))?;
+
    diff.find_similar(Some(&mut find_opts))?;
+
    let diff = surf::diff::Diff::try_from(diff)?;
+

+
    Ok::<_, error::Error>(diff)
+
}
modified crates/radicle-tauri/src/error.rs
@@ -50,6 +50,10 @@ pub enum Error {
    #[error(transparent)]
    Git2(#[from] radicle::git::raw::Error),

+
    /// Diff error.
+
    #[error(transparent)]
+
    Diff(#[from] radicle_surf::diff::git::error::Diff),
+

    /// Storage refs error.
    #[error(transparent)]
    StorageRef(#[from] radicle::storage::refs::Error),
modified crates/radicle-tauri/src/lib.rs
@@ -8,7 +8,7 @@ use tauri::Manager;
use radicle::node::Handle;
use radicle::Node;

-
use commands::{auth, cob, profile, repo, thread};
+
use commands::{auth, cob, diff, profile, repo, thread};

struct AppState {
    profile: radicle::Profile,
@@ -76,6 +76,7 @@ pub fn run() {
            repo::list_repos,
            repo::repo_by_id,
            repo::diff_stats,
+
            diff::get_diff,
            cob::get_file_by_oid,
            cob::issue::list_issues,
            cob::issue::issue_by_id,
modified src/views/repo/Patch.svelte
@@ -5,6 +5,7 @@
  import type { Revision } from "@bindings/Revision";

  import { formatTimestamp, formatOid, patchStatusColor } from "@app/lib/utils";
+
  import { invoke } from "@tauri-apps/api/core";

  import Border from "@app/components/Border.svelte";
  import CopyableId from "@app/components/CopyableId.svelte";
@@ -21,6 +22,15 @@
  export let revisions: Revision[];
  export let config: Config;

+
  $: void invoke("get_diff", {
+
    rid: repo.rid,
+
    options: {
+
      base: revisions[0].base,
+
      head: revisions[0].head,
+
      unified: 10,
+
    },
+
  }).then(console.log);
+

  $: project = repo.payloads["xyz.radicle.project"]!;
</script>