Files
gitks/diff/raw.rs
T
zhenyi 934858bebf refactor(cache): redesign cache system with structured keys and improved performance
- Add repo_path parameter to cached_response and cached_vec_response functions
- Implement structured cache key format with namespace, repo_path, and request proto
- Replace global cache with Moka in-memory cache using weight-based eviction
- Set 256MB memory cap with 10-minute TTL and 2-minute TTI policy
- Add metrics collection for cache operations and evictions
- Implement efficient repo-scoped invalidation using key structure
- Add detailed documentation comments explaining cache architecture
- Remove outdated dependencies and update dependency versions
- Add error handling for encoding failures in cache operations
- Optimize Vec responses with length-delimited encoding and pre-allocation
2026-06-12 12:53:23 +08:00

123 lines
4.1 KiB
Rust

use crate::bare::GitBare;
use crate::error::GitResult;
use crate::pb::*;
const MAX_RAW_DIFF_OUTPUT_BYTES: usize = 64 * 1024 * 1024;
impl GitBare {
/// Stream raw diff output.
pub fn raw_diff(&self, request: RawDiffRequest) -> GitResult<Vec<RawDiffResponse>> {
let base = &request.base;
let head = &request.head;
crate::sanitize::validate_revision(base)?;
crate::sanitize::validate_revision(head)?;
let mut args = vec![
"--git-dir".to_string(),
self.bare_dir.to_string_lossy().into_owned(),
"diff".to_string(),
];
let mut pathspecs = Vec::new();
// Apply options if present
if let Some(ref opts) = request.options {
if opts.recursive {
args.push("--recursive".to_string());
}
if opts.include_binary {
args.push("--binary".to_string());
} else {
args.push("--no-binary".to_string());
}
for pathspec in &opts.pathspec {
crate::sanitize::validate_file_path(pathspec)?;
pathspecs.push(pathspec.clone());
}
}
args.push(base.clone());
args.push(head.clone());
if !pathspecs.is_empty() {
args.push("--".to_string());
args.extend(pathspecs);
}
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 Err(crate::error::GitError::CommandFailed {
status_code: output.status.code(),
stderr: String::from_utf8_lossy(&output.stderr).trim().to_string(),
});
}
if output.stdout.len() > MAX_RAW_DIFF_OUTPUT_BYTES {
return Err(crate::error::GitError::InvalidArgument(format!(
"raw diff output too large (max {MAX_RAW_DIFF_OUTPUT_BYTES} bytes)"
)));
}
// Chunk the output for streaming
const CHUNK_SIZE: usize = 32768;
let data = output.stdout;
let chunks: Vec<RawDiffResponse> = data
.chunks(CHUNK_SIZE)
.map(|c| RawDiffResponse { data: c.to_vec() })
.collect();
Ok(chunks)
}
/// Stream raw patch (format-patch) output.
pub fn raw_patch(&self, request: RawPatchRequest) -> GitResult<Vec<RawPatchResponse>> {
crate::sanitize::validate_revision(&request.base)?;
crate::sanitize::validate_revision(&request.head)?;
let range = format!("{}..{}", request.base, request.head);
let output = std::process::Command::new("git")
.args([
"--git-dir",
&self.bare_dir.to_string_lossy(),
"format-patch",
"--stdout",
&range,
])
.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 Err(crate::error::GitError::CommandFailed {
status_code: output.status.code(),
stderr: String::from_utf8_lossy(&output.stderr).trim().to_string(),
});
}
if output.stdout.len() > MAX_RAW_DIFF_OUTPUT_BYTES {
return Err(crate::error::GitError::InvalidArgument(format!(
"raw patch output too large (max {MAX_RAW_DIFF_OUTPUT_BYTES} bytes)"
)));
}
const CHUNK_SIZE: usize = 32768;
let data = output.stdout;
let chunks: Vec<RawPatchResponse> = data
.chunks(CHUNK_SIZE)
.map(|c| RawPatchResponse { data: c.to_vec() })
.collect();
Ok(chunks)
}
}