65 lines
2.1 KiB
Rust
65 lines
2.1 KiB
Rust
//! Copyright (c) 2022-2026 GitDataAi All rights reserved.
|
|
|
|
use crate::bare::GitBare;
|
|
use crate::error::{GitError, GitResult};
|
|
use crate::pb::*;
|
|
|
|
impl GitBare {
|
|
/// Find a single commit by revision.
|
|
pub fn find_commit(&self, request: FindCommitRequest) -> GitResult<Commit> {
|
|
let revision = match request.revision.and_then(|s| s.selector) {
|
|
Some(object_selector::Selector::Oid(oid)) => {
|
|
crate::sanitize::validate_oid_hex(&oid.hex)?;
|
|
oid.hex
|
|
}
|
|
Some(object_selector::Selector::Revision(name)) => name.revision,
|
|
None => "HEAD".to_string(),
|
|
};
|
|
crate::sanitize::validate_revision(&revision)?;
|
|
|
|
let repo = self.gix_repo()?;
|
|
let oid = repo
|
|
.rev_parse_single(revision.as_str())
|
|
.map_err(|e| GitError::Gix(e.to_string()))?;
|
|
let commit = oid
|
|
.object()
|
|
.map_err(|e| GitError::Gix(e.to_string()))?
|
|
.try_into_commit()
|
|
.map_err(|e| GitError::Gix(format!("not a commit: {e}")))?;
|
|
|
|
Ok(crate::commit::get_commit::commit_to_pb(
|
|
self,
|
|
&commit,
|
|
request.include_stats,
|
|
))
|
|
}
|
|
|
|
/// Batch lookup commits by OID list.
|
|
pub fn list_commits_by_oid(
|
|
&self,
|
|
request: ListCommitsByOidRequest,
|
|
) -> GitResult<ListCommitsByOidResponse> {
|
|
let repo = self.gix_repo()?;
|
|
let mut commits = Vec::new();
|
|
|
|
for oid_bytes in &request.oids {
|
|
let hex: String = oid_bytes.iter().map(|b| format!("{b:02x}")).collect();
|
|
if let Ok(oid) = gix::ObjectId::from_hex(hex.as_bytes())
|
|
&& let Ok(obj) = repo.find_object(oid)
|
|
&& let Ok(commit) = obj.try_into_commit()
|
|
{
|
|
commits.push(crate::commit::get_commit::commit_to_pb(
|
|
self,
|
|
&commit,
|
|
request.include_stats,
|
|
));
|
|
}
|
|
if commits.len() >= 100 {
|
|
break;
|
|
}
|
|
}
|
|
|
|
Ok(ListCommitsByOidResponse { commits })
|
|
}
|
|
}
|