mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-15 11:20:36 +08:00
Add list_commits tauri command
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.
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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>;
|
||||
};
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user