Files
gitks/branch/compare_branch.rs
T
zhenyi dcb0fb74c5 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
2026-06-04 13:05:38 +08:00

55 lines
2.0 KiB
Rust

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,
})
}
}