gitnodegit

Getting all commits on all branches with nodegit


I'm writing a little app that analyses the git repositories inside a folder. I'm using nodegit to parse the repo which is based on libgit2.

How can I get all the commits from all the branches in a repo using nodegit?

Here is my current code:

var git = require('nodegit');
var fs = require('fs');
var path = require('path');

var getDirectories = function(srcpath) {
    return fs.readdirSync(srcpath).filter(function(file) {
        return fs.statSync(path.join(srcpath, file)).isDirectory();
    });
}

var getData = function(srcDir) {

    var repos = getDirectories(srcDir);
    var globalCommits = [];

    var promises = repos.map(repoName => {
        return git.Repository.open(path.join(srcDir, repoName)).then(function(repo) {
            var walker = git.Revwalk.create(repo);
            walker.pushHead();
            return walker.getCommitsUntil(c => true).then(function (commits) {
                var cmts = commits.map(x => ({
                    sha:  x.sha(),
                    msg: x.message().split('\n')[0],
                    date: x.date(),
                    author: x.author(),
                    repo: repoName
                }));
                globalCommits = globalCommits.concat(cmts);
            });
        });
    });

    return Promise.all(promises).then(function() {
        return Promise.resolve(globalCommits);  
    });

}

module.exports = {
    getData: getData
};

Solution

  • You should change this:

    walker.pushHead();
    

    to:

    walker.pushGlob('refs/heads/*');