补充任务自动提交脚本与工作流文档
This commit is contained in:
56
scripts/commit-task.mjs
Normal file
56
scripts/commit-task.mjs
Normal file
@@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { spawnSync } from 'node:child_process';
|
||||
|
||||
function fail(message) {
|
||||
console.error(`[commit-task] ${message}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
function runGit(args, options = {}) {
|
||||
const result = spawnSync('git', args, {
|
||||
cwd: '/home/Genarrative',
|
||||
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], { stdio: 'inherit' });
|
||||
|
||||
Reference in New Issue
Block a user