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
  • you could try swapping out exec for spawn and see what events are firing. Commented Mar 22, 2013 at 6:38
  • Thanks, I've tried it, but I've the same issue. Commented Mar 22, 2013 at 7:08
  • Next stab: nodejs.org/api/process.html#process_process_chdir_directory use process.chdir instead of exec "cd ..." Commented Mar 22, 2013 at 7:13
  • Arg, thanks for this answer but I've the same behviour. I've edited my post Commented Mar 22, 2013 at 8:25

3 Answers 3

20

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);
});
Sign up to request clarification or add additional context in comments.

1 Comment

For Windows, use CON instead of /dev/tty
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);
});

Comments

0

I just stumbled over this issue too. All other git commands including git log etc. worked, but shortlog would never return. In the end, ChatGPT could answer the reason why – for some reason, when running git shortlog from Node.js, shortlog wants a revision and expects it from stdin. By adding HEAD as revision, the command terminates.

// Fails because shortlog indefinitely waits from stdin
// Manually closing stdin does not help either and just returns no output
child_process.spawn('git', [ 'shortlog', '-nse', '--', filename ], { cwd: mydir });

// WORKS with specifying a revision number
child_process.spawn('git', [ 'shortlog', '-nse', 'HEAD', '--', filename ], { cwd: mydir });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.