From 78a9e26476611b00f64783540ee41421295e69f3 Mon Sep 17 00:00:00 2001 From: zhb <50901800+zhbdesign@users.noreply.github.com> Date: Fri, 26 Jun 2026 08:48:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=9D=9Egit=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E5=9C=A8=E6=96=B0=E4=BC=9A=E8=AF=9D=E6=89=A7=E8=A1=8C?= =?UTF-8?q?git=E5=91=BD=E4=BB=A4=E5=AF=BC=E8=87=B4=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E6=85=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复非git项目在新会话执行git命令导致加载慢 --- src/server/api/sessions.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/server/api/sessions.ts b/src/server/api/sessions.ts index 33fc73b0..3efda631 100644 --- a/src/server/api/sessions.ts +++ b/src/server/api/sessions.ts @@ -41,6 +41,7 @@ import { SessionBranchingError, } from '../../utils/sessionBranching.js' import { registerChangedFileAccessRoot, registerFilesystemAccessRoot } from '../services/filesystemAccessRoots.js' +import { findGitRoot } from '../../utils/git.js' import { traceCaptureService, trimTraceCallPreviews } from '../services/traceCaptureService.js' const workspaceService = new WorkspaceService( @@ -711,6 +712,21 @@ async function getGitInfo(sessionId: string): Promise { } : null + // Fast check: if workDir is not inside a git repo, skip spawning git commands. + // findGitRoot uses fs.stat traversal with LRU cache (<5ms), avoiding 3 slow git + // spawns on non-git directories (each can take seconds as git searches upward). + const gitRoot = findGitRoot(workDir) + if (!gitRoot) { + const dirName = workDir.split('/').pop() || workDir.split(path.sep).pop() || '' + return Response.json({ + branch: sessionBranch, + repoName: dirName, + workDir, + changedFiles: 0, + worktree, + }) + } + try { // Get branch name const branchProc = Bun.spawn(['git', 'rev-parse', '--abbrev-ref', 'HEAD'], {