调用git shortlog -sn时,节点child_process spawn挂起

问题描述 投票:1回答:2

我的情景

在我的节点应用程序中,我使用child_process.spawn查询当前存储库中的信息

我已经构建了一个小函数来返回一个promise,该promise通过命令的响应来解析:

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 branchworks:

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);
})

通过命令行调用qazxsw poi我得到了预期的结果:

git shortlog -sn

我(迄今为止不成功)尝试

使用 154 Andreas Gack 89 Some other dude 6 Whoever else (同时更改我的gitExec函数以适应同步方法)返回一个记录的对象 - 所以过程似乎实际上退出 - 但对象spawnSync outputstdout的相关道具都是空的。 对象的stderrstatus,表示成功执行的命令

我已经读过必须在spawn选项中重新定义0,但是既不将它设置为(荒谬的)高值也不是非常小的值确实会在同步或异步方法中产生影响。

maxBuffer选项设置为shell也不会对上述所有场景产生影响。

问题出现在我的Win10x64以及运行节点v6.9.x或7.x的MacO上

同样调用别名true不会提供结果

我的实际问题

  • 有没有人设法通过child_process.spawn成功查询git log --pretty=short
  • 有没有人知道Node的模块,它允许查询当前的本地git-repository?

我不知何故认为两个命令git shortlog -sngit branch以不同的方式在内部处理它们的输出。

我很乐意在他们的github页面上创建一个问题,但我实际上不知道如何识别该问题的实际根本原因。

任何进一步的输入非常感谢!

node.js git command-line-interface child-process spawn
2个回答
9
投票

git shortlog认为它需要从git shortlog读取一些东西,这就是整个过程悬而未决的原因。要解决这个问题,您可以将main进程中的stdin作为选项传递,并像往常一样管道其他所有内容。然后它应该运行。

stdin

也许来自const spawn = require('child_process').spawn; const gitExec = command => ( new Promise((resolve, reject) => { const thread = spawn('git', command, { stdio: ['inherit', 'pipe', 'pipe'] }); 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 shortlog将输出从标准输入读取的日志摘要,而不引用当前存储库。

产生子进程的情况是这样的。所以它希望通过git documentation传递一些东西。通过将stdin设置为主进程,stdin知道终端,因此将运行。


0
投票

我通过指定提交前后哈希来实现它。

git shortlog
© www.soinside.com 2019 - 2024. All rights reserved.