Skip to content

Commit a6417dd

Browse files
committed
fix: 使用 exec 来获取 git 信息时可能会出现 ERR_CHILD_PROCESS_STDIO_MAXBUFFER 问题 #8
1 parent 0329664 commit a6417dd

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

src/parsers/diff.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,41 @@ export class DiffParser implements Parser {
4646

4747
// 执行 diff
4848
this.stdout = await new Promise((resolve, reject) => {
49-
child.exec(`git diff ${this.hash}`, this.opt, (err, stdout) => {
50-
if (err) {
51-
reject(err);
49+
const command = `git diff ${this.hash}`;
50+
51+
// 注意不能用 exec, 原因详见 https://github.com/matmanjs/incremental-coverage/issues/8
52+
const cmd = child.spawn(command, [], {
53+
...(this.opt || {}),
54+
// windows 中如果不设置它的话会出错
55+
shell: true,
56+
},);
57+
58+
let result = '';
59+
let errMsg = '';
60+
61+
// 增加一个超时限制
62+
const checkT = setTimeout(() => {
63+
resolve(result);
64+
}, 5000);
65+
66+
cmd.stdout.on('data', (data) => {
67+
result += data;
68+
});
69+
70+
cmd.stderr.on('data', (data) => {
71+
// console.error(`stderr: ${data}`);
72+
errMsg += data;
73+
});
74+
75+
cmd.on('close', (code) => {
76+
clearTimeout(checkT);
77+
78+
if (code) {
79+
// console.error(`${command} close: ${code}`);
80+
reject(new Error(`(close=${code})${errMsg}`));
81+
} else {
82+
resolve(result);
5283
}
53-
resolve(stdout);
5484
});
5585
});
5686
}

0 commit comments

Comments
 (0)