Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting all commits on all branches with nodegit

Tags:

git

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
};
like image 969
Calin Leafshade Avatar asked Jul 12 '16 17:07

Calin Leafshade


1 Answers

You should change this:

walker.pushHead();

to:

walker.pushGlob('refs/heads/*');
like image 182
user8481179 Avatar answered Oct 23 '22 07:10

user8481179