Skip to content

Instantly share code, notes, and snippets.

@taichi
Forked from kazuho/git-blame-pr.pl
Last active December 28, 2017 08:06
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 taichi/365b933ad80ea078cf1270b47b1ae19e to your computer and use it in GitHub Desktop.
Save taichi/365b933ad80ea078cf1270b47b1ae19e to your computer and use it in GitHub Desktop.
git-blame by PR #
const { spawn, spawnSync } = require('child_process');
const readline = require('readline');
const blame = spawn('git', ['blame', '--first-parent', process.argv[2]]);
const lines = readline.createInterface({ input: blame.stdout });
const lookup = hash => {
const ret = spawnSync('git', ['show', '--oneline', hash]);
const msg = /Merge\s+(?:pull\s+request|pr)\s+\#?(\d+)\s/i;
const matcher = msg.exec(ret.stdout.toString());
if (matcher) {
return `PR #${matcher[1]}`;
}
return hash;
};
const cache = new Map();
const get = hash => {
const val = cache.get(hash);
if (val) {
return val;
} else {
const no = lookup(hash).padEnd(8, ' ');
cache.set(hash, no);
return no;
}
};
lines.on('line', (input) => {
const [hash, content] = input.split(/ .*?\) /);
console.log(get(hash), content);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment