164 lines
5.4 KiB
Rust
164 lines
5.4 KiB
Rust
//! Copyright (c) 2022-2026 GitDataAi All rights reserved.
|
|
|
|
use crate::bare::GitBare;
|
|
use crate::commit::create_commit::command_ok;
|
|
use crate::error::{GitError, GitResult};
|
|
use crate::pb::{CreateCommitResponse, GetCommitRequest, RevertCommitRequest};
|
|
|
|
const MAX_REVERT_PATCH_BYTES: usize = 100 * 1024 * 1024;
|
|
|
|
impl GitBare {
|
|
pub fn revert_commit(&self, request: RevertCommitRequest) -> GitResult<CreateCommitResponse> {
|
|
let target_branch = request.branch.clone();
|
|
crate::sanitize::validate_ref_name(&target_branch)?;
|
|
let revert_revision = match request.commit.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("commit is required".into())),
|
|
};
|
|
|
|
let repo = self.gix_repo()?;
|
|
|
|
let branch_ref = format!("refs/heads/{}", target_branch);
|
|
let branch_tip = 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 revert_id = repo.rev_parse_single(revert_revision.as_str())?;
|
|
let revert_obj = revert_id
|
|
.object()?
|
|
.try_into_commit()
|
|
.map_err(|e| GitError::Gix(e.to_string()))?;
|
|
|
|
let parent_ids: Vec<String> = revert_obj.parent_ids().map(|p| p.to_string()).collect();
|
|
if parent_ids.len() > 1 {
|
|
return Err(GitError::InvalidArgument(
|
|
"reverting merge commits is not supported without mainline".into(),
|
|
));
|
|
}
|
|
let parent_hex = parent_ids
|
|
.first()
|
|
.ok_or_else(|| GitError::InvalidArgument("cannot revert root commit".into()))?;
|
|
|
|
let tmp_index = tempfile::Builder::new()
|
|
.prefix("gitks-revert-")
|
|
.tempfile_in(&self.bare_dir)?;
|
|
let idx_path = tmp_index.path().to_string_lossy().into_owned();
|
|
let bare = self.bare_dir.to_string_lossy().into_owned();
|
|
|
|
let read_tree = duct::cmd(
|
|
"git",
|
|
["--git-dir", bare.as_str(), "read-tree", branch_tip.as_str()],
|
|
)
|
|
.env("GIT_INDEX_FILE", &idx_path)
|
|
.stdout_capture()
|
|
.stderr_capture()
|
|
.unchecked()
|
|
.run()?;
|
|
command_ok(read_tree)?;
|
|
|
|
let diff = duct::cmd(
|
|
"git",
|
|
[
|
|
"--git-dir",
|
|
bare.as_str(),
|
|
"diff",
|
|
"--binary",
|
|
"--full-index",
|
|
revert_revision.as_str(),
|
|
parent_hex.as_str(),
|
|
],
|
|
)
|
|
.stdout_capture()
|
|
.stderr_capture()
|
|
.unchecked()
|
|
.run()?;
|
|
let patch_data = command_ok(diff)?;
|
|
if patch_data.len() > MAX_REVERT_PATCH_BYTES {
|
|
return Err(GitError::InvalidArgument(format!(
|
|
"revert patch too large ({} bytes, max {MAX_REVERT_PATCH_BYTES})",
|
|
patch_data.len()
|
|
)));
|
|
}
|
|
|
|
let apply = duct::cmd(
|
|
"git",
|
|
[
|
|
"--git-dir",
|
|
bare.as_str(),
|
|
"apply",
|
|
"--cached",
|
|
"--allow-empty",
|
|
"-",
|
|
],
|
|
)
|
|
.env("GIT_INDEX_FILE", &idx_path)
|
|
.stdin_bytes(patch_data.as_bytes())
|
|
.stdout_capture()
|
|
.stderr_capture()
|
|
.unchecked()
|
|
.run()?;
|
|
if !apply.status.success() {
|
|
return Err(GitError::Internal(format!(
|
|
"revert apply failed: {}",
|
|
String::from_utf8_lossy(&apply.stderr)
|
|
)));
|
|
}
|
|
|
|
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 subject = revert_obj
|
|
.message_raw()?
|
|
.to_string()
|
|
.lines()
|
|
.next()
|
|
.unwrap_or_default()
|
|
.to_string();
|
|
let message = format!(
|
|
"Revert \"{}\"\n\nThis reverts commit {}.",
|
|
subject, revert_revision
|
|
);
|
|
|
|
let commit_id = self.commit_tree(
|
|
&tree_id,
|
|
std::slice::from_ref(&branch_tip),
|
|
&message,
|
|
request.committer.as_ref(),
|
|
request.committer.as_ref(),
|
|
)?;
|
|
|
|
self.update_branch_ref(&target_branch, &commit_id, Some(&branch_tip), false)?;
|
|
|
|
Ok(CreateCommitResponse {
|
|
commit: Some(self.get_commit(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,
|
|
})?),
|
|
branch: target_branch,
|
|
})
|
|
}
|
|
}
|