forked from dmnd/git-diff-blame
-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-diff-blame
executable file
路127 lines (116 loc) 路 3.44 KB
/
git-diff-blame
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/perl -w
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
use List::Util qw(first);
sub parse_hunk_header {
my ($line) = @_;
my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
$line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
$o_cnt = 1 unless defined $o_cnt;
$n_cnt = 1 unless defined $n_cnt;
return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
}
sub get_blame_prefix {
my ($line) = @_;
return "" if not $line;
$line =~ /^(\^?[0-9a-f]+\s+(\S+\s+)?\([^\)]+\))/ or die "bad blame output: $line";
return $1;
}
$git_root = `git rev-parse --show-toplevel`;
$git_root =~ s/^\s+//;
$git_root =~ s/\s+$//;
chdir($git_root) or die "$!";
my ($oldrev, $newrev, @REST);
# First check if file arguments are present.
my $index = first { $ARGV[$_] eq '--' } 0..$#ARGV;
if (defined $index) {
($oldrev, $newrev) = @ARGV[0 .. $index-1];
@REST = @ARGV[$index .. $#ARGV];
} else {
($oldrev, $newrev, @REST) = @ARGV;
}
# Then check if any of the revisons accidentally were assigned git diff option values.
if (defined $newrev && $newrev =~ /^-/) {
unshift(@REST, $newrev);
$newrev = undef;
}
if (defined $oldrev && $oldrev =~ /^-/) {
unshift(@REST, $oldrev);
$oldrev = undef;
}
# 123abc 123abc --> 123abc^ 123abc
if (defined $oldrev && defined $newrev && $oldrev eq $newrev) {
$oldrev = $oldrev . "^";
}
if (defined $oldrev) {
if (defined $newrev) {
open($diff, '-|', 'git', '--no-pager', 'diff', $oldrev, $newrev, @REST) or die;
} else {
open($diff, '-|', 'git', '--no-pager', 'diff', $oldrev, @REST) or die;
}
} else {
open($diff, '-|', 'git', '--no-pager', 'diff', @REST) or die;
}
my $istty = -t STDOUT;
my $ANSI_RED = $istty ? "[31m" : "";
my $ANSI_GRN = $istty ? "[32m" : "";
my $ANSI_RST = $istty ? "[m" : "";
my ($pre, $post);
my $filename;
while (<$diff>) {
if (m{^diff --git a/(.*) b/(.*)$} || m{^diff --git ./(.*) ./(.*)$}) {
close $pre if defined $pre;
close $post if defined $post;
print;
$prefilename = "./" . $1;
$postfilename = "./" . $2;
$delete = $create = 0;
} elsif (m{^new file}) {
$create = 1;
$prefilename = '/dev/null';
} elsif (m{^deleted file}) {
$delete = 1;
$postfilename = '/dev/null';
} elsif (m{^--- $prefilename\t?$}) {
# ignore
print;
} elsif (m{^\+\+\+ $postfilename$}) {
# ignore
print;
} elsif (m{^@@ }) {
my ($o_ofs, $o_cnt, $n_ofs, $n_cnt)
= parse_hunk_header($_);
my $o_end = $o_ofs + $o_cnt - 1;
my $n_end = $n_ofs + $n_cnt - 1;
if (!$create) {
if ($oldrev) {
open($pre, '-|', 'git', 'blame', '-M', "-L$o_ofs,$o_end",
$oldrev, '--', $prefilename) or die;
} else {
open($pre, '-|', 'git', 'blame', '-M', "-L$o_ofs,$o_end",
'HEAD', '--', $prefilename) or die;
}
}
if (!$delete) {
if ($newrev) {
open($post, '-|', 'git', 'blame', '-M', "-L$n_ofs,$n_end",
$newrev, '--', $postfilename) or die;
} else {
open($post, '-|', 'git', 'blame', '-M', "-L$n_ofs,$n_end",
'--', $postfilename) or die;
}
}
} elsif (m{^ }) {
print " ", get_blame_prefix(scalar <$pre>), "\t", $_;
scalar <$post>; # discard
} elsif (m{^\-}) {
print "${ANSI_RED} - ", get_blame_prefix(scalar <$pre>), "\t", $_,"${ANSI_RST}";
} elsif (m{^\+}) {
print "${ANSI_GRN} + ", get_blame_prefix(scalar <$post>), "\t", $_,"${ANSI_RST}";
}
}