4

I'm writing a node API server that needs to send user a list of local branches in a git repo residing on the server. A lot of places suggest using the Repository#getReferenceNames from NodeGit and this is what I do:

exports.getBranches = function (req, res) {
    NodeGit.Repository.open(config.database).then(function(repo) {
        return repo.getReferenceNames(NodeGit.Reference.TYPE.LISTALL);
    }).then(function (arrayString) {
        res.status(200).json({message: "OK", data: arrayString});
    }).catch(function (err) {
        res.status(500).json({message: err, data: []});
    }).done(function () {
        console.log("finish");
    });
};

However, this function returns all the branches. Is there a way to only get local branch like the command line git branch does?

Calvin Hu
  • 3,595
  • 4
  • 18
  • 23

1 Answers1

4

Sure there is! The local branches are prefixed refs/heads, while the remote branches are prefixed refs/remotes, like this:

enter image description here

Good luck!

Edit:

There is even a better method. You can traverse the returned references before returning it, and weed out the remotes with reference.isRemote().

simme
  • 1,514
  • 12
  • 23