/** * 读取当前仓库生效的 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 { return new Promise((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 { const [name, email] = await Promise.all([ readGitConfig(cwd, 'user.name'), readGitConfig(cwd, 'user.email'), ]) return { name, email } }