Add repo::diff tauri command

This commit is contained in:
Sebastian Martinez
2024-09-25 16:59:47 +02:00
parent 8aae8e66ab
commit 608d211083
7 changed files with 61 additions and 1 deletions
+2
View File
@@ -5,6 +5,8 @@ export type Patch = {
id: string;
author: Author;
title: string;
base: string;
head: string;
state:
| {
status: "draft";
+7
View File
@@ -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;
};
+20
View File
@@ -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<cobs::Stats, Error> {
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))
}
+1
View File
@@ -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,
+24
View File
@@ -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,
}
}
}
+6
View File
@@ -1,13 +1,16 @@
<script lang="ts">
import type { Patch } from "@bindings/Patch";
import type { Stats } from "@bindings/Stats";
import { formatOid, formatTimestamp } from "@app/lib/utils";
import { invoke } from "@tauri-apps/api/core";
import Icon from "./Icon.svelte";
import InlineTitle from "./InlineTitle.svelte";
import NodeId from "./NodeId.svelte";
export let patch: Patch;
export let rid: string;
const statusColor: Record<Patch["state"]["status"], string> = {
draft: "var(--color-fill-gray)",
@@ -78,6 +81,9 @@
</div>
</div>
<div class="global-flex">
{#await invoke<Stats>( "diff", { rid, base: patch.base, head: patch.head }, ) then stats}
<div>{stats.insertions} / {stats.deletions}</div>
{/await}
{#each patch.labels as label}
<div class="global-counter txt-small">{label}</div>
{/each}
+1 -1
View File
@@ -117,7 +117,7 @@
<div class="list">
{#each patches as patch}
<PatchTeaser {patch} />
<PatchTeaser rid={repo.rid} {patch} />
{/each}
{#if patches.length === 0}