Files
gitks/api/repo/git/git_diff_extras.rs
T
zhenyi e8fa433588 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
2026-06-10 18:49:11 +08:00

239 lines
6.2 KiB
Rust

use crate::models::common::DEFAULT_REVISION;
use actix_web::{HttpResponse, web};
use futures_util::StreamExt;
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 RevisionPathParams {
pub workspace_name: String,
pub repo_name: String,
pub revision: String,
}
#[utoipa::path(
get,
path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/git/commit-diff/{revision}",
tag = "Git",
operation_id = "gitCommitDiff",
params(RevisionPathParams),
responses((
status = 200,
body = ApiResponse<crate::pb::repo::GetDiffResponse>
)),
security(("session_cookie" = []))
)]
pub async fn git_commit_diff(
service: web::Data<AppService>,
session: Session,
path: web::Path<RevisionPathParams>,
) -> Result<HttpResponse, AppError> {
let r = service
.repo
.git_get_commit_diff(
&session,
&path.workspace_name,
&path.repo_name,
&path.revision,
)
.await?;
Ok(HttpResponse::Ok().json(ApiResponse::new(r)))
}
#[derive(Debug, Deserialize, IntoParams)]
pub struct PatchQuery {
pub old: String,
pub new: String,
}
#[utoipa::path(
get,
path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/git/patch",
tag = "Git",
operation_id = "gitPatch",
params(PathParams, PatchQuery),
responses((status = 200, content_type = "text/plain")),
security(("session_cookie" = []))
)]
pub async fn git_patch(
service: web::Data<AppService>,
session: Session,
path: web::Path<PathParams>,
q: web::Query<PatchQuery>,
) -> Result<HttpResponse, AppError> {
let r = service
.repo
.git_get_patch(
&session,
&path.workspace_name,
&path.repo_name,
&q.old,
&q.new,
)
.await?;
Ok(HttpResponse::Ok().content_type("text/plain").body(r))
}
#[derive(Debug, Deserialize, IntoParams)]
pub struct DiffQuery {
pub old: String,
pub new: String,
}
#[utoipa::path(
get,
path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/git/raw-diff",
tag = "Git",
operation_id = "gitRawDiff",
params(PathParams, DiffQuery),
responses((status = 200, content_type = "text/plain")),
security(("session_cookie" = []))
)]
pub async fn git_raw_diff(
service: web::Data<AppService>,
session: Session,
path: web::Path<PathParams>,
q: web::Query<DiffQuery>,
) -> Result<HttpResponse, AppError> {
let r = service
.repo
.git_raw_diff(
&session,
&path.workspace_name,
&path.repo_name,
&q.old,
&q.new,
)
.await?;
Ok(HttpResponse::Ok().content_type("text/plain").body(r))
}
#[derive(Debug, Deserialize, IntoParams)]
pub struct ChangedPathsQuery {
pub old: String,
pub new: String,
}
#[utoipa::path(
get,
path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/git/changed-paths",
tag = "Git",
operation_id = "gitChangedPaths",
params(PathParams, ChangedPathsQuery),
responses((
status = 200,
body = ApiResponse<crate::pb::repo::FindChangedPathsResponse>
)),
security(("session_cookie" = []))
)]
pub async fn git_changed_paths(
service: web::Data<AppService>,
session: Session,
path: web::Path<PathParams>,
q: web::Query<ChangedPathsQuery>,
) -> Result<HttpResponse, AppError> {
let r = service
.repo
.git_find_changed_paths(
&session,
&path.workspace_name,
&path.repo_name,
&q.old,
&q.new,
)
.await?;
Ok(HttpResponse::Ok().json(ApiResponse::new(r)))
}
#[derive(Debug, Deserialize, IntoParams)]
pub struct StreamBlameQuery {
pub path: String,
pub revision: Option<String>,
}
#[utoipa::path(
get,
path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/git/stream-blame",
tag = "Git",
operation_id = "gitStreamBlame",
params(PathParams, StreamBlameQuery),
responses((
status = 200,
body = ApiResponse<Vec<crate::pb::repo::BlameHunk>>
)),
security(("session_cookie" = []))
)]
pub async fn git_stream_blame(
service: web::Data<AppService>,
session: Session,
path: web::Path<PathParams>,
q: web::Query<StreamBlameQuery>,
) -> Result<HttpResponse, AppError> {
let mut stream = service
.repo
.git_stream_blame(
&session,
&path.workspace_name,
&path.repo_name,
&q.path,
q.revision.as_deref().unwrap_or(DEFAULT_REVISION),
)
.await?;
let mut hunks = Vec::new();
while let Some(Ok(hunk)) = stream.next().await {
hunks.push(hunk);
}
Ok(HttpResponse::Ok().json(ApiResponse::new(hunks)))
}
#[derive(Debug, Deserialize, utoipa::ToSchema)]
pub struct ResolveConflictsBody {
pub target_branch: String,
pub source_revision: String,
pub message: Option<String>,
}
#[utoipa::path(
post,
path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/git/resolve-conflicts",
tag = "Git",
operation_id = "gitResolveConflicts",
params(PathParams),
request_body(content = ResolveConflictsBody),
responses((
status = 200,
body = ApiResponse<crate::pb::repo::MergeResult>
)),
security(("session_cookie" = []))
)]
pub async fn git_resolve_conflicts(
service: web::Data<AppService>,
session: Session,
path: web::Path<PathParams>,
body: web::Json<ResolveConflictsBody>,
) -> Result<HttpResponse, AppError> {
let r = service
.repo
.git_resolve_merge_conflicts(
&session,
&path.workspace_name,
&path.repo_name,
&body.target_branch,
&body.source_revision,
body.message.as_deref().unwrap_or(""),
)
.await?;
Ok(HttpResponse::Ok().json(ApiResponse::new(r)))
}