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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
#!/usr/bin/env perl
# Copyright (c) MediaTek USA Inc., 2020-2024
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see
# <http://www.gnu.org/licenses/>.
#
#
# p4annotate.pm
#
# This script runs "p4 annotate" for the specified file and formats the result
# to match the diffcov(1) 'annotate' callback specification:
# use p4annotate;
# my $callback = p4annotate->new([--log logfile] [--cache cache_dir] [--verify]);
# $callback->annotate(filename);
#
# If the '--cache' flag is used:
# Goal is to improve runtime performance by not calling GIT if file is
# unchanged and previous result is available.
# - First look into the provided cache before calling GIT.
# Hope to find that we already have data for the file we wanted.
# - If we do call GIT - then store the result back into cache.
# Note that this callback uses the `--version-script' (if specified)
# to extract and compare file versions.
# Also note that ignoring "version" errors will disable version checking
# of cached files - and may result in out-of-sync annotated file data.
#
# The '--verify' flag tells the tool to do some additional consistency
# checking when merging local edits into the annotated file.
#
# The '--log' flag specifies a file where the tool writes various annotation-
# related log messages.
#
# This utility is implemented so that it can be loaded as a Perl module such
# that the callback can be executed without incurring an additional process
# overhead - which appears to be large and hightly variable in our compute
# farm environment.
#
# It can also be called directly, as
# p4annotate [--log logfile] [--verify] filename
package p4annotate;
use strict;
use File::Basename;
use File::Spec;
use Getopt::Long qw(GetOptionsFromArray);
use Fcntl qw(:flock);
use annotateutil qw(get_modify_time not_in_repo call_annotate
resolve_cache_dir find_in_cache store_in_cache);
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(new);
use constant {
SCRIPT => 0,
CACHE => 1,
VERIFY => 2,
LOGFILE => 3,
LOG => 4,
};
sub printlog
{
my ($self, $msg) = @_;
my $fh = $self->[LOG];
return unless $fh;
flock($fh, LOCK_EX) or die('cannot lock ' . $self->[LOGFILE] . ": $!");
print($fh $msg);
flock($fh, LOCK_UN) or die('cannot unlock ' . $self->[LOGFILE] . ": $!");
}
sub new
{
my $class = shift;
my @args = @_;
my $script = shift; # this should be 'me'
#other arguments are as passed...
my $logfile;
my $cache_dir;
my $verify = 0; # if set, check that we merged local changes correctly
my $exe = basename($script ? $script : $0);
my $standalone = $0 eq $script;
if (exists($ENV{LOG_P4ANNOTATE})) {
$logfile = $ENV{LOG_P4ANNOTATE};
}
my $help;
if (!GetOptionsFromArray(\@_,
("verify" => \$verify,
"log=s" => \$logfile,
'cache:s' => \$cache_dir,
'help' => \$help)) ||
(!$standalone && scalar(@_)) ||
$help
) {
print(STDERR ($help ? '' : ("unexpected arg: $script " . join(@_, ' '))
),
"usage: $exe [--log logfile] [--cache dir] [--verify] filename\n"
);
exit(scalar(@_) == 0 && $help ? 0 : 1) if $standalone;
return undef;
}
my @notset;
foreach my $var ("P4USER", "P4PORT", "P4CLIENT") {
push(@notset, $var) unless exists($ENV{$var});
}
if (@notset) {
die("$exe requires environment variable" .
(1 < scalar(@notset) ? 's' : '') . ' ' .
join(' ', @notset) . " to be set.");
}
$cache_dir = resolve_cache_dir($cache_dir);
my $self = [$exe, $cache_dir, $verify];
bless $self, $class;
if ($logfile) {
open(LOGFILE, ">>", $logfile) or
die("unable to open $logfile");
$self->[LOG] = \*LOGFILE;
$self->[LOGFILE] = $logfile;
$self->printlog("$exe " . join(" ", @args) . "\n");
}
return $self;
}
sub annotate
{
my ($self, $pathname) = @_;
defined($pathname) or die("expected filename");
my ($cache_path, $version);
if ($self->[CACHE]) {
my $lines;
($cache_path, $version, $lines) =
find_in_cache($self->[CACHE], $pathname);
return (0, $lines) if defined($lines); # cache hit
}
if (-e $pathname && -l $pathname) {
$pathname = File::Spec->catfile(File::Basename::dirname($pathname),
readlink($pathname));
my @c;
foreach my $component (split(m@/@, $pathname)) {
next unless length($component);
if ($component eq ".") { next; }
if ($component eq "..") { pop @c; next }
push @c, $component;
}
$pathname = File::Spec->catfile(@c);
}
my $null = File::Spec->devnull(); # more portable
my @lines;
my $status;
if (0 == system(
"p4 files $pathname 2>$null|grep -qv -- '- no such file' >$null")
) {
# this file is in p4..
my $version;
my $have = `p4 have $pathname`;
if ($have =~ /#([0-9]+) - /) {
$version = "#$1";
} else {
$version = '@head';
}
$self->printlog(" have $pathname:$version\n");
my @annotated;
# check if this file is open in the current sandbox...
# redirect stderr because p4 print "$path not opened on this client" if file not opened
my $opened = `p4 opened $pathname 2>$null`;
my %localAdd;
my %localDelete;
my ($localChangeList, $owner, $now);
if ($opened =~ /edit (default change|change (\S+)) /) {
$localChangeList = $2 ? $2 : 'default';
$self->printlog(" local edit in CL $localChangeList\n");
$owner = $ENV{P4USER}; # current user is responsible for changes
$now = get_modify_time($pathname)
; # assume changes happened when file was liast modified
# what is different in the local file vs the one we started with
if (open(PIPE, "-|", "p4 diff $pathname")) {
my $line = <PIPE>; # eat first line
die("unexpected content '$line'")
unless $line =~ m/^==== /;
my ($action, $fromStart, $fromEnd, $toStart, $toEnd);
while ($line = <PIPE>) {
chomp $line;
# Also remove CR from line-end
s/\015$//;
if ($line =~
m/^([0-9]+)(,([0-9]+))?([acd])([0-9]+)(,([0-9]+))?/) {
# change
$action = $4;
$fromStart = $1;
$fromEnd = $3 ? $3 : $1;
$toStart = $5;
$toEnd = $7 ? $7 : $5;
} elsif ($line =~ m/^> (.*)$/) {
$localAdd{$toStart++} = $1;
} elsif ($line =~ m/^< (.*)$/) {
$localDelete{$fromStart++} = $1;
} else {
die("unexpected line '$line'")
unless $line =~ m/^---$/;
}
}
close(PIPE) or die("unable to close p4 diff pipe: $!\n");
if (0 != $?) {
$? & 0x7F &
die("p4 pipe died from signal ", ($? & 0x7F), "\n");
die("p4 pipe exited with error ", ($? >> 8), "\n");
}
} else {
die("unable to open pipe to p4 diff $pathname");
}
}
# -i: follow history across branches
# -I: follow history across integrations
# (seem to be able to use -i or -I - but not both, together)
# -u: print user name
# -c: print changelist rather than file version ID
# -q: quiet - suppress the 1-line header for each line
my $annotateLineNo = 1;
my $emitLineNo = 1;
if (open(HANDLE, "-|", "p4 annotate -Iucq $pathname$version")) {
while (my $line = <HANDLE>) {
if (exists $localDelete{$annotateLineNo++}) {
next; # line was deleted .. skip it
}
while (exists $localAdd{$emitLineNo}) {
my $l = $localAdd{$emitLineNo};
push(@lines, [$l, $owner, undef, $now, $localChangeList]);
push(@annotated, $l) if ($self->[VERIFY]);
delete $localAdd{$emitLineNo};
++$emitLineNo;
}
chomp $line;
# Also remove CR from line-end
s/\015$//;
if ($line =~ m/([0-9]+):\s+(\S+)\s+([0-9\/]+)\s(.*)/) {
my $changelist = $1;
my $owner = $2;
my $when = $3;
my $text = $4;
$owner =~ s/^.*<//;
$owner =~ s/>.*$//;
$when =~ s:/:-:g;
$when =~ s/$/T00:00:00-05:00/;
push(@lines, [$text, $owner, undef, $when, $changelist]);
push(@annotated, $text) if ($self->[VERIFY]);
} else {
push(@lines, [$line, 'NONE', undef, 'NONE', 'NONE']);
push(@annotated, $line) if ($self->[VERIFY]);
}
++$emitLineNo;
} # while (HANDLE)
# now handle lines added at end of file
die("lost track of lines")
unless (0 == scalar(%localAdd) ||
exists($localAdd{$emitLineNo}));
while (exists $localAdd{$emitLineNo}) {
my $l = $localAdd{$emitLineNo};
push(@lines, [$l, $owner, undef, $now, $localChangeList]);
delete $localAdd{$emitLineNo};
push(@annotated, $l) if ($self->[VERIFY]);
++$emitLineNo;
die("lost track of lines")
unless (0 == scalar(%localAdd) ||
exists($localAdd{$emitLineNo}));
}
if ($self->[VERIFY]) {
if (open(DEBUG, "<", $pathname)) {
my $lineNo = 0;
while (my $line = <DEBUG>) {
chomp($line);
my $a = $annotated[$lineNo];
die("mismatched annotation at $pathname:$lineNo: '$line' -> '$a'"
) unless $line eq $a;
++$lineNo;
}
}
}
close(HANDLE) or die("unable to close p4 annotate pipe: $!\n");
$status = $?;
}
if ($self->[CACHE] &&
0 == $status) {
store_in_cache($cache_path, $pathname, $version, \@lines);
}
} else {
$self->printlog(" $pathname not in P4\n");
not_in_repo($pathname, \@lines);
}
return ($status, \@lines);
}
unless (caller) {
call_annotate("p4annotate", @ARGV);
}
1;
|