Last active
June 28, 2022 07:15
-
-
Save kazuho/eab551e5527cb465847d6b0796d64a39 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
#! /usr/bin/perl | |
# | |
# Written in 2017 by Kazuho Oku | |
# | |
# To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty. | |
# You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. | |
# | |
use strict; | |
use warnings; | |
my $git_blame_pid = open(my $fh, "-|", "git", "blame", "--first-parent", @ARGV) | |
or die "failed to invoke git-blame:$!"; | |
my %cached; # commit-id -> substitution string | |
while (my $line = <$fh>) { | |
my ($commit, $src) = split / .*?\) /, $line, 2; | |
$cached{$commit} = lookup($commit) | |
unless $cached{$commit}; | |
print $cached{$commit}, ' ', $src; | |
} | |
while (waitpid($git_blame_pid, 0) != $git_blame_pid) {} | |
exit $?; | |
sub lookup { | |
my $commit = shift; | |
my $message = `git show --oneline $commit`; | |
if ($message =~ /Merge\s+(?:pull\s+request|pr)\s+\#?(\d+)\s/i) { | |
return sprintf '%-9s', "PR #$1"; | |
} | |
return $commit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
see also: http://blog.kazuhooku.com/2017/12/git-blame.html