Skip to content

Instantly share code, notes, and snippets.

@dmsnell
Last active May 26, 2018 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmsnell/ac8b49e559441d16e7f56548837b4299 to your computer and use it in GitHub Desktop.
Save dmsnell/ac8b49e559441d16e7f56548837b4299 to your computer and use it in GitHub Desktop.
Print info about each commit for a given author
// @ts-check
/* eslint-disable no-console */
/**
* Calculates commit info per person in a git repo
*
* Run: node lines-per-person.js 'first@email.com' 'first@go.email.com' 'iamthesame@pers.on'
*/
const Git = require("nodegit");
const flatten = (a, n) => [...a, ...n];
const emails = process.argv.slice(2);
async function main() {
const repo = await Git.Repository.open(".");
const master = await repo.getMasterCommit();
const history = master.history();
const commits = [];
history.on("commit", async function (commit) {
const author = commit.author();
const authorEmail = author.email();
if (!emails.some(e => e === authorEmail)) {
return;
}
const diff = await commit.getDiff();
const patches = (await Promise.all(diff.map(d => d.patches()))).reduce(flatten, []);
const stats = patches.map(p => p.lineStats());
const changes = stats.reduce(([a, d, c], {
total_additions: na,
total_context: nc,
total_deletions: nd
}) => [a + na, d + nd, c + nc], [0, 0, 0]);
const t = new Date(commit.time() * 1000);
const p = s => s < 10 ? `0${ s }` : s;
const ts = `${ t.getFullYear() }-${ p( t.getMonth() + 1 ) }-${ p( t.getDate() ) } ${ p( t.getHours() ) }:${ p( t.getMinutes() ) }:${ p( t.getSeconds() ) }`;
commits.push([commit.sha(), ts, ...changes, changes[0] - changes[1]]);
});
history.start();
history.on('end', () => {
console.log('Commit Hash;Date;Added;Removed;Context');
commits
.sort((a, b) => a[1].localeCompare(b[1]))
.reduce((cs, c, i) => [...cs, [...c, i > 0 ? cs[i - 1][6] + c[5] : c[5]]], [])
.forEach(l => console.log(l.join(';')));
});
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment