Skip to content

Commit ec50e8c

Browse files
fix: fix code blame error about refs (#1577)
Signed-off-by: allure <[email protected]>
1 parent 8a37f60 commit ec50e8c

File tree

3 files changed

+67
-52
lines changed

3 files changed

+67
-52
lines changed

ceres/src/model/blame.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,16 @@ impl From<jupiter::model::blame_dto::BlameQuery> for BlameQuery {
7979
}
8080
}
8181

82+
fn default_path() -> String {
83+
"/".to_string()
84+
}
85+
8286
/// Request parameters for blame API endpoints
8387
#[derive(Debug, Deserialize, IntoParams, ToSchema)]
8488
pub struct BlameRequest {
8589
#[serde(default)]
8690
pub refs: String,
87-
#[serde(default)]
91+
#[serde(default = "default_path")]
8892
pub path: String,
8993
#[serde(default)]
9094
pub start_line: Option<usize>,

jupiter/src/service/blame_service.rs

Lines changed: 59 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -251,43 +251,7 @@ impl BlameService {
251251
format!("refs/heads/{}", ref_str)
252252
};
253253
tracing::debug!("Trying to resolve ref: {}", full_ref_name);
254-
match self.mono_storage.get_ref_by_name(&full_ref_name).await {
255-
Ok(Some(commit_id)) => self
256-
.get_commit_by_hash(&commit_id.ref_commit_hash)
257-
.await?
258-
.ok_or_else(|| GitError::ObjectNotFound(ref_str.to_string())),
259-
Ok(None) => {
260-
// Fallback to default branch
261-
tracing::debug!(
262-
"Ref not found by name, falling back to get_ref with root path"
263-
);
264-
match self.mono_storage.get_ref("/").await {
265-
Ok(Some(commit_id)) => {
266-
tracing::debug!(
267-
"Found commit via fallback: {}",
268-
commit_id.ref_commit_hash
269-
);
270-
self.get_commit_by_hash(&commit_id.ref_commit_hash)
271-
.await?
272-
.ok_or_else(|| {
273-
GitError::ObjectNotFound(ref_str.to_string())
274-
})
275-
}
276-
Ok(None) => {
277-
tracing::debug!("No commit found via fallback");
278-
Err(GitError::ObjectNotFound(ref_str.to_string()))
279-
}
280-
Err(e) => Err(GitError::CustomError(format!(
281-
"Failed to resolve reference: {}",
282-
e
283-
))),
284-
}
285-
}
286-
Err(e) => Err(GitError::CustomError(format!(
287-
"Failed to resolve reference: {}",
288-
e
289-
))),
290-
}
254+
self.commit_by_ref_or_default(&full_ref_name, ref_str).await
291255
}
292256
}
293257
None => {
@@ -319,6 +283,64 @@ impl BlameService {
319283
Ok(resolved_commit)
320284
}
321285

286+
// Extracted helper: resolve commit by ref name with safe fallback
287+
async fn commit_by_ref_or_default(
288+
&self,
289+
full_ref_name: &str,
290+
ref_display_name: &str,
291+
) -> Result<Commit, GitError> {
292+
match self.mono_storage.get_ref_by_name(full_ref_name).await {
293+
Ok(Some(ref_row)) => match self.get_commit_by_hash(&ref_row.ref_commit_hash).await? {
294+
Some(commit) => Ok(commit),
295+
None => {
296+
tracing::warn!(
297+
"Ref {} -> {} missing in mono commits, falling back to root default",
298+
full_ref_name,
299+
ref_row.ref_commit_hash
300+
);
301+
match self.mono_storage.get_ref("/").await {
302+
Ok(Some(default_ref)) => self
303+
.get_commit_by_hash(&default_ref.ref_commit_hash)
304+
.await?
305+
.ok_or_else(|| GitError::ObjectNotFound(ref_display_name.to_string())),
306+
Ok(None) => {
307+
tracing::debug!("No default ref found at root path");
308+
Err(GitError::ObjectNotFound(ref_display_name.to_string()))
309+
}
310+
Err(e) => Err(GitError::CustomError(format!(
311+
"Failed to resolve default reference: {}",
312+
e
313+
))),
314+
}
315+
}
316+
},
317+
Ok(None) => {
318+
tracing::debug!(
319+
"Ref not found by name: {}, falling back to root default",
320+
full_ref_name
321+
);
322+
match self.mono_storage.get_ref("/").await {
323+
Ok(Some(default_ref)) => self
324+
.get_commit_by_hash(&default_ref.ref_commit_hash)
325+
.await?
326+
.ok_or_else(|| GitError::ObjectNotFound(ref_display_name.to_string())),
327+
Ok(None) => {
328+
tracing::debug!("No default ref found at root path");
329+
Err(GitError::ObjectNotFound(ref_display_name.to_string()))
330+
}
331+
Err(e) => Err(GitError::CustomError(format!(
332+
"Failed to resolve reference: {}",
333+
e
334+
))),
335+
}
336+
}
337+
Err(e) => Err(GitError::CustomError(format!(
338+
"Failed to resolve reference: {}",
339+
e
340+
))),
341+
}
342+
}
343+
322344
/// Get file version at specific commit (with caching)
323345
async fn get_file_version(
324346
&self,

mono/src/api/router/preview_router.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,6 @@ async fn get_file_blame(
315315
params.refs
316316
);
317317

318-
// Validate input parameters
319-
if params.path.is_empty() {
320-
return Err(ApiError::from(anyhow::anyhow!("File path cannot be empty")));
321-
}
322-
323318
// Use refs parameter if provided, otherwise use None to let the service handle defaults
324319
let ref_name = if params.refs.is_empty() {
325320
None
@@ -330,18 +325,12 @@ async fn get_file_blame(
330325
// Convert BlameRequest to BlameQuery
331326
let query = BlameQuery::from(&params);
332327
// Call the business logic in ceres module
333-
match state
328+
let result = state
334329
.api_handler(params.path.as_ref())
335330
.await?
336331
.get_file_blame(&params.path, ref_name, query)
337-
.await
338-
{
339-
Ok(result) => Ok(Json(CommonResult::success(Some(result)))),
340-
Err(e) => {
341-
tracing::error!("Blame operation failed for {}: {}", params.path, e);
342-
Err(ApiError::from(anyhow::anyhow!("Blame failed: {}", e)))
343-
}
344-
}
332+
.await?;
333+
Ok(Json(CommonResult::success(Some(result))))
345334
}
346335

347336
/// Preview unified diff for a single file before saving

0 commit comments

Comments
 (0)