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,
|
||||
|
||||
@@ -9,6 +9,13 @@ use crate::pb::{
|
||||
impl GitBare {
|
||||
pub fn create_commit(&self, request: CreateCommitRequest) -> GitResult<CreateCommitResponse> {
|
||||
let repo = self.gix_repo()?;
|
||||
let branch = request.branch.clone();
|
||||
tracing::debug!(
|
||||
repo = %self.bare_dir.display(),
|
||||
branch = %branch,
|
||||
actions = request.actions.len(),
|
||||
"creating commit"
|
||||
);
|
||||
let start_rev = match request.start_revision.clone().and_then(|s| s.selector) {
|
||||
Some(object_selector::Selector::Oid(oid)) => oid.hex,
|
||||
Some(object_selector::Selector::Revision(name)) => name.revision,
|
||||
|
||||
+166
-51
@@ -1,7 +1,6 @@
|
||||
use crate::bare::GitBare;
|
||||
use crate::error::{GitError, GitResult};
|
||||
use crate::paginate;
|
||||
use crate::pb::{GetCommitRequest, ListCommitsRequest, ListCommitsResponse, object_selector};
|
||||
use crate::pb::{Commit, ListCommitsRequest, ListCommitsResponse, object_selector};
|
||||
|
||||
impl GitBare {
|
||||
pub fn list_commits(&self, request: ListCommitsRequest) -> GitResult<ListCommitsResponse> {
|
||||
@@ -11,40 +10,56 @@ impl GitBare {
|
||||
None => "HEAD".into(),
|
||||
};
|
||||
|
||||
let mut 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());
|
||||
}
|
||||
if request.reverse {
|
||||
args.push("--reverse".into());
|
||||
}
|
||||
if request.max_parents > 0 {
|
||||
args.push(format!("--max-parents={}", request.max_parents));
|
||||
}
|
||||
if request.min_parents > 0 {
|
||||
args.push(format!("--min-parents={}", request.min_parents));
|
||||
}
|
||||
if let Some(since) = request.since.as_ref() {
|
||||
args.push(format!("--since=@{}", since.seconds));
|
||||
}
|
||||
if let Some(until) = request.until.as_ref() {
|
||||
args.push(format!("--until=@{}", until.seconds));
|
||||
}
|
||||
if request.all {
|
||||
args.push("--all".into());
|
||||
} else {
|
||||
args.push(revision);
|
||||
}
|
||||
if !request.path.is_empty() {
|
||||
args.push("--".into());
|
||||
args.push(request.path.clone());
|
||||
}
|
||||
let base_args = build_rev_list_args(self, &request, &revision);
|
||||
|
||||
let result = duct::cmd("git", &args)
|
||||
// 1. Get total count via rev-list --count (lightweight, no object parsing)
|
||||
let total = {
|
||||
let mut args = base_args.clone();
|
||||
// Insert after "rev-list" (index 2) so it's a rev-list flag, not a git flag
|
||||
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. Apply git-side pagination: --skip + -n to only fetch the page
|
||||
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) so they are rev-list flags, not git flags
|
||||
fetch_args.insert(3, format!("--skip={start_offset}"));
|
||||
fetch_args.insert(4, format!("-n{page_size}"));
|
||||
|
||||
let result = duct::cmd("git", &fetch_args)
|
||||
.stdout_capture()
|
||||
.stderr_capture()
|
||||
.unchecked()
|
||||
@@ -56,27 +71,36 @@ impl GitBare {
|
||||
});
|
||||
}
|
||||
|
||||
let ids = String::from_utf8_lossy(&result.stdout)
|
||||
let page_ids: Vec<String> = String::from_utf8_lossy(&result.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();
|
||||
|
||||
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,
|
||||
})?);
|
||||
}
|
||||
// 3. Batch-read commits via gix (one repo open, zero subprocess per commit)
|
||||
let commits = if page_ids.is_empty() {
|
||||
Vec::new()
|
||||
} else {
|
||||
let repo = self.gix_repo()?;
|
||||
let mut commits = Vec::with_capacity(page_ids.len());
|
||||
for id in &page_ids {
|
||||
commits.push(read_commit_from_repo(self, &repo, id)?);
|
||||
}
|
||||
commits
|
||||
};
|
||||
|
||||
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,
|
||||
};
|
||||
|
||||
Ok(ListCommitsResponse {
|
||||
commits,
|
||||
@@ -84,3 +108,94 @@ impl GitBare {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn build_rev_list_args(gb: &GitBare, request: &ListCommitsRequest, revision: &str) -> Vec<String> {
|
||||
let mut args = vec![
|
||||
"--git-dir".to_string(),
|
||||
gb.bare_dir.to_string_lossy().into_owned(),
|
||||
"rev-list".into(),
|
||||
];
|
||||
if request.first_parent {
|
||||
args.push("--first-parent".into());
|
||||
}
|
||||
if request.reverse {
|
||||
args.push("--reverse".into());
|
||||
}
|
||||
if request.max_parents > 0 {
|
||||
args.push(format!("--max-parents={}", request.max_parents));
|
||||
}
|
||||
if request.min_parents > 0 {
|
||||
args.push(format!("--min-parents={}", request.min_parents));
|
||||
}
|
||||
if let Some(since) = request.since.as_ref() {
|
||||
args.push(format!("--since=@{}", since.seconds));
|
||||
}
|
||||
if let Some(until) = request.until.as_ref() {
|
||||
args.push(format!("--until=@{}", until.seconds));
|
||||
}
|
||||
if request.all {
|
||||
args.push("--all".into());
|
||||
} else {
|
||||
args.push(revision.to_string());
|
||||
}
|
||||
if !request.path.is_empty() {
|
||||
args.push("--".into());
|
||||
args.push(request.path.clone());
|
||||
}
|
||||
args
|
||||
}
|
||||
|
||||
/// Read a single commit from an already-opened gix repo (no subprocess).
|
||||
pub(crate) fn read_commit_from_repo(
|
||||
gb: &GitBare,
|
||||
repo: &gix::Repository,
|
||||
hex: &str,
|
||||
) -> GitResult<Commit> {
|
||||
let id = gix::hash::ObjectId::from_hex(hex.as_bytes())
|
||||
.map_err(|e| crate::error::GitError::InvalidOid(e.to_string()))?;
|
||||
let commit = repo
|
||||
.find_object(id)?
|
||||
.try_into_commit()
|
||||
.map_err(|e| crate::error::GitError::Gix(e.to_string()))?;
|
||||
let tree_hex = commit.tree_id()?.to_string();
|
||||
let message = commit.message_raw()?.to_string();
|
||||
let (subject, body) = message
|
||||
.split_once('\n')
|
||||
.map(|(s, b)| (s.to_string(), b.trim_start_matches('\n').to_string()))
|
||||
.unwrap_or_else(|| (message.clone(), String::new()));
|
||||
let author_sig = commit.author().ok();
|
||||
let committer_sig = commit.committer().ok();
|
||||
Ok(Commit {
|
||||
oid: Some(gb.oid_to_pb(hex.to_string())),
|
||||
abbreviated_oid: commit
|
||||
.short_id()
|
||||
.map(|s| s.to_string())
|
||||
.unwrap_or_else(|_| hex.chars().take(7).collect()),
|
||||
parent_oids: commit
|
||||
.parent_ids()
|
||||
.map(|p| gb.oid_to_pb(p.to_string()))
|
||||
.collect(),
|
||||
tree_oid: Some(gb.oid_to_pb(tree_hex)),
|
||||
author: author_sig
|
||||
.as_ref()
|
||||
.map(crate::commit::get_commit::gix_sig_to_pb),
|
||||
committer: committer_sig
|
||||
.as_ref()
|
||||
.map(crate::commit::get_commit::gix_sig_to_pb),
|
||||
subject,
|
||||
body,
|
||||
message,
|
||||
trailers: Vec::new(),
|
||||
signature: None,
|
||||
stats: None,
|
||||
authored_at: author_sig.as_ref().map(|s| prost_types::Timestamp {
|
||||
seconds: s.seconds(),
|
||||
nanos: 0,
|
||||
}),
|
||||
committed_at: committer_sig.as_ref().map(|s| prost_types::Timestamp {
|
||||
seconds: s.seconds(),
|
||||
nanos: 0,
|
||||
}),
|
||||
raw: Vec::new(),
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user