Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exec not returning anything when trying to run git shortlog with nodejs

Tags:

git

node.js

exec

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);
});
like image 722
manuquentin Avatar asked Nov 29 '22 13:11

manuquentin


1 Answers

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);
});
like image 84
robertklep Avatar answered Dec 06 '22 11:12

robertklep