Files
gitks/refs/update_refs.rs
T
zhenyi 9a0c26e5f6 refactor(actor): implement Raft consensus algorithm for cluster leader election
- Add voting mechanism with term tracking and vote persistence
- Implement election triggering logic with majority vote counting
- Add primary/replica role transition handling with state management
- Integrate health check failure detection for automatic elections
- Refactor actor messaging system for distributed coordination
- Update repository registration to query cluster for existing primary
- Add broadcast mechanism for role change notifications
- Implement proper term comparison and duplicate request filtering
- Upgrade dependency versions including tokio-util for async utilities
- Optimize code formatting and line wrapping for improved readability
- Remove redundant blank lines and improve code structure consistency
- Enhance error logging and trace information for debugging purposes
2026-06-10 12:35:10 +08:00

168 lines
5.7 KiB
Rust

use crate::bare::GitBare;
use crate::error::GitResult;
use crate::pb::*;
impl GitBare {
/// Update multiple refs atomically using `git update-ref --stdin`.
pub fn update_references(
&self,
request: UpdateReferencesRequest,
) -> GitResult<UpdateReferencesResponse> {
let mut stdin_input = String::new();
for update in &request.updates {
crate::sanitize::validate_ref_name(&update.ref_name)?;
crate::sanitize::validate_revision(&update.new_oid)?;
if !update.old_oid.is_empty() {
crate::sanitize::validate_revision(&update.old_oid)?;
stdin_input.push_str(&format!(
"update {} {}\0{}\n",
update.ref_name, update.new_oid, update.old_oid
));
} else {
stdin_input.push_str(&format!("update {} {}\n", update.ref_name, update.new_oid));
}
}
if stdin_input.is_empty() {
return Ok(UpdateReferencesResponse::default());
}
let output = std::process::Command::new("git")
.args([
"--git-dir",
&self.bare_dir.to_string_lossy(),
"update-ref",
"--stdin",
"-z",
])
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.output()
.map_err(|e| crate::error::GitError::CommandFailed {
status_code: None,
stderr: e.to_string(),
})?;
let stderr = String::from_utf8_lossy(&output.stderr).into_owned();
if !output.status.success() {
return Ok(UpdateReferencesResponse {
failed_refs: request.updates.iter().map(|u| u.ref_name.clone()).collect(),
error: stderr.trim().to_string(),
});
}
Ok(UpdateReferencesResponse::default())
}
/// Delete refs in bulk.
pub fn delete_refs(&self, request: DeleteRefsRequest) -> GitResult<DeleteRefsResponse> {
let mut failed = Vec::new();
let mut error_msg = String::new();
for ref_name in &request.ref_names {
crate::sanitize::validate_ref_name(ref_name)?;
let output = std::process::Command::new("git")
.args([
"--git-dir",
&self.bare_dir.to_string_lossy(),
"update-ref",
"-d",
ref_name,
])
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.output()
.map_err(|e| crate::error::GitError::CommandFailed {
status_code: None,
stderr: e.to_string(),
})?;
if !output.status.success() {
failed.push(ref_name.clone());
if error_msg.is_empty() {
error_msg = String::from_utf8_lossy(&output.stderr).trim().to_string();
}
}
}
Ok(DeleteRefsResponse {
failed_refs: failed,
error: error_msg,
})
}
/// Write a single ref with optional expected-old-oid check.
pub fn write_ref(&self, request: WriteRefRequest) -> GitResult<WriteRefResponse> {
crate::sanitize::validate_ref_name(&request.ref_name)?;
crate::sanitize::validate_revision(&request.new_oid)?;
let mut args = vec![
"--git-dir".to_string(),
self.bare_dir.to_string_lossy().into_owned(),
"update-ref".to_string(),
request.ref_name.clone(),
request.new_oid.clone(),
];
if !request.old_oid.is_empty() {
crate::sanitize::validate_revision(&request.old_oid)?;
args.push(request.old_oid.clone());
}
let output = std::process::Command::new("git")
.args(&args)
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.output()
.map_err(|e| crate::error::GitError::CommandFailed {
status_code: None,
stderr: e.to_string(),
})?;
if !output.status.success() {
return Ok(WriteRefResponse {
ok: false,
error: String::from_utf8_lossy(&output.stderr).trim().to_string(),
});
}
Ok(WriteRefResponse {
ok: true,
error: String::new(),
})
}
/// Check if a ref exists.
pub fn ref_exists(&self, request: RefExistsRequest) -> GitResult<RefExistsResponse> {
crate::sanitize::validate_ref_name(&request.ref_name)?;
let repo = self.gix_repo()?;
let exists = repo
.try_find_reference(&request.ref_name)
.ok()
.flatten()
.is_some();
Ok(RefExistsResponse { exists })
}
/// Find the default branch name.
pub fn find_default_branch_name(&self) -> GitResult<FindDefaultBranchNameResponse> {
let result = std::process::Command::new("git")
.args([
"--git-dir",
&self.bare_dir.to_string_lossy(),
"symbolic-ref",
"HEAD",
])
.output()
.map_err(|e| crate::error::GitError::CommandFailed {
status_code: None,
stderr: e.to_string(),
})?;
let name = String::from_utf8_lossy(&result.stdout)
.trim()
.strip_prefix("refs/heads/")
.map(|b| b.to_string())
.unwrap_or_default();
Ok(FindDefaultBranchNameResponse { name })
}
}