feat(api): extend commit and diff services with new functionality

- Add FindCommit, ListCommitsByOid, CommitIsAncestor RPCs to CommitService
- Add CheckObjectsExist, CommitsByMessage, GetCommitStats RPCs to CommitService
- Add LastCommitForPath, CountCommits, CountDivergingCommits RPCs to CommitService
- Add RawDiff, RawPatch, FindChangedPaths RPCs to DiffService
- Add FindMergeBase, WriteRef, SearchFilesByContent RPCs to RepositoryService
- Add SearchFilesByName, ObjectsSize, RepositorySize RPCs to RepositoryService
- Add FindLicense, OptimizeRepository, GetRawChanges RPCs to RepositoryService
- Add FetchRemote, CreateRepositoryFromURL RPCs to RepositoryService
- Implement server handlers for all new RPC methods
- Add new modules for commit counting, finding, and querying features
- Add new modules for diff changed paths and raw operations
- Add new modules for refs and remote operations
- Remove unnecessary comments from various source files
- Update proto definitions with new message types and service methods
This commit is contained in:
zhenyi
2026-06-08 15:37:08 +08:00
parent 8f472a0443
commit 66afd932ed
43 changed files with 3070 additions and 75 deletions
+73
View File
@@ -0,0 +1,73 @@
use crate::bare::GitBare;
use crate::error::GitResult;
use crate::pb::*;
impl GitBare {
/// Find changed paths between two revisions (no diff content).
pub fn find_changed_paths(&self, request: FindChangedPathsRequest) -> GitResult<FindChangedPathsResponse> {
crate::sanitize::validate_revision(&request.base)?;
crate::sanitize::validate_revision(&request.head)?;
let mut args = vec![
"--git-dir".to_string(),
self.bare_dir.to_string_lossy().into_owned(),
"diff-tree".to_string(),
"--name-status".to_string(),
"-r".to_string(),
];
if !request.paths.is_empty() {
args.push("--".to_string());
for p in &request.paths {
args.push(p.clone());
}
}
args.push(request.base.clone());
args.push(request.head.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(),
})?;
let stdout = String::from_utf8_lossy(&output.stdout);
let mut paths = Vec::new();
for line in stdout.lines() {
let line = line.trim();
if line.is_empty() { continue; }
let parts: Vec<&str> = line.split('\t').collect();
if parts.is_empty() { continue; }
let status_str = parts[0];
let status_letter = status_str.chars().next().unwrap_or('M');
let (status, old_path, new_path) = match status_letter {
'A' => (changed_path::Status::ChangedPathStatusAdded as i32, String::new(), parts.get(1).cloned().unwrap_or_default().to_string()),
'D' => (changed_path::Status::ChangedPathStatusDeleted as i32, parts.get(1).cloned().unwrap_or_default().to_string(), String::new()),
'R' => (changed_path::Status::ChangedPathStatusRenamed as i32, parts.get(1).cloned().unwrap_or_default().to_string(), parts.get(2).cloned().unwrap_or_default().to_string()),
'C' => (changed_path::Status::ChangedPathStatusCopied as i32, parts.get(1).cloned().unwrap_or_default().to_string(), parts.get(2).cloned().unwrap_or_default().to_string()),
'T' => (changed_path::Status::ChangedPathStatusTypeChanged as i32, String::new(), parts.get(1).cloned().unwrap_or_default().to_string()),
_ => (changed_path::Status::ChangedPathStatusModified as i32, String::new(), parts.get(1).cloned().unwrap_or_default().to_string()),
};
paths.push(ChangedPath {
status,
old_path,
new_path,
additions: 0,
deletions: 0,
binary: false,
});
}
Ok(FindChangedPathsResponse { paths })
}
}
-4
View File
@@ -50,7 +50,6 @@ impl GitBare {
let options = request.options.as_ref();
let want_patch = options.is_some_and(|o| o.include_patch);
// ── Call 1: --raw -z --numstat -z (all metadata + line counts) ──
let (raw_entries, numstat_map) = self.diff_raw_and_numstat(&base, &head, options)?;
let max_files = options.and_then(|o| (o.max_files > 0).then_some(o.max_files as usize));
@@ -59,14 +58,12 @@ impl GitBare {
&raw_entries[..raw_entries.len().min(max)]
});
// ── Call 2 (optional): --patch for all files at once ──
let patch_map = if want_patch {
self.diff_patch_batch(&base, &head, options)?
} else {
HashMap::new()
};
// ── Merge results (zero additional subprocess calls) ──
let mut files = Vec::with_capacity(entries_to_build.len());
for entry in entries_to_build {
let path = if !entry.new_path.is_empty() {
@@ -127,7 +124,6 @@ impl GitBare {
});
}
// ── Call 3: diff --shortstat (already efficient, single call) ──
let stats = diff_stats_for_range(self, &base, &head, options)?;
let (files, page_info) = paginate::paginate(&files, request.pagination.as_ref());
+2
View File
@@ -1,4 +1,6 @@
pub mod changed_paths;
pub mod get_commit_diff;
pub mod get_diff;
pub mod get_diff_stats;
pub mod get_patch;
pub mod raw;
+89
View File
@@ -0,0 +1,89 @@
use crate::bare::GitBare;
use crate::error::GitResult;
use crate::pb::*;
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(),
];
// 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 ps in &opts.pathspec {
args.push("--".to_string());
args.push(ps.clone());
}
}
args.push(base.clone());
args.push(head.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(),
})?;
// 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(),
})?;
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)
}
}