Skip to content

Commit 0e6901c

Browse files
committed
fix(app-server): 改为异步执行 Git 命令
1 parent 7954d02 commit 0e6901c

1 file changed

Lines changed: 53 additions & 27 deletions

File tree

  • src/cortex-app-server/src/api

src/cortex-app-server/src/api/git.rs

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Git operations endpoints.
22
3-
use std::process::Command;
3+
use tokio::process::Command;
44

55
use axum::{Json, extract::Query};
66

@@ -17,7 +17,8 @@ use super::types::{
1717
pub async fn git_status(Query(query): Query<GitPathQuery>) -> AppResult<Json<GitStatusResponse>> {
1818
let output = Command::new("git")
1919
.args(["-C", &query.path, "status", "--porcelain=v2", "-b"])
20-
.output();
20+
.output()
21+
.await;
2122

2223
match output {
2324
Ok(output) => {
@@ -139,7 +140,8 @@ fn status_char_to_string(c: &str) -> String {
139140
pub async fn git_branch(Query(query): Query<GitPathQuery>) -> AppResult<Json<serde_json::Value>> {
140141
let output = Command::new("git")
141142
.args(["-C", &query.path, "branch", "--show-current"])
142-
.output();
143+
.output()
144+
.await;
143145

144146
match output {
145147
Ok(output) => {
@@ -155,7 +157,8 @@ pub async fn git_branches(Query(query): Query<GitPathQuery>) -> AppResult<Json<s
155157
// Get local branches with verbose info
156158
let output = Command::new("git")
157159
.args(["-C", &query.path, "branch", "-vv", "--format=%(HEAD)%(refname:short)|%(upstream:short)|%(upstream:track)|%(objectname:short)|%(subject)"])
158-
.output();
160+
.output()
161+
.await;
159162

160163
match output {
161164
Ok(output) => {
@@ -213,7 +216,8 @@ pub async fn git_branches(Query(query): Query<GitPathQuery>) -> AppResult<Json<s
213216
"-r",
214217
"--format=%(refname:short)|%(objectname:short)",
215218
])
216-
.output();
219+
.output()
220+
.await;
217221

218222
if let Ok(remote_output) = remote_output {
219223
let remote_stdout = String::from_utf8_lossy(&remote_output.stdout);
@@ -274,7 +278,7 @@ pub async fn git_diff(Query(query): Query<GitDiffQuery>) -> AppResult<Json<serde
274278
args.push("--");
275279
args.push(&query.file);
276280

277-
let output = Command::new("git").args(&args).output();
281+
let output = Command::new("git").args(&args).output().await;
278282

279283
match output {
280284
Ok(output) => {
@@ -385,7 +389,8 @@ fn parse_diff_with_stats(diff: &str) -> (Vec<serde_json::Value>, usize, usize) {
385389
pub async fn git_blame(Query(query): Query<GitBlameQuery>) -> AppResult<Json<serde_json::Value>> {
386390
let output = Command::new("git")
387391
.args(["-C", &query.path, "blame", "--porcelain", &query.file])
388-
.output();
392+
.output()
393+
.await;
389394

390395
match output {
391396
Ok(output) => {
@@ -469,7 +474,8 @@ pub async fn git_log(Query(query): Query<GitLogQuery>) -> AppResult<Json<serde_j
469474
&format!("-{}", limit),
470475
"--format=%H|%h|%s|%an|%ae|%aI|%P|%D",
471476
])
472-
.output();
477+
.output()
478+
.await;
473479

474480
match output {
475481
Ok(output) => {
@@ -548,7 +554,8 @@ pub async fn git_stage(Json(req): Json<GitStageRequest>) -> AppResult<Json<serde
548554
for file in &req.files {
549555
let _ = Command::new("git")
550556
.args(["-C", &req.path, "add", file])
551-
.output();
557+
.output()
558+
.await;
552559
}
553560

554561
Ok(Json(serde_json::json!({ "success": true })))
@@ -559,7 +566,8 @@ pub async fn git_unstage(Json(req): Json<GitStageRequest>) -> AppResult<Json<ser
559566
for file in &req.files {
560567
let _ = Command::new("git")
561568
.args(["-C", &req.path, "restore", "--staged", file])
562-
.output();
569+
.output()
570+
.await;
563571
}
564572

565573
Ok(Json(serde_json::json!({ "success": true })))
@@ -569,7 +577,8 @@ pub async fn git_unstage(Json(req): Json<GitStageRequest>) -> AppResult<Json<ser
569577
pub async fn git_stage_all(Json(req): Json<GitPathRequest>) -> AppResult<Json<serde_json::Value>> {
570578
let _ = Command::new("git")
571579
.args(["-C", &req.path, "add", "-A"])
572-
.output();
580+
.output()
581+
.await;
573582

574583
Ok(Json(serde_json::json!({ "success": true })))
575584
}
@@ -580,7 +589,8 @@ pub async fn git_unstage_all(
580589
) -> AppResult<Json<serde_json::Value>> {
581590
let _ = Command::new("git")
582591
.args(["-C", &req.path, "reset", "HEAD"])
583-
.output();
592+
.output()
593+
.await;
584594

585595
Ok(Json(serde_json::json!({ "success": true })))
586596
}
@@ -589,7 +599,8 @@ pub async fn git_unstage_all(
589599
pub async fn git_commit(Json(req): Json<GitCommitRequest>) -> AppResult<Json<serde_json::Value>> {
590600
let output = Command::new("git")
591601
.args(["-C", &req.path, "commit", "-m", &req.message])
592-
.output();
602+
.output()
603+
.await;
593604

594605
match output {
595606
Ok(output) => {
@@ -606,7 +617,8 @@ pub async fn git_checkout(
606617
) -> AppResult<Json<serde_json::Value>> {
607618
let output = Command::new("git")
608619
.args(["-C", &req.path, "checkout", &req.branch])
609-
.output();
620+
.output()
621+
.await;
610622

611623
match output {
612624
Ok(output) => {
@@ -624,7 +636,10 @@ pub async fn git_checkout(
624636

625637
/// Push to remote.
626638
pub async fn git_push(Json(req): Json<GitPathRequest>) -> AppResult<Json<serde_json::Value>> {
627-
let output = Command::new("git").args(["-C", &req.path, "push"]).output();
639+
let output = Command::new("git")
640+
.args(["-C", &req.path, "push"])
641+
.output()
642+
.await;
628643

629644
match output {
630645
Ok(output) => {
@@ -645,7 +660,10 @@ pub async fn git_push(Json(req): Json<GitPathRequest>) -> AppResult<Json<serde_j
645660

646661
/// Pull from remote.
647662
pub async fn git_pull(Json(req): Json<GitPathRequest>) -> AppResult<Json<serde_json::Value>> {
648-
let output = Command::new("git").args(["-C", &req.path, "pull"]).output();
663+
let output = Command::new("git")
664+
.args(["-C", &req.path, "pull"])
665+
.output()
666+
.await;
649667

650668
match output {
651669
Ok(output) => {
@@ -668,7 +686,8 @@ pub async fn git_pull(Json(req): Json<GitPathRequest>) -> AppResult<Json<serde_j
668686
pub async fn git_fetch(Json(req): Json<GitPathRequest>) -> AppResult<Json<serde_json::Value>> {
669687
let output = Command::new("git")
670688
.args(["-C", &req.path, "fetch", "--all", "--prune"])
671-
.output();
689+
.output()
690+
.await;
672691

673692
match output {
674693
Ok(output) => {
@@ -693,15 +712,17 @@ pub async fn git_discard(Json(req): Json<GitStageRequest>) -> AppResult<Json<ser
693712
// First try to restore tracked files
694713
let restore_output = Command::new("git")
695714
.args(["-C", &req.path, "checkout", "--", file])
696-
.output();
715+
.output()
716+
.await;
697717

698718
// If that fails (e.g., untracked file), try to clean
699719
if let Ok(output) = restore_output {
700720
if !output.status.success() {
701721
// Try removing untracked file
702722
let clean_output = Command::new("git")
703723
.args(["-C", &req.path, "clean", "-fd", "--", file])
704-
.output();
724+
.output()
725+
.await;
705726

706727
if let Ok(clean_out) = clean_output
707728
&& !clean_out.status.success()
@@ -736,7 +757,7 @@ pub async fn git_create_branch(
736757
args.push(start.clone());
737758
}
738759

739-
let output = Command::new("git").args(&args).output();
760+
let output = Command::new("git").args(&args).output().await;
740761

741762
match output {
742763
Ok(output) => {
@@ -761,7 +782,8 @@ pub async fn git_delete_branch(
761782

762783
let output = Command::new("git")
763784
.args(["-C", &req.path, "branch", delete_flag, &req.name])
764-
.output();
785+
.output()
786+
.await;
765787

766788
match output {
767789
Ok(output) => {
@@ -793,7 +815,7 @@ pub async fn git_merge(Json(req): Json<GitMergeRequest>) -> AppResult<Json<serde
793815

794816
args.push(req.branch.clone());
795817

796-
let output = Command::new("git").args(&args).output();
818+
let output = Command::new("git").args(&args).output().await;
797819

798820
match output {
799821
Ok(output) => {
@@ -823,7 +845,8 @@ pub async fn git_stash_list(
823845
) -> AppResult<Json<serde_json::Value>> {
824846
let output = Command::new("git")
825847
.args(["-C", &query.path, "stash", "list", "--format=%gd|%H|%s|%aI"])
826-
.output();
848+
.output()
849+
.await;
827850

828851
match output {
829852
Ok(output) => {
@@ -886,7 +909,7 @@ pub async fn git_stash_create(
886909
args.push(msg);
887910
}
888911

889-
let output = Command::new("git").args(&args).output();
912+
let output = Command::new("git").args(&args).output().await;
890913

891914
match output {
892915
Ok(output) => {
@@ -919,7 +942,8 @@ pub async fn git_stash_apply(
919942

920943
let output = Command::new("git")
921944
.args(["-C", &req.path, "stash", "apply", &stash_ref])
922-
.output();
945+
.output()
946+
.await;
923947

924948
match output {
925949
Ok(output) => {
@@ -947,7 +971,8 @@ pub async fn git_stash_pop(
947971

948972
let output = Command::new("git")
949973
.args(["-C", &req.path, "stash", "pop", &stash_ref])
950-
.output();
974+
.output()
975+
.await;
951976

952977
match output {
953978
Ok(output) => {
@@ -975,7 +1000,8 @@ pub async fn git_stash_drop(
9751000

9761001
let output = Command::new("git")
9771002
.args(["-C", &req.path, "stash", "drop", &stash_ref])
978-
.output();
1003+
.output()
1004+
.await;
9791005

9801006
match output {
9811007
Ok(output) => {

0 commit comments

Comments
 (0)