Skip to content

Commit dd2eb33

Browse files
committed
Add '--abbrev regexp' option to gitblame.pm
This enables easy author grouping - say, for those inside vs outside the company. Signed-off-by: Henry Cox <[email protected]>
1 parent d363421 commit dd2eb33

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

scripts/annotateutil.pm

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ sub call_annotate
6868
{
6969
my $cb = shift;
7070
my $class;
71-
eval { $class = $cb->new(@_); };
7271
my $filename = pop;
72+
eval { $class = $cb->new(@_); };
73+
die("$cb construction error: $@") if $@;
7374
my ($status, $list) = $class->annotate($filename);
7475
foreach my $line (@$list) {
7576
my ($text, $abbrev, $full, $when, $cl) = @$line;
@@ -82,9 +83,10 @@ sub call_get_version
8283
{
8384
my $cb = shift;
8485
my $class;
85-
eval { $class = $cb->new(@_); };
8686
my $filename = pop;
87-
my $v = $class->extract_version($filename);
87+
eval { $class = $cb->new(@_); };
88+
die("$cb construction error: $@") if $@;
89+
my $v = $class->extract_version($filename);
8890
print($v, "\n");
8991
exit 0;
9092
}

scripts/gitblame

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# <http://www.gnu.org/licenses/>.
1818
#
1919
#
20-
# gitblame [--p4] [--prefix path] [domain] pathname
20+
# gitblame [--p4] [--prefix path] [--abbrev regexp] [domain] pathname
2121
#
2222
# This script runs "git blame" for the specified file and formats the result
2323
# to match the diffcov(1) age/ownership annotation specification.
@@ -33,6 +33,8 @@
3333
# If passed a domain name (or domain regexp):
3434
# strip that domain from the author's address, and treat all users outside
3535
# the matching domain as "External".
36+
# The --abbrev argument enables you to specify one or more regexp patterns
37+
# which are used to compute the user name abbreviation that are applied.
3638

3739
use strict;
3840
use FindBin;

scripts/gitblame.pm

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# <http://www.gnu.org/licenses/>.
1818
#
1919
#
20-
# gitblame [--p4] [--prefix path] [domain] pathname
20+
# gitblame [--p4] [--prefix path] [--abbrev regexp] [domain] pathname
2121
#
2222
# This script runs "git blame" for the specified file and formats the result
2323
# to match the diffcov(1) age/ownership annotation specification.
@@ -33,6 +33,8 @@
3333
# If passed a domain name (or domain regexp):
3434
# strip that domain from the author's address, and treat all users outside
3535
# the matching domain as "External".
36+
# The --abbrev argument enables you to specify one or more regexp patterns
37+
# which are used to compute the user name abbreviation that are applied.
3638

3739
package gitblame;
3840
use strict;
@@ -47,7 +49,7 @@ our @EXPORT_OK = qw(new);
4749

4850
use constant {
4951
P4 => 0,
50-
DOMAIN => 1,
52+
ABBREV => 1,
5153
PREFIX => 2,
5254
};
5355

@@ -59,21 +61,27 @@ sub new
5961
my $mapP4;
6062
my $prefix;
6163
my @args = @_;
62-
64+
my @abbrev;
6365
if (!GetOptionsFromArray(\@_,
6466
("p4" => \$mapP4,
65-
"prefix:s" => \$prefix)
67+
"prefix:s" => \$prefix,
68+
'abbrev:s' => \@abbrev)
6669
)) {
6770
my $exe = basename($script ? $script : $0);
68-
print(STDERR "usage: $exe [--p4] [domain] pathname\n");
71+
print(STDERR
72+
"usage: $exe [--p4] [--abbrev regexp]* [domain] pathname\n");
6973
exit(1) if ($script eq $0);
7074
return undef;
7175
}
72-
7376
my $internal_domain = shift;
77+
if ($internal_domain) {
78+
push(@abbrev, 's/^([^@]+)\@' . $internal_domain . '$/$1/');
79+
push(@abbrev, 's/^([^@]+)\@.+$/External/');
80+
# else leave domain in place
81+
}
7482
my @prefix;
7583
push(@prefix, $prefix) if $prefix;
76-
my $self = [$mapP4, $internal_domain, \@prefix];
84+
my $self = [$mapP4, \@abbrev, \@prefix];
7785
return bless $self, $class;
7886
}
7987

@@ -109,6 +117,7 @@ sub annotate
109117
open(HANDLE, "-|",
110118
"cd $dir ; git blame -e $basename 2> /dev/null")
111119
) {
120+
my %abbrev; # user name abbreviations
112121
while (my $line = <HANDLE>) {
113122
chomp $line;
114123
# Also remove CR from line-end
@@ -152,14 +161,18 @@ sub annotate
152161
$owner =~ s/ at /\@/;
153162
my $fullname = $owner;
154163

155-
if ($self->[DOMAIN]) {
156-
## strip domain part for internal users...
157-
$owner =~ s/\@$self->[DOMAIN]//;
158-
# replace everybody else with "External"
159-
$owner =~ s/.*\@.*/External/;
164+
if (exists($abbrev{$fullname})) {
165+
$owner = $abbrev{$fullname};
166+
} else {
167+
# compute only once...
168+
foreach my $re (@{$self->[ABBREV]}) {
169+
## strip domain part for internal users...
170+
eval '$owner =~ ' . $re . ';';
171+
die("invalid domain pattern '$re': $@")
172+
if $@;
173+
}
174+
$abbrev{$fullname} = $owner;
160175
}
161-
# else leave domain in place
162-
163176
# Convert Git date/time to diffcov canonical format
164177
# replace space between date and time with 'T'
165178
$when =~ s/\s/T/;

0 commit comments

Comments
 (0)