| 1 | # Copyright 2005-2026, Paul Johnson (paul@pjcj.net) | ||
| 2 | |||
| 3 | # This software is free. It is licensed under the same terms as Perl itself. | ||
| 4 | |||
| 5 | # The latest version of this software should be available from my homepage: | ||
| 6 | # https://pjcj.net | ||
| 7 | |||
| 8 | package Devel::Cover::Annotation::Git; | ||
| 9 | |||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | |||
| 13 | our $VERSION = '1.52'; # VERSION | ||
| 14 | |||
| 15 | use Getopt::Long; | ||
| 16 | |||
| 17 | sub new { | ||
| 18 | my $class = shift; | ||
| 19 | my $annotate_arg = $ENV{DEVEL_COVER_GIT_ANNOTATE} || ""; | ||
| 20 | my $self = { | ||
| 21 | annotations => [qw( version author date )], | ||
| 22 | command => "git blame --porcelain $annotate_arg [[file]]", | ||
| 23 | @_, | ||
| 24 | }; | ||
| 25 | |||
| 26 | bless $self, $class | ||
| 27 | } | ||
| 28 | |||
| 29 | sub get_annotations { | ||
| 30 | my $self = shift; | ||
| 31 | my ($file) = @_; | ||
| 32 | |||
| 33 | return if exists $self->{_annotations}{$file}; | ||
| 34 | my $annotations = $self->{_annotations}{$file} = []; | ||
| 35 | |||
| 36 | print "cover: Getting git annotation information for $file\n"; | ||
| 37 | |||
| 38 | my $command = $self->{command}; | ||
| 39 | $command =~ s/\[\[file\]\]/$file/g; | ||
| 40 | # print "Running [$command]\n"; | ||
| 41 | open my $c, "-|", $command or warn("cover: Can't run $command: $!\n"), return; | ||
| 42 | my @annotation; | ||
| 43 | my $start = 1; | ||
| 44 | while (my $line = <$c>) { | ||
| 45 | # print "[$_]\n"; | ||
| 46 | if ($line =~ /^\t/) { | ||
| 47 | push @$annotations, [@annotation]; | ||
| 48 | $start = 1; | ||
| 49 | next; | ||
| 50 | } | ||
| 51 | |||
| 52 | if ($start == 1) { | ||
| 53 | $annotation[0] = substr $1, 0, 8 if /$line =~ ^(\w+)/; | ||
| 54 | $start = 0; | ||
| 55 | } else { | ||
| 56 | $annotation[1] = $1 if $line =~ /^author (.*)/; | ||
| 57 | $annotation[2] = localtime $1 if $line =~ /^author-time (.*)/; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | close $c or warn "cover: Failed running $command: $!\n" | ||
| 61 | } | ||
| 62 | |||
| 63 | sub get_options { | ||
| 64 | my ($self, $opt) = @_; | ||
| 65 | $self->{$_} = 1 for @{ $self->{annotations} }; | ||
| 66 | die "Bad option" unless GetOptions( | ||
| 67 | $self, qw( | ||
| 68 | author | ||
| 69 | command=s | ||
| 70 | date | ||
| 71 | version | ||
| 72 | ) | ||
| 73 | ); | ||
| 74 | } | ||
| 75 | |||
| 76 | sub count { | ||
| 77 | my $self = shift; | ||
| 78 | $self->{author} + $self->{date} + $self->{version} | ||
| 79 | } | ||
| 80 | |||
| 81 | sub header { | ||
| 82 | my $self = shift; | ||
| 83 | my ($annotation) = @_; | ||
| 84 | $self->{annotations}[$annotation] | ||
| 85 | } | ||
| 86 | |||
| 87 | sub width { | ||
| 88 | my $self = shift; | ||
| 89 | my ($annotation) = @_; | ||
| 90 | (8, 16, 24)[$annotation] | ||
| 91 | } | ||
| 92 | |||
| 93 | sub text { | ||
| 94 | my $self = shift; | ||
| 95 | my ($file, $line, $annotation) = @_; | ||
| 96 | return "" unless $line; | ||
| 97 | $self->get_annotations($file); | ||
| 98 | $self->{_annotations}{$file}[ $line - 1 ][$annotation] | ||
| 99 | } | ||
| 100 | |||
| 101 | sub error { | ||
| 102 | my $self = shift; | ||
| 103 | my ($file, $line, $annotation) = @_; | ||
| 104 | 0 | ||
| 105 | } | ||
| 106 | |||
| 107 | sub class { | ||
| 108 | my $self = shift; | ||
| 109 | my ($file, $line, $annotation) = @_; | ||
| 110 | "" | ||
| 111 | } | ||
| 112 | |||
| 113 | 1 | ||
| 114 | |||
| 115 | __END__ |