Getting all commits on all branches with nodegit
Asked Answered
E

1

8

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
};
Epineurium answered 12/7, 2016 at 17:49 Comment(0)
H
3

You should change this:

walker.pushHead();

to:

walker.pushGlob('refs/heads/*');
Hardihood answered 18/8, 2017 at 2:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.