Radish alpha
r
Radicle desktop app
Radicle
Git (anonymous pull)
Log in to clone via SSH
Add `list_commits` tauri command
Sebastian Martinez committed 1 year ago
commit f5d559158b0ce6e102f9a992f05dcb79d538c413
parent a2776c27a4eff361cc0fbd412ce1934b9b2d3ef2
5 files changed +88 -0
modified crates/radicle-tauri/src/commands/repo.rs
@@ -32,3 +32,14 @@ pub async fn diff_stats(
) -> Result<types::cobs::Stats, Error> {
    ctx.diff_stats(rid, base, head)
}
+

+
#[tauri::command]
+
pub async fn list_commits(
+
    ctx: tauri::State<'_, AppState>,
+
    rid: RepoId,
+
    parent: Option<String>,
+
    skip: Option<usize>,
+
    take: Option<usize>,
+
) -> Result<types::cobs::PaginatedQuery<Vec<types::repo::Commit>>, Error> {
+
    ctx.list_commits(rid, parent, skip, take)
+
}
modified crates/radicle-tauri/src/lib.rs
@@ -96,6 +96,7 @@ pub fn run() {
            repo::list_repos,
            repo::repo_by_id,
            repo::diff_stats,
+
            repo::list_commits,
            diff::get_diff,
            cob::get_file_by_oid,
            cob::activity_by_id,
added crates/radicle-types/bindings/repo/Commit.ts
@@ -0,0 +1,10 @@
+
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
+

+
export type Commit = {
+
  id: string;
+
  author: { name: string; email: string; time: number };
+
  committer: { name: string; email: string; time: number };
+
  message: string;
+
  summary: string;
+
  parents: Array<string>;
+
};
modified crates/radicle-types/src/repo.rs
@@ -1,5 +1,6 @@
use std::collections::BTreeSet;

+
use radicle_surf as surf;
use serde::{Deserialize, Serialize};
use ts_rs::TS;

@@ -112,3 +113,33 @@ pub struct ProjectPayloadMeta {
    #[ts(type = "number")]
    pub last_commit_timestamp: i64,
}
+

+
#[derive(Clone, Serialize, TS)]
+
#[serde(rename_all = "camelCase")]
+
#[ts(export)]
+
#[ts(export_to = "repo/")]
+
pub struct Commit {
+
    #[ts(as = "String")]
+
    pub id: git::Oid,
+
    #[ts(type = "{ name: string; email: string; time: number; }")]
+
    pub author: surf::Author,
+
    #[ts(type = "{ name: string; email: string; time: number; }")]
+
    pub committer: surf::Author,
+
    pub message: String,
+
    pub summary: String,
+
    #[ts(as = "Vec<String>")]
+
    pub parents: Vec<git::Oid>,
+
}
+

+
impl From<surf::Commit> for Commit {
+
    fn from(value: surf::Commit) -> Self {
+
        Self {
+
            id: value.id,
+
            author: value.author,
+
            committer: value.committer,
+
            message: value.message,
+
            summary: value.summary,
+
            parents: value.parents,
+
        }
+
    }
+
}
modified crates/radicle-types/src/traits/repo.rs
@@ -157,4 +157,39 @@ pub trait Repo: Profile {

        Ok::<_, Error>(diff)
    }
+

+
    fn list_commits(
+
        &self,
+
        rid: identity::RepoId,
+
        parent: Option<String>,
+
        skip: Option<usize>,
+
        take: Option<usize>,
+
    ) -> Result<cobs::PaginatedQuery<Vec<repo::Commit>>, Error> {
+
        let profile = self.profile();
+
        let cursor = skip.unwrap_or(0);
+
        let take = take.unwrap_or(20);
+
        let repo = profile.storage.repository(rid)?;
+

+
        let sha = match parent {
+
            Some(commit) => commit,
+
            None => repo.head()?.1.to_string(),
+
        };
+

+
        let repo = surf::Repository::open(repo.path())?;
+
        let history = repo.history(&sha)?;
+

+
        let mut commits = history
+
            .filter_map(|c| c.map(Into::into).ok())
+
            .skip(cursor)
+
            .take(take + 1); // Take one extra item to check if there's more.
+

+
        let paginated_commits: Vec<_> = commits.by_ref().take(take).collect();
+
        let more = commits.next().is_some();
+

+
        Ok::<_, Error>(cobs::PaginatedQuery {
+
            cursor,
+
            more,
+
            content: paginated_commits.to_vec(),
+
        })
+
    }
}