Skip to content

Commit

Permalink
Add list_commits tauri command
Browse files Browse the repository at this point in the history
Returns all commits for a specific repo.

Takes a `parent` argument as starting point for the git history.
Uses the `head` field of the project payload if not provided.

Instead of since and until timestamps uses cursor and take params to
paginate the commit history.
  • Loading branch information
sebastinez committed Nov 27, 2024
1 parent a5ab32f commit e52b239
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
11 changes: 11 additions & 0 deletions crates/radicle-tauri/src/commands/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions crates/radicle-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
31 changes: 31 additions & 0 deletions crates/radicle-types/src/repo.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::BTreeSet;

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

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

#[derive(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,
}
}
}
36 changes: 36 additions & 0 deletions crates/radicle-types/src/traits/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,40 @@ 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 (_, head) = repo.head()?;

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

let repo = surf::Repository::open(repo.path())?;
let stats = repo.stats()?;
let more = cursor + take < stats.commits;

let commits = repo
.history(&sha)?
.skip(cursor)
.take(take)
.filter_map(|c| c.map(Into::into).ok())
.collect::<Vec<repo::Commit>>();

Ok::<_, Error>(cobs::PaginatedQuery {
cursor,
more,
content: commits,
})
}
}

0 comments on commit e52b239

Please sign in to comment.