git - 尝试使用 nodejs 运行 git shortlog 时,Exec 不返回任何内容

标签 git node.js exec

我试图通过这种方式在带有 nodejs 的存储库上获取 git shortlog 的结果:

var exec = require('child_process').exec;
exec("cd /tmp/"+folder +" && git shortlog", {maxBuffer: 500*1024}, function(error, stdout, stderror){
    console.log(arguments);
});

此方法从未调用我的回调,程序似乎在无限期地处理某些事情。

当我在提示中启动此命令时,我得到了结果。

尝试将结果重定向到创建一个空文件的文件:

"cd /tmp/"+folder +" && git shortlog > stats.txt"

但如果我改用 git log,我就会得到结果。

你知道为什么我的回调从来没有用 git shortlog 调用过吗?

编辑: 我使用 spawn 得到了相同的结果:

exec("cd /tmp/"+folder, function(err){
    if(err){
        throw err;
    }

    var shortlog = spawn('git', ['shortlog']);
    shortlog.stdout.on('data', function (data) {
        console.log('stdout: ' + data);
    });

    shortlog.stderr.on('data', function (data) {
        console.log('stderr: ' + data);
    });

    shortlog.on('close', function (code) {
        console.log('child process exited with code ' + code);
    });
});

编辑 2: process.chdir() 不会改变任何东西:

process.chdir('/tmp/'+folder);
var shortlog = spawn('git', ['shortlog']);
shortlog.stdout.on('data', function (data) {
        console.log('stdout: ' + data);
});

最佳答案

git shortlog 认为它必须从 stdin 读取一些东西,因此无限期等待。尝试这个:

exec('git shortlog < /dev/tty', { cwd : "/tmp/" + folder }, function(err, stdout, stderr) {
  console.log(arguments);
});

关于git - 尝试使用 nodejs 运行 git shortlog 时,Exec 不返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15564185/

相关文章:

规范化后的 Git 行尾

GitHub Desktop 正在提交被忽略的文件

node.js - Mocha 测试失败,但它不应该失败

node.js - AngularJS:Karma + Jasmine 使用 _real_ 后端进行测试

linux - 为什么/bin/login 在最近的 Linux 上 fork

Java运行时执行: Get information from Firefox

git - 在 GitHub 上创建空分支

git - Force SourceTree 忽略 git 文件中的行结尾

node.js - 如何获取当前用户的sessionToken?

c - 使用 exec() 函数在 C 中调用 Linux 命令