Files
gitks/diff/get_diff_stats.rs
T
zhenyi dcb0fb74c5 feat(core): implement Git repository operations with gRPC services
- Add advertise_refs functionality for Git protocol communication
- Implement archive service with TAR/ZIP format support and streaming
- Create blame service for Git file annotation with line tracking
- Add branch management including create, delete, rename and compare operations
- Implement merge checking with conflict detection and fast-forward handling
- Add cherry-pick functionality for applying commits between branches
- Integrate gix library for Git repository operations and object handling
- Add comprehensive test suite covering all Git operations
- Implement proper error handling and repository validation
- Add pagination support for large result sets
- Create protobuf definitions for all Git operations and data structures
- Add build system for gRPC code generation and dependency management
2026-06-04 13:05:38 +08:00

109 lines
3.6 KiB
Rust

use crate::bare::GitBare;
use crate::error::{GitError, GitResult};
use crate::pb::GetDiffStatsRequest;
impl GitBare {
pub fn get_diff_stats(&self, request: GetDiffStatsRequest) -> GitResult<crate::pb::DiffStats> {
let base = match request.base.and_then(|s| s.selector) {
Some(crate::pb::object_selector::Selector::Oid(oid)) => oid.hex,
Some(crate::pb::object_selector::Selector::Revision(name)) => name.revision,
None => "HEAD".into(),
};
let head = match request.head.and_then(|s| s.selector) {
Some(crate::pb::object_selector::Selector::Oid(oid)) => oid.hex,
Some(crate::pb::object_selector::Selector::Revision(name)) => name.revision,
None => "HEAD".into(),
};
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());
}
_ => {}
}
}