1

Trying to access the HEAD reference using NodeGit. I am new to nodejs so this might simply be because I missed something. The following code finds the head but it always returns {}. Not sure what I am doing wrong.

Code starts outside this file by calling getHead(res).

var NodeGit = require("nodegit");
var pathToRepo = require("path").resolve("C:\\Users\\Betsegaw\\Desktop\\windowwalker");

function _getHead() {
    var head = new Promise(
        function (resolve, reject){
            NodeGit.Repository.open(pathToRepo).then(function (repo) {
                return repo.head();
            }).then(function (reference) {
                    console.log("Found head " + JSON.stringify(reference));
                    resolve(reference);
                });
        });
    return head;
}

module.exports = {
    getHEAD: function (res) {
        _getHead().then(function(head) {
            console.log(head);
            res.send(head);
        });
    }               
};

Edit: Minor typo in sample code

Betsegaw
  • 141
  • 2
  • 9
  • Slightly unrelated but you're using a promise [anti-pattern](https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns). – Ben Fortune Mar 23 '15 at 15:15
  • @BenFortune Thanks for the pointer, I need it :) I will clean up as soon as I get it into a working state. – Betsegaw Mar 23 '15 at 16:32

1 Answers1

0

NodeGit currently for a lot of classes has most of the values come back in functions rather than properties. We're trying to change that but currently logging out most values returned in the library are going to yield what you just saw.

Now with that being said your code is actually working. Let's reformat it a bit and get rid of that anti-pattern that Betsegaw noted.

var NodeGit = require("nodegit");
var pathToRepo = require("path").resolve("C:\\Users\\Betsegaw\\Desktop\\windowwalker");

module.exports = {
    getHEAD: function (res) {
        NodeGit.Repository.open(pathToRepo).then(function (repo) {
            return repo.head();
        }).then(function (reference) {
            res.send({
                name: reference.name(),
                target: reference.target()
            });
        });
    }               
};

That should give you some output that you're looking for. Check out Reference for some more ideas with what you can do with that returned reference

Community
  • 1
  • 1
johnhaley81
  • 1,153
  • 10
  • 14
  • 1
    Works like a charm! I didn't know it was being returned as a funciton I needed to call. Also, it was me who wrote up the nice anti-pattern and @benfortune who suggested I correct it :) Thanks for the quick response! – Betsegaw Mar 23 '15 at 16:53