Files
gitks/diff/get_diff_stats.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

102 lines
3.2 KiB
Rust

use crate::bare::GitBare;
use crate::error::{GitError, GitResult};
use crate::pb::GetDiffStatsRequest;
use crate::resolve_revision;
impl GitBare {
pub fn get_diff_stats(&self, request: GetDiffStatsRequest) -> GitResult<crate::pb::DiffStats> {
let base = resolve_revision!(request.base);
let head = resolve_revision!(request.head);
diff_stats_for_range(self, &base, &head, request.options.as_ref())
}
}
pub(crate) fn diff_stats_for_range(
repo: &GitBare,
base: &str,
head: &str,
options: Option<&crate::pb::DiffOptions>,
) -> GitResult<crate::pb::DiffStats> {
let mut args = vec![
"--git-dir".to_string(),
repo.bare_dir.to_string_lossy().into_owned(),
"diff".into(),
"--shortstat".into(),
];
push_diff_options(&mut args, options);
args.push(base.to_string());
args.push(head.to_string());
if let Some(options) = options
&& !options.pathspec.is_empty()
{
args.push("--".into());
args.extend(options.pathspec.iter().cloned());
}
let result = duct::cmd("git", &args)
.stdout_capture()
.stderr_capture()
.unchecked()
.run()?;
if !result.status.success() {
return Err(GitError::CommandFailed {
status_code: result.status.code(),
stderr: String::from_utf8_lossy(&result.stderr).into_owned(),
});
}
Ok(parse_shortstat(&String::from_utf8_lossy(&result.stdout)))
}
pub(crate) fn parse_shortstat(output: &str) -> crate::pb::DiffStats {
let mut stats = crate::pb::DiffStats::default();
for part in output.trim().split(',') {
let part = part.trim();
if let Some(n) = part
.strip_suffix(" insertion(+)")
.or_else(|| part.strip_suffix(" insertions(+)"))
{
stats.additions = n.trim().parse().unwrap_or(0);
} else if let Some(n) = part
.strip_suffix(" deletion(-)")
.or_else(|| part.strip_suffix(" deletions(-)"))
{
stats.deletions = n.trim().parse().unwrap_or(0);
} else if let Some(n) = part
.strip_suffix(" file changed")
.or_else(|| part.strip_suffix(" files changed"))
{
stats.changed_files = n.trim().parse().unwrap_or(0);
}
}
stats
}
pub(crate) fn push_diff_options(args: &mut Vec<String>, options: Option<&crate::pb::DiffOptions>) {
let Some(options) = options else {
return;
};
if options.rename_detection {
args.push("-M".into());
}
if options.copy_detection {
args.push("-C".into());
}
match crate::pb::diff_options::WhitespaceMode::try_from(options.whitespace_mode)
.unwrap_or(crate::pb::diff_options::WhitespaceMode::DiffWhitespaceModeDefault)
{
crate::pb::diff_options::WhitespaceMode::DiffWhitespaceModeIgnoreAll => {
args.push("-w".into())
}
crate::pb::diff_options::WhitespaceMode::DiffWhitespaceModeIgnoreChange => {
args.push("-b".into())
}
crate::pb::diff_options::WhitespaceMode::DiffWhitespaceModeIgnoreEol => {
args.push("--ignore-space-at-eol".into());
}
_ => {}
}
}