【发布时间】:2017-11-10 09:06:01
【问题描述】:
我的场景
在我的节点应用程序中,我使用child_process.spawn 从当前存储库中查询信息
我已经构建了一个小函数来返回一个承诺,该承诺会通过命令的响应来解决:
const spawn = require('child_process').spawn;
const gitExec = command => (
new Promise((resolve, reject) => {
const thread = spawn('git', command);
const stdOut = [];
const stdErr = [];
thread.stdout.on('data', (data) => {
stdOut.push(data.toString('utf8'));
});
thread.stderr.on('data', (data) => {
stdErr.push(data.toString('utf8'));
});
thread.on('close', () => {
if (stdErr.length) {
reject(stdErr.join(''));
return;
}
resolve(stdOut.join());
});
})
);
module.exports = gitExec;
调用git branch按预期工作:
gitExec(['branch'])
.then((branchInfo) => {
console.log(branchInfo);
})
(如预期)结果
* develop
feature/forever
feature/sourceconfig
feature/testing
master
据我了解,这证明我使用的方法确实有效。
当调用 git shortlog -sn 时,生成的进程“挂起”并且无法解决任何问题
gitExec(['shortlog', '-sn'])
.then((shortlogInfo) => {
console.log(shortlogInfo);
})
通过命令行调用git shortlog -sn 我得到了预期的结果:
154 Andreas Gack
89 Some other dude
6 Whoever else
我的(到目前为止不成功)尝试
使用spawnSync(同时更改我的 gitExec 函数以适应同步方法)返回一个记录的对象-因此该过程似乎实际上退出了-但对象outputstdout和stderr的相关道具都是空的。
对象的status为0,表示命令执行成功
我了解到必须在 spawn 选项中重新定义 maxBuffer,但将其设置为(荒谬的)高值或非常小的值都不会对同步或异步方法产生影响。
将shell 选项设置为true 也不会对上述所有情况产生影响。
问题出现在我的 Win10x64 以及运行 node v6.9.x 或 7.x 的 MacO 上
同时调用别名git log --pretty=short 不提供结果
我的实际问题
- 有没有人成功通过 child_process.spawn 查询
git shortlog -sn? - 有谁知道 Node 的一个模块,它允许查询当前的本地 git-repository?
我不知何故认为git branch 和git shortlog 这两个命令在内部以不同的方式处理它们的输出。
我很乐意在他们的 github 页面上创建一个问题,但我实际上不知道如何确定该问题的实际根本原因。
非常感谢任何进一步的意见!
【问题讨论】:
标签: node.js git command-line-interface child-process spawn