293102e5f2
- 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
152 lines
5.3 KiB
Rust
152 lines
5.3 KiB
Rust
use crate::bare::GitBare;
|
|
use crate::commit::create_commit::command_ok;
|
|
use crate::error::{GitError, GitResult};
|
|
use crate::pb::{MergeResult, ResolveMergeConflictsRequest, merge_result};
|
|
|
|
const MAX_RESOLUTION_CONTENT_BYTES: usize = 100 * 1024 * 1024;
|
|
|
|
impl GitBare {
|
|
pub fn resolve_merge_conflicts(
|
|
&self,
|
|
request: ResolveMergeConflictsRequest,
|
|
) -> GitResult<MergeResult> {
|
|
let target_branch = request.target_branch.clone();
|
|
crate::sanitize::validate_ref_name(&target_branch)?;
|
|
let source_revision = match request.source.and_then(|s| s.selector) {
|
|
Some(crate::pb::object_selector::Selector::Oid(oid)) => {
|
|
crate::sanitize::validate_oid_hex(&oid.hex)?;
|
|
oid.hex
|
|
}
|
|
Some(crate::pb::object_selector::Selector::Revision(name)) => {
|
|
crate::sanitize::validate_revision(&name.revision)?;
|
|
name.revision
|
|
}
|
|
None => return Err(GitError::InvalidArgument("source is required".into())),
|
|
};
|
|
|
|
let repo = self.gix_repo()?;
|
|
let branch_ref = format!("refs/heads/{}", target_branch);
|
|
let target_id = repo
|
|
.find_reference(branch_ref.as_str())
|
|
.ok()
|
|
.and_then(|mut r| r.peel_to_id().ok())
|
|
.map(|id| id.to_string())
|
|
.ok_or_else(|| GitError::RefNotFound(target_branch.clone()))?;
|
|
|
|
let source_id = repo.rev_parse_single(source_revision.as_str())?.to_string();
|
|
|
|
let bare = self.bare_dir.to_string_lossy().into_owned();
|
|
let tmp_index = tempfile::Builder::new()
|
|
.prefix("gitks-resolve-")
|
|
.tempfile_in(&self.bare_dir)?;
|
|
let idx_path = tmp_index.path().to_string_lossy().into_owned();
|
|
|
|
let read_tree = duct::cmd(
|
|
"git",
|
|
["--git-dir", bare.as_str(), "read-tree", target_id.as_str()],
|
|
)
|
|
.env("GIT_INDEX_FILE", &idx_path)
|
|
.stdout_capture()
|
|
.stderr_capture()
|
|
.unchecked()
|
|
.run()?;
|
|
command_ok(read_tree)?;
|
|
|
|
const MAX_RESOLUTIONS_COUNT: usize = 10_000;
|
|
if request.resolutions.len() > MAX_RESOLUTIONS_COUNT {
|
|
return Err(GitError::InvalidArgument(format!(
|
|
"too many resolutions (max {MAX_RESOLUTIONS_COUNT})"
|
|
)));
|
|
}
|
|
|
|
for resolution in &request.resolutions {
|
|
crate::sanitize::validate_file_path(&resolution.path)?;
|
|
if resolution.content.len() > MAX_RESOLUTION_CONTENT_BYTES {
|
|
return Err(GitError::InvalidArgument(format!(
|
|
"resolution content too large ({} bytes, max {MAX_RESOLUTION_CONTENT_BYTES})",
|
|
resolution.content.len()
|
|
)));
|
|
}
|
|
let hash = duct::cmd(
|
|
"git",
|
|
["--git-dir", bare.as_str(), "hash-object", "-w", "--stdin"],
|
|
)
|
|
.stdin_bytes(resolution.content.clone())
|
|
.stdout_capture()
|
|
.stderr_capture()
|
|
.unchecked()
|
|
.run()?;
|
|
let blob = command_ok(hash)?.trim().to_string();
|
|
|
|
let update = duct::cmd(
|
|
"git",
|
|
[
|
|
"--git-dir",
|
|
bare.as_str(),
|
|
"update-index",
|
|
"--add",
|
|
"--cacheinfo",
|
|
"100644",
|
|
&blob,
|
|
&resolution.path,
|
|
],
|
|
)
|
|
.env("GIT_INDEX_FILE", &idx_path)
|
|
.env("GIT_WORK_TREE", bare.as_str())
|
|
.stdout_capture()
|
|
.stderr_capture()
|
|
.unchecked()
|
|
.run()?;
|
|
command_ok(update)?;
|
|
}
|
|
|
|
let write_tree = duct::cmd("git", ["--git-dir", bare.as_str(), "write-tree"])
|
|
.env("GIT_INDEX_FILE", &idx_path)
|
|
.stdout_capture()
|
|
.stderr_capture()
|
|
.unchecked()
|
|
.run()?;
|
|
let tree_id = command_ok(write_tree)?.trim().to_string();
|
|
|
|
let message = if !request.message.is_empty() {
|
|
request.message.clone()
|
|
} else {
|
|
format!(
|
|
"Merge '{}' into {} (resolved conflicts)",
|
|
source_revision, target_branch
|
|
)
|
|
};
|
|
|
|
let parents = vec![target_id.clone(), source_id.clone()];
|
|
let commit_id = self.commit_tree(
|
|
&tree_id,
|
|
&parents,
|
|
&message,
|
|
request.committer.as_ref(),
|
|
request.committer.as_ref(),
|
|
)?;
|
|
|
|
self.update_branch_ref(&target_branch, &commit_id, Some(&target_id), false)?;
|
|
|
|
Ok(MergeResult {
|
|
status: merge_result::Status::MergeResultStatusMerged as i32,
|
|
commit: Some(self.get_commit(crate::pb::GetCommitRequest {
|
|
repository: request.repository,
|
|
revision: Some(crate::pb::ObjectSelector {
|
|
selector: Some(crate::pb::object_selector::Selector::Revision(
|
|
crate::pb::ObjectName {
|
|
revision: commit_id,
|
|
},
|
|
)),
|
|
}),
|
|
include_stats: false,
|
|
include_raw: false,
|
|
})?),
|
|
merge_base: None,
|
|
conflicts: vec![],
|
|
stats: None,
|
|
message,
|
|
})
|
|
}
|
|
}
|