feat(tree): add recent commit metadata and LFS support to file metadata

- Added RecentCommit message definition with oid, subject and timestamp fields
- Extended TreeEntry, Tree, and FileMetadata messages with is_lfs and recent_commit fields
- Updated get_file_metadata function to include recent commit information
- Added tree module import and recent_commit lookup functionality
- Updated protobuf definitions to include new metadata fields
- Enhanced file metadata response with LFS status and commit history
This commit is contained in:
zhenyi
2026-06-04 13:47:46 +08:00
parent dcb0fb74c5
commit 737e934043
8 changed files with 83 additions and 9 deletions
+7 -1
View File
@@ -4,10 +4,12 @@ use crate::paginate;
use crate::pb::{
FileMetadata, FindFilesRequest, FindFilesResponse, ListTreeRequest, ObjectType, tree_entry,
};
use crate::tree;
impl GitBare {
pub fn find_files(&self, request: FindFilesRequest) -> GitResult<FindFilesResponse> {
let revision = request.revision.clone();
let rev = tree::resolve_revision(&revision);
let root = if request.pathspec.is_empty() {
vec![String::new()]
} else {
@@ -35,13 +37,17 @@ impl GitBare {
tree_entry::EntryType::TreeEntryTypeUnspecified => ObjectType::Unspecified,
_ => ObjectType::Blob,
} as i32;
let entry_path = entry.path.clone();
let rc = tree::recent_commit(self, &rev, &entry_path);
files.push(FileMetadata {
path: entry.path,
path: entry_path,
oid: entry.oid,
mode: entry.mode,
size: entry.size,
r#type: object_type,
binary: false,
is_lfs: false,
recent_commit: rc,
});
}
}
+4
View File
@@ -3,6 +3,7 @@ use gix::object::tree::EntryKind;
use crate::bare::GitBare;
use crate::error::{GitError, GitResult};
use crate::pb::{FileMetadata, GetFileMetadataRequest, ObjectType, object_selector};
use crate::tree;
impl GitBare {
pub fn get_file_metadata(&self, request: GetFileMetadataRequest) -> GitResult<FileMetadata> {
@@ -26,6 +27,7 @@ impl GitBare {
EntryKind::Commit => ObjectType::Commit,
_ => ObjectType::Blob,
} as i32;
let rc = tree::recent_commit(self, &revision, &request.path);
Ok(FileMetadata {
path: request.path,
oid: Some(self.oid_to_pb(hex)),
@@ -33,6 +35,8 @@ impl GitBare {
size: 0,
r#type: kind,
binary: false,
is_lfs: false,
recent_commit: rc,
})
}
}
+5 -1
View File
@@ -4,6 +4,7 @@ use crate::bare::GitBare;
use crate::error::{GitError, GitResult};
use crate::paginate;
use crate::pb::{ListTreeRequest, ListTreeResponse, TreeEntry, object_selector, tree_entry};
use crate::tree;
impl GitBare {
pub fn list_tree(&self, request: ListTreeRequest) -> GitResult<ListTreeResponse> {
@@ -40,13 +41,16 @@ impl GitBare {
};
let kind = entry.kind();
let hex = entry.id().to_string();
let entry_path = path.clone();
entries.push(TreeEntry {
name,
path: path.clone(),
path: entry_path.clone(),
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: tree::recent_commit(self, &revision, &entry_path),
});
if request.recursive && matches!(kind, EntryKind::Tree) {
+46
View File
@@ -2,3 +2,49 @@ pub mod find_files;
pub mod get_file_metadata;
pub mod get_tree;
pub mod list_tree;
use crate::bare::GitBare;
use crate::pb::{self, RecentCommit, object_selector};
pub(crate) fn resolve_revision(sel: &Option<pb::ObjectSelector>) -> String {
match sel.as_ref().and_then(|s| s.selector.as_ref()) {
Some(object_selector::Selector::Oid(oid)) => oid.hex.clone(),
Some(object_selector::Selector::Revision(name)) => name.revision.clone(),
None => "HEAD".into(),
}
}
pub(crate) fn recent_commit(gb: &GitBare, revision: &str, path: &str) -> Option<RecentCommit> {
let output = std::process::Command::new("git")
.args([
"--git-dir",
&gb.bare_dir.to_string_lossy(),
"log",
"-1",
"--format=%H %s %at",
revision,
"--",
path,
])
.output()
.ok()?;
if !output.status.success() {
return None;
}
let line = String::from_utf8_lossy(&output.stdout).trim().to_string();
if line.is_empty() {
return None;
}
let (hex, rest) = line.split_once(' ')?;
let (subject, ts_str) = rest.rsplit_once(' ')?;
let ts: i64 = ts_str.parse().ok()?;
Some(RecentCommit {
oid: Some(gb.oid_to_pb(hex)),
subject: subject.to_string(),
committed_timestamp: ts,
})
}
pub(crate) fn is_lfs_pointer(data: &[u8]) -> bool {
data.starts_with(b"version https://git-lfs.github.com/spec/v1")
}