refactor(git): use DEFAULT_REVISION constant across git operations

- Replace 15 occurrences of unwrap_or("HEAD") with
  unwrap_or(DEFAULT_REVISION) across 10 files
- All git API handlers and service methods now reference the shared
  constant from models::common
This commit is contained in:
zhenyi
2026-06-10 18:49:11 +08:00
parent 6205a6de0a
commit e8fa433588
10 changed files with 1210 additions and 71 deletions
+159
View File
@@ -0,0 +1,159 @@
use crate::models::common::DEFAULT_REVISION;
use actix_web::{HttpResponse, web};
use serde::Deserialize;
use utoipa::IntoParams;
use crate::api::response::ApiResponse;
use crate::error::AppError;
use crate::service::AppService;
use crate::session::Session;
#[derive(Debug, Deserialize, IntoParams)]
pub struct PathParams {
pub workspace_name: String,
pub repo_name: String,
}
#[derive(Debug, Deserialize, IntoParams)]
pub struct RawBlobQuery {
pub revision: Option<String>,
pub path: String,
}
#[utoipa::path(
get,
path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/git/raw-blob",
tag = "Git",
operation_id = "gitRawBlob",
params(PathParams, RawBlobQuery),
responses((status = 200, content_type = "application/octet-stream")),
security(("session_cookie" = []))
)]
pub async fn git_raw_blob(
service: web::Data<AppService>,
session: Session,
path: web::Path<PathParams>,
q: web::Query<RawBlobQuery>,
) -> Result<HttpResponse, AppError> {
let r = service
.repo
.git_get_raw_blob(
&session,
&path.workspace_name,
&path.repo_name,
q.revision.as_deref().unwrap_or(DEFAULT_REVISION),
&q.path,
)
.await?;
Ok(HttpResponse::Ok()
.content_type("application/octet-stream")
.body(r))
}
#[derive(Debug, Deserialize, IntoParams)]
pub struct FileMetaQuery {
pub revision: Option<String>,
pub path: String,
}
#[utoipa::path(
get,
path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/git/file-metadata",
tag = "Git",
operation_id = "gitFileMetadata",
params(PathParams, FileMetaQuery),
responses((
status = 200,
body = ApiResponse<crate::pb::repo::FileMetadata>
)),
security(("session_cookie" = []))
)]
pub async fn git_file_metadata(
service: web::Data<AppService>,
session: Session,
path: web::Path<PathParams>,
q: web::Query<FileMetaQuery>,
) -> Result<HttpResponse, AppError> {
let r = service
.repo
.git_get_file_metadata(
&session,
&path.workspace_name,
&path.repo_name,
q.revision.as_deref().unwrap_or(DEFAULT_REVISION),
&q.path,
)
.await?;
Ok(HttpResponse::Ok().json(ApiResponse::new(r)))
}
#[derive(Debug, Deserialize, IntoParams)]
pub struct FindFilesQuery {
pub revision: Option<String>,
pub pattern: String,
}
#[utoipa::path(
get,
path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/git/find-files",
tag = "Git",
operation_id = "gitFindFiles",
params(PathParams, FindFilesQuery),
responses((
status = 200,
body = ApiResponse<crate::pb::repo::FindFilesResponse>
)),
security(("session_cookie" = []))
)]
pub async fn git_find_files(
service: web::Data<AppService>,
session: Session,
path: web::Path<PathParams>,
q: web::Query<FindFilesQuery>,
) -> Result<HttpResponse, AppError> {
let r = service
.repo
.git_find_files(
&session,
&path.workspace_name,
&path.repo_name,
q.revision.as_deref().unwrap_or(DEFAULT_REVISION),
&q.pattern,
)
.await?;
Ok(HttpResponse::Ok().json(ApiResponse::new(r)))
}
#[derive(Debug, Deserialize, IntoParams)]
pub struct GetTreeQuery {
pub revision: Option<String>,
pub path: Option<String>,
}
#[utoipa::path(
get,
path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/git/get-tree",
tag = "Git",
operation_id = "gitGetTree",
params(PathParams, GetTreeQuery),
responses((status = 200, body = ApiResponse<crate::pb::repo::Tree>)),
security(("session_cookie" = []))
)]
pub async fn git_get_tree(
service: web::Data<AppService>,
session: Session,
path: web::Path<PathParams>,
q: web::Query<GetTreeQuery>,
) -> Result<HttpResponse, AppError> {
let r = service
.repo
.git_get_tree(
&session,
&path.workspace_name,
&path.repo_name,
q.revision.as_deref().unwrap_or(DEFAULT_REVISION),
q.path.as_deref().unwrap_or(""),
)
.await?;
Ok(HttpResponse::Ok().json(ApiResponse::new(r)))
}