Files
gitks/commit/find_commit.rs
T
zhenyi 934858bebf refactor(cache): redesign cache system with structured keys and improved performance
- Add repo_path parameter to cached_response and cached_vec_response functions
- Implement structured cache key format with namespace, repo_path, and request proto
- Replace global cache with Moka in-memory cache using weight-based eviction
- Set 256MB memory cap with 10-minute TTL and 2-minute TTI policy
- Add metrics collection for cache operations and evictions
- Implement efficient repo-scoped invalidation using key structure
- Add detailed documentation comments explaining cache architecture
- Remove outdated dependencies and update dependency versions
- Add error handling for encoding failures in cache operations
- Optimize Vec responses with length-delimited encoding and pre-allocation
2026-06-12 12:53:23 +08:00

63 lines
2.0 KiB
Rust

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 })
}
}