我正在编写一个小应用程序来分析文件夹中的git存储库.我正在使用nodegit来解析基于libgit2的repo.
如何使用nodegit获取repo中所有分支的所有提交?
这是我目前的代码:
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
};
最佳答案 你应该改变这个:
walker.pushHead();
至:
walker.pushGlob('refs/heads/*');