0

What I'm trying now is to use shelljs, but for some reason shell.exec('git rev-parse HEAD') just hangs.

This is my code:

function getLatestCommit() {
    return shell.exec("git rev-parse HEAD", {
        silent: true,
    }).output.trim().substr(0, 7);
}

Does anyone know any other way to achieve this?

I'm working on windows...

categulario
  • 111
  • 1
  • 6

1 Answers1

0

shell.exec uses a callback. You're passing a parameter to it, but not utilizing the callback:

var shell = require('child_process');

function getLatestCommit() {
    shell.exec("git rev-parse HEAD", { silent: true }, function(error, stdout, stderr) {
        console.log(error, stdout);
    });
}
getLatestCommit();
brandonscript
  • 68,675
  • 32
  • 163
  • 220
  • we are talking about diferent shell modules, I was using the shelljs module while you assume child_process. However I will consider your suggestion. I ended up doing it the hard way: reading .git/HEAD file contents, no child process. – categulario Jul 28 '15 at 17:50
  • Ah, wasn't sure - you didn't indicate that in your post. Still, I believe they both behave the same way. – brandonscript Jul 28 '15 at 17:50
  • I did, is the 9th word of my question ;) Anyway your approach is better than mine as you are not using an external module. The reason I didn't use the callback was that I wanted a synchronous function. – categulario Jul 28 '15 at 17:56
  • OH haha! Missed that completely. And I see what you're saying about wanting a synchronous function - in that case, check out http://stackoverflow.com/questions/4443597/node-js-execute-system-command-synchronously – brandonscript Jul 28 '15 at 17:57