Files
gitks/tree/list_tree.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

110 lines
4.1 KiB
Rust

use gix::object::tree::EntryKind;
use crate::bare::GitBare;
use crate::error::{GitError, GitResult};
use crate::paginate;
use crate::pb::{ListTreeRequest, ListTreeResponse, TreeEntry, object_selector, tree_entry};
const MAX_RECURSIVE_TREE_DEPTH: usize = 256;
impl GitBare {
pub fn list_tree(&self, request: ListTreeRequest) -> GitResult<ListTreeResponse> {
let repo = self.gix_repo()?;
let revision = match request.revision.clone().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)) => {
crate::sanitize::validate_revision(&name.revision)?;
name.revision
}
None => "HEAD".into(),
};
let mut tree = repo
.rev_parse_single(format!("{}^{{tree}}", revision).as_str())?
.object()?
.try_into_tree()
.map_err(|e| GitError::Gix(e.to_string()))?;
if !request.path.is_empty() {
crate::sanitize::validate_file_path(&request.path)?;
let depth = request
.path
.split('/')
.filter(|part| !part.is_empty())
.count();
if depth > MAX_RECURSIVE_TREE_DEPTH {
return Err(GitError::InvalidArgument(format!(
"tree depth exceeds maximum of {MAX_RECURSIVE_TREE_DEPTH}"
)));
}
let entry = tree
.lookup_entry_by_path(&request.path)?
.ok_or_else(|| GitError::NotFound(request.path.clone()))?;
tree = entry
.object()?
.try_into_tree()
.map_err(|e| GitError::Gix(e.to_string()))?;
}
let base = request.path.trim_matches('/').to_string();
let mut entries = Vec::new();
for entry in tree.iter() {
let entry = entry?;
let name = String::from_utf8_lossy(entry.filename()).into_owned();
let path = if base.is_empty() {
name.clone()
} else {
format!("{base}/{name}")
};
let kind = entry.kind();
let hex = entry.id().to_string();
let child_path = path.clone();
entries.push(TreeEntry {
name,
path,
oid: Some(self.oid_to_pb(hex)),
r#type: entry_type(kind) as i32,
mode: u32::from_str_radix(&format!("{:o}", entry.mode()), 8).unwrap_or(0),
size: entry_size(&repo, entry.id().to_string().as_str()).unwrap_or(0),
is_lfs: false,
recent_commit: None, // populated on demand, not per-entry subprocess
});
if request.recursive && matches!(kind, EntryKind::Tree) {
let child = self.list_tree(ListTreeRequest {
repository: request.repository.clone(),
revision: request.revision.clone(),
path: child_path,
recursive: true,
pagination: None,
})?;
entries.extend(child.entries);
}
}
let (entries, page_info) = paginate::paginate(&entries, request.pagination.as_ref());
Ok(ListTreeResponse {
entries,
page_info: Some(page_info),
truncated: false,
})
}
}
fn entry_type(kind: EntryKind) -> tree_entry::EntryType {
match kind {
EntryKind::Tree => tree_entry::EntryType::TreeEntryTypeTree,
EntryKind::Blob => tree_entry::EntryType::TreeEntryTypeBlob,
EntryKind::BlobExecutable => tree_entry::EntryType::TreeEntryTypeExecutable,
EntryKind::Link => tree_entry::EntryType::TreeEntryTypeSymlink,
EntryKind::Commit => tree_entry::EntryType::TreeEntryTypeCommit,
}
}
fn entry_size(repo: &gix::Repository, oid: &str) -> Option<i64> {
let id = gix::hash::ObjectId::from_hex(oid.as_bytes()).ok()?;
let object = repo.find_object(id).ok()?;
object.data.len().try_into().ok()
}