Files
superwork/vscode/src/git/identity.ts
T
cruldra 73a0c46f9f ✨ feat(vscode): 提交代码按钮实时显示进度并预检 git 身份
- spawnClaude 新增 onProgress 回调,给了回调就改走 stream-json 逐行输出,按行解析出工具调用 / 回复文本
- 新增 claudeProgress 把每行 NDJSON 压成可读进度短语
- 提交前先读 git user.name / user.email,缺失直接提示,不再让 claude 白跑
- 运行期间用带转圈的 toast 滚动显示进度,完成后带上 claude 的结果摘要

Claude-Session: https://claude.ai/code/session_01HTMgwaE3woqM5yvsHiuSAa
2026-08-31 22:11:09 +08:00

47 lines
1.3 KiB
TypeScript

/**
* 读取当前仓库生效的 git 提交者身份(user.name / user.email)。
*
* 提交代码前先查一遍:身份缺失时 git commit 会直接失败,与其让后台 claude
* 跑十几分钟后才报错,不如一开始就把话说清楚。
*/
import { execFile } from 'node:child_process'
export interface GitIdentity {
name: string
email: string
}
export type GitIdentityKey = 'user.name' | 'user.email'
export function missingGitIdentityKeys(identity: GitIdentity): GitIdentityKey[] {
const missing: GitIdentityKey[] = []
if (!identity.name.trim())
missing.push('user.name')
if (!identity.email.trim())
missing.push('user.email')
return missing
}
function readGitConfig(cwd: string, key: GitIdentityKey): Promise<string> {
return new Promise<string>((resolve) => {
execFile(
'git',
['-C', cwd, 'config', '--get', key],
{ timeout: 10_000, encoding: 'utf8' },
(err, stdout) => {
// 未设置该项时 git 以退出码 1 结束且无输出,视同空串即可
resolve(err ? '' : (stdout ?? '').trim())
},
)
})
}
export async function readGitIdentity(cwd: string): Promise<GitIdentity> {
const [name, email] = await Promise.all([
readGitConfig(cwd, 'user.name'),
readGitConfig(cwd, 'user.email'),
])
return { name, email }
}