Files
gitks/branch/compare_branch.rs
T
zhenyi e386f44ee2 feat(git): add size limits for git operations
- Added MAX_CHERRY_PICK_PATCH_BYTES limit of 100MB for cherry-pick operations
- Added MAX_ACTION_CONTENT_BYTES limit of 100MB for commit action content
- Added MAX_COMMIT_MESSAGE_BYTES limit of 10MB for commit messages
- Added MAX_CHECK_REVISIONS limit of 10,000 for revision checks
- Added MAX_REBASE_COMMITS limit of 10,000 for rebase operations
- Added MAX_REBASE_PATCH_BYTES limit of 100MB for rebase patches
- Added MAX_RESOLUTION_CONTENT_BYTES limit of 100MB for merge conflict resolutions
- Added MAX_REVERT_PATCH_BYTES limit of 100MB for revert operations
- Return InvalidArgument error when size limits are exceeded with descriptive messages
2026-06-12 12:59:50 +08:00

57 lines
2.1 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> {
crate::sanitize::validate_ref_name(&request.source_branch)?;
crate::sanitize::validate_ref_name(&request.target_branch)?;
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,
})
}
}