feat(core): implement Git repository operations with gRPC services

- Add advertise_refs functionality for Git protocol communication
- Implement archive service with TAR/ZIP format support and streaming
- Create blame service for Git file annotation with line tracking
- Add branch management including create, delete, rename and compare operations
- Implement merge checking with conflict detection and fast-forward handling
- Add cherry-pick functionality for applying commits between branches
- Integrate gix library for Git repository operations and object handling
- Add comprehensive test suite covering all Git operations
- Implement proper error handling and repository validation
- Add pagination support for large result sets
- Create protobuf definitions for all Git operations and data structures
- Add build system for gRPC code generation and dependency management
This commit is contained in:
zhenyi
2026-06-04 13:05:38 +08:00
commit dcb0fb74c5
98 changed files with 20569 additions and 0 deletions
+56
View File
@@ -0,0 +1,56 @@
use crate::bare::GitBare;
use crate::error::GitResult;
use crate::paginate;
use crate::pb::{
FileMetadata, FindFilesRequest, FindFilesResponse, ListTreeRequest, ObjectType, tree_entry,
};
impl GitBare {
pub fn find_files(&self, request: FindFilesRequest) -> GitResult<FindFilesResponse> {
let revision = request.revision.clone();
let root = if request.pathspec.is_empty() {
vec![String::new()]
} else {
request.pathspec.clone()
};
let mut files = Vec::new();
for pathspec in root {
let response = self.list_tree(ListTreeRequest {
repository: request.repository.clone(),
revision: revision.clone(),
path: pathspec,
recursive: true,
pagination: None,
})?;
for entry in response.entries {
if !request.pattern.is_empty() && !entry.path.contains(&request.pattern) {
continue;
}
let object_type = match tree_entry::EntryType::try_from(entry.r#type)
.unwrap_or(tree_entry::EntryType::TreeEntryTypeUnspecified)
{
tree_entry::EntryType::TreeEntryTypeTree => ObjectType::Tree,
tree_entry::EntryType::TreeEntryTypeCommit => ObjectType::Commit,
tree_entry::EntryType::TreeEntryTypeUnspecified => ObjectType::Unspecified,
_ => ObjectType::Blob,
} as i32;
files.push(FileMetadata {
path: entry.path,
oid: entry.oid,
mode: entry.mode,
size: entry.size,
r#type: object_type,
binary: false,
});
}
}
files.sort_by(|a, b| a.path.cmp(&b.path));
let (files, page_info) = paginate::paginate(&files, request.pagination.as_ref());
Ok(FindFilesResponse {
files,
page_info: Some(page_info),
})
}
}
+38
View File
@@ -0,0 +1,38 @@
use gix::object::tree::EntryKind;
use crate::bare::GitBare;
use crate::error::{GitError, GitResult};
use crate::pb::{FileMetadata, GetFileMetadataRequest, ObjectType, object_selector};
impl GitBare {
pub fn get_file_metadata(&self, request: GetFileMetadataRequest) -> GitResult<FileMetadata> {
let repo = self.gix_repo()?;
let revision = match request.revision.and_then(|s| s.selector) {
Some(object_selector::Selector::Oid(oid)) => oid.hex,
Some(object_selector::Selector::Revision(name)) => name.revision,
None => "HEAD".into(),
};
let tree = repo
.rev_parse_single(format!("{}^{{tree}}", revision).as_str())?
.object()?
.try_into_tree()
.map_err(|e| GitError::Gix(e.to_string()))?;
let entry = tree
.lookup_entry_by_path(&request.path)?
.ok_or_else(|| GitError::NotFound(request.path.clone()))?;
let hex = entry.id().to_string();
let kind = match entry.mode().kind() {
EntryKind::Tree => ObjectType::Tree,
EntryKind::Commit => ObjectType::Commit,
_ => ObjectType::Blob,
} as i32;
Ok(FileMetadata {
path: request.path,
oid: Some(self.oid_to_pb(hex)),
mode: u32::from_str_radix(&format!("{:o}", entry.mode()), 8).unwrap_or(0),
size: 0,
r#type: kind,
binary: false,
})
}
}
+43
View File
@@ -0,0 +1,43 @@
use crate::bare::GitBare;
use crate::error::{GitError, GitResult};
use crate::pb::{GetTreeRequest, ListTreeRequest, Tree};
impl GitBare {
pub fn get_tree(&self, request: GetTreeRequest) -> GitResult<Tree> {
let entries = self.list_tree(ListTreeRequest {
repository: request.repository,
revision: request.revision.clone(),
path: request.path.clone(),
recursive: false,
pagination: None,
})?;
let repo = self.gix_repo()?;
let revision = request
.revision
.and_then(|s| s.selector)
.map(|s| match s {
crate::pb::object_selector::Selector::Oid(oid) => oid.hex,
crate::pb::object_selector::Selector::Revision(name) => name.revision,
})
.unwrap_or_else(|| "HEAD".into());
let root = repo
.rev_parse_single(format!("{}^{{tree}}", revision).as_str())?
.object()?
.try_into_tree()
.map_err(|e| GitError::Gix(e.to_string()))?;
let tree_hex = if request.path.is_empty() {
root.id.to_string()
} else {
root.lookup_entry_by_path(&request.path)?
.ok_or_else(|| GitError::NotFound(request.path.clone()))?
.id()
.to_string()
};
Ok(Tree {
oid: Some(self.oid_to_pb(tree_hex)),
path: request.path,
entries: entries.entries,
truncated: false,
})
}
}
+87
View File
@@ -0,0 +1,87 @@
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};
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)) => oid.hex,
Some(object_selector::Selector::Revision(name)) => 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() {
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();
entries.push(TreeEntry {
name,
path: 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),
});
if request.recursive && matches!(kind, EntryKind::Tree) {
let child = self.list_tree(ListTreeRequest {
repository: request.repository.clone(),
revision: request.revision.clone(),
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()
}
+4
View File
@@ -0,0 +1,4 @@
pub mod find_files;
pub mod get_file_metadata;
pub mod get_tree;
pub mod list_tree;