修复非git项目在新会话执行git命令导致加载慢

修复非git项目在新会话执行git命令导致加载慢
This commit is contained in:
zhb 2026-06-26 08:48:53 +08:00 committed by GitHub
parent eddaec547e
commit 78a9e26476
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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<Response> {
}
: 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'], {