Skip to content

Commit

Permalink
Add get_diff tauri command
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastinez authored and rudolfs committed Oct 15, 2024
1 parent ebbcccc commit 6d649ae
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/radicle-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
1 change: 1 addition & 0 deletions crates/radicle-tauri/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod auth;
pub mod cob;
pub mod diff;
pub mod profile;
pub mod repo;
pub mod thread;
45 changes: 45 additions & 0 deletions crates/radicle-tauri/src/commands/diff.rs
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 4 additions & 0 deletions crates/radicle-tauri/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
3 changes: 2 additions & 1 deletion crates/radicle-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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::activity_by_id,
cob::save_embed,
Expand Down
10 changes: 10 additions & 0 deletions src/views/repo/Patch.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
formatTimestamp,
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";
Expand All @@ -26,6 +27,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>

Expand Down

0 comments on commit 6d649ae

Please sign in to comment.