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
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