Files
gitks/merge/check_merge.rs
T
zhenyi d243dce027 refactor(server): replace custom remote clients with macro-based implementation
- Replaced manual remote client functions with remote_client! macro for archive, blame, branch, commit, and diff services
- Simplified remote client creation logic using declarative macro approach
- Maintained same functionality while reducing code duplication across services

security(bare): enhance path traversal protection with comprehensive validation

- Added early relative_path validation to prevent path traversal attacks
- Implemented unified path validation to avoid TOCTOU race conditions
- Enhanced canonicalization checks for both existing and non-existent paths
- Added detailed logging for path traversal detection attempts

feat(cache): migrate from CLruCache to Moka with TTL and invalidation support

- Replaced clru dependency with moka for improved caching capabilities
- Added 300-second time-to-live for cache entries
- Implemented repository-specific cache invalidation mechanism
- Enhanced cache operations with thread-safe async support

refactor(commit): improve security validation for commit operations

- Added ref name validation to prevent command injection in cherry_pick_commit
- Implemented revision validation for commit selectors
- Added comprehensive input validation for create_commit parameters
- Enhanced file path validation to prevent traversal
2026-06-08 09:43:57 +08:00

105 lines
3.6 KiB
Rust

use crate::bare::GitBare;
use crate::error::GitResult;
use crate::pb::{CheckMergeRequest, MergeResult, merge_result};
use crate::resolve_revision;
impl GitBare {
pub fn check_merge(&self, request: CheckMergeRequest) -> GitResult<MergeResult> {
let target = resolve_revision!(request.target);
let source = resolve_revision!(request.source);
let repo = self.gix_repo()?;
let target_id = repo.rev_parse_single(target.as_str())?;
let source_id = repo.rev_parse_single(source.as_str())?;
if target_id == source_id {
return Ok(MergeResult {
status: merge_result::Status::MergeResultStatusAlreadyUpToDate as i32,
commit: None,
merge_base: Some(self.oid_to_pb(target_id.to_string())),
conflicts: vec![],
stats: None,
message: String::from("Already up to date"),
});
}
let merge_base = repo
.merge_base(target_id.detach(), source_id.detach())
.ok()
.map(|id| id.to_string());
if let Some(ref base) = merge_base {
if *base == target_id.to_string() {
return Ok(MergeResult {
status: merge_result::Status::MergeResultStatusFastForward as i32,
commit: None,
merge_base: Some(self.oid_to_pb(base.clone())),
conflicts: vec![],
stats: None,
message: String::from("Fast-forward"),
});
}
if *base == source_id.to_string() {
return Ok(MergeResult {
status: merge_result::Status::MergeResultStatusAlreadyUpToDate as i32,
commit: None,
merge_base: Some(self.oid_to_pb(base.clone())),
conflicts: vec![],
stats: None,
message: String::from("Already up to date"),
});
}
}
let merge_base_oid = merge_base.as_ref().map(|b| self.oid_to_pb(b.clone()));
let result = duct::cmd(
"git",
[
"--git-dir",
self.bare_dir.to_string_lossy().as_ref(),
"merge-tree",
"--write-tree",
"--no-messages",
"-z",
&target,
&source,
],
)
.stdout_capture()
.stderr_capture()
.unchecked()
.run()?;
let stdout = String::from_utf8_lossy(&result.stdout).into_owned();
if result.status.success() {
Ok(MergeResult {
status: merge_result::Status::MergeResultStatusMerged as i32,
commit: None,
merge_base: merge_base_oid,
conflicts: vec![],
stats: None,
message: stdout.trim().to_string(),
})
} else {
let conflicts = stdout
.split('\0')
.filter(|s| !s.is_empty())
.map(|path| crate::pb::MergeConflict {
path: path.to_string(),
..Default::default()
})
.collect();
Ok(MergeResult {
status: merge_result::Status::MergeResultStatusConflicts as i32,
commit: None,
merge_base: merge_base_oid,
conflicts,
stats: None,
message: String::from("Merge conflicts detected"),
})
}
}
}