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,54 @@
|
||||
use crate::bare::GitBare;
|
||||
use crate::error::{GitError, GitResult};
|
||||
use crate::pb::{CompareBranchRequest, CompareBranchResponse};
|
||||
|
||||
impl GitBare {
|
||||
pub fn compare_branch(
|
||||
&self,
|
||||
request: CompareBranchRequest,
|
||||
) -> GitResult<CompareBranchResponse> {
|
||||
let repo = self.gix_repo()?;
|
||||
let source_ref = format!("refs/heads/{}", request.source_branch);
|
||||
let target_ref = format!("refs/heads/{}", request.target_branch);
|
||||
let source_id = repo.find_reference(source_ref.as_str())?.peel_to_id()?;
|
||||
let target_id = repo.find_reference(target_ref.as_str())?.peel_to_id()?;
|
||||
let source_hex = source_id.to_string();
|
||||
let target_hex = target_id.to_string();
|
||||
let merge_base = repo
|
||||
.merge_base(source_id.detach(), target_id.detach())
|
||||
.ok()
|
||||
.map(|id| self.oid_to_pb(id.to_string()));
|
||||
let result = duct::cmd(
|
||||
"git",
|
||||
[
|
||||
"--git-dir",
|
||||
self.bare_dir.to_string_lossy().as_ref(),
|
||||
"rev-list",
|
||||
"--left-right",
|
||||
"--count",
|
||||
&format!("{}...{}", source_hex, target_hex),
|
||||
],
|
||||
)
|
||||
.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: Vec<&str> = output.split_whitespace().collect();
|
||||
let ahead_by = parts.first().and_then(|s| s.parse().ok()).unwrap_or(0);
|
||||
let behind_by = parts.get(1).and_then(|s| s.parse().ok()).unwrap_or(0);
|
||||
Ok(CompareBranchResponse {
|
||||
ahead: ahead_by > 0,
|
||||
behind: behind_by > 0,
|
||||
ahead_by,
|
||||
behind_by,
|
||||
merge_base,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
use std::process::Command;
|
||||
|
||||
use crate::bare::GitBare;
|
||||
use crate::error::{GitError, GitResult};
|
||||
use crate::pb::{Branch, CreateBranchRequest, GetBranchRequest, object_selector};
|
||||
|
||||
impl GitBare {
|
||||
pub fn create_branch(&self, request: CreateBranchRequest) -> GitResult<Branch> {
|
||||
let revision = match request.start_point.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 args = vec!["branch".to_string()];
|
||||
if request.force {
|
||||
args.push("-f".into());
|
||||
}
|
||||
args.push(request.name.clone());
|
||||
args.push(revision);
|
||||
let output = Command::new("git")
|
||||
.arg("--git-dir")
|
||||
.arg(&self.bare_dir)
|
||||
.args(&args)
|
||||
.output()?;
|
||||
if !output.status.success() {
|
||||
return Err(GitError::CommandFailed {
|
||||
status_code: output.status.code(),
|
||||
stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
|
||||
});
|
||||
}
|
||||
self.get_branch(GetBranchRequest {
|
||||
repository: request.repository,
|
||||
name: request.name,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
use std::process::Command;
|
||||
|
||||
use crate::bare::GitBare;
|
||||
use crate::error::{GitError, GitResult};
|
||||
use crate::pb::DeleteBranchRequest;
|
||||
|
||||
impl GitBare {
|
||||
pub fn delete_branch(&self, request: DeleteBranchRequest) -> GitResult<()> {
|
||||
let flag = if request.force { "-D" } else { "-d" };
|
||||
let output = Command::new("git")
|
||||
.arg("--git-dir")
|
||||
.arg(&self.bare_dir)
|
||||
.args(["branch", flag, &request.name])
|
||||
.output()?;
|
||||
if !output.status.success() {
|
||||
return Err(GitError::CommandFailed {
|
||||
status_code: output.status.code(),
|
||||
stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
use crate::bare::GitBare;
|
||||
use crate::error::GitResult;
|
||||
use crate::pb::{Branch, GetBranchRequest};
|
||||
|
||||
impl GitBare {
|
||||
pub fn get_branch(&self, request: GetBranchRequest) -> GitResult<Branch> {
|
||||
let repo = self.gix_repo()?;
|
||||
let refname = format!("refs/heads/{}", request.name);
|
||||
let mut r = repo.find_reference(refname.as_str())?;
|
||||
let hex = r.peel_to_id()?.to_string();
|
||||
Ok(Branch {
|
||||
name: request.name,
|
||||
full_ref: refname,
|
||||
target_oid: Some(self.oid_to_pb(hex)),
|
||||
commit: None,
|
||||
upstream: None,
|
||||
is_default: false,
|
||||
is_head: false,
|
||||
is_merged: false,
|
||||
is_detached: false,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
use crate::bare::GitBare;
|
||||
use crate::error::GitResult;
|
||||
use crate::paginate;
|
||||
use crate::pb::{Branch, ListBranchesRequest, ListBranchesResponse};
|
||||
|
||||
impl GitBare {
|
||||
pub fn list_branches(&self, request: ListBranchesRequest) -> GitResult<ListBranchesResponse> {
|
||||
let repo = self.gix_repo()?;
|
||||
|
||||
let merged_set = if request.merged_into_head || request.not_merged_into_head {
|
||||
let flag = if request.merged_into_head {
|
||||
"--merged"
|
||||
} else {
|
||||
"--no-merged"
|
||||
};
|
||||
let check = duct::cmd(
|
||||
"git",
|
||||
[
|
||||
"--git-dir",
|
||||
self.bare_dir.to_string_lossy().as_ref(),
|
||||
"branch",
|
||||
flag,
|
||||
"HEAD",
|
||||
],
|
||||
)
|
||||
.stdout_capture()
|
||||
.stderr_capture()
|
||||
.unchecked()
|
||||
.run();
|
||||
match check {
|
||||
Ok(out) => String::from_utf8_lossy(&out.stdout)
|
||||
.lines()
|
||||
.map(|l| l.trim().trim_start_matches("* ").to_string())
|
||||
.collect::<Vec<_>>(),
|
||||
Err(_) => Vec::new(),
|
||||
}
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
|
||||
let mut branches: Vec<Branch> = Vec::new();
|
||||
for r in repo.references()?.local_branches()? {
|
||||
let mut r = r.map_err(|e| crate::error::GitError::Gix(e.to_string()))?;
|
||||
let name = r.name().shorten().to_string();
|
||||
if !request.pattern.is_empty() && !name.contains(&request.pattern) {
|
||||
continue;
|
||||
}
|
||||
if request.merged_into_head && !merged_set.contains(&name) {
|
||||
continue;
|
||||
}
|
||||
if request.not_merged_into_head && merged_set.contains(&name) {
|
||||
continue;
|
||||
}
|
||||
let hex = r
|
||||
.peel_to_id()
|
||||
.ok()
|
||||
.map(|id| id.to_string())
|
||||
.unwrap_or_default();
|
||||
branches.push(Branch {
|
||||
name,
|
||||
full_ref: r.name().to_string(),
|
||||
target_oid: Some(self.oid_to_pb(hex)),
|
||||
commit: None,
|
||||
upstream: None,
|
||||
is_default: false,
|
||||
is_head: false,
|
||||
is_merged: false,
|
||||
is_detached: false,
|
||||
});
|
||||
}
|
||||
paginate::apply_sort(&mut branches, request.sort_direction);
|
||||
let (branches, page_info) = paginate::paginate(&branches, request.pagination.as_ref());
|
||||
Ok(ListBranchesResponse {
|
||||
branches,
|
||||
page_info: Some(page_info),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
pub mod compare_branch;
|
||||
pub mod create_branch;
|
||||
pub mod delete_branch;
|
||||
pub mod get_branch;
|
||||
pub mod list_branches;
|
||||
pub mod rename_branch;
|
||||
pub mod set_branch_upstream;
|
||||
pub mod update_branch_target;
|
||||
@@ -0,0 +1,25 @@
|
||||
use std::process::Command;
|
||||
|
||||
use crate::bare::GitBare;
|
||||
use crate::error::{GitError, GitResult};
|
||||
use crate::pb::{Branch, GetBranchRequest, RenameBranchRequest};
|
||||
|
||||
impl GitBare {
|
||||
pub fn rename_branch(&self, request: RenameBranchRequest) -> GitResult<Branch> {
|
||||
let output = Command::new("git")
|
||||
.arg("--git-dir")
|
||||
.arg(&self.bare_dir)
|
||||
.args(["branch", "-m", &request.old_name, &request.new_name])
|
||||
.output()?;
|
||||
if !output.status.success() {
|
||||
return Err(GitError::CommandFailed {
|
||||
status_code: output.status.code(),
|
||||
stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
|
||||
});
|
||||
}
|
||||
self.get_branch(GetBranchRequest {
|
||||
repository: request.repository,
|
||||
name: request.new_name,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
use crate::bare::GitBare;
|
||||
use crate::error::{GitError, GitResult};
|
||||
use crate::pb::{Branch, GetBranchRequest, SetBranchUpstreamRequest};
|
||||
|
||||
impl GitBare {
|
||||
pub fn set_branch_upstream(&self, request: SetBranchUpstreamRequest) -> GitResult<Branch> {
|
||||
if let Some(upstream) = request.upstream {
|
||||
let tracking = format!("{}/{}", upstream.remote_name, upstream.remote_branch_name);
|
||||
let result = duct::cmd(
|
||||
"git",
|
||||
[
|
||||
"--git-dir",
|
||||
self.bare_dir.to_string_lossy().as_ref(),
|
||||
"branch",
|
||||
"--set-upstream-to",
|
||||
&tracking,
|
||||
&request.name,
|
||||
],
|
||||
)
|
||||
.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(),
|
||||
});
|
||||
}
|
||||
}
|
||||
self.get_branch(GetBranchRequest {
|
||||
repository: request.repository,
|
||||
name: request.name,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#![allow(clippy::collapsible_if)]
|
||||
use std::process::Command;
|
||||
|
||||
use crate::bare::GitBare;
|
||||
use crate::error::{GitError, GitResult};
|
||||
use crate::pb::{Branch, GetBranchRequest, UpdateBranchTargetRequest};
|
||||
|
||||
impl GitBare {
|
||||
pub fn update_branch_target(&self, request: UpdateBranchTargetRequest) -> GitResult<Branch> {
|
||||
let new_oid = request
|
||||
.new_oid
|
||||
.as_ref()
|
||||
.ok_or_else(|| GitError::InvalidArgument("new_oid is required".into()))?
|
||||
.hex
|
||||
.clone();
|
||||
let refname = format!("refs/heads/{}", request.name);
|
||||
let mut args = vec!["update-ref".to_string(), refname.clone(), new_oid];
|
||||
if !request.force
|
||||
&& let Some(old) = request.expected_old_oid.as_ref()
|
||||
{
|
||||
args.push(old.hex.clone());
|
||||
}
|
||||
let output = Command::new("git")
|
||||
.arg("--git-dir")
|
||||
.arg(&self.bare_dir)
|
||||
.args(&args)
|
||||
.output()?;
|
||||
if !output.status.success() {
|
||||
return Err(GitError::CommandFailed {
|
||||
status_code: output.status.code(),
|
||||
stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
|
||||
});
|
||||
}
|
||||
self.get_branch(GetBranchRequest {
|
||||
repository: request.repository,
|
||||
name: refname.trim_start_matches("refs/heads/").to_string(),
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user