-
-
Save taichi/365b933ad80ea078cf1270b47b1ae19e to your computer and use it in GitHub Desktop.
git-blame by PR #
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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