#!/usr/bin/env node import { spawnSync } from 'node:child_process'; const repoRoot = process.env.GENARRATIVE_COMMIT_REPO_ROOT?.trim() || '/home/Genarrative'; function fail(message) { console.error(`[commit-task] ${message}`); process.exit(1); } function runGit(args, options = {}) { const result = spawnSync('git', args, { cwd: repoRoot, encoding: 'utf8', stdio: ['inherit', 'pipe', 'pipe'], ...options, }); if (result.status !== 0) { const stderr = result.stderr?.trim(); fail(stderr || `git ${args.join(' ')} 执行失败`); } return result.stdout?.trim() || ''; } const args = process.argv.slice(2); const messageIndex = args.findIndex((arg) => arg === '-m' || arg === '--message'); if (messageIndex === -1) { fail('缺少提交信息,请使用 -m "中文摘要"'); } const message = args[messageIndex + 1]?.trim() || ''; if (!message) { fail('提交信息不能为空'); } const fileArgs = args.filter((_, index) => index !== messageIndex && index !== messageIndex + 1); if (fileArgs.length === 0) { fail('至少传入一个需要提交的文件路径'); } const statusOutput = runGit(['status', '--short', '--', ...fileArgs]); if (!statusOutput) { fail('指定文件没有可提交改动'); } runGit(['add', '--', ...fileArgs]); const stagedOutput = runGit(['diff', '--cached', '--name-only', '--', ...fileArgs]); if (!stagedOutput) { fail('暂存后没有检测到可提交内容'); } runGit(['commit', '-m', message, '--', ...fileArgs], { stdio: 'inherit' });