Skip to content

Instantly share code, notes, and snippets.

@bengl bengl/commits.js
Last active Aug 29, 2015

Embed
What would you like to do?
Analyse the commits on node core to see how many are doc-only, etc.
/**
* Plop this in your node git repo and run:
*
* $ node commits.js [num of commits you want to analyse (default 1000)]
*/
var exec = require('child_process').exec;
function gitDiffTree(commit, recursive, cb) {
exec('git diff-tree --name-only --no-commit-id ' + (recursive ? '-r ' : '') + commit, function(err, data) {
if (err) return cb(err);
cb(null, data.trim().split('\n'));
});
}
var onlyDocs = 0, onlyNonDocs = 0, docsAndNonDocs = 0;
var onlyApiDocs = 0, onlyNonApiDocs = 0, apiAndNonApiDocs = 0;
function getCommits(i) {
process.stdout.write('\033[2K\033[200Dcommits left to process: '+i);
i--;
if (i === -1) {
return processResults();
}
gitDiffTree('HEAD~'+i, false, function(err, list){
if (err) throw err;
if (list.length === 1 && list[0] === 'doc') {
onlyDocs++;
return processOnlyDocs(i, function(){
getCommits(i);
});
} else if (list.length > 0 && list.indexOf('doc') === -1) {
onlyNonDocs++;
} else {
docsAndNonDocs++;
}
getCommits(i);
});
}
function processOnlyDocs(i, cb) {
gitDiffTree('HEAD~'+i, true, function(err, list) {
if (err) throw err;
var hasApiDoc = false, hasNonApiDoc = false;
list.forEach(function(item){
if (/^doc\/api\//.test(item)) {
hasApiDoc = true;
} else {
hasNonApiDoc = true;
}
});
if (hasApiDoc && !hasNonApiDoc) {
onlyApiDocs++;
} else if (!hasApiDoc && hasNonApiDoc) {
onlyNonApiDocs++;
} else {
apiAndNonApiDocs++;
}
cb();
});
}
function processResults() {
process.stdout.write('\033[2K\033[200DDone!\n\n');
console.log('In the last', totalCommits, 'commits:');
console.log('---');
console.log('both docs and non-docs: ', percentage(docsAndNonDocs));
console.log('non-docs only: ', percentage(onlyNonDocs));
console.log('docs only: ', percentage(onlyDocs));
console.log(' -> both api and non-api: ', percentage(apiAndNonApiDocs));
console.log(' -> non-api only (minutes, etc): ', percentage(onlyNonApiDocs));
console.log(' -> api only: ', percentage(onlyApiDocs));
}
function percentage (num) {
return num+' ('+Math.round(10000*num/totalCommits)/100+'%)';
}
var totalCommits = process.argv[2] ?
(isNaN(+process.argv[2]) ? 1000 : +process.argv[2]) :
1000;
getCommits(totalCommits);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.