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:
@@ -0,0 +1,86 @@
|
||||
use crate::bare::GitBare;
|
||||
use crate::error::{GitError, GitResult};
|
||||
use crate::pb::{GetCommitDiffRequest, GetDiffRequest, GetDiffResponse};
|
||||
|
||||
impl GitBare {
|
||||
pub fn get_commit_diff(&self, request: GetCommitDiffRequest) -> GitResult<GetDiffResponse> {
|
||||
let commit = match request.commit.and_then(|s| s.selector) {
|
||||
Some(crate::pb::object_selector::Selector::Oid(oid)) => oid.hex,
|
||||
Some(crate::pb::object_selector::Selector::Revision(name)) => name.revision,
|
||||
None => "HEAD".into(),
|
||||
};
|
||||
let base = self.first_parent_or_empty_tree(&commit)?;
|
||||
self.get_diff(GetDiffRequest {
|
||||
repository: request.repository,
|
||||
base: Some(crate::pb::ObjectSelector {
|
||||
selector: Some(crate::pb::object_selector::Selector::Revision(
|
||||
crate::pb::ObjectName { revision: base },
|
||||
)),
|
||||
}),
|
||||
head: Some(crate::pb::ObjectSelector {
|
||||
selector: Some(crate::pb::object_selector::Selector::Revision(
|
||||
crate::pb::ObjectName { revision: commit },
|
||||
)),
|
||||
}),
|
||||
options: request.options,
|
||||
pagination: request.pagination,
|
||||
})
|
||||
}
|
||||
|
||||
fn first_parent_or_empty_tree(&self, commit: &str) -> GitResult<String> {
|
||||
let result = duct::cmd(
|
||||
"git",
|
||||
[
|
||||
"--git-dir",
|
||||
self.bare_dir.to_string_lossy().as_ref(),
|
||||
"rev-list",
|
||||
"--parents",
|
||||
"-n",
|
||||
"1",
|
||||
commit,
|
||||
],
|
||||
)
|
||||
.stdout_capture()
|
||||
.stderr_capture()
|
||||
.unchecked()
|
||||
.run()?;
|
||||
if !result.status.success() {
|
||||
return Err(GitError::CommandFailed {
|
||||
status_code: result.status.code(),
|
||||
stderr: String::from_utf8_lossy(&result.stderr).into_owned(),
|
||||
});
|
||||
}
|
||||
let output = String::from_utf8_lossy(&result.stdout);
|
||||
let parts = output.split_whitespace().collect::<Vec<_>>();
|
||||
if let Some(parent) = parts.get(1) {
|
||||
return Ok((*parent).to_string());
|
||||
}
|
||||
|
||||
let empty_tree = duct::cmd(
|
||||
"git",
|
||||
[
|
||||
"--git-dir",
|
||||
self.bare_dir.to_string_lossy().as_ref(),
|
||||
"hash-object",
|
||||
"-t",
|
||||
"tree",
|
||||
"-w",
|
||||
"--stdin",
|
||||
],
|
||||
)
|
||||
.stdin_bytes(Vec::<u8>::new())
|
||||
.stdout_capture()
|
||||
.stderr_capture()
|
||||
.unchecked()
|
||||
.run()?;
|
||||
if !empty_tree.status.success() {
|
||||
return Err(GitError::CommandFailed {
|
||||
status_code: empty_tree.status.code(),
|
||||
stderr: String::from_utf8_lossy(&empty_tree.stderr).into_owned(),
|
||||
});
|
||||
}
|
||||
Ok(String::from_utf8_lossy(&empty_tree.stdout)
|
||||
.trim()
|
||||
.to_string())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user