4

I'm trying to get the result of git shortlog on a repository with nodejs this way:

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

My callback is never called with this method, the program appears processing something indefinitely.

When I launch this command in my prompt I've the results.

Trying to redirect the result to a file create an empty file :

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

But if I use git log instead I've got my results.

Do you know why my callback is never called with git shortlog ?

Edit : I've the same result using 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);
    });
});

Edit 2 : process.chdir() doesn't change anything :

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

2 回答 2

19

git shortlog thinks that it has to read something from stdin, hence the indefinite wait. Try this:

exec('git shortlog < /dev/tty', { cwd : "/tmp/" + folder }, function(err, stdout, stderr) {
  console.log(arguments);
});
于 2013-03-22T08:45:53.630 回答
3

Since @robertklep 's answer is correct, I'm afraid it's not complete as well. On *nix systems, yes, you can use it with /dev/tty but on Windows this will throw an error like

The system cannot find the path specified.

To solve this problem on Windows, you can simply do this:

var exec = require('child_process').exec;
var tty = process.platform === 'win32' ? 'CON' : '/dev/tty';

exec('git shortlog < ' + tty, { cwd : '/tmp/' + folder }, function(error, stdout, stderr) {
  console.log(arguments);
});
于 2016-02-08T11:42:43.793 回答