feat(server): add tracing spans and caching to archive and blame services
- Add tracing spans with repo labels for archive and blame operations - Implement caching for archive list entries when using OID selectors - Implement caching for blame operations when using OID selectors - Add detailed
This commit is contained in:
+72
-22
@@ -1,10 +1,8 @@
|
||||
use crate::bare::GitBare;
|
||||
use crate::commit::list_commits::read_commit_from_repo;
|
||||
use crate::diff::get_diff_stats::diff_stats_for_range;
|
||||
use crate::error::{GitError, GitResult};
|
||||
use crate::paginate;
|
||||
use crate::pb::{
|
||||
CommitStats, CompareCommitsRequest, CompareCommitsResponse, GetCommitRequest, object_selector,
|
||||
};
|
||||
use crate::pb::{CommitStats, CompareCommitsRequest, CompareCommitsResponse, object_selector};
|
||||
|
||||
impl GitBare {
|
||||
pub fn compare_commits(
|
||||
@@ -35,17 +33,66 @@ impl GitBare {
|
||||
} else {
|
||||
format!("{base}...{head}")
|
||||
};
|
||||
let mut args = vec![
|
||||
|
||||
// Build base rev-list args
|
||||
let mut base_args = vec![
|
||||
"--git-dir".to_string(),
|
||||
self.bare_dir.to_string_lossy().into_owned(),
|
||||
"rev-list".into(),
|
||||
];
|
||||
if request.first_parent {
|
||||
args.push("--first-parent".into());
|
||||
base_args.push("--first-parent".into());
|
||||
}
|
||||
args.push(range);
|
||||
base_args.push(range);
|
||||
|
||||
let rev_list = duct::cmd("git", &args)
|
||||
// 1. Total count
|
||||
let total = {
|
||||
let mut args = base_args.clone();
|
||||
// Insert after "rev-list" (index 2)
|
||||
args.insert(3, "--count".into());
|
||||
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(),
|
||||
});
|
||||
}
|
||||
String::from_utf8_lossy(&result.stdout)
|
||||
.trim()
|
||||
.parse::<usize>()
|
||||
.unwrap_or(0)
|
||||
};
|
||||
|
||||
// 2. Git-side pagination
|
||||
let page_size = request
|
||||
.pagination
|
||||
.as_ref()
|
||||
.map(|p| p.page_size as usize)
|
||||
.unwrap_or(total.max(1))
|
||||
.max(1);
|
||||
let start_offset = request
|
||||
.pagination
|
||||
.as_ref()
|
||||
.and_then(|p| {
|
||||
if p.page_token.is_empty() {
|
||||
None
|
||||
} else {
|
||||
p.page_token.parse::<usize>().ok()
|
||||
}
|
||||
})
|
||||
.unwrap_or(0)
|
||||
.min(total);
|
||||
|
||||
let mut fetch_args = base_args;
|
||||
// Insert after "rev-list" (index 2)
|
||||
fetch_args.insert(3, format!("--skip={start_offset}"));
|
||||
fetch_args.insert(4, format!("-n{page_size}"));
|
||||
|
||||
let rev_list = duct::cmd("git", &fetch_args)
|
||||
.stdout_capture()
|
||||
.stderr_capture()
|
||||
.unchecked()
|
||||
@@ -57,28 +104,31 @@ impl GitBare {
|
||||
});
|
||||
}
|
||||
|
||||
let ids = String::from_utf8_lossy(&rev_list.stdout)
|
||||
let page_ids: Vec<String> = String::from_utf8_lossy(&rev_list.stdout)
|
||||
.lines()
|
||||
.map(str::trim)
|
||||
.filter(|line| !line.is_empty())
|
||||
.map(ToOwned::to_owned)
|
||||
.collect::<Vec<_>>();
|
||||
let (page_ids, page_info) = paginate::paginate(&ids, request.pagination.as_ref());
|
||||
.collect();
|
||||
|
||||
// 3. Batch-read commits via gix (one repo open, no subprocess per commit)
|
||||
let mut commits = Vec::with_capacity(page_ids.len());
|
||||
for id in page_ids {
|
||||
commits.push(self.get_commit(GetCommitRequest {
|
||||
repository: request.repository.clone(),
|
||||
revision: Some(crate::pb::ObjectSelector {
|
||||
selector: Some(object_selector::Selector::Revision(crate::pb::ObjectName {
|
||||
revision: id,
|
||||
})),
|
||||
}),
|
||||
include_stats: false,
|
||||
include_raw: false,
|
||||
})?);
|
||||
for id in &page_ids {
|
||||
commits.push(read_commit_from_repo(self, &repo, id)?);
|
||||
}
|
||||
|
||||
let end = start_offset + page_ids.len();
|
||||
let has_next = end < total;
|
||||
let page_info = crate::pb::PageInfo {
|
||||
next_page_token: if has_next {
|
||||
end.to_string()
|
||||
} else {
|
||||
String::new()
|
||||
},
|
||||
has_next_page: has_next,
|
||||
total_count: total as u64,
|
||||
};
|
||||
|
||||
let diff_stats = diff_stats_for_range(self, &base, &head, None)?;
|
||||
Ok(CompareCommitsResponse {
|
||||
commits,
|
||||
|
||||
Reference in New Issue
Block a user